1
1
package cmd
2
2
3
3
import (
4
- "fmt"
5
4
"net/http"
6
5
"net/http/httptest"
7
6
"net/url"
@@ -32,222 +31,13 @@ type AlertTest struct {
32
31
33
32
func TestAlertCmd (t * testing.T ) {
34
33
35
- alertTestDataSet1 := fmt .Sprintf (`
36
- {
37
- "status": "success",
38
- "data": {
39
- "groups": [
40
- {
41
- "name": "Foo",
42
- "file": "alerts.yaml",
43
- "rules": [
44
- {
45
- "state": "inactive",
46
- "name": "HostOutOfMemory",
47
- "query": "up",
48
- "duration": 120,
49
- "labels": {
50
- "severity": "critical"
51
- },
52
- "annotations": {
53
- "description": "Foo",
54
- "summary": "Foo"
55
- },
56
- "alerts": [],
57
- "health": "ok",
58
- "evaluationTime": 0.000553928,
59
- "lastEvaluation": "2022-11-24T14:08:17.597083058Z",
60
- "type": "alerting"
61
- }
62
- ],
63
- "interval": 10,
64
- "limit": 0,
65
- "evaluationTime": 0.000581212,
66
- "lastEvaluation": "2022-11-24T14:08:17.59706083Z"
67
- },
68
- {
69
- "name": "SQL",
70
- "file": "alerts.yaml",
71
- "rules": [
72
- {
73
- "state": "pending",
74
- "name": "SqlAccessDeniedRate",
75
- "query": "mysql",
76
- "duration": 17280000,
77
- "labels": {
78
- "severity": "warning"
79
- },
80
- "annotations": {
81
- "description": "MySQL",
82
- "summary": "MySQL"
83
- },
84
- "alerts": [
85
- {
86
- "labels": {
87
- "alertname": "SqlAccessDeniedRate",
88
- "instance": "localhost",
89
- "job": "mysql",
90
- "severity": "warning"
91
- },
92
- "annotations": {
93
- "description": "MySQL",
94
- "summary": "MySQL"
95
- },
96
- "state": "pending",
97
- "activeAt": "2022-11-21T10:38:35.373483748Z",
98
- "value": "4.03448275862069e-01"
99
- }
100
- ],
101
- "health": "ok",
102
- "evaluationTime": 0.002909617,
103
- "lastEvaluation": "2022-11-24T14:08:25.375220595Z",
104
- "type": "alerting"
105
- }
106
- ],
107
- "interval": 10,
108
- "limit": 0,
109
- "evaluationTime": 0.003046259,
110
- "lastEvaluation": "2022-11-24T14:08:25.375096825Z"
111
- },
112
- {
113
- "name": "TLS",
114
- "file": "alerts.yaml",
115
- "rules": [
116
- {
117
- "state": "firing",
118
- "name": "BlackboxTLS",
119
- "query": "SSL",
120
- "duration": 0,
121
- "labels": {
122
- "severity": "critical"
123
- },
124
- "annotations": {
125
- "description": "TLS",
126
- "summary": "TLS"
127
- },
128
- "alerts": [
129
- {
130
- "labels": {
131
- "alertname": "TLS",
132
- "instance": "https://localhost:443",
133
- "job": "blackbox",
134
- "severity": "critical"
135
- },
136
- "annotations": {
137
- "description": "TLS",
138
- "summary": "TLS"
139
- },
140
- "state": "firing",
141
- "activeAt": "2022-11-24T05:11:27.211699259Z",
142
- "value": "-6.065338210999966e+06"
143
- }
144
- ],
145
- "health": "ok",
146
- "evaluationTime": 0.000713955,
147
- "lastEvaluation": "2022-11-24T14:08:17.212720815Z",
148
- "type": "alerting"
149
- }
150
- ],
151
- "interval": 10,
152
- "limit": 0,
153
- "evaluationTime": 0.000738927,
154
- "lastEvaluation": "2022-11-24T14:08:17.212700182Z"
155
- }
156
- ]
157
- }
158
- }` )
34
+ alertTestDataSet1 := "../testdata/unittest/alertDataset1.json"
159
35
160
- alertTestDataSet2 := fmt .Sprintf (`
161
- {
162
- "status": "success",
163
- "data": {
164
- "groups": [
165
- {
166
- "name": "Foo",
167
- "file": "alerts.yaml",
168
- "rules": [
169
- {
170
- "state": "inactive",
171
- "name": "InactiveAlert",
172
- "query": "foo",
173
- "duration": 120,
174
- "labels": {
175
- "severity": "critical"
176
- },
177
- "annotations": {
178
- "description": "Inactive",
179
- "summary": "Inactive"
180
- },
181
- "alerts": [],
182
- "health": "ok",
183
- "evaluationTime": 0.000462382,
184
- "lastEvaluation": "2022-11-18T14:01:07.597034323Z",
185
- "type": "alerting"
186
- }
187
- ],
188
- "interval": 10,
189
- "limit": 0,
190
- "evaluationTime": 0.000478395,
191
- "lastEvaluation": "2022-11-18T14:01:07.597021953Z"
192
- }
193
- ]
194
- }
195
- }` )
36
+ alertTestDataSet2 := "../testdata/unittest/alertDataset2.json"
196
37
197
- alertTestDataSet3 := fmt .Sprintf (`
198
- {
199
- "status": "success",
200
- "data": {
201
- "groups": [
202
- {
203
- "name": "k8s",
204
- "file": "/etc/prometheus/rules/al.yaml",
205
- "rules": [
206
- {
207
- "state": "inactive",
208
- "name": "NodeHasMemoryPressure",
209
- "query": "kube_node{condition=\"MemoryPressure\",status=\"true\"} == 1",
210
- "duration": 300,
211
- "keepFiringFor": 0,
212
- "labels": {},
213
- "annotations": {
214
- "summary": "Memory pressure on instance {{ $labels.instance }}"
215
- },
216
- "alerts": [],
217
- "health": "ok",
218
- "evaluationTime": 0.00023339,
219
- "lastEvaluation": "2024-12-18T17:50:01.483161228Z",
220
- "type": "alerting"
221
- }
222
- ],
223
- "interval": 15,
224
- "limit": 0,
225
- "evaluationTime": 0.000262616,
226
- "lastEvaluation": "2024-12-18T17:50:01.483135426Z"
227
- },
228
- {
229
- "name": "example",
230
- "file": "/etc/prometheus/rules/rec.yaml",
231
- "rules": [
232
- {
233
- "name": "rule:prometheus_http_requests_total:sum",
234
- "query": "sum by (code) (rate(prometheus_http_requests_total[5m]))",
235
- "health": "ok",
236
- "evaluationTime": 0.000472562,
237
- "lastEvaluation": "2024-12-18T17:50:12.420737469Z",
238
- "type": "recording"
239
- }
240
- ],
241
- "interval": 15,
242
- "limit": 0,
243
- "evaluationTime": 0.000497618,
244
- "lastEvaluation": "2024-12-18T17:50:12.42071533Z"
245
- }
246
- ],
247
- "groupNextToken:omitempty": ""
248
- }
249
- }
250
- ` )
38
+ alertTestDataSet3 := "../testdata/unittest/alertDataset3.json"
39
+
40
+ alertTestDataSet4 := "../testdata/unittest/alertDataset4.json"
251
41
252
42
tests := []AlertTest {
253
43
{
@@ -290,7 +80,7 @@ func TestAlertCmd(t *testing.T) {
290
80
name : "alert-default" ,
291
81
server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
292
82
w .WriteHeader (http .StatusOK )
293
- w .Write ([] byte (alertTestDataSet1 ))
83
+ w .Write (loadTestdata (alertTestDataSet1 ))
294
84
})),
295
85
args : []string {"run" , "../main.go" , "alert" },
296
86
expected : `[CRITICAL] - 3 Alerts: 1 Firing - 1 Pending - 1 Inactive
@@ -306,7 +96,7 @@ exit status 2
306
96
name : "alert-problems-only" ,
307
97
server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
308
98
w .WriteHeader (http .StatusOK )
309
- w .Write ([] byte (alertTestDataSet1 ))
99
+ w .Write (loadTestdata (alertTestDataSet1 ))
310
100
})),
311
101
args : []string {"run" , "../main.go" , "alert" , "--problems" },
312
102
expected : `[CRITICAL] - 2 Alerts: 1 Firing - 1 Pending - 0 Inactive
@@ -321,7 +111,7 @@ exit status 2
321
111
name : "alert-problems-only-with-exlude-on-one-group" ,
322
112
server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
323
113
w .WriteHeader (http .StatusOK )
324
- w .Write ([] byte (alertTestDataSet1 ))
114
+ w .Write (loadTestdata (alertTestDataSet1 ))
325
115
})),
326
116
args : []string {"run" , "../main.go" , "alert" , "--problems" , "-g" , "TLS" },
327
117
expected : `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
@@ -334,7 +124,7 @@ exit status 2
334
124
name : "alert-problems-only-with-exlude-on-two-groups" ,
335
125
server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
336
126
w .WriteHeader (http .StatusOK )
337
- w .Write ([] byte (alertTestDataSet1 ))
127
+ w .Write (loadTestdata (alertTestDataSet1 ))
338
128
})),
339
129
args : []string {"run" , "../main.go" , "alert" , "--problems" , "-g" , "SQL" , "-g" , "TLS" },
340
130
expected : `[CRITICAL] - 2 Alerts: 1 Firing - 1 Pending - 0 Inactive
@@ -349,7 +139,7 @@ exit status 2
349
139
name : "alert-problems-only-with-exlude" ,
350
140
server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
351
141
w .WriteHeader (http .StatusOK )
352
- w .Write ([] byte (alertTestDataSet1 ))
142
+ w .Write (loadTestdata (alertTestDataSet1 ))
353
143
})),
354
144
args : []string {"run" , "../main.go" , "alert" , "--problems" , "--exclude-alert" , "Sql.*DeniedRate" },
355
145
expected : `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
@@ -362,7 +152,7 @@ exit status 2
362
152
name : "alert-with-exclude-error" ,
363
153
server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
364
154
w .WriteHeader (http .StatusOK )
365
- w .Write ([] byte (alertTestDataSet3 ))
155
+ w .Write (loadTestdata (alertTestDataSet3 ))
366
156
})),
367
157
args : []string {"run" , "../main.go" , "alert" , "--exclude-alert" , "[a-z" },
368
158
expected : "[UNKNOWN] - Invalid regular expression provided: error parsing regexp: missing closing ]: `[a-z`\n exit status 3\n " ,
@@ -371,7 +161,7 @@ exit status 2
371
161
name : "alert-no-such-alert" ,
372
162
server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
373
163
w .WriteHeader (http .StatusOK )
374
- w .Write ([] byte (alertTestDataSet2 ))
164
+ w .Write (loadTestdata (alertTestDataSet2 ))
375
165
})),
376
166
args : []string {"run" , "../main.go" , "alert" , "--name" , "NoSuchAlert" },
377
167
expected : "[UNKNOWN] - 0 Alerts: 0 Firing - 0 Pending - 0 Inactive\n \n exit status 3\n " ,
@@ -380,7 +170,7 @@ exit status 2
380
170
name : "alert-inactive-with-problems" ,
381
171
server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
382
172
w .WriteHeader (http .StatusOK )
383
- w .Write ([] byte (alertTestDataSet2 ))
173
+ w .Write (loadTestdata (alertTestDataSet2 ))
384
174
})),
385
175
args : []string {"run" , "../main.go" , "alert" , "--name" , "InactiveAlert" , "--problems" },
386
176
expected : "[UNKNOWN] - 0 Alerts: 0 Firing - 0 Pending - 0 Inactive\n \n exit status 3\n " ,
@@ -389,7 +179,7 @@ exit status 2
389
179
name : "alert-multiple-alerts" ,
390
180
server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
391
181
w .WriteHeader (http .StatusOK )
392
- w .Write ([] byte (alertTestDataSet1 ))
182
+ w .Write (loadTestdata (alertTestDataSet1 ))
393
183
})),
394
184
args : []string {"run" , "../main.go" , "alert" , "--name" , "HostOutOfMemory" , "--name" , "BlackboxTLS" },
395
185
expected : `[CRITICAL] - 2 Alerts: 1 Firing - 0 Pending - 1 Inactive
@@ -404,7 +194,7 @@ exit status 2
404
194
name : "alert-multiple-alerts-problems-only" ,
405
195
server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
406
196
w .WriteHeader (http .StatusOK )
407
- w .Write ([] byte (alertTestDataSet1 ))
197
+ w .Write (loadTestdata (alertTestDataSet1 ))
408
198
})),
409
199
args : []string {"run" , "../main.go" , "alert" , "--name" , "HostOutOfMemory" , "--name" , "BlackboxTLS" , "--problems" },
410
200
expected : `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
@@ -418,7 +208,7 @@ exit status 2
418
208
name : "alert-inactive" ,
419
209
server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
420
210
w .WriteHeader (http .StatusOK )
421
- w .Write ([] byte (alertTestDataSet2 ))
211
+ w .Write (loadTestdata (alertTestDataSet2 ))
422
212
})),
423
213
args : []string {"run" , "../main.go" , "alert" , "--name" , "InactiveAlert" },
424
214
expected : "[OK] - 1 Alerts: 0 Firing - 0 Pending - 1 Inactive\n \\ _ [OK] [InactiveAlert] is inactive\n |firing=0 pending=0 inactive=1\n \n " ,
@@ -427,7 +217,7 @@ exit status 2
427
217
name : "alert-recording-rule" ,
428
218
server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
429
219
w .WriteHeader (http .StatusOK )
430
- w .Write ([] byte ( `{"status":"success","data":{"groups":[{"name":"example","file":"recoding.yaml","rules":[{"name":"job:foo","query":"sum by(job) (requests_total)","health":"ok","evaluationTime":0.000391321,"lastEvaluation":"2023-01-13T14:26:08.687065894Z","type":"recording"}],"interval":10,"evaluationTime":0.000403777,"lastEvaluation":"2023-01-13T14:26:08.687058029Z"},{"name":"Foo","file":"alerts.yaml","rules":[{"state":"inactive","name":"InactiveAlert","query":"foo","duration":120,"labels":{"severity":"critical"},"annotations":{"description":"Inactive","summary":"Inactive"},"alerts":[],"health":"ok","evaluationTime":0.000462382,"lastEvaluation":"2022-11-18T14:01:07.597034323Z","type":"alerting"}],"interval":10,"limit":0,"evaluationTime":0.000478395,"lastEvaluation":"2022-11-18T14:01:07.597021953Z"}]}}` ))
220
+ w .Write (loadTestdata ( alertTestDataSet4 ))
431
221
})),
432
222
args : []string {"run" , "../main.go" , "alert" , "--name" , "InactiveAlert" },
433
223
expected : "[OK] - 1 Alerts: 0 Firing - 0 Pending - 1 Inactive\n \\ _ [OK] [InactiveAlert] is inactive\n |firing=0 pending=0 inactive=1\n \n " ,
0 commit comments