-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathagent_test.go
151 lines (116 loc) · 3.2 KB
/
agent_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
package zagent
import (
"fmt"
"os"
"strings"
"testing"
)
// You must set and export the shell variable ZABBIX_HOST in
// order to specify which host to test against. If you leave
// this unset it will default to localhost. At least on the
// Mac. This test is pretty rudimentary.
func TestAgent(t *testing.T) {
zabbixHost := os.Getenv("ZABBIX_HOST")
fmt.Println("Testing against:", zabbixHost)
agent := NewAgent(zabbixHost)
res, err := agent.Query("agent.ping", 0)
if err != nil {
t.Fatal(err)
}
if res.String() != "1" {
t.Fatal("agent.ping results are incorrect.")
}
}
func TestAgentPing(t *testing.T) {
zabbixHost := os.Getenv("ZABBIX_HOST")
agent := NewAgent(zabbixHost)
pingable, err := agent.AgentPing(0)
if err != nil {
t.Fatal("Ping test failed with error:", err)
}
if !pingable {
t.Fatal("Zabbix agent wasn't pingable")
}
}
func TestAgentHostname(t *testing.T) {
zabbixHost := os.Getenv("ZABBIX_HOST")
agent := NewAgent(zabbixHost)
hostname, err := agent.AgentHostname(0)
if err != nil {
t.Fatal("Hostname test failed with error:", err)
}
if hostname == "" {
t.Fatal("Zabbix hostname was empty")
}
fmt.Println("Zabbix hostname:", hostname)
}
func TestAgentVersion(t *testing.T) {
zabbixHost := os.Getenv("ZABBIX_HOST")
agent := NewAgent(zabbixHost)
version, err := agent.AgentVersion(0)
if err != nil {
t.Fatal("Version test failed with error:", err)
}
if version == "" {
t.Fatal("Zabbix version was empty")
}
fmt.Println("Zabbix version:", version)
}
func TestAgentUnsupported(t *testing.T) {
zabbixHost := os.Getenv("ZABBIX_HOST")
fmt.Println("Testing against:", zabbixHost)
agent := NewAgent(zabbixHost)
res, err := agent.Query("Supercalifragilisticexpialidocious", 0)
if err != nil {
t.Fatal("An error was thrown on an unknown key. Response.Supported() should return false not throw an error.")
}
if res.Supported() {
t.Fatal("Response says supported when calling an unknown key")
}
if err != nil && !strings.HasSuffix(err.Error(), " is not supported") {
t.Fatal(err)
}
if res.Supported() {
t.Fatal("Response.Supported() reports true")
}
}
func TestQueryInterface(t *testing.T) {
zabbixHost := os.Getenv("ZABBIX_HOST")
agent := NewAgent(zabbixHost)
// Should return a string
res, err := agent.Query("agent.hostname", 0)
if err != nil {
t.Fatal("Couldn't get hostname: " + err.Error())
}
hostname := res.Interface()
switch hostname.(type) {
default:
t.Fatal("agent.hostname didn't get converted to a string")
case string:
fmt.Println("agent.hostname returned a string")
}
// Should return a float64
res, err = agent.Query("system.cpu.load", 0)
if err != nil {
t.Fatal("Couldn't get cpu load: " + err.Error())
}
load := res.Interface()
switch load.(type) {
default:
t.Fatal("system.cpu.load didn't get converted to a float64")
case float64:
fmt.Println("system.cpu.load returned a float64")
}
// Should return an int64
res, err = agent.Query("system.cpu.num", 0)
if err != nil {
t.Fatal("Couldn't get number of cpus: " + err.Error())
}
cpus := res.Interface()
switch cpus.(type) {
default:
t.Fatal("system.cpu.num didn't get converted to an int64")
case int64:
fmt.Println("system.cpu.num was converted to int64")
}
}