-
Notifications
You must be signed in to change notification settings - Fork 71
/
client_builder_test.go
115 lines (88 loc) · 2.6 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
package zabbix
import (
"fmt"
"io/ioutil"
"testing"
)
const (
fakeURL = "http://localhost/api_jsonrpc.php"
fakeToken = "0424bd59b807674191e7d77572075f33"
fakeAPIVersion = "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) SessionAbstractCache {
sessionFilePath := baseDir + "/" + ".zabbix_session"
return NewSessionFileCache().SetFilePath(sessionFilePath)
}
func TestSessionCache(t *testing.T) {
// Create a fake session for r/w test
fakeSession := &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 *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 != fakeAPIVersion {
return fmt.Errorf("Session version '%s' is not equal to '%s'", session.APIVersion, fakeAPIVersion)
}
return nil
}
// should started by TestSessionCache
func testClientBuilder(t *testing.T, cache SessionAbstractCache) {
username, password, url := 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 := 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)
}
}