forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
488 lines (429 loc) · 11 KB
/
testing.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package physical
import (
"context"
"reflect"
"sort"
"testing"
"time"
)
func ExerciseBackend(t testing.TB, b Backend) {
t.Helper()
// Should be empty
keys, err := b.List(context.Background(), "")
if err != nil {
t.Fatalf("initial list failed: %v", err)
}
if len(keys) != 0 {
t.Errorf("initial not empty: %v", keys)
}
// Delete should work if it does not exist
err = b.Delete(context.Background(), "foo")
if err != nil {
t.Fatalf("idempotent delete: %v", err)
}
// Get should not fail, but be nil
out, err := b.Get(context.Background(), "foo")
if err != nil {
t.Fatalf("initial get failed: %v", err)
}
if out != nil {
t.Errorf("initial get was not nil: %v", out)
}
// Make an entry
e := &Entry{Key: "foo", Value: []byte("test")}
err = b.Put(context.Background(), e)
if err != nil {
t.Fatalf("put failed: %v", err)
}
// Get should work
out, err = b.Get(context.Background(), "foo")
if err != nil {
t.Fatalf("get failed: %v", err)
}
if !reflect.DeepEqual(out, e) {
t.Errorf("bad: %v expected: %v", out, e)
}
// List should not be empty
keys, err = b.List(context.Background(), "")
if err != nil {
t.Fatalf("list failed: %v", err)
}
if len(keys) != 1 || keys[0] != "foo" {
t.Errorf("keys[0] did not equal foo: %v", keys)
}
// Delete should work
err = b.Delete(context.Background(), "foo")
if err != nil {
t.Fatalf("delete: %v", err)
}
// Should be empty
keys, err = b.List(context.Background(), "")
if err != nil {
t.Fatalf("list after delete: %v", err)
}
if len(keys) != 0 {
t.Errorf("list after delete not empty: %v", keys)
}
// Get should fail
out, err = b.Get(context.Background(), "foo")
if err != nil {
t.Fatalf("get after delete: %v", err)
}
if out != nil {
t.Errorf("get after delete not nil: %v", out)
}
// Multiple Puts should work; GH-189
e = &Entry{Key: "foo", Value: []byte("test")}
err = b.Put(context.Background(), e)
if err != nil {
t.Fatalf("multi put 1 failed: %v", err)
}
e = &Entry{Key: "foo", Value: []byte("test")}
err = b.Put(context.Background(), e)
if err != nil {
t.Fatalf("multi put 2 failed: %v", err)
}
// Make a nested entry
e = &Entry{Key: "foo/bar", Value: []byte("baz")}
err = b.Put(context.Background(), e)
if err != nil {
t.Fatalf("nested put failed: %v", err)
}
keys, err = b.List(context.Background(), "")
if err != nil {
t.Fatalf("list multi failed: %v", err)
}
sort.Strings(keys)
if len(keys) != 2 || keys[0] != "foo" || keys[1] != "foo/" {
t.Errorf("expected 2 keys [foo, foo/]: %v", keys)
}
// Delete with children should work
err = b.Delete(context.Background(), "foo")
if err != nil {
t.Fatalf("delete after multi: %v", err)
}
// Get should return the child
out, err = b.Get(context.Background(), "foo/bar")
if err != nil {
t.Fatalf("get after multi delete: %v", err)
}
if out == nil {
t.Errorf("get after multi delete not nil: %v", out)
}
// Removal of nested secret should not leave artifacts
e = &Entry{Key: "foo/nested1/nested2/nested3", Value: []byte("baz")}
err = b.Put(context.Background(), e)
if err != nil {
t.Fatalf("deep nest: %v", err)
}
err = b.Delete(context.Background(), "foo/nested1/nested2/nested3")
if err != nil {
t.Fatalf("failed to remove deep nest: %v", err)
}
keys, err = b.List(context.Background(), "foo/")
if err != nil {
t.Fatalf("err: %v", err)
}
if len(keys) != 1 || keys[0] != "bar" {
t.Errorf("should be exactly 1 key == bar: %v", keys)
}
// Make a second nested entry to test prefix removal
e = &Entry{Key: "foo/zip", Value: []byte("zap")}
err = b.Put(context.Background(), e)
if err != nil {
t.Fatalf("failed to create second nested: %v", err)
}
// Delete should not remove the prefix
err = b.Delete(context.Background(), "foo/bar")
if err != nil {
t.Fatalf("failed to delete nested prefix: %v", err)
}
keys, err = b.List(context.Background(), "")
if err != nil {
t.Fatalf("list nested prefix: %v", err)
}
if len(keys) != 1 || keys[0] != "foo/" {
t.Errorf("should be exactly 1 key == foo/: %v", keys)
}
// Delete should remove the prefix
err = b.Delete(context.Background(), "foo/zip")
if err != nil {
t.Fatalf("failed to delete second prefix: %v", err)
}
keys, err = b.List(context.Background(), "")
if err != nil {
t.Fatalf("listing after second delete failed: %v", err)
}
if len(keys) != 0 {
t.Errorf("should be empty at end: %v", keys)
}
// When the root path is empty, adding and removing deep nested values should not break listing
e = &Entry{Key: "foo/nested1/nested2/value1", Value: []byte("baz")}
err = b.Put(context.Background(), e)
if err != nil {
t.Fatalf("deep nest: %v", err)
}
e = &Entry{Key: "foo/nested1/nested2/value2", Value: []byte("baz")}
err = b.Put(context.Background(), e)
if err != nil {
t.Fatalf("deep nest: %v", err)
}
err = b.Delete(context.Background(), "foo/nested1/nested2/value2")
if err != nil {
t.Fatalf("failed to remove deep nest: %v", err)
}
keys, err = b.List(context.Background(), "")
if err != nil {
t.Fatalf("listing of root failed after deletion: %v", err)
}
if len(keys) == 0 {
t.Errorf("root is returning empty after deleting a single nested value, expected nested1/: %v", keys)
keys, err = b.List(context.Background(), "foo/nested1")
if err != nil {
t.Fatalf("listing of expected nested path 'foo/nested1' failed: %v", err)
}
// prove that the root should not be empty and that foo/nested1 exists
if len(keys) != 0 {
t.Logf(" keys can still be listed from nested1/ so it's not empty, expected nested2/: %v", keys)
}
}
// cleanup left over listing bug test value
err = b.Delete(context.Background(), "foo/nested1/nested2/value1")
if err != nil {
t.Fatalf("failed to remove deep nest: %v", err)
}
keys, err = b.List(context.Background(), "")
if err != nil {
t.Fatalf("listing of root failed after delete of deep nest: %v", err)
}
if len(keys) != 0 {
t.Errorf("should be empty at end: %v", keys)
}
}
func ExerciseBackend_ListPrefix(t testing.TB, b Backend) {
t.Helper()
e1 := &Entry{Key: "foo", Value: []byte("test")}
e2 := &Entry{Key: "foo/bar", Value: []byte("test")}
e3 := &Entry{Key: "foo/bar/baz", Value: []byte("test")}
defer func() {
b.Delete(context.Background(), "foo")
b.Delete(context.Background(), "foo/bar")
b.Delete(context.Background(), "foo/bar/baz")
}()
err := b.Put(context.Background(), e1)
if err != nil {
t.Fatalf("failed to put entry 1: %v", err)
}
err = b.Put(context.Background(), e2)
if err != nil {
t.Fatalf("failed to put entry 2: %v", err)
}
err = b.Put(context.Background(), e3)
if err != nil {
t.Fatalf("failed to put entry 3: %v", err)
}
// Scan the root
keys, err := b.List(context.Background(), "")
if err != nil {
t.Fatalf("list root: %v", err)
}
sort.Strings(keys)
if len(keys) != 2 || keys[0] != "foo" || keys[1] != "foo/" {
t.Errorf("root expected [foo foo/]: %v", keys)
}
// Scan foo/
keys, err = b.List(context.Background(), "foo/")
if err != nil {
t.Fatalf("list level 1: %v", err)
}
sort.Strings(keys)
if len(keys) != 2 || keys[0] != "bar" || keys[1] != "bar/" {
t.Errorf("level 1 expected [bar bar/]: %v", keys)
}
// Scan foo/bar/
keys, err = b.List(context.Background(), "foo/bar/")
if err != nil {
t.Fatalf("list level 2: %v", err)
}
sort.Strings(keys)
if len(keys) != 1 || keys[0] != "baz" {
t.Errorf("level 1 expected [baz]: %v", keys)
}
}
func ExerciseHABackend(t testing.TB, b HABackend, b2 HABackend) {
t.Helper()
// Get the lock
lock, err := b.LockWith("foo", "bar")
if err != nil {
t.Fatalf("initial lock: %v", err)
}
// Attempt to lock
leaderCh, err := lock.Lock(nil)
if err != nil {
t.Fatalf("lock attempt 1: %v", err)
}
if leaderCh == nil {
t.Fatalf("missing leaderCh")
}
// Check the value
held, val, err := lock.Value()
if err != nil {
t.Fatalf("err: %v", err)
}
if !held {
t.Errorf("should be held")
}
if val != "bar" {
t.Errorf("expected value bar: %v", err)
}
// Second acquisition should fail
lock2, err := b2.LockWith("foo", "baz")
if err != nil {
t.Fatalf("lock 2: %v", err)
}
// Cancel attempt in 50 msec
stopCh := make(chan struct{})
time.AfterFunc(50*time.Millisecond, func() {
close(stopCh)
})
// Attempt to lock
leaderCh2, err := lock2.Lock(stopCh)
if err != nil {
t.Fatalf("stop lock 2: %v", err)
}
if leaderCh2 != nil {
t.Errorf("should not have gotten leaderCh: %v", leaderCh2)
}
// Release the first lock
lock.Unlock()
// Attempt to lock should work
leaderCh2, err = lock2.Lock(nil)
if err != nil {
t.Fatalf("lock 2 lock: %v", err)
}
if leaderCh2 == nil {
t.Errorf("should get leaderCh")
}
// Check the value
held, val, err = lock2.Value()
if err != nil {
t.Fatalf("value: %v", err)
}
if !held {
t.Errorf("should still be held")
}
if val != "baz" {
t.Errorf("expected: baz, got: %v", val)
}
// Cleanup
lock2.Unlock()
}
func ExerciseTransactionalBackend(t testing.TB, b Backend) {
t.Helper()
tb, ok := b.(Transactional)
if !ok {
t.Fatal("Not a transactional backend")
}
txns := SetupTestingTransactions(t, b)
if err := tb.Transaction(context.Background(), txns); err != nil {
t.Fatal(err)
}
keys, err := b.List(context.Background(), "")
if err != nil {
t.Fatal(err)
}
expected := []string{"foo", "zip"}
sort.Strings(keys)
sort.Strings(expected)
if !reflect.DeepEqual(keys, expected) {
t.Fatalf("mismatch: expected\n%#v\ngot\n%#v\n", expected, keys)
}
entry, err := b.Get(context.Background(), "foo")
if err != nil {
t.Fatal(err)
}
if entry == nil {
t.Fatal("got nil entry")
}
if entry.Value == nil {
t.Fatal("got nil value")
}
if string(entry.Value) != "bar3" {
t.Fatal("updates did not apply correctly")
}
entry, err = b.Get(context.Background(), "zip")
if err != nil {
t.Fatal(err)
}
if entry == nil {
t.Fatal("got nil entry")
}
if entry.Value == nil {
t.Fatal("got nil value")
}
if string(entry.Value) != "zap3" {
t.Fatal("updates did not apply correctly")
}
}
func SetupTestingTransactions(t testing.TB, b Backend) []*TxnEntry {
t.Helper()
// Add a few keys so that we test rollback with deletion
if err := b.Put(context.Background(), &Entry{
Key: "foo",
Value: []byte("bar"),
}); err != nil {
t.Fatal(err)
}
if err := b.Put(context.Background(), &Entry{
Key: "zip",
Value: []byte("zap"),
}); err != nil {
t.Fatal(err)
}
if err := b.Put(context.Background(), &Entry{
Key: "deleteme",
}); err != nil {
t.Fatal(err)
}
if err := b.Put(context.Background(), &Entry{
Key: "deleteme2",
}); err != nil {
t.Fatal(err)
}
txns := []*TxnEntry{
&TxnEntry{
Operation: PutOperation,
Entry: &Entry{
Key: "foo",
Value: []byte("bar2"),
},
},
&TxnEntry{
Operation: DeleteOperation,
Entry: &Entry{
Key: "deleteme",
},
},
&TxnEntry{
Operation: PutOperation,
Entry: &Entry{
Key: "foo",
Value: []byte("bar3"),
},
},
&TxnEntry{
Operation: DeleteOperation,
Entry: &Entry{
Key: "deleteme2",
},
},
&TxnEntry{
Operation: PutOperation,
Entry: &Entry{
Key: "zip",
Value: []byte("zap3"),
},
},
}
return txns
}