11package cmd
22
33import (
4- "fmt"
54 "net/http"
65 "net/http/httptest"
76 "net/url"
@@ -32,222 +31,13 @@ type AlertTest struct {
3231
3332func TestAlertCmd (t * testing.T ) {
3433
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"
15935
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"
19637
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"
25141
25242 tests := []AlertTest {
25343 {
@@ -290,7 +80,7 @@ func TestAlertCmd(t *testing.T) {
29080 name : "alert-default" ,
29181 server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
29282 w .WriteHeader (http .StatusOK )
293- w .Write ([] byte (alertTestDataSet1 ))
83+ w .Write (loadTestdata (alertTestDataSet1 ))
29484 })),
29585 args : []string {"run" , "../main.go" , "alert" },
29686 expected : `[CRITICAL] - 3 Alerts: 1 Firing - 1 Pending - 1 Inactive
@@ -306,7 +96,7 @@ exit status 2
30696 name : "alert-problems-only" ,
30797 server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
30898 w .WriteHeader (http .StatusOK )
309- w .Write ([] byte (alertTestDataSet1 ))
99+ w .Write (loadTestdata (alertTestDataSet1 ))
310100 })),
311101 args : []string {"run" , "../main.go" , "alert" , "--problems" },
312102 expected : `[CRITICAL] - 2 Alerts: 1 Firing - 1 Pending - 0 Inactive
@@ -321,7 +111,7 @@ exit status 2
321111 name : "alert-problems-only-with-exlude-on-one-group" ,
322112 server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
323113 w .WriteHeader (http .StatusOK )
324- w .Write ([] byte (alertTestDataSet1 ))
114+ w .Write (loadTestdata (alertTestDataSet1 ))
325115 })),
326116 args : []string {"run" , "../main.go" , "alert" , "--problems" , "-g" , "TLS" },
327117 expected : `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
@@ -334,7 +124,7 @@ exit status 2
334124 name : "alert-problems-only-with-exlude-on-two-groups" ,
335125 server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
336126 w .WriteHeader (http .StatusOK )
337- w .Write ([] byte (alertTestDataSet1 ))
127+ w .Write (loadTestdata (alertTestDataSet1 ))
338128 })),
339129 args : []string {"run" , "../main.go" , "alert" , "--problems" , "-g" , "SQL" , "-g" , "TLS" },
340130 expected : `[CRITICAL] - 2 Alerts: 1 Firing - 1 Pending - 0 Inactive
@@ -349,7 +139,7 @@ exit status 2
349139 name : "alert-problems-only-with-exlude" ,
350140 server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
351141 w .WriteHeader (http .StatusOK )
352- w .Write ([] byte (alertTestDataSet1 ))
142+ w .Write (loadTestdata (alertTestDataSet1 ))
353143 })),
354144 args : []string {"run" , "../main.go" , "alert" , "--problems" , "--exclude-alert" , "Sql.*DeniedRate" },
355145 expected : `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
@@ -362,7 +152,7 @@ exit status 2
362152 name : "alert-with-exclude-error" ,
363153 server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
364154 w .WriteHeader (http .StatusOK )
365- w .Write ([] byte (alertTestDataSet3 ))
155+ w .Write (loadTestdata (alertTestDataSet3 ))
366156 })),
367157 args : []string {"run" , "../main.go" , "alert" , "--exclude-alert" , "[a-z" },
368158 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
371161 name : "alert-no-such-alert" ,
372162 server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
373163 w .WriteHeader (http .StatusOK )
374- w .Write ([] byte (alertTestDataSet2 ))
164+ w .Write (loadTestdata (alertTestDataSet2 ))
375165 })),
376166 args : []string {"run" , "../main.go" , "alert" , "--name" , "NoSuchAlert" },
377167 expected : "[UNKNOWN] - 0 Alerts: 0 Firing - 0 Pending - 0 Inactive\n \n exit status 3\n " ,
@@ -380,7 +170,7 @@ exit status 2
380170 name : "alert-inactive-with-problems" ,
381171 server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
382172 w .WriteHeader (http .StatusOK )
383- w .Write ([] byte (alertTestDataSet2 ))
173+ w .Write (loadTestdata (alertTestDataSet2 ))
384174 })),
385175 args : []string {"run" , "../main.go" , "alert" , "--name" , "InactiveAlert" , "--problems" },
386176 expected : "[UNKNOWN] - 0 Alerts: 0 Firing - 0 Pending - 0 Inactive\n \n exit status 3\n " ,
@@ -389,7 +179,7 @@ exit status 2
389179 name : "alert-multiple-alerts" ,
390180 server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
391181 w .WriteHeader (http .StatusOK )
392- w .Write ([] byte (alertTestDataSet1 ))
182+ w .Write (loadTestdata (alertTestDataSet1 ))
393183 })),
394184 args : []string {"run" , "../main.go" , "alert" , "--name" , "HostOutOfMemory" , "--name" , "BlackboxTLS" },
395185 expected : `[CRITICAL] - 2 Alerts: 1 Firing - 0 Pending - 1 Inactive
@@ -404,7 +194,7 @@ exit status 2
404194 name : "alert-multiple-alerts-problems-only" ,
405195 server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
406196 w .WriteHeader (http .StatusOK )
407- w .Write ([] byte (alertTestDataSet1 ))
197+ w .Write (loadTestdata (alertTestDataSet1 ))
408198 })),
409199 args : []string {"run" , "../main.go" , "alert" , "--name" , "HostOutOfMemory" , "--name" , "BlackboxTLS" , "--problems" },
410200 expected : `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
@@ -418,7 +208,7 @@ exit status 2
418208 name : "alert-inactive" ,
419209 server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
420210 w .WriteHeader (http .StatusOK )
421- w .Write ([] byte (alertTestDataSet2 ))
211+ w .Write (loadTestdata (alertTestDataSet2 ))
422212 })),
423213 args : []string {"run" , "../main.go" , "alert" , "--name" , "InactiveAlert" },
424214 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
427217 name : "alert-recording-rule" ,
428218 server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
429219 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 ))
431221 })),
432222 args : []string {"run" , "../main.go" , "alert" , "--name" , "InactiveAlert" },
433223 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