forked from claranet/go-zabbix-api
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhost_test.go
195 lines (174 loc) · 4.83 KB
/
host_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package zabbix_test
import (
"fmt"
"math/rand"
"reflect"
"testing"
zapi "github.com/claranet/go-zabbix-api"
)
func testCreateHost(group *zapi.HostGroup, t *testing.T) *zapi.Host {
name := fmt.Sprintf("%s-%d", testGetHost(), rand.Int())
iface := zapi.HostInterface{DNS: name, Port: "42", Type: zapi.Agent, UseIP: 0, Main: 1}
hosts := zapi.Hosts{{
Host: name,
Name: "Name for " + name,
GroupIds: zapi.HostGroupIDs{{group.GroupID}},
Interfaces: zapi.HostInterfaces{iface},
}}
err := testGetAPI(t).HostsCreate(hosts)
if err != nil {
t.Fatal(err)
}
return &hosts[0]
}
func testDeleteHost(host *zapi.Host, t *testing.T) {
err := testGetAPI(t).HostsDelete(zapi.Hosts{*host})
if err != nil {
t.Fatal(err)
}
}
func TestHosts(t *testing.T) {
api := testGetAPI(t)
group := testCreateHostGroup(t)
defer testDeleteHostGroup(group, t)
hosts, err := api.HostsGetByHostGroups(zapi.HostGroups{*group})
if err != nil {
t.Fatal(err)
}
if len(hosts) != 0 {
t.Errorf("Bad hosts: %#v", hosts)
}
host := testCreateHost(group, t)
if host.HostID == "" || host.Host == "" {
t.Errorf("Something is empty: %#v", host)
}
iface := host.Interfaces[0]
host.GroupIds = nil
host.Interfaces = nil
newName := fmt.Sprintf("%s-%d", testGetHost(), rand.Int())
host.Host = newName
err = api.HostsUpdate(zapi.Hosts{*host})
if err != nil {
t.Fatal(err)
}
host2, err := api.HostGetByHost(host.Host)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(host, host2) {
t.Errorf("Hosts are not equal:\n%#v\n%#v", host, host2)
}
host2, err = api.HostGetByID(host.HostID)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(host, host2) {
t.Errorf("Hosts are not equal:\n%#v\n%#v", host, host2)
}
hosts, err = api.HostsGetByHostGroups(zapi.HostGroups{*group})
if err != nil {
t.Fatal(err)
}
if len(hosts) != 1 {
t.Errorf("Bad hosts: %#v", hosts)
}
hosts, err = api.HostsGet(zapi.Params{
"hostids": host.HostID,
"selectInterfaces": "extend",
"selectMacros": "extend",
"selectParentTemplates": "extend",
})
if err != nil {
t.Fatal(err)
}
if len(hosts) != 1 {
t.Errorf("Bad hosts: %#v", hosts)
}
if len(hosts[0].Interfaces) != 1 {
t.Errorf("Bad interfaces: %#v", hosts[0].Interfaces)
}
iface2 := hosts[0].Interfaces[0]
iface.InterfaceID = iface2.InterfaceID
if !reflect.DeepEqual(iface, iface2) {
t.Errorf("Interfaces are not equal:\n%#v\n%#v", iface, iface2)
}
if len(hosts[0].Templates) != 0 {
t.Errorf("Templates is not empty: %#v", hosts[0].Templates)
}
if len(hosts[0].UserMacros) != 0 {
t.Errorf("UserMacros is not empty: %#v", hosts[0].UserMacros)
}
// Zabbix v6.2 introduced Template Groups and requires them for Templates
var groupIds zapi.HostGroupIDs
if compLessThan, _ := isVersionLessThan(t, "6.2"); compLessThan {
hostGroup := testCreateHostGroup(t)
defer testDeleteHostGroup(hostGroup, t)
groupIds = zapi.HostGroupIDs{
{
GroupID: hostGroup.GroupID,
},
}
} else {
templateGroup := testCreateTemplateGroup(t)
defer testDeleteTemplateGroup(templateGroup, t)
groupIds = zapi.HostGroupIDs{
{
GroupID: templateGroup.GroupID,
},
}
}
template := testCreateTemplate(&groupIds, t)
defer testDeleteTemplate(template, t)
iface = zapi.HostInterface{IP: "127.0.0.1", Port: "10050", Type: zapi.Agent, UseIP: 1, Main: 1}
host.Interfaces = zapi.HostInterfaces{iface}
host.TemplateIDs = zapi.TemplateIDs{{template.TemplateID}}
macro := zapi.Macro{MacroName: "{$NAME}", Value: "Value"}
host.UserMacros = zapi.Macros{macro}
err = api.HostsUpdate(zapi.Hosts{*host})
if err != nil {
t.Fatal(err)
}
hosts, err = api.HostsGet(zapi.Params{
"hostids": host.HostID,
"selectInterfaces": "extend",
"selectMacros": "extend",
"selectParentTemplates": "extend",
})
if err != nil {
t.Fatal(err)
}
if len(hosts) != 1 {
t.Errorf("Bad hosts: %#v", hosts)
}
if len(hosts[0].Interfaces) != 1 {
t.Errorf("Bad interfaces: %#v", hosts[0].Interfaces)
}
iface2 = hosts[0].Interfaces[0]
iface.InterfaceID = iface2.InterfaceID
if !reflect.DeepEqual(iface, iface2) {
t.Errorf("Interfaces are not equal:\n%#v\n%#v", iface, iface2)
}
if len(hosts[0].Templates) != 1 {
t.Errorf("Bad templates: %#v", hosts[0].Templates)
}
template2 := hosts[0].Templates[0]
if template.Host != template2.Host {
t.Errorf("Templates are not equal:\n%#v\n%#v", template.Host, template2.Host)
}
if len(hosts[0].UserMacros) != 1 {
t.Errorf("Bad userMacros: %#v", hosts[0].UserMacros)
}
macro2 := hosts[0].UserMacros[0]
macro.HostID = hosts[0].HostID
if !reflect.DeepEqual(macro, macro2) {
t.Errorf("UserMacros are not equal:\n%#v\n%#v", macro, macro2)
}
testDeleteHost(host, t)
hosts, err = api.HostsGetByHostGroups(zapi.HostGroups{*group})
if err != nil {
t.Fatal(err)
}
if len(hosts) != 0 {
t.Errorf("Bad hosts: %#v", hosts)
}
}