Skip to content

Commit 223941c

Browse files
committed
fix: Improve test coverage
1 parent 8f6c6cf commit 223941c

File tree

2 files changed

+162
-58
lines changed

2 files changed

+162
-58
lines changed

pkg/services/pagerduty.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ func (p pagerdutyService) Send(notification Notification, dest Destination) erro
122122
return fmt.Errorf("no config found for pagerduty")
123123
}
124124

125+
event := buildEvent(routingKey, notification)
126+
127+
response, err := pagerduty.ManageEventWithContext(context.TODO(), event)
128+
if err != nil {
129+
log.Errorf("Error: %v", err)
130+
return err
131+
}
132+
log.Debugf("PagerDuty event triggered succesfully. Status: %v, Message: %v", response.Status, response.Message)
133+
return nil
134+
}
135+
136+
func buildEvent(routingKey string, notification Notification) pagerduty.V2Event {
125137
payload := pagerduty.V2Payload{
126138
Summary: notification.Pagerduty.Summary,
127139
Severity: notification.Pagerduty.Severity,
@@ -149,11 +161,5 @@ func (p pagerdutyService) Send(notification Notification, dest Destination) erro
149161
event.ClientURL = notification.Pagerduty.URL
150162
}
151163

152-
response, err := pagerduty.ManageEventWithContext(context.TODO(), event)
153-
if err != nil {
154-
log.Errorf("Error: %v", err)
155-
return err
156-
}
157-
log.Debugf("PagerDuty event triggered succesfully. Status: %v, Message: %v", response.Status, response.Message)
158-
return nil
164+
return event
159165
}

pkg/services/pagerduty_test.go

+149-51
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package services
22

33
import (
44
"errors"
5-
"os"
65
"testing"
76
"text/template"
87

@@ -53,6 +52,125 @@ func TestGetTemplater_PagerDuty(t *testing.T) {
5352
assert.Equal(t, "http://example.com", notification.Pagerduty.URL)
5453
})
5554

55+
t.Run("handle error for summary", func(t *testing.T) {
56+
n := Notification{
57+
Pagerduty: &PagerDutyNotification{
58+
Summary: "{{.summary}",
59+
Severity: "{{.severity}",
60+
Source: "{{.source}",
61+
Component: "{{.component}",
62+
Group: "{{.group}",
63+
Class: "{{.class}",
64+
URL: "{{.url}",
65+
},
66+
}
67+
68+
_, err := n.GetTemplater("", template.FuncMap{})
69+
assert.Error(t, err)
70+
})
71+
72+
t.Run("handle error for severity", func(t *testing.T) {
73+
n := Notification{
74+
Pagerduty: &PagerDutyNotification{
75+
Summary: "{{.summary}}",
76+
Severity: "{{.severity}",
77+
Source: "{{.source}",
78+
Component: "{{.component}",
79+
Group: "{{.group}",
80+
Class: "{{.class}",
81+
URL: "{{.url}",
82+
},
83+
}
84+
85+
_, err := n.GetTemplater("", template.FuncMap{})
86+
assert.Error(t, err)
87+
})
88+
89+
t.Run("handle error for source", func(t *testing.T) {
90+
n := Notification{
91+
Pagerduty: &PagerDutyNotification{
92+
Summary: "{{.summary}}",
93+
Severity: "{{.severity}}",
94+
Source: "{{.source}",
95+
Component: "{{.component}",
96+
Group: "{{.group}",
97+
Class: "{{.class}",
98+
URL: "{{.url}",
99+
},
100+
}
101+
102+
_, err := n.GetTemplater("", template.FuncMap{})
103+
assert.Error(t, err)
104+
})
105+
106+
t.Run("handle error for component", func(t *testing.T) {
107+
n := Notification{
108+
Pagerduty: &PagerDutyNotification{
109+
Summary: "{{.summary}}",
110+
Severity: "{{.severity}}",
111+
Source: "{{.source}}",
112+
Component: "{{.component}",
113+
Group: "{{.group}",
114+
Class: "{{.class}",
115+
URL: "{{.url}",
116+
},
117+
}
118+
119+
_, err := n.GetTemplater("", template.FuncMap{})
120+
assert.Error(t, err)
121+
})
122+
123+
t.Run("handle error for group", func(t *testing.T) {
124+
n := Notification{
125+
Pagerduty: &PagerDutyNotification{
126+
Summary: "{{.summary}}",
127+
Severity: "{{.severity}}",
128+
Source: "{{.source}}",
129+
Component: "{{.component}}",
130+
Group: "{{.group}",
131+
Class: "{{.class}",
132+
URL: "{{.url}",
133+
},
134+
}
135+
136+
_, err := n.GetTemplater("", template.FuncMap{})
137+
assert.Error(t, err)
138+
})
139+
140+
t.Run("handle error for class", func(t *testing.T) {
141+
n := Notification{
142+
Pagerduty: &PagerDutyNotification{
143+
Summary: "{{.summary}}",
144+
Severity: "{{.severity}}",
145+
Source: "{{.source}}",
146+
Component: "{{.component}}",
147+
Group: "{{.group}}",
148+
Class: "{{.class}",
149+
URL: "{{.url}",
150+
},
151+
}
152+
153+
_, err := n.GetTemplater("", template.FuncMap{})
154+
assert.Error(t, err)
155+
})
156+
157+
t.Run("handle error for url", func(t *testing.T) {
158+
n := Notification{
159+
Pagerduty: &PagerDutyNotification{
160+
Summary: "{{.summary}}",
161+
Severity: "{{.severity}}",
162+
Source: "{{.source}}",
163+
Component: "{{.component}}",
164+
Group: "{{.group}}",
165+
Class: "{{.class}}",
166+
URL: "{{.url}",
167+
},
168+
}
169+
170+
_, err := n.GetTemplater("", template.FuncMap{})
171+
assert.Error(t, err)
172+
})
173+
56174
t.Run("only required parameters specified", func(t *testing.T) {
57175
n := Notification{
58176
Pagerduty: &PagerDutyNotification{
@@ -87,58 +205,38 @@ func TestGetTemplater_PagerDuty(t *testing.T) {
87205
}
88206

89207
func TestSend_PagerDuty(t *testing.T) {
90-
if len(os.Getenv("PAGERDUTY_KEY")) > 0 {
91-
t.Run("full payload", func(t *testing.T) {
92-
service := NewPagerdutyService(PagerdutyOptions{
93-
ServiceKeys: map[string]string{
94-
"test-service": os.Getenv("PAGERDUTY_KEY"),
95-
},
96-
})
97-
err := service.Send(Notification{
98-
Message: "message",
99-
Pagerduty: &PagerDutyNotification{
100-
Summary: "test-app failed to deploy",
101-
Severity: "error",
102-
Source: "test-app",
103-
Component: "test-component",
104-
Group: "platform",
105-
Class: "test-class",
106-
URL: "https://www.example.com/",
107-
},
108-
}, Destination{
109-
Service: "pagerduty",
110-
Recipient: "test-service",
111-
})
112-
113-
if !assert.NoError(t, err) {
114-
t.FailNow()
115-
}
116-
})
208+
t.Run("builds event with full payload", func(t *testing.T) {
209+
routingKey := "routing-key"
210+
summary := "test-app failed to deploy"
211+
severity := "error"
212+
source := "test-app"
213+
component := "test-component"
214+
group := "platform"
215+
class := "test-class"
216+
url := "https://www.example.com/"
117217

118-
t.Run("partial payload", func(t *testing.T) {
119-
service := NewPagerdutyService(PagerdutyOptions{
120-
ServiceKeys: map[string]string{
121-
"test-service": os.Getenv("PAGERDUTY_KEY"),
122-
},
123-
})
124-
err := service.Send(Notification{
125-
Message: "message",
126-
Pagerduty: &PagerDutyNotification{
127-
Summary: "test-app failed to deploy",
128-
Severity: "error",
129-
Source: "test-app",
130-
URL: "https://www.example.com/",
131-
},
132-
}, Destination{
133-
Service: "pagerduty",
134-
Recipient: "test-service",
135-
})
136-
137-
if !assert.NoError(t, err) {
138-
t.FailNow()
139-
}
218+
event := buildEvent(routingKey, Notification{
219+
Message: "message",
220+
Pagerduty: &PagerDutyNotification{
221+
Summary: summary,
222+
Severity: severity,
223+
Source: source,
224+
Component: component,
225+
Group: group,
226+
Class: class,
227+
URL: url,
228+
},
140229
})
141-
}
230+
231+
assert.Equal(t, routingKey, event.RoutingKey)
232+
assert.Equal(t, summary, event.Payload.Summary)
233+
assert.Equal(t, severity, event.Payload.Severity)
234+
assert.Equal(t, source, event.Payload.Source)
235+
assert.Equal(t, component, event.Payload.Component)
236+
assert.Equal(t, group, event.Payload.Group)
237+
assert.Equal(t, class, event.Payload.Class)
238+
assert.Equal(t, url, event.ClientURL)
239+
})
142240

143241
t.Run("missing config", func(t *testing.T) {
144242
service := NewPagerdutyService(PagerdutyOptions{

0 commit comments

Comments
 (0)