-
Notifications
You must be signed in to change notification settings - Fork 17
/
host_test.go
192 lines (150 loc) · 3.95 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
package zabbix
import (
"reflect"
"testing"
)
const (
testHostName = "testHost"
testHostIP = "10.1.1.1"
testHostPort = "10150"
testMacro = "{$TEST_MACRO}"
testMacroValue = "testMacroValue"
)
func TestHostCRUD(t *testing.T) {
var z Context
// Login
loginTest(&z, t)
defer logoutTest(&z, t)
// Preparing auxiliary data
hgCreatedIDs := testHostgroupCreate(t, z)
defer testHostgroupDelete(t, z, hgCreatedIDs)
tCreatedIDs := testTemplateCreate(t, z, hgCreatedIDs)
defer testTemplateDelete(t, z, tCreatedIDs)
// Create and delete
hCreatedIDs := testHostCreate(t, z, hgCreatedIDs, tCreatedIDs)
defer testHostDelete(t, z, hCreatedIDs)
// Update
testHostUpdate(t, z, hCreatedIDs)
// Get
testHostGet(t, z, hCreatedIDs, tCreatedIDs, hgCreatedIDs)
}
func testHostCreate(t *testing.T, z Context, hgCreatedIDs, tCreatedIDs []int) []int {
var groups []HostgroupObject
var templates []TemplateObject
// Add groups to host
for _, e := range hgCreatedIDs {
groups = append(groups, HostgroupObject{
GroupID: e,
})
}
// Add templates to host
for _, e := range tCreatedIDs {
templates = append(templates, TemplateObject{
TemplateID: e,
})
}
hCreatedIDs, _, err := z.HostCreate([]HostObject{
{
Host: testHostName,
Groups: groups,
Templates: templates,
Interfaces: []HostinterfaceObject{
{
IP: testHostIP,
Main: HostinterfaceMainDefault,
Port: testHostPort,
Type: HostinterfaceTypeAgent,
UseIP: HostinterfaceUseipIP,
},
},
Macros: []UsermacroObject{
{
Macro: testMacro,
Value: testMacroValue,
},
},
},
})
if err != nil {
t.Fatal("Host create error:", err)
}
if len(hCreatedIDs) == 0 {
t.Fatal("Host create error: empty IDs array")
}
t.Logf("Host create: success")
return hCreatedIDs
}
func testHostUpdate(t *testing.T, z Context, hCreatedIDs []int) []int {
var hObjects []HostObject
// Preparing host objects array to update
for _, i := range hCreatedIDs {
hObjects = append(hObjects, HostObject{
HostID: i,
Name: testHostName + "_upd",
})
}
hUpdatedIDs, _, err := z.HostUpdate(hObjects)
if err != nil {
t.Fatal("Host update error:", err)
}
if len(hUpdatedIDs) == 0 {
t.Fatal("Host update error: empty IDs array")
}
if reflect.DeepEqual(hUpdatedIDs, hCreatedIDs) == false {
t.Fatal("Host update error: IDs arrays for created and updated hosts are mismatch")
}
t.Logf("Host update: success")
return hUpdatedIDs
}
func testHostDelete(t *testing.T, z Context, hCreatedIDs []int) []int {
hDeletedIDs, _, err := z.HostDelete(hCreatedIDs)
if err != nil {
t.Fatal("Host delete error:", err)
}
if len(hDeletedIDs) == 0 {
t.Fatal("Host delete error: empty IDs array")
}
if reflect.DeepEqual(hDeletedIDs, hCreatedIDs) == false {
t.Fatal("Host delete error: IDs arrays for created and deleted host are mismatch")
}
t.Logf("Host delete: success")
return hDeletedIDs
}
func testHostGet(t *testing.T, z Context, hCreatedIDs, tCreatedIDs, hgCreatedIDs []int) []HostObject {
hObjects, _, err := z.HostGet(HostGetParams{
SelectParentTemplates: SelectExtendedOutput,
SelectMacros: SelectExtendedOutput,
HostIDs: hCreatedIDs,
TemplateIDs: tCreatedIDs,
GroupIDs: hgCreatedIDs,
GetParameters: GetParameters{
Filter: map[string]interface{}{
"name": testHostName + "_upd",
},
Output: SelectExtendedOutput,
},
})
if err != nil {
t.Error("Host get error:", err)
} else {
if len(hObjects) == 0 {
t.Error("Host get error: unable to find created host")
} else {
// Check macro in each created host
for _, h := range hObjects {
foundMacro := false
for _, m := range h.Macros {
if m.Macro == testMacro && m.Value == testMacroValue {
foundMacro = true
break
}
}
if foundMacro == false {
t.Error("Host get error: unable to find macro in created host")
}
}
t.Logf("Host get: success")
}
}
return hObjects
}