forked from AlekSi/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_test.go
90 lines (77 loc) · 1.6 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
package zabbix_test
import (
. "."
"log"
"math/rand"
"os"
"regexp"
"testing"
"time"
)
var (
_host string
_api *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 getHost() string {
return _host
}
func getAPI(t *testing.T) *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 = NewAPI(url)
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 TestBadCalls(t *testing.T) {
api := getAPI(t)
res, err := api.Call("", nil)
if err != nil {
t.Fatal(err)
}
if res.Error.Code != -32602 {
t.Errorf("Expected code -32602, got %s", res.Error)
}
}
func TestVersion(t *testing.T) {
api := getAPI(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)
}
}
func ExampleAPI_Call() {
api := NewAPI("http://host/api_jsonrpc.php")
api.Login("user", "password")
res, _ := api.Call("item.get", Params{"itemids": "23970", "output": "extend"})
log.Print(res)
}