2015-03-02 18:48:53 +00:00
|
|
|
package physical
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-03-02 18:48:53 +00:00
|
|
|
"reflect"
|
2015-04-29 01:51:21 +00:00
|
|
|
"sort"
|
2015-03-02 18:48:53 +00:00
|
|
|
"testing"
|
2015-04-14 18:49:46 +00:00
|
|
|
"time"
|
2015-03-02 18:48:53 +00:00
|
|
|
)
|
|
|
|
|
2018-02-15 01:31:20 +00:00
|
|
|
func ExerciseBackend(t testing.TB, b Backend) {
|
2017-10-23 20:42:56 +00:00
|
|
|
t.Helper()
|
2018-02-15 01:31:20 +00:00
|
|
|
|
2015-03-02 18:48:53 +00:00
|
|
|
// Should be empty
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err := b.List(context.Background(), "")
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("initial list failed: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
if len(keys) != 0 {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Errorf("initial not empty: %v", keys)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete should work if it does not exist
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Delete(context.Background(), "foo")
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("idempotent delete: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 01:31:20 +00:00
|
|
|
// Get should not fail, but be nil
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := b.Get(context.Background(), "foo")
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-20 05:26:41 +00:00
|
|
|
t.Fatalf("initial get failed: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
if out != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Errorf("initial get was not nil: %v", out)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make an entry
|
|
|
|
e := &Entry{Key: "foo", Value: []byte("test")}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Put(context.Background(), e)
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("put failed: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get should work
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = b.Get(context.Background(), "foo")
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("get failed: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(out, e) {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Errorf("bad: %v expected: %v", out, e)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List should not be empty
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = b.List(context.Background(), "")
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("list failed: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
2018-02-15 01:31:20 +00:00
|
|
|
if len(keys) != 1 || keys[0] != "foo" {
|
|
|
|
t.Errorf("keys[0] did not equal foo: %v", keys)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete should work
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Delete(context.Background(), "foo")
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("delete: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should be empty
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = b.List(context.Background(), "")
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("list after delete: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
if len(keys) != 0 {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Errorf("list after delete not empty: %v", keys)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get should fail
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = b.Get(context.Background(), "foo")
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("get after delete: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
if out != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Errorf("get after delete not nil: %v", out)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
2015-05-12 14:12:00 +00:00
|
|
|
|
|
|
|
// Multiple Puts should work; GH-189
|
|
|
|
e = &Entry{Key: "foo", Value: []byte("test")}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Put(context.Background(), e)
|
2015-05-12 14:12:00 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("multi put 1 failed: %v", err)
|
2015-05-12 14:12:00 +00:00
|
|
|
}
|
|
|
|
e = &Entry{Key: "foo", Value: []byte("test")}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Put(context.Background(), e)
|
2015-05-12 14:12:00 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("multi put 2 failed: %v", err)
|
2015-05-12 14:12:00 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 16:25:22 +00:00
|
|
|
// Make a nested entry
|
|
|
|
e = &Entry{Key: "foo/bar", Value: []byte("baz")}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Put(context.Background(), e)
|
2015-05-12 16:25:22 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("nested put failed: %v", err)
|
2015-05-12 16:25:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = b.List(context.Background(), "")
|
2016-08-31 18:12:28 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("list multi failed: %v", err)
|
2016-08-31 18:12:28 +00:00
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
2018-02-15 01:31:20 +00:00
|
|
|
if len(keys) != 2 || keys[0] != "foo" || keys[1] != "foo/" {
|
|
|
|
t.Errorf("expected 2 keys [foo, foo/]: %v", keys)
|
2016-08-31 18:12:28 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 16:25:22 +00:00
|
|
|
// Delete with children should work
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Delete(context.Background(), "foo")
|
2015-05-12 16:25:22 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("delete after multi: %v", err)
|
2015-05-12 16:25:22 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 08:59:40 +00:00
|
|
|
// Get should return the child
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = b.Get(context.Background(), "foo/bar")
|
2015-07-13 08:59:40 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("get after multi delete: %v", err)
|
2015-07-13 08:59:40 +00:00
|
|
|
}
|
|
|
|
if out == nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Errorf("get after multi delete not nil: %v", out)
|
2015-07-13 08:59:40 +00:00
|
|
|
}
|
2016-08-31 18:12:28 +00:00
|
|
|
|
2016-10-04 13:26:07 +00:00
|
|
|
// Removal of nested secret should not leave artifacts
|
|
|
|
e = &Entry{Key: "foo/nested1/nested2/nested3", Value: []byte("baz")}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Put(context.Background(), e)
|
2016-10-04 13:26:07 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("deep nest: %v", err)
|
2016-10-04 13:26:07 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Delete(context.Background(), "foo/nested1/nested2/nested3")
|
2016-10-04 13:26:07 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("failed to remove deep nest: %v", err)
|
2016-10-04 13:26:07 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = b.List(context.Background(), "foo/")
|
2016-10-04 13:26:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-02-15 01:31:20 +00:00
|
|
|
if len(keys) != 1 || keys[0] != "bar" {
|
|
|
|
t.Errorf("should be exactly 1 key == bar: %v", keys)
|
2016-10-04 13:26:07 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 18:12:28 +00:00
|
|
|
// Make a second nested entry to test prefix removal
|
|
|
|
e = &Entry{Key: "foo/zip", Value: []byte("zap")}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Put(context.Background(), e)
|
2016-08-31 18:12:28 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("failed to create second nested: %v", err)
|
2016-08-31 18:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete should not remove the prefix
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Delete(context.Background(), "foo/bar")
|
2016-08-31 18:12:28 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("failed to delete nested prefix: %v", err)
|
2016-08-31 18:12:28 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = b.List(context.Background(), "")
|
2016-08-31 18:12:28 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("list nested prefix: %v", err)
|
2016-08-31 18:12:28 +00:00
|
|
|
}
|
2018-02-15 01:31:20 +00:00
|
|
|
if len(keys) != 1 || keys[0] != "foo/" {
|
|
|
|
t.Errorf("should be exactly 1 key == foo/: %v", keys)
|
2016-08-31 18:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete should remove the prefix
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Delete(context.Background(), "foo/zip")
|
2016-08-31 18:12:28 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("failed to delete second prefix: %v", err)
|
2016-08-31 18:12:28 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = b.List(context.Background(), "")
|
2016-08-31 18:12:28 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("listing after second delete failed: %v", err)
|
2016-08-31 18:12:28 +00:00
|
|
|
}
|
|
|
|
if len(keys) != 0 {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Errorf("should be empty at end: %v", keys)
|
2016-08-31 18:12:28 +00:00
|
|
|
}
|
2018-05-16 17:30:56 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 01:31:20 +00:00
|
|
|
func ExerciseBackend_ListPrefix(t testing.TB, b Backend) {
|
2017-10-23 20:42:56 +00:00
|
|
|
t.Helper()
|
2018-02-15 01:31:20 +00:00
|
|
|
|
2015-03-02 18:48:53 +00:00
|
|
|
e1 := &Entry{Key: "foo", Value: []byte("test")}
|
|
|
|
e2 := &Entry{Key: "foo/bar", Value: []byte("test")}
|
|
|
|
e3 := &Entry{Key: "foo/bar/baz", Value: []byte("test")}
|
|
|
|
|
2017-01-03 19:43:46 +00:00
|
|
|
defer func() {
|
2018-01-19 06:44:44 +00:00
|
|
|
b.Delete(context.Background(), "foo")
|
|
|
|
b.Delete(context.Background(), "foo/bar")
|
|
|
|
b.Delete(context.Background(), "foo/bar/baz")
|
2017-01-03 19:43:46 +00:00
|
|
|
}()
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err := b.Put(context.Background(), e1)
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("failed to put entry 1: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Put(context.Background(), e2)
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("failed to put entry 2: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Put(context.Background(), e3)
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("failed to put entry 3: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan the root
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err := b.List(context.Background(), "")
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("list root: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
2015-04-29 02:01:01 +00:00
|
|
|
sort.Strings(keys)
|
2018-02-15 01:31:20 +00:00
|
|
|
if len(keys) != 2 || keys[0] != "foo" || keys[1] != "foo/" {
|
|
|
|
t.Errorf("root expected [foo foo/]: %v", keys)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan foo/
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = b.List(context.Background(), "foo/")
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("list level 1: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
2015-04-29 01:51:21 +00:00
|
|
|
sort.Strings(keys)
|
2018-02-15 01:31:20 +00:00
|
|
|
if len(keys) != 2 || keys[0] != "bar" || keys[1] != "bar/" {
|
|
|
|
t.Errorf("level 1 expected [bar bar/]: %v", keys)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan foo/bar/
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err = b.List(context.Background(), "foo/bar/")
|
2015-03-02 18:48:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("list level 2: %v", err)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
2015-04-29 02:01:01 +00:00
|
|
|
sort.Strings(keys)
|
2018-02-15 01:31:20 +00:00
|
|
|
if len(keys) != 1 || keys[0] != "baz" {
|
|
|
|
t.Errorf("level 1 expected [baz]: %v", keys)
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-14 18:49:46 +00:00
|
|
|
|
2018-02-15 01:31:20 +00:00
|
|
|
func ExerciseHABackend(t testing.TB, b HABackend, b2 HABackend) {
|
2017-10-23 20:42:56 +00:00
|
|
|
t.Helper()
|
2018-02-15 01:31:20 +00:00
|
|
|
|
2015-04-14 18:49:46 +00:00
|
|
|
// Get the lock
|
2015-04-14 23:36:53 +00:00
|
|
|
lock, err := b.LockWith("foo", "bar")
|
2015-04-14 18:49:46 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("initial lock: %v", err)
|
2015-04-14 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to lock
|
|
|
|
leaderCh, err := lock.Lock(nil)
|
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("lock attempt 1: %v", err)
|
2015-04-14 18:49:46 +00:00
|
|
|
}
|
|
|
|
if leaderCh == nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("missing leaderCh")
|
2015-04-14 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 23:36:53 +00:00
|
|
|
// Check the value
|
|
|
|
held, val, err := lock.Value()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !held {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Errorf("should be held")
|
2015-04-14 23:36:53 +00:00
|
|
|
}
|
|
|
|
if val != "bar" {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Errorf("expected value bar: %v", err)
|
2015-04-14 23:36:53 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 18:49:46 +00:00
|
|
|
// Second acquisition should fail
|
2015-04-14 23:36:53 +00:00
|
|
|
lock2, err := b2.LockWith("foo", "baz")
|
2015-04-14 18:49:46 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("lock 2: %v", err)
|
2015-04-14 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("stop lock 2: %v", err)
|
2015-04-14 18:49:46 +00:00
|
|
|
}
|
|
|
|
if leaderCh2 != nil {
|
2018-11-01 17:31:09 +00:00
|
|
|
t.Errorf("should not have gotten leaderCh: %v", leaderCh2)
|
2015-04-14 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release the first lock
|
|
|
|
lock.Unlock()
|
|
|
|
|
|
|
|
// Attempt to lock should work
|
|
|
|
leaderCh2, err = lock2.Lock(nil)
|
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("lock 2 lock: %v", err)
|
2015-04-14 18:49:46 +00:00
|
|
|
}
|
|
|
|
if leaderCh2 == nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Errorf("should get leaderCh")
|
2015-04-14 18:49:46 +00:00
|
|
|
}
|
2015-04-14 23:36:53 +00:00
|
|
|
|
|
|
|
// Check the value
|
2018-11-01 17:31:09 +00:00
|
|
|
held, val, err = lock2.Value()
|
2015-04-14 23:36:53 +00:00
|
|
|
if err != nil {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Fatalf("value: %v", err)
|
2015-04-14 23:36:53 +00:00
|
|
|
}
|
|
|
|
if !held {
|
2018-02-15 01:31:20 +00:00
|
|
|
t.Errorf("should still be held")
|
2015-04-14 23:36:53 +00:00
|
|
|
}
|
|
|
|
if val != "baz" {
|
2018-11-01 17:31:09 +00:00
|
|
|
t.Errorf("expected: baz, got: %v", val)
|
2015-04-14 23:36:53 +00:00
|
|
|
}
|
2018-02-15 01:31:20 +00:00
|
|
|
|
2015-05-26 04:12:16 +00:00
|
|
|
// Cleanup
|
|
|
|
lock2.Unlock()
|
2015-04-14 18:49:46 +00:00
|
|
|
}
|
2016-12-01 19:42:31 +00:00
|
|
|
|
2018-02-15 01:31:20 +00:00
|
|
|
func ExerciseTransactionalBackend(t testing.TB, b Backend) {
|
2017-10-23 20:42:56 +00:00
|
|
|
t.Helper()
|
2017-07-23 12:54:33 +00:00
|
|
|
tb, ok := b.(Transactional)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Not a transactional backend")
|
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
txns := SetupTestingTransactions(t, b)
|
2017-07-23 12:54:33 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := tb.Transaction(context.Background(), txns); err != nil {
|
2017-07-23 12:54:33 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err := b.List(context.Background(), "")
|
2017-07-23 12:54:33 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err := b.Get(context.Background(), "foo")
|
2017-07-23 12:54:33 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err = b.Get(context.Background(), "zip")
|
2017-07-23 12:54:33 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
2017-08-03 17:24:27 +00:00
|
|
|
|
2018-02-15 01:31:20 +00:00
|
|
|
func SetupTestingTransactions(t testing.TB, b Backend) []*TxnEntry {
|
2017-10-23 20:42:56 +00:00
|
|
|
t.Helper()
|
2017-08-03 17:24:27 +00:00
|
|
|
// Add a few keys so that we test rollback with deletion
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Put(context.Background(), &Entry{
|
2017-08-03 17:24:27 +00:00
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Put(context.Background(), &Entry{
|
2017-08-03 17:24:27 +00:00
|
|
|
Key: "zip",
|
|
|
|
Value: []byte("zap"),
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Put(context.Background(), &Entry{
|
2017-08-03 17:24:27 +00:00
|
|
|
Key: "deleteme",
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Put(context.Background(), &Entry{
|
2017-08-03 17:24:27 +00:00
|
|
|
Key: "deleteme2",
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-23 20:42:56 +00:00
|
|
|
txns := []*TxnEntry{
|
|
|
|
&TxnEntry{
|
2017-08-03 17:24:27 +00:00
|
|
|
Operation: PutOperation,
|
|
|
|
Entry: &Entry{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar2"),
|
|
|
|
},
|
|
|
|
},
|
2017-10-23 20:42:56 +00:00
|
|
|
&TxnEntry{
|
2017-08-03 17:24:27 +00:00
|
|
|
Operation: DeleteOperation,
|
|
|
|
Entry: &Entry{
|
|
|
|
Key: "deleteme",
|
|
|
|
},
|
|
|
|
},
|
2017-10-23 20:42:56 +00:00
|
|
|
&TxnEntry{
|
2017-08-03 17:24:27 +00:00
|
|
|
Operation: PutOperation,
|
|
|
|
Entry: &Entry{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar3"),
|
|
|
|
},
|
|
|
|
},
|
2017-10-23 20:42:56 +00:00
|
|
|
&TxnEntry{
|
2017-08-03 17:24:27 +00:00
|
|
|
Operation: DeleteOperation,
|
|
|
|
Entry: &Entry{
|
|
|
|
Key: "deleteme2",
|
|
|
|
},
|
|
|
|
},
|
2017-10-23 20:42:56 +00:00
|
|
|
&TxnEntry{
|
2017-08-03 17:24:27 +00:00
|
|
|
Operation: PutOperation,
|
|
|
|
Entry: &Entry{
|
|
|
|
Key: "zip",
|
|
|
|
Value: []byte("zap3"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return txns
|
|
|
|
}
|