forked from dainis/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 18
/
base_test.go
120 lines (102 loc) · 2.89 KB
/
base_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
package zabbix_test
import (
"log"
"math/rand"
"net/http"
"os"
"regexp"
"testing"
"time"
zapi "github.com/claranet/go-zabbix-api"
"github.com/hashicorp/go-version"
)
var (
_host string
_api *zapi.API
)
func init() {
rand.Seed(time.Now().UnixNano())
var err error
_host, err = os.Hostname()
if err != nil {
log.Fatal(err)
}
_host += "-testing"
if os.Getenv("TEST_ZABBIX_URL") == "" {
log.Fatal("Set environment variables TEST_ZABBIX_URL (and optionally TEST_ZABBIX_USER and TEST_ZABBIX_PASSWORD)")
}
}
func testGetHost() string {
return _host
}
func testGetAPI(t *testing.T) *zapi.API {
if _api != nil {
return _api
}
url, user, password := os.Getenv("TEST_ZABBIX_URL"), os.Getenv("TEST_ZABBIX_USER"), os.Getenv("TEST_ZABBIX_PASSWORD")
_api = zapi.NewAPI(url)
_api.SetClient(http.DefaultClient)
v := os.Getenv("TEST_ZABBIX_VERBOSE")
if v != "" && v != "0" {
_api.Logger = log.New(os.Stderr, "[zabbix] ", 0)
}
if user != "" {
auth, err := _api.Login(user, password)
if err != nil {
t.Fatal(err)
}
if auth == "" {
t.Fatal("Login failed")
}
}
return _api
}
func compVersion(t *testing.T, comparedVersion string) (int, string) {
api := testGetAPI(t)
serverVersion, err := api.Version()
if err != nil {
t.Fatal(err)
}
sVersion, _ := version.NewVersion(serverVersion)
cVersion, _ := version.NewVersion(comparedVersion)
return sVersion.Compare(cVersion), serverVersion
}
func isVersionLessThan(t *testing.T, comparedVersion string) (bool, string) {
comp, serverVersion := compVersion(t, comparedVersion)
return comp < 0, serverVersion
}
func isVersionGreaterThanOrEqual(t *testing.T, comparedVersion string) (bool, string) {
comp, serverVersion := compVersion(t, comparedVersion)
return comp >= 0, serverVersion
}
func skipTestIfVersionGreaterThanOrEqual(t *testing.T, comparedVersion, msg string) {
if compGreaterThanOrEqual, serverVersion := isVersionGreaterThanOrEqual(t, comparedVersion); compGreaterThanOrEqual {
t.Skipf("Zabbix version %s is greater than or equal to %s which %s, skipping test.", serverVersion, comparedVersion, msg)
}
}
func skipTestIfVersionLessThan(t *testing.T, comparedVersion, msg string) {
if compGreaterThanOrEqual, serverVersion := isVersionLessThan(t, comparedVersion); compGreaterThanOrEqual {
t.Skipf("Zabbix version %s is less than to %s which %s, skipping test.", serverVersion, comparedVersion, msg)
}
}
func TestBadCalls(t *testing.T) {
api := testGetAPI(t)
res, err := api.Call("", nil)
if err != nil {
t.Fatal(err)
}
if res.Error.Code != -32600 && res.Error.Code != -32602 {
t.Errorf("Expected code -32600 or -32602 depending on Zabbix Server version, got %s", res.Error)
}
}
func TestVersion(t *testing.T) {
api := testGetAPI(t)
v, err := api.Version()
if err != nil {
t.Fatal(err)
}
t.Logf("Zabbix version %s", v)
if !regexp.MustCompile(`^\d\.\d\.\d+$`).MatchString(v) {
t.Errorf("Unexpected version: %s", v)
}
}