forked from zabbix-tools/go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_builder_test.go
127 lines (97 loc) · 2.87 KB
/
client_builder_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
package zabbix_test
import (
"fmt"
"io/ioutil"
"testing"
"github.com/fabiang/go-zabbix"
"github.com/fabiang/go-zabbix/test"
"github.com/fabiang/go-zabbix/types"
)
const (
fakeURL = "http://localhost/api_jsonrpc.php"
fakeToken = "0424bd59b807674191e7d77572075f33"
)
var fakeAPIVersion *types.ZBXVersion
func init() {
fakeAPIVersion, _ = types.NewZBXVersion("2.0")
}
func prepareTemporaryDir(t *testing.T) (dir string, success bool) {
tempDir, err := ioutil.TempDir("", "zabbix-session-test")
if err != nil {
t.Fatalf("cannot create a temporary dir for session cache: %v", err)
return "", false
}
t.Logf("used %s directory as temporary dir", tempDir)
return tempDir, true
}
func getTestFileCache(baseDir string) zabbix.SessionAbstractCache {
sessionFilePath := baseDir + "/" + ".zabbix_session"
return zabbix.NewSessionFileCache().SetFilePath(sessionFilePath)
}
func TestSessionCache(t *testing.T) {
// Create a fake session for r/w test
fakeSession := &zabbix.Session{
URL: fakeURL,
Token: fakeToken,
APIVersion: fakeAPIVersion,
}
tempDir, success := prepareTemporaryDir(t)
if !success {
return
}
cache := getTestFileCache(tempDir)
if err := cache.SaveSession(fakeSession); err != nil {
t.Errorf("failed to save mock session - %v", err)
return
}
if !cache.HasSession() {
t.Errorf("session was saved but not detected again by cache")
return
}
// Try to get a cached session
cachedSession, err := cache.GetSession()
if err != nil {
t.Error(err)
return
}
// Check session integrity
if err := compareSessionWithMock(cachedSession); err != nil {
t.Error(err)
}
testClientBuilder(t, cache)
if err := cache.Flush(); err != nil {
t.Error("failed to remove a cached session file")
}
}
func compareSessionWithMock(session *zabbix.Session) error {
if session.URL != fakeURL {
return fmt.Errorf("Session URL '%s' is not equal to '%s'", session.URL, fakeURL)
}
if session.Token != fakeToken {
return fmt.Errorf("Session token '%s' is not equal to '%s'", session.Token, fakeToken)
}
if session.APIVersion.String() != fakeAPIVersion.String() {
return fmt.Errorf(
"Session version %q is not equal to %q",
session.APIVersion.String(),
fakeAPIVersion.String())
}
return nil
}
// should started by TestSessionCache
func testClientBuilder(t *testing.T, cache zabbix.SessionAbstractCache) {
username, password, url := test.GetTestCredentials()
if !cache.HasSession() {
t.Errorf("ManualTestClientBuilder test requires a cached session, run TestSessionCache before running this test case")
return
}
// Try to build a session using the session builder
client, err := zabbix.CreateClient(url).WithCache(cache).WithCredentials(username, password).Connect()
if err != nil {
t.Errorf("failed to create a session using cache - %s", err)
return
}
if err := compareSessionWithMock(client); err != nil {
t.Error(err)
}
}