-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathicinga2_test.go
122 lines (110 loc) · 2.48 KB
/
icinga2_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
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
package main
import (
"os"
"reflect"
"testing"
)
var (
i Icinga2
)
const (
HOST_NAME = "google.com"
SERVICE_NAME = "apt"
)
func init() {
i = Icinga2{
Host: os.Getenv("HOST_ICINGA"),
Username: os.Getenv("USERNAME_ICINGA"),
Password: os.Getenv("PASSWORD_ICINGA"),
}
}
func TestConstructFilter(t *testing.T) {
expectedFilterOneHost := Filter{
Filter: "match(\"*" + HOST_NAME + "*\", host.display_name)",
Attrs: []string{
"display_name",
"name",
"last_check",
"state",
"check_command",
},
}
resultFilterOneHost := i.constructFilter(HOST_NAME, HOSTS, false)
if !reflect.DeepEqual(expectedFilterOneHost, resultFilterOneHost) {
t.Error(
"For 'check one host status'",
"expected", expectedFilterOneHost,
"got", resultFilterOneHost,
)
}
expectedFilterOneService := Filter{
Filter: "match(\"*" + SERVICE_NAME + "*\", service.display_name)",
Attrs: []string{
"display_name",
"name",
"last_check",
"state",
"check_command",
},
}
resultFilterOneService := i.constructFilter(SERVICE_NAME, SERVICES, false)
if !reflect.DeepEqual(expectedFilterOneService, resultFilterOneService) {
t.Error(
"For 'check one service status'",
"expected", expectedFilterOneService,
"got", resultFilterOneService,
)
}
expectedFilterAllHosts := Filter{
Attrs: []string{
"display_name",
"name",
"last_check",
"state",
"check_command",
},
}
resultFilterAllHosts := i.constructFilter("", HOSTS, true)
if !reflect.DeepEqual(expectedFilterAllHosts, resultFilterAllHosts) {
t.Error(
"For 'check all hosts status'",
"expected", expectedFilterAllHosts,
"got", resultFilterAllHosts,
)
}
expectedFilterAllServices := Filter{
Attrs: []string{
"display_name",
"name",
"last_check",
"state",
"check_command",
},
}
resultFilterAllServices := i.constructFilter("", SERVICES, true)
if !reflect.DeepEqual(expectedFilterAllServices, resultFilterAllServices) {
t.Error(
"For 'check all hosts status'",
"expected", expectedFilterAllServices,
"got", resultFilterAllServices,
)
}
}
func TestCheck(t *testing.T) {
_, err := i.check("", HOSTS, true)
if err != nil {
t.Error("Cannot get list of hosts")
}
_, err = i.check("", SERVICES, true)
if err != nil {
t.Error("Cannot get list of services")
}
_, err = i.check(SERVICE_NAME, SERVICES, false)
if err != nil {
t.Error("Cannot check the service")
}
_, err = i.check(HOST_NAME, HOSTS, false)
if err != nil {
t.Error("Cannot check the host")
}
}