forked from claranet/go-zabbix-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_test.go
97 lines (82 loc) · 2.02 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
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)
}
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)
}
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)
}
}