@@ -2,7 +2,6 @@ package services
2
2
3
3
import (
4
4
"errors"
5
- "os"
6
5
"testing"
7
6
"text/template"
8
7
@@ -53,6 +52,125 @@ func TestGetTemplater_PagerDuty(t *testing.T) {
53
52
assert .Equal (t , "http://example.com" , notification .Pagerduty .URL )
54
53
})
55
54
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
+
56
174
t .Run ("only required parameters specified" , func (t * testing.T ) {
57
175
n := Notification {
58
176
Pagerduty : & PagerDutyNotification {
@@ -87,58 +205,38 @@ func TestGetTemplater_PagerDuty(t *testing.T) {
87
205
}
88
206
89
207
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/"
117
217
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
+ },
140
229
})
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
+ })
142
240
143
241
t .Run ("missing config" , func (t * testing.T ) {
144
242
service := NewPagerdutyService (PagerdutyOptions {
0 commit comments