Commit a4a7aba5 authored by John Jarvis's avatar John Jarvis

Add config tests.

This were dropped before because of some refactoring.
parent 834ea117
Pipeline #14150322 passed with stage
in 1 minute and 16 seconds
package config
import (
"fmt"
"os"
"reflect"
"strings"
"testing"
)
func TestReadConfig(t *testing.T) {
filename := "invalid-cfg.json"
_, err := ReadConfig(filename)
if err == nil {
t.Fatalf("could not read configuration file %s", filename)
}
want := "stat invalid-cfg.json: no such file or directory"
got := err.Error()
if got != want {
t.Fatalf("wrong configuration error, got %s; expected %s", got, want)
}
}
func TestParseConfigWrong(t *testing.T) {
_, err := ParseConfig(strings.NewReader("invalid yaml"))
expected := "could not parse configuration yaml: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `invalid...` into config.Config"
if fmt.Sprintf("%s", err) != expected {
t.Fatalf("unexpected error, got %s", err)
}
}
func TestParseConfigCorrectly(t *testing.T) {
f, err := os.Open("../settings-example.yaml")
defer f.Close()
if err != nil {
t.Fatalf("failed to open the configuration file: %s", err)
}
c, err := ParseConfig(f)
if err != nil {
t.Fatalf("failed to parse the configuration: %s", err)
}
assertEquals(t, "gitlab token", "*** GL API TOKEN GOES HERE ***", c.GitLab.Token)
assertEquals(t, "pagerduty token", "*** PD API TOKEN GOES HERE ***", c.PagerDuty.Token)
}
func assertEquals(t *testing.T, name string, expected, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("%s is not as expected: %+v; got %+v", name, expected, actual)
}
}
......@@ -19,7 +19,7 @@ func CreateReportIssue(config *config.Config, title string, report string) *gitl
}
}
func GetIssuesClosedDuringMilestone(config *config.Config) []*gitlab.Issue {
func getIssuesClosedDuringMilestone(config *config.Config) []*gitlab.Issue {
git := gitlab.NewClient(nil, config.GitLab.Token)
labels := []string{config.OncallLabel}
opts := gitlab.ListProjectIssuesOptions{}
......@@ -35,7 +35,7 @@ func GetIssuesClosedDuringMilestone(config *config.Config) []*gitlab.Issue {
return issues
}
}
func GetIssuesOpenedDuringShift(config *config.Config) []*gitlab.Issue {
func getIssuesOpenedDuringShift(config *config.Config) []*gitlab.Issue {
git := gitlab.NewClient(nil, config.GitLab.Token)
labels := []string{config.OncallLabel}
var opts gitlab.ListProjectIssuesOptions
......@@ -49,7 +49,7 @@ func GetIssuesOpenedDuringShift(config *config.Config) []*gitlab.Issue {
}
}
func GetIssuesOpenAll(config *config.Config) []*gitlab.Issue {
func getIssuesOpenAll(config *config.Config) []*gitlab.Issue {
git := gitlab.NewClient(nil, config.GitLab.Token)
labels := []string{config.OncallLabel}
var opts gitlab.ListProjectIssuesOptions
......@@ -63,7 +63,7 @@ func GetIssuesOpenAll(config *config.Config) []*gitlab.Issue {
}
}
func FilterIssuesByLabel(label string, issues []*gitlab.Issue) (filtered_issues []*gitlab.Issue) {
func filterIssuesByLabel(label string, issues []*gitlab.Issue) (filtered_issues []*gitlab.Issue) {
for _, p := range issues {
if stringInSlice(label, p.Labels) {
filtered_issues = append(filtered_issues, p)
......@@ -72,7 +72,7 @@ func FilterIssuesByLabel(label string, issues []*gitlab.Issue) (filtered_issues
return
}
func FilterIssuesByState(state string, issues []*gitlab.Issue) (filtered_issues []*gitlab.Issue) {
func filterIssuesByState(state string, issues []*gitlab.Issue) (filtered_issues []*gitlab.Issue) {
for _, p := range issues {
fmt.Println(state, p.Labels)
if p.State == state {
......
......@@ -6,21 +6,9 @@ import (
"time"
)
// PagerDuty helper
type PagerDuty struct {
token string
}
// NewPagerDuty returns a new PagerDuty object
func NewPagerDuty(token string) PagerDuty {
return PagerDuty{
token: token,
}
}
// GetOncallPersons returns the team members who were on call
// for the last 7 days for the given schedule
func GetOncallPersons(config *config.Config, schedule string) []pagerduty.User {
func getOncallPersons(config *config.Config, schedule string) []pagerduty.User {
var options pagerduty.ListOnCallUsersOptions
options.Since = nowPdDateWithOffset(-config.DayOffset)
options.Until = nowPdDate()
......@@ -32,7 +20,7 @@ func GetOncallPersons(config *config.Config, schedule string) []pagerduty.User {
}
}
func GetOncallIncidents(config *config.Config) []pagerduty.Incident {
func getOncallIncidents(config *config.Config) []pagerduty.Incident {
client := pagerduty.NewClient(config.PagerDuty.Token)
var opts pagerduty.ListIncidentsOptions
opts.ServiceIDs = []string{config.PagerDuty.ServiceId}
......
......@@ -56,7 +56,7 @@ func GenerateTemplate(config *config.Config) *bytes.Buffer {
template_data := TemplateData{}
for _, schd := range config.PagerDuty.Schedules {
onCallUsers := GetOncallPersons(config, schd.Id)
onCallUsers := getOncallPersons(config, schd.Id)
for _, u := range onCallUsers {
otm := OnCallTeamMember{}
otm.Schedule = schd.Name
......@@ -65,7 +65,7 @@ func GenerateTemplate(config *config.Config) *bytes.Buffer {
}
}
incidents := GetOncallIncidents(config)
incidents := getOncallIncidents(config)
template_data.IncidentCount = len(incidents)
// Populate list of incidents for the template
for _, p := range incidents {
......@@ -75,22 +75,22 @@ func GenerateTemplate(config *config.Config) *bytes.Buffer {
inc.CreatedAt = p.CreatedAt
template_data.Incidents = append(template_data.Incidents, inc)
}
oncall_issues := GetIssuesOpenedDuringShift(config)
closed_issues := GetIssuesClosedDuringMilestone(config)
all_open_issues := GetIssuesOpenAll(config)
oncall_issues := getIssuesOpenedDuringShift(config)
closed_issues := getIssuesClosedDuringMilestone(config)
all_open_issues := getIssuesOpenAll(config)
// Issue stats
template_data.IssuesOpenedDuringShift.Count = len(oncall_issues)
template_data.IssuesOpenedDuringShift.AccessRequest = len(FilterIssuesByLabel("access request", oncall_issues))
template_data.IssuesOpenedDuringShift.Critical = len(FilterIssuesByLabel("critical", oncall_issues))
template_data.IssuesOpenedDuringShift.AccessRequest = len(filterIssuesByLabel("access request", oncall_issues))
template_data.IssuesOpenedDuringShift.Critical = len(filterIssuesByLabel("critical", oncall_issues))
template_data.IssuesClosedDuringMilestone.Count = len(closed_issues)
template_data.IssuesClosedDuringMilestone.AccessRequest = len(FilterIssuesByLabel("access request", closed_issues))
template_data.IssuesClosedDuringMilestone.Critical = len(FilterIssuesByLabel("critical", closed_issues))
template_data.IssuesClosedDuringMilestone.AccessRequest = len(filterIssuesByLabel("access request", closed_issues))
template_data.IssuesClosedDuringMilestone.Critical = len(filterIssuesByLabel("critical", closed_issues))
template_data.IssuesOpenAll.Count = len(all_open_issues)
template_data.IssuesOpenAll.AccessRequest = len(FilterIssuesByLabel("access request", all_open_issues))
template_data.IssuesOpenAll.Critical = len(FilterIssuesByLabel("critical", all_open_issues))
template_data.IssuesOpenAll.AccessRequest = len(filterIssuesByLabel("access request", all_open_issues))
template_data.IssuesOpenAll.Critical = len(filterIssuesByLabel("critical", all_open_issues))
// Populate list of issues for the template
for _, p := range all_open_issues {
......
......@@ -17,7 +17,7 @@ milestone: WoW
# Pagerduty configuration
pagerduty:
token: **************
token: "*** PD API TOKEN GOES HERE ***"
service_id: PATDFCE
schedules:
- name: AMA
......@@ -29,7 +29,7 @@ pagerduty:
# GitLab API configuration
gitlab:
token: ***************
token: "*** GL API TOKEN GOES HERE ***"
# Project id of the infrastructure project
id: 1304532
......
---
pagerduty:
token: XXYY
schedules:
- PrimaryID
- SecondaryID
gitlab-token: ZZXX
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment