forked from mdlayher/unifi_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalarmcollector_test.go
74 lines (63 loc) · 1.5 KB
/
alarmcollector_test.go
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
package unifiexporter
import (
"regexp"
"strings"
"testing"
"github.com/mdlayher/unifi"
)
func TestAlarmCollector(t *testing.T) {
var tests = []struct {
desc string
input string
sites []*unifi.Site
matches []*regexp.Regexp
}{
{
desc: "one alarm, one site",
input: strings.TrimSpace(`
{
"data": [
{
"_id": "abcdef",
"ap": "de:ad:be:ef:de:ad",
"ap_name": "foo",
"archived": false,
"datetime": "2018-09-08T18:56:12Z",
"key": "EVT_AP_Lost_Contact",
"msg": "AP[de:ad:be:ef:de:ad] was disconnected",
"subsystem": "wlan",
"time": 1536432972388
}
]
}
`),
matches: []*regexp.Regexp{
regexp.MustCompile(`unifi_alarms_total{site="Default"} 1`),
regexp.MustCompile(`unifi_alarms{id="abcdef",key="EVT_AP_Lost_Contact",mac="de:ad:be:ef:de:ad",message="AP\[de:ad:be:ef:de:ad\] was disconnected",name="foo",site="Default",subsytem="wlan"} 1`),
},
sites: []*unifi.Site{{
Name: "default",
Description: "Default",
}},
},
}
for i, tt := range tests {
t.Logf("[%02d] test %q", i, tt.desc)
out := testAlarmCollector(t, []byte(tt.input), tt.sites)
for j, m := range tt.matches {
t.Logf("\t[%02d:%02d] match: %s", i, j, m.String())
if !m.Match(out) {
t.Fatal("\toutput failed to match regex")
}
}
}
}
func testAlarmCollector(t *testing.T, input []byte, sites []*unifi.Site) []byte {
c, done := testUniFiClient(t, input)
defer done()
collector := NewAlarmCollector(
c,
sites,
)
return testCollector(t, collector)
}