@@ -39,6 +39,8 @@ func TestAlertCmd(t *testing.T) {
3939
4040 alertTestDataSet4 := "../testdata/unittest/alertDataset4.json"
4141
42+ alertTestDataSet5 := "../testdata/unittest/alertDataset5.json"
43+
4244 tests := []AlertTest {
4345 {
4446 name : "alert-none" ,
@@ -85,8 +87,8 @@ func TestAlertCmd(t *testing.T) {
8587 args : []string {"run" , "../main.go" , "alert" },
8688 expected : `[CRITICAL] - 3 Alerts: 1 Firing - 1 Pending - 1 Inactive
8789\_ [OK] [HostOutOfMemory] is inactive
88- \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning"}
89- \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
90+ \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning","team":"database" }
91+ \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","icinga":"ok"," instance":"https://localhost:443","job":"blackbox","severity":"critical","team":"network "}
9092|total=3 firing=1 pending=1 inactive=1
9193
9294exit status 2
@@ -100,8 +102,8 @@ exit status 2
100102 })),
101103 args : []string {"run" , "../main.go" , "alert" , "--problems" },
102104 expected : `[CRITICAL] - 2 Alerts: 1 Firing - 1 Pending - 0 Inactive
103- \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning"}
104- \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
105+ \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning","team":"database" }
106+ \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","icinga":"ok"," instance":"https://localhost:443","job":"blackbox","severity":"critical","team":"network "}
105107|total=2 firing=1 pending=1 inactive=0
106108
107109exit status 2
@@ -115,7 +117,7 @@ exit status 2
115117 })),
116118 args : []string {"run" , "../main.go" , "alert" , "--problems" , "-g" , "TLS" },
117119 expected : `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
118- \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
120+ \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","icinga":"ok"," instance":"https://localhost:443","job":"blackbox","severity":"critical","team":"network "}
119121|total=1 firing=1 pending=0 inactive=0
120122
121123exit status 2
@@ -129,8 +131,8 @@ exit status 2
129131 })),
130132 args : []string {"run" , "../main.go" , "alert" , "--problems" , "-g" , "SQL" , "-g" , "TLS" },
131133 expected : `[CRITICAL] - 2 Alerts: 1 Firing - 1 Pending - 0 Inactive
132- \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning"}
133- \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
134+ \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning","team":"database" }
135+ \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","icinga":"ok"," instance":"https://localhost:443","job":"blackbox","severity":"critical","team":"network "}
134136|total=2 firing=1 pending=1 inactive=0
135137
136138exit status 2
@@ -144,7 +146,7 @@ exit status 2
144146 })),
145147 args : []string {"run" , "../main.go" , "alert" , "--problems" , "--exclude-alert" , "Sql.*DeniedRate" },
146148 expected : `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
147- \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
149+ \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","icinga":"ok"," instance":"https://localhost:443","job":"blackbox","severity":"critical","team":"network "}
148150|total=1 firing=1 pending=0 inactive=0
149151
150152exit status 2
@@ -196,7 +198,7 @@ exit status 3
196198 args : []string {"run" , "../main.go" , "alert" , "--name" , "HostOutOfMemory" , "--name" , "BlackboxTLS" },
197199 expected : `[CRITICAL] - 2 Alerts: 1 Firing - 0 Pending - 1 Inactive
198200\_ [OK] [HostOutOfMemory] is inactive
199- \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
201+ \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","icinga":"ok"," instance":"https://localhost:443","job":"blackbox","severity":"critical","team":"network "}
200202|total=2 firing=1 pending=0 inactive=1
201203
202204exit status 2
@@ -210,7 +212,7 @@ exit status 2
210212 })),
211213 args : []string {"run" , "../main.go" , "alert" , "--name" , "HostOutOfMemory" , "--name" , "BlackboxTLS" , "--problems" },
212214 expected : `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
213- \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
215+ \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","icinga":"ok"," instance":"https://localhost:443","job":"blackbox","severity":"critical","team":"network "}
214216|total=1 firing=1 pending=0 inactive=0
215217
216218exit status 2
@@ -250,10 +252,10 @@ exit status 2
250252 w .Write (loadTestdata (alertTestDataSet1 ))
251253 })),
252254 args : []string {"run" , "../main.go" , "alert" , "--include-label" , "severity=critical" },
253- expected : `[CRITICAL] - 2 Alerts: 1 Firing - 0 Pending - 1 Inactive
255+ expected : `[CRITICAL] - 3 Alerts: 1 Firing - 1 Pending - 1 Inactive
254256\_ [OK] [HostOutOfMemory] is inactive
255- \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
256- |total=2 firing=1 pending=0 inactive=1
257+ \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","icinga":"ok"," instance":"https://localhost:443","job":"blackbox","severity":"critical","team":"network "}
258+ |total=3 firing=1 pending=1 inactive=1
257259
258260exit status 2
259261` ,
@@ -266,7 +268,21 @@ exit status 2
266268 })),
267269 args : []string {"run" , "../main.go" , "alert" , "--exclude-label" , "severity=critical" },
268270 expected : `[WARNING] - 1 Alerts: 0 Firing - 1 Pending - 0 Inactive
269- \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning"}
271+ \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning","team":"database"}
272+ |total=1 firing=0 pending=1 inactive=0
273+
274+ exit status 1
275+ ` ,
276+ },
277+ {
278+ name : "alert-exclude-label-regex" ,
279+ server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
280+ w .WriteHeader (http .StatusOK )
281+ w .Write (loadTestdata (alertTestDataSet1 ))
282+ })),
283+ args : []string {"run" , "../main.go" , "alert" , "--exclude-label" , "severity=crit.*" },
284+ expected : `[WARNING] - 1 Alerts: 0 Firing - 1 Pending - 0 Inactive
285+ \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning","team":"database"}
270286|total=1 firing=0 pending=1 inactive=0
271287
272288exit status 1
@@ -281,8 +297,24 @@ exit status 1
281297 args : []string {"run" , "../main.go" , "alert" , "--include-label" , "team=database" , "--include-label" , "severity=critical" },
282298 expected : `[CRITICAL] - 3 Alerts: 1 Firing - 1 Pending - 1 Inactive
283299\_ [OK] [HostOutOfMemory] is inactive
284- \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning"}
285- \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
300+ \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning","team":"database"}
301+ \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","icinga":"ok","instance":"https://localhost:443","job":"blackbox","severity":"critical","team":"network"}
302+ |total=3 firing=1 pending=1 inactive=1
303+
304+ exit status 2
305+ ` ,
306+ },
307+ {
308+ name : "alert-include-label-multiple-regex" ,
309+ server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
310+ w .WriteHeader (http .StatusOK )
311+ w .Write (loadTestdata (alertTestDataSet1 ))
312+ })),
313+ args : []string {"run" , "../main.go" , "alert" , "--include-label" , "team=data.+" , "--include-label" , "severity=critical" },
314+ expected : `[CRITICAL] - 3 Alerts: 1 Firing - 1 Pending - 1 Inactive
315+ \_ [OK] [HostOutOfMemory] is inactive
316+ \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning","team":"database"}
317+ \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","icinga":"ok","instance":"https://localhost:443","job":"blackbox","severity":"critical","team":"network"}
286318|total=3 firing=1 pending=1 inactive=1
287319
288320exit status 2
@@ -297,8 +329,8 @@ exit status 2
297329 args : []string {"run" , "../main.go" , "alert" , "--include-label" , "severity=warning" , "--include-label" , "severity=critical" },
298330 expected : `[CRITICAL] - 3 Alerts: 1 Firing - 1 Pending - 1 Inactive
299331\_ [OK] [HostOutOfMemory] is inactive
300- \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning"}
301- \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
332+ \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning","team":"database" }
333+ \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","icinga":"ok"," instance":"https://localhost:443","job":"blackbox","severity":"critical","team":"network "}
302334|total=3 firing=1 pending=1 inactive=1
303335
304336exit status 2
@@ -322,11 +354,26 @@ exit status 2
322354 args : []string {"run" , "../main.go" , "alert" , "--label-key-state=icinga" },
323355 expected : `[WARNING] - 3 Alerts: 1 Firing - 1 Pending - 1 Inactive
324356\_ [OK] [HostOutOfMemory] is inactive
325- \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning"}
326- \_ [OK] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","instance":"https://localhost:443","job":"blackbox","severity":"critical"}
357+ \_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning","team":"database" }
358+ \_ [OK] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","icinga":"ok"," instance":"https://localhost:443","job":"blackbox","severity":"critical","team":"network "}
327359|total=3 firing=1 pending=1 inactive=1
328360
329361exit status 1
362+ ` ,
363+ },
364+ {
365+ name : "alert-include-with-name-and-regex" ,
366+ server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
367+ w .WriteHeader (http .StatusOK )
368+ w .Write (loadTestdata (alertTestDataSet5 ))
369+ })),
370+ args : []string {"run" , "../main.go" , "alert" , "--name" , "ContainerKilled" , "--include-label" , "name=(mosquitto|nodered)" },
371+ expected : `[CRITICAL] - 3 Alerts: 3 Firing - 0 Pending - 0 Inactive
372+ \_ [CRITICAL] [ContainerKilled] - Job: [cadvisor] on Instance: [example:8888] is firing - value: 123.40 - {"alertname":"ContainerKilled","instance":"example:8888","job":"cadvisor","name":"nodered","severity":"warning"}
373+ \_ [CRITICAL] [ContainerKilled] - Job: [cadvisor] on Instance: [example:8888] is firing - value: 123.40 - {"alertname":"ContainerKilled","instance":"example:8888","job":"cadvisor","name":"mosquitto","severity":"warning"}
374+ |total=3 firing=3 pending=0 inactive=0
375+
376+ exit status 2
330377` ,
331378 },
332379 }
0 commit comments