-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
173 lines (157 loc) · 3.51 KB
/
client_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func createTempfile(t *testing.T, content string) string {
t.Helper()
f, err := ioutil.TempFile("", "efestest")
if err != nil {
t.Fatal(err)
}
_, err = f.WriteString(content)
if err != nil {
t.Fatal(err)
}
err = f.Close()
if err != nil {
t.Fatal(err)
}
return f.Name()
}
func TestClient(t *testing.T) {
const chunkSize = 2
content := "12345" // a string length of 5 for testing chunk size of 2
content2 := "qwerty" // a string length of 5 for testing chunk size of 2
source := createTempfile(t, content)
source2 := createTempfile(t, content2)
defer os.Remove(source)
defer os.Remove(source2)
tr, err := NewTracker(testConfig)
if err != nil {
t.Fatal(err)
}
cleanDB(t, tr.db)
go tr.Run()
defer tr.Shutdown()
_, err = tr.db.Exec("insert into zone(zoneid, name) values(1, 'zone1')")
if err != nil {
t.Fatal(err)
}
_, err = tr.db.Exec("insert into rack(rackid, zoneid, name) values(1, 1, 'rack1')")
if err != nil {
t.Fatal(err)
}
_, err = tr.db.Exec("insert into host(hostid, hostname, status, hostip, rackid) values(1, '127.0.0.1', 'alive', '127.0.0.1', 1)")
if err != nil {
t.Fatal(err)
}
_, err = tr.db.Exec("insert into device(devid, status, hostid, bytes_total, bytes_used, bytes_free, read_port, write_port) values(2, 'alive', 1, 1000000000, 500000000, 500000000, 8500, 8501)")
if err != nil {
t.Fatal(err)
}
tempDir, err := ioutil.TempDir("", "efes-test-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
devPath := filepath.Join(tempDir, "dev2")
err = os.Mkdir(devPath, 0700)
if err != nil {
t.Fatal(err)
}
serverConfig := *testConfig
serverConfig.Server.DataDir = devPath
srv, err := NewServer(&serverConfig)
if err != nil {
t.Fatal(err)
}
go srv.Run()
defer srv.Shutdown()
clientConfig := NewConfig()
clientConfig.Client.ChunkSize = chunkSize
clt, err := NewClient(clientConfig)
if err != nil {
t.Fatal(err)
}
<-tr.Ready
<-srv.Ready
exist, err := clt.Exists("foo")
if err != nil {
t.Fatal(err)
}
if exist {
t.Fatal("key must not exist")
}
err = clt.Write("foo", source)
if err != nil {
t.Fatal(err)
}
exist, err = clt.Exists("foo")
if err != nil {
t.Fatal(err)
}
if !exist {
t.Fatal("key must exist")
}
copied := createTempfile(t, "")
defer os.Remove(copied)
err = clt.Read("foo", copied)
if err != nil {
t.Fatal(err)
}
copyContent, err := ioutil.ReadFile(copied)
if err != nil {
t.Fatal(err)
}
if string(copyContent) != content {
t.Fatal("invalid content:", copyContent)
}
// test overwrite case
err = clt.Write("foo", source2)
if err != nil {
t.Fatal(err)
}
copied2 := createTempfile(t, "")
defer os.Remove(copied2)
err = clt.Read("foo", copied2)
if err != nil {
t.Fatal(err)
}
copyContent2, err := ioutil.ReadFile(copied2)
if err != nil {
t.Fatal(err)
}
if string(copyContent2) != content2 {
t.Fatal("invalid content:", copyContent2)
}
// test delete
err = clt.Delete("foo")
if err != nil {
t.Fatal(err)
}
exist, err = clt.Exists("foo")
if err != nil {
t.Fatal(err)
}
if exist {
t.Fatal("key must not exist")
}
// test injecting checksums in keys
contentFox := "the quick brown fox jumps over the lazy dog\n"
expectedSha1 := "5d2781d78fa5a97b7bafa849fe933dfc9dc93eba"
err = clt.WriteReader("foo-{{.Sha1}}-bar", strings.NewReader(contentFox))
if err != nil {
t.Fatal(err)
}
exist, err = clt.Exists("foo-" + expectedSha1 + "-bar")
if err != nil {
t.Fatal(err)
}
if !exist {
t.Fatal("key must exist")
}
}