File size: 5,682 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package smoke

import (
	"encoding/json"
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/target/goalert/test/smoke/harness"
)

func TestAlertLog(t *testing.T) {
	t.Parallel()

	type alertLogs struct {
		Alert struct {
			RecentEvents struct {
				Nodes []struct {
					Message string
					State   struct {
						Details string
					}
				}
			}
		}
	}

	type config struct {
		CMDisabled bool
		CMType     string
		NR         bool
		EPStep     bool
		EPStepUser bool
	}

	doQL := func(t *testing.T, h *harness.Harness, query string, res interface{}) {
		t.Helper()
		g := h.GraphQLQuery2(query)
		for _, err := range g.Errors {
			t.Error("GraphQL Error:", err.Message)
		}

		if len(g.Errors) > 0 {
			t.Fatal("errors returned from GraphQL")
		}

		t.Log("Response:", string(g.Data))
		if res == nil {
			return
		}

		err := json.Unmarshal(g.Data, &res)
		if err != nil {
			t.Fatal("failed to parse response:", err)
		}
	}

	makeCreateAlertMut := func(h *harness.Harness) string {
		return fmt.Sprintf(
			`mutation {
				createAlert(input: {
					summary: "foo",
					serviceID: "%s"
				}){ id }
			}`,
			h.UUID("sid"),
		)
	}

	const alertLogSQLTmpl = `
		insert into users (id, name, email) 
		values ({{uuid "user"}}, 'bob', 'joe');

		insert into user_contact_methods (id, user_id, name, type, value, disabled) 
		values ({{uuid "cm1"}}, {{uuid "user"}}, 'personal', '{{.CMType}}', {{phone "1"}}, {{.CMDisabled}});

		{{- if .NR}}
		insert into user_notification_rules (user_id, contact_method_id, delay_minutes) 
		values ({{uuid "user"}}, {{uuid "cm1"}}, 0);
		{{- end}}

		insert into escalation_policies (id, name) 
		values ({{uuid "eid"}}, 'esc policy');

		{{- if .EPStep}}
		insert into escalation_policy_steps (id, escalation_policy_id) 
		values ({{uuid "esid"}}, {{uuid "eid"}});
		{{- end}}

		{{- if and .EPStep .EPStepUser}}
		insert into escalation_policy_actions (escalation_policy_step_id, user_id) 
		values ({{uuid "esid"}}, {{uuid "user"}});
		{{- end}}

		insert into services (id, escalation_policy_id, name) 
		values ({{uuid "sid"}}, {{uuid "eid"}}, 'service');
	`

	check := func(desc string, c config, before func(*testing.T, *harness.Harness), after func(*testing.T, *harness.Harness, alertLogs)) {
		t.Run(desc, func(t *testing.T) {
			// setup sql
			t.Parallel()
			h := harness.NewHarnessWithData(t, alertLogSQLTmpl, c, "add-no-notification-alert-log")
			defer h.Close()

			// create alert
			doQL(t, h, makeCreateAlertMut(h), nil)

			if before != nil {
				before(t, h)
			}
			h.Trigger()

			// get logs
			var l alertLogs
			doQL(t, h, fmt.Sprintf(`
			query {
					alert(id: %d) {
						recentEvents(input: { limit: 15 }) {
							nodes {
								message
								state {
									details
								}
							}
						}
					}
			}
		`, 1), &l)

			// make assertions
			after(t, h, l)
		})
	}

	// test successful notification sent
	check("NotificationSentSuccess", config{
		CMDisabled: false,
		CMType:     "SMS",
		NR:         true,
		EPStep:     true,
		EPStepUser: true,
	}, nil, func(t *testing.T, h *harness.Harness, l alertLogs) {
		msg := l.Alert.RecentEvents.Nodes[0].Message
		assert.Contains(t, msg, "Notification sent")
		h.Twilio(t).Device(h.Phone("1")).ExpectSMS("Alert #1: foo")
	})

	// test disabled contact method
	check("DisabledContactMethod", config{
		CMDisabled: true,
		CMType:     "SMS",
		NR:         true,
		EPStep:     true,
		EPStepUser: true,
	}, nil, func(t *testing.T, h *harness.Harness, l alertLogs) {
		details := l.Alert.RecentEvents.Nodes[0].State.Details
		assert.Contains(t, details, "contact method disabled")
	})

	// test SMS failure
	check("SMSFailure", config{
		CMDisabled: false,
		CMType:     "SMS",
		NR:         true,
		EPStep:     true,
		EPStepUser: true,
	}, func(t *testing.T, h *harness.Harness) {
		h.Twilio(t).Device(h.Phone("1")).RejectSMS("Alert #1: foo")
	}, func(t *testing.T, h *harness.Harness, l alertLogs) {
		msg := l.Alert.RecentEvents.Nodes[0].Message
		details := l.Alert.RecentEvents.Nodes[0].State.Details
		assert.Contains(t, msg, "Notification sent")
		assert.Contains(t, details, "failed")
	})

	// test VOICE failure
	check("VOICEFailure", config{
		CMDisabled: false,
		CMType:     "VOICE",
		NR:         true,
		EPStep:     true,
		EPStepUser: true,
	}, func(t *testing.T, h *harness.Harness) {
		h.Twilio(t).Device(h.Phone("1")).RejectVoice("foo")
	}, func(t *testing.T, h *harness.Harness, l alertLogs) {
		msg := l.Alert.RecentEvents.Nodes[0].Message
		details := l.Alert.RecentEvents.Nodes[0].State.Details
		assert.Contains(t, msg, "Notification sent")
		assert.Contains(t, details, "failed")
	})

	// test no immediate notification rule
	check("NoImmediateNR", config{
		CMDisabled: false,
		CMType:     "SMS",
		NR:         false,
		EPStep:     true,
		EPStepUser: true,
	}, nil, func(t *testing.T, h *harness.Harness, l alertLogs) {
		msg := l.Alert.RecentEvents.Nodes[0].Message
		assert.Contains(t, msg, "no immediate rule")
	})

	// test no on-call users
	check("NoOnCallUsers", config{
		CMDisabled: false,
		CMType:     "SMS",
		NR:         true,
		EPStep:     true,
		EPStepUser: false,
	}, nil, func(t *testing.T, h *harness.Harness, l alertLogs) {
		details := l.Alert.RecentEvents.Nodes[0].State.Details
		assert.Contains(t, details, "No one was on-call")
	})

	// test no steps on an escalation policy
	check("NoEPSteps", config{
		CMDisabled: false,
		CMType:     "SMS",
		NR:         true,
		EPStep:     false,
		EPStepUser: false,
	}, nil, func(t *testing.T, h *harness.Harness, l alertLogs) {
		details := l.Alert.RecentEvents.Nodes[0].State.Details
		assert.Contains(t, details, "No escalation policy steps")
	})
}