This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keySaver_test.go
304 lines (279 loc) · 7.8 KB
/
keySaver_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package store
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"testing"
)
var (
failed = errors.New("Failed hook")
)
type TestVal struct {
Name string
Val string
hook string
failHook bool
}
func (t *TestVal) doHook(hook string) error {
t.hook = hook
if t.failHook {
return failed
}
return nil
}
func (t *TestVal) Prefix() string {
return "testVal"
}
func (t *TestVal) Key() string {
return t.Name
}
func (t *TestVal) KeyName() string {
return "Name"
}
func (t *TestVal) New() KeySaver {
return &TestVal{}
}
func (t *TestVal) OnLoad() error {
return t.doHook("OnLoad")
}
func (t *TestVal) OnChange(KeySaver) error {
return t.doHook("OnChange")
}
func (t *TestVal) BeforeDelete() error {
return t.doHook("BeforeDelete")
}
func (t *TestVal) AfterDelete() {
t.doHook("AfterDelete")
}
func (t *TestVal) OnCreate() error {
return t.doHook("OnCreate")
}
func (t *TestVal) BeforeSave() error {
return t.doHook("BeforeSave")
}
func (t *TestVal) AfterSave() {
t.doHook("AfterSave")
}
type op func(Store, KeySaver) (bool, error)
type test struct {
op op
name string
val *TestVal
pass bool
hookFail bool
lastHook string
}
var tv = TestVal{"Name", "Value", "", false}
var ntv = TestVal{"Uncreated", "Value", "", false}
var tests = []test{
test{op(Create), "Create Hook Fail", &tv, false, true, "OnCreate"},
test{op(Create), "Create Succeed", &tv, true, false, "AfterSave"},
test{op(Create), "Create Duplicate Fail", &tv, false, false, ""},
test{op(Load), "Load Hook Fail", &tv, true, true, "OnLoad"},
test{op(Load), "Load Nonexistent Fail", &ntv, false, false, ""},
test{op(Load), "Load Succeed", &tv, true, false, "OnLoad"},
test{op(Save), "Save Before Hook Fail", &tv, false, true, "BeforeSave"},
test{op(Save), "Save Succeed", &tv, true, false, "AfterSave"},
test{op(Update), "Update Before Hook Fail", &tv, false, true, "OnChange"},
test{op(Update), "Update Succeed", &tv, true, false, "AfterSave"},
test{op(Remove), "Remove Hook Fail", &tv, false, true, "BeforeDelete"},
test{op(Remove), "Remove Success", &tv, true, false, "AfterDelete"},
test{op(Remove), "Remove Nonexistent Fail", &ntv, false, false, "BeforeDelete"},
}
var createTests = []test{
test{op(Create), "Create Hook Fail", &tv, false, true, "OnCreate"},
test{op(Create), "Create Succeed", &tv, true, false, "AfterSave"},
test{op(Create), "Create Duplicate Fail", &tv, false, false, ""},
}
var roTests = []test{
test{op(Load), "Load Hook Fail", &tv, true, true, "OnLoad"},
test{op(Load), "Load Nonexistent Fail", &ntv, false, false, ""},
test{op(Load), "Load Succeed", &tv, true, false, "OnLoad"},
test{op(Save), "Save Before Hook Fail", &tv, false, true, "BeforeSave"},
test{op(Save), "Save Succeed", &tv, false, false, "BeforeSave"},
test{op(Update), "Update Before Hook Fail", &tv, false, true, "OnChange"},
test{op(Update), "Update Succeed", &tv, false, false, "BeforeSave"},
test{op(Remove), "Remove Hook Fail", &tv, false, true, "BeforeDelete"},
test{op(Remove), "Remove Success", &tv, false, false, "BeforeDelete"},
test{op(Remove), "Remove Nonexistent Fail", &ntv, false, false, "BeforeDelete"},
}
func runTests(t *testing.T, backend Store, toRun []test) {
for _, s := range toRun {
expectedTo := "fail"
if s.pass {
expectedTo = "pass"
}
actuallyDid := "fail"
s.val.hook = ""
s.val.failHook = s.hookFail
ok, err := s.op(backend, s.val)
if ok {
actuallyDid = "pass"
}
passMsg := fmt.Sprintf("%s: Expected to %s, actually %s", s.name, expectedTo, actuallyDid)
hookMsg := fmt.Sprintf("%s: Expected last hook to be `%s`, was `%s`", s.name, s.lastHook, s.val.hook)
if ok != s.pass {
t.Error(passMsg)
} else {
t.Log(passMsg)
}
if s.lastHook != s.val.hook {
t.Error(hookMsg)
} else {
t.Log(hookMsg)
}
if s.hookFail {
if err == nil {
t.Errorf("%s: Expected hook to fail, but got no error!", s.name)
} else if err != failed {
t.Errorf("%s: Expected hook to fail with `%v`, but got `%v`", s.name, failed, err)
} else {
t.Logf("%s: Got expected hook failure `%v`", s.name, failed)
}
} else if !s.pass {
if err != nil {
t.Logf("%s: Got error %v", s.name, err)
} else {
t.Logf("%s: Expected to fail, but got no error!", s.name)
}
}
}
}
// Expects a freshly-created store
func testOneStore(t *testing.T, s Store) {
runTests(t, s, tests)
// At the end, the store should be empty
ents, err := s.Keys()
if err != nil {
t.Errorf("Error listing store: %v", err)
} else if len(ents) != 0 {
t.Errorf("Too many entries in store: wanted 0, got %d", len(ents))
}
ok, err := Create(s, &tv)
if !ok {
t.Errorf("Failed to create an entry for the list test: %v", err)
}
nents, err := List(s, &tv)
if err != nil {
t.Errorf("Error listing the entries for the list test: %v", err)
} else if len(nents) != 1 {
t.Errorf("Expected 1 entry, got %d", len(nents))
}
keys, err := s.Keys()
if err != nil {
t.Errorf("Error getting keys for the keys test: %v", err)
} else if len(keys) != 1 {
t.Errorf("Expected 1 key, got %d", len(keys))
}
for _, k := range keys {
if err := s.Remove(k); err != nil {
t.Errorf("Error removing key %s", k)
}
}
runTests(t, s, createTests)
if !s.SetReadOnly() {
t.Errorf("Unable to set store to read only")
}
runTests(t, s, roTests)
}
func testStore(t *testing.T, s Store) {
t.Logf("Testing top-level store")
testOneStore(t, s)
var err error
_, err = s.MakeSub("sub1")
if err != nil {
t.Errorf("Error creating substore sub1")
return
}
t.Logf("Testing substore sub1")
sub1 := s.GetSub("sub1")
testOneStore(t, sub1)
_, err = sub1.MakeSub("sub2")
if err != nil {
t.Errorf("Error creating substore sub2")
return
}
t.Logf("Testing substore sub2")
testOneStore(t, sub1.GetSub("sub2"))
}
func TestMemoryStore(t *testing.T) {
s, _ := Open("memory:///")
t.Log("Testing simple memory store")
testStore(t, s)
t.Log("Memory store test finished")
}
func testPersistent(t *testing.T, storeType, storeCodec string) {
t.Logf("Creating tmpdir for persistent testing")
tmpDir, err := ioutil.TempDir("", "store-")
if err != nil {
t.Errorf("Failed to create tmp dir for persistent testing")
return
}
t.Logf("Running in %s", tmpDir)
defer os.RemoveAll(tmpDir)
var storeLoc string
if storeType == "file" {
storeLoc = path.Join(tmpDir, "data."+storeCodec)
} else {
storeLoc = tmpDir
}
storeURI := fmt.Sprintf("%s:%s?codec=%s", storeType, storeLoc, storeCodec)
s, err := Open(storeURI)
if err != nil {
t.Errorf("Failed to create store: %v", err)
return
}
t.Logf("Store: %#v", s)
t.Log("Testing store")
testStore(t, s)
if meta, ok := s.(MetaSaver); ok {
if err := meta.SetMetaData(map[string]string{"foo": "bar"}); err != nil {
t.Errorf("Error setting metadata: %v", err)
}
}
t.Log("Testing persistence of local substore hierarchy")
s.Close()
s, err = Open(storeURI)
if err != nil {
t.Errorf("Failed to reload store: %v", err)
return
}
sub1 := s.GetSub("sub1")
if sub1 == nil {
t.Errorf("Did not load expected substore sub1")
return
}
sub2 := sub1.GetSub("sub2")
if sub2 == nil {
t.Errorf("Did not load expected substore sub2")
return
}
if meta, ok := s.(MetaSaver); ok {
vals := meta.MetaData()
if vals["foo"] != "bar" {
t.Errorf("Metadata did not persist")
}
}
t.Log("Testing copy capabilities")
dst, _ := Open("memory:///")
if err := Copy(dst, s); err != nil {
t.Errorf("Error copying stores: %v", err)
}
t.Logf("Persistent test finished")
}
func TestPersistentStores(t *testing.T) {
storeCodecs := []string{"json", "yaml", "default"}
storeType := []string{"bolt", "directory", "file"}
for _, codec := range storeCodecs {
for _, storeType := range storeType {
t.Logf("Testing persistent store %s with codec %s", storeType, codec)
testPersistent(t, storeType, codec)
t.Logf("--------------------------------------------------------")
if t.Failed() {
return
}
}
}
}