From cab7572b003a1774470153ff536eebe13f85cf7c Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Tue, 15 Aug 2017 10:54:02 -0700 Subject: [PATCH] store: separate tests that need Store from those needing *store --- store/metrics.go | 6 +- store/store_test.go | 651 +++++++++++----------------------------- store/store_ttl_test.go | 360 ++++++++++++++++++++++ store/store_v2_test.go | 62 ++++ 4 files changed, 606 insertions(+), 473 deletions(-) create mode 100644 store/store_ttl_test.go create mode 100644 store/store_v2_test.go diff --git a/store/metrics.go b/store/metrics.go index 26404ba72e3..08cd4f030ce 100644 --- a/store/metrics.go +++ b/store/metrics.go @@ -86,7 +86,11 @@ const ( ) func init() { - prometheus.MustRegister(readCounter) + if prometheus.Register(readCounter) != nil { + // Tests will try to double register sicne the tests use both + // store and store_test packages; ignore second attempts. + return + } prometheus.MustRegister(writeCounter) prometheus.MustRegister(expireCounter) prometheus.MustRegister(watchRequests) diff --git a/store/store_test.go b/store/store_test.go index 49f6fc620e9..00e837e8117 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -12,19 +12,24 @@ // See the License for the specific language governing permissions and // limitations under the License. -package store +package store_test import ( "testing" - "time" etcdErr "github.com/coreos/etcd/error" "github.com/coreos/etcd/pkg/testutil" - "github.com/jonboulle/clockwork" + "github.com/coreos/etcd/store" ) +type StoreCloser interface { + store.Store + Close() +} + func TestNewStoreWithNamespaces(t *testing.T) { - s := newStore("/0", "/1") + s := newTestStore(t, "/0", "/1") + defer s.Close() _, err := s.Get("/0", false, false) testutil.AssertNil(t, err) @@ -34,8 +39,10 @@ func TestNewStoreWithNamespaces(t *testing.T) { // Ensure that the store can retrieve an existing value. func TestStoreGetValue(t *testing.T) { - s := newStore() - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s := newTestStore(t) + defer s.Close() + + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) var eidx uint64 = 1 e, err := s.Get("/foo", false, false) testutil.AssertNil(t, err) @@ -45,90 +52,23 @@ func TestStoreGetValue(t *testing.T) { testutil.AssertEqual(t, *e.Node.Value, "bar") } -// Ensure that any TTL <= minExpireTime becomes Permanent -func TestMinExpireTime(t *testing.T) { - s := newStore() - fc := clockwork.NewFakeClock() - s.clock = fc - // FakeClock starts at 0, so minExpireTime should be far in the future.. but just in case - testutil.AssertTrue(t, minExpireTime.After(fc.Now()), "minExpireTime should be ahead of FakeClock!") - s.Create("/foo", false, "Y", false, TTLOptionSet{ExpireTime: fc.Now().Add(3 * time.Second)}) - fc.Advance(5 * time.Second) - // Ensure it hasn't expired - s.DeleteExpiredKeys(fc.Now()) - var eidx uint64 = 1 - e, err := s.Get("/foo", true, false) - testutil.AssertNil(t, err) - testutil.AssertEqual(t, e.EtcdIndex, eidx) - testutil.AssertEqual(t, e.Action, "get") - testutil.AssertEqual(t, e.Node.Key, "/foo") - testutil.AssertEqual(t, e.Node.TTL, int64(0)) -} - -// Ensure that the store can recursively retrieve a directory listing. -// Note that hidden files should not be returned. -func TestStoreGetDirectory(t *testing.T) { - s := newStore() - fc := newFakeClock() - s.clock = fc - s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/bar", false, "X", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/_hidden", false, "*", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/baz", true, "", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/baz/bat", false, "Y", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/baz/_hidden", false, "*", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/baz/ttl", false, "Y", false, TTLOptionSet{ExpireTime: fc.Now().Add(time.Second * 3)}) - var eidx uint64 = 7 - e, err := s.Get("/foo", true, false) - testutil.AssertNil(t, err) - testutil.AssertEqual(t, e.EtcdIndex, eidx) - testutil.AssertEqual(t, e.Action, "get") - testutil.AssertEqual(t, e.Node.Key, "/foo") - testutil.AssertEqual(t, len(e.Node.Nodes), 2) - var bazNodes NodeExterns - for _, node := range e.Node.Nodes { - switch node.Key { - case "/foo/bar": - testutil.AssertEqual(t, *node.Value, "X") - testutil.AssertEqual(t, node.Dir, false) - case "/foo/baz": - testutil.AssertEqual(t, node.Dir, true) - testutil.AssertEqual(t, len(node.Nodes), 2) - bazNodes = node.Nodes - default: - t.Errorf("key = %s, not matched", node.Key) - } - } - for _, node := range bazNodes { - switch node.Key { - case "/foo/baz/bat": - testutil.AssertEqual(t, *node.Value, "Y") - testutil.AssertEqual(t, node.Dir, false) - case "/foo/baz/ttl": - testutil.AssertEqual(t, *node.Value, "Y") - testutil.AssertEqual(t, node.Dir, false) - testutil.AssertEqual(t, node.TTL, int64(3)) - default: - t.Errorf("key = %s, not matched", node.Key) - } - } -} - // Ensure that the store can retrieve a directory in sorted order. func TestStoreGetSorted(t *testing.T) { - s := newStore() - s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/x", false, "0", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/z", false, "0", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/y", true, "", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/y/a", false, "0", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/y/b", false, "0", false, TTLOptionSet{ExpireTime: Permanent}) + s := newTestStore(t) + defer s.Close() + + s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}) + s.Create("/foo/x", false, "0", false, store.TTLOptionSet{ExpireTime: store.Permanent}) + s.Create("/foo/z", false, "0", false, store.TTLOptionSet{ExpireTime: store.Permanent}) + s.Create("/foo/y", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}) + s.Create("/foo/y/a", false, "0", false, store.TTLOptionSet{ExpireTime: store.Permanent}) + s.Create("/foo/y/b", false, "0", false, store.TTLOptionSet{ExpireTime: store.Permanent}) var eidx uint64 = 6 e, err := s.Get("/foo", true, true) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) - var yNodes NodeExterns + var yNodes store.NodeExterns sortedStrings := []string{"/foo/x", "/foo/y", "/foo/z"} for i := range e.Node.Nodes { node := e.Node.Nodes[i] @@ -150,11 +90,12 @@ func TestStoreGetSorted(t *testing.T) { } func TestSet(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() // Set /foo="" var eidx uint64 = 1 - e, err := s.Set("/foo", false, "", TTLOptionSet{ExpireTime: Permanent}) + e, err := s.Set("/foo", false, "", store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "set") @@ -168,7 +109,7 @@ func TestSet(t *testing.T) { // Set /foo="bar" eidx = 2 - e, err = s.Set("/foo", false, "bar", TTLOptionSet{ExpireTime: Permanent}) + e, err = s.Set("/foo", false, "bar", store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "set") @@ -186,7 +127,7 @@ func TestSet(t *testing.T) { testutil.AssertEqual(t, e.PrevNode.ModifiedIndex, uint64(1)) // Set /foo="baz" (for testing prevNode) eidx = 3 - e, err = s.Set("/foo", false, "baz", TTLOptionSet{ExpireTime: Permanent}) + e, err = s.Set("/foo", false, "baz", store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "set") @@ -205,7 +146,7 @@ func TestSet(t *testing.T) { // Set /dir as a directory eidx = 4 - e, err = s.Set("/dir", true, "", TTLOptionSet{ExpireTime: Permanent}) + e, err = s.Set("/dir", true, "", store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "set") @@ -220,10 +161,12 @@ func TestSet(t *testing.T) { // Ensure that the store can create a new key if it doesn't already exist. func TestStoreCreateValue(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() + // Create /foo=bar var eidx uint64 = 1 - e, err := s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + e, err := s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "create") @@ -237,7 +180,7 @@ func TestStoreCreateValue(t *testing.T) { // Create /empty="" eidx = 2 - e, err = s.Create("/empty", false, "", false, TTLOptionSet{ExpireTime: Permanent}) + e, err = s.Create("/empty", false, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "create") @@ -253,9 +196,11 @@ func TestStoreCreateValue(t *testing.T) { // Ensure that the store can create a new directory if it doesn't already exist. func TestStoreCreateDirectory(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() + var eidx uint64 = 1 - e, err := s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) + e, err := s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "create") @@ -265,12 +210,14 @@ func TestStoreCreateDirectory(t *testing.T) { // Ensure that the store fails to create a key if it already exists. func TestStoreCreateFailsIfExists(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() + // create /foo as dir - s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}) // create /foo as dir again - e, _err := s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) + e, _err := s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}) err := _err.(*etcdErr.Error) testutil.AssertEqual(t, err.ErrorCode, etcdErr.EcodeNodeExist) testutil.AssertEqual(t, err.Message, "Key already exists") @@ -281,12 +228,14 @@ func TestStoreCreateFailsIfExists(t *testing.T) { // Ensure that the store can update a key if it already exists. func TestStoreUpdateValue(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() + // create /foo=bar - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) // update /foo="bzr" var eidx uint64 = 2 - e, err := s.Update("/foo", "baz", TTLOptionSet{ExpireTime: Permanent}) + e, err := s.Update("/foo", "baz", store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "update") @@ -307,7 +256,7 @@ func TestStoreUpdateValue(t *testing.T) { // update /foo="" eidx = 3 - e, err = s.Update("/foo", "", TTLOptionSet{ExpireTime: Permanent}) + e, err = s.Update("/foo", "", store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "update") @@ -329,9 +278,11 @@ func TestStoreUpdateValue(t *testing.T) { // Ensure that the store cannot update a directory. func TestStoreUpdateFailsIfDirectory(t *testing.T) { - s := newStore() - s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) - e, _err := s.Update("/foo", "baz", TTLOptionSet{ExpireTime: Permanent}) + s := newTestStore(t) + defer s.Close() + + s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}) + e, _err := s.Update("/foo", "baz", store.TTLOptionSet{ExpireTime: store.Permanent}) err := _err.(*etcdErr.Error) testutil.AssertEqual(t, err.ErrorCode, etcdErr.EcodeNotFile) testutil.AssertEqual(t, err.Message, "Not a file") @@ -339,55 +290,13 @@ func TestStoreUpdateFailsIfDirectory(t *testing.T) { testutil.AssertNil(t, e) } -// Ensure that the store can update the TTL on a value. -func TestStoreUpdateValueTTL(t *testing.T) { - s := newStore() - fc := newFakeClock() - s.clock = fc - - var eidx uint64 = 2 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) - _, err := s.Update("/foo", "baz", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)}) - testutil.AssertNil(t, err) - e, _ := s.Get("/foo", false, false) - testutil.AssertEqual(t, *e.Node.Value, "baz") - testutil.AssertEqual(t, e.EtcdIndex, eidx) - fc.Advance(600 * time.Millisecond) - s.DeleteExpiredKeys(fc.Now()) - e, err = s.Get("/foo", false, false) - testutil.AssertNil(t, e) - testutil.AssertEqual(t, err.(*etcdErr.Error).ErrorCode, etcdErr.EcodeKeyNotFound) -} - -// Ensure that the store can update the TTL on a directory. -func TestStoreUpdateDirTTL(t *testing.T) { - s := newStore() - fc := newFakeClock() - s.clock = fc - - var eidx uint64 = 3 - s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent}) - e, err := s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)}) - testutil.AssertNil(t, err) - testutil.AssertEqual(t, e.Node.Dir, true) - testutil.AssertEqual(t, e.EtcdIndex, eidx) - e, _ = s.Get("/foo/bar", false, false) - testutil.AssertEqual(t, *e.Node.Value, "baz") - testutil.AssertEqual(t, e.EtcdIndex, eidx) - - fc.Advance(600 * time.Millisecond) - s.DeleteExpiredKeys(fc.Now()) - e, err = s.Get("/foo/bar", false, false) - testutil.AssertNil(t, e) - testutil.AssertEqual(t, err.(*etcdErr.Error).ErrorCode, etcdErr.EcodeKeyNotFound) -} - // Ensure that the store can delete a value. func TestStoreDeleteValue(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() + var eidx uint64 = 2 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) e, err := s.Delete("/foo", false, false) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) @@ -399,11 +308,13 @@ func TestStoreDeleteValue(t *testing.T) { } // Ensure that the store can delete a directory if recursive is specified. -func TestStoreDeleteDiretory(t *testing.T) { - s := newStore() +func TestStoreDeleteDirectory(t *testing.T) { + s := newTestStore(t) + defer s.Close() + // create directory /foo var eidx uint64 = 2 - s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}) // delete /foo with dir = true and recursive = false // this should succeed, since the directory is empty e, err := s.Delete("/foo", true, false) @@ -416,7 +327,8 @@ func TestStoreDeleteDiretory(t *testing.T) { testutil.AssertEqual(t, e.PrevNode.Dir, true) // create directory /foo and directory /foo/bar - s.Create("/foo/bar", true, "", false, TTLOptionSet{ExpireTime: Permanent}) + _, err = s.Create("/foo/bar", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}) + testutil.AssertNil(t, err) // delete /foo with dir = true and recursive = false // this should fail, since the directory is not empty _, err = s.Delete("/foo", true, false) @@ -434,9 +346,11 @@ func TestStoreDeleteDiretory(t *testing.T) { // Ensure that the store cannot delete a directory if both of recursive // and dir are not specified. -func TestStoreDeleteDiretoryFailsIfNonRecursiveAndDir(t *testing.T) { - s := newStore() - s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) +func TestStoreDeleteDirectoryFailsIfNonRecursiveAndDir(t *testing.T) { + s := newTestStore(t) + defer s.Close() + + s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}) e, _err := s.Delete("/foo", false, false) err := _err.(*etcdErr.Error) testutil.AssertEqual(t, err.ErrorCode, etcdErr.EcodeNotFile) @@ -445,30 +359,33 @@ func TestStoreDeleteDiretoryFailsIfNonRecursiveAndDir(t *testing.T) { } func TestRootRdOnly(t *testing.T) { - s := newStore("/0") + s := newTestStore(t, "/0") + defer s.Close() for _, tt := range []string{"/", "/0"} { - _, err := s.Set(tt, true, "", TTLOptionSet{ExpireTime: Permanent}) + _, err := s.Set(tt, true, "", store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNotNil(t, err) _, err = s.Delete(tt, true, true) testutil.AssertNotNil(t, err) - _, err = s.Create(tt, true, "", false, TTLOptionSet{ExpireTime: Permanent}) + _, err = s.Create(tt, true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNotNil(t, err) - _, err = s.Update(tt, "", TTLOptionSet{ExpireTime: Permanent}) + _, err = s.Update(tt, "", store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNotNil(t, err) - _, err = s.CompareAndSwap(tt, "", 0, "", TTLOptionSet{ExpireTime: Permanent}) + _, err = s.CompareAndSwap(tt, "", 0, "", store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNotNil(t, err) } } func TestStoreCompareAndDeletePrevValue(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() + var eidx uint64 = 2 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) e, err := s.CompareAndDelete("/foo", "bar", 0) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) @@ -484,9 +401,11 @@ func TestStoreCompareAndDeletePrevValue(t *testing.T) { } func TestStoreCompareAndDeletePrevValueFailsIfNotMatch(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() + var eidx uint64 = 1 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) e, _err := s.CompareAndDelete("/foo", "baz", 0) err := _err.(*etcdErr.Error) testutil.AssertEqual(t, err.ErrorCode, etcdErr.EcodeTestFailed) @@ -498,9 +417,11 @@ func TestStoreCompareAndDeletePrevValueFailsIfNotMatch(t *testing.T) { } func TestStoreCompareAndDeletePrevIndex(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() + var eidx uint64 = 2 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) e, err := s.CompareAndDelete("/foo", "", 1) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) @@ -514,9 +435,11 @@ func TestStoreCompareAndDeletePrevIndex(t *testing.T) { } func TestStoreCompareAndDeletePrevIndexFailsIfNotMatch(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() + var eidx uint64 = 1 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) e, _err := s.CompareAndDelete("/foo", "", 100) testutil.AssertNotNil(t, _err) err := _err.(*etcdErr.Error) @@ -529,9 +452,11 @@ func TestStoreCompareAndDeletePrevIndexFailsIfNotMatch(t *testing.T) { } // Ensure that the store cannot delete a directory. -func TestStoreCompareAndDeleteDiretoryFail(t *testing.T) { - s := newStore() - s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) +func TestStoreCompareAndDeleteDirectoryFail(t *testing.T) { + s := newTestStore(t) + defer s.Close() + + s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}) _, _err := s.CompareAndDelete("/foo", "", 0) testutil.AssertNotNil(t, _err) err := _err.(*etcdErr.Error) @@ -540,10 +465,12 @@ func TestStoreCompareAndDeleteDiretoryFail(t *testing.T) { // Ensure that the store can conditionally update a key if it has a previous value. func TestStoreCompareAndSwapPrevValue(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() + var eidx uint64 = 2 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) - e, err := s.CompareAndSwap("/foo", "bar", 0, "baz", TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) + e, err := s.CompareAndSwap("/foo", "bar", 0, "baz", store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "compareAndSwap") @@ -561,10 +488,11 @@ func TestStoreCompareAndSwapPrevValue(t *testing.T) { // Ensure that the store cannot conditionally update a key if it has the wrong previous value. func TestStoreCompareAndSwapPrevValueFailsIfNotMatch(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 1 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) - e, _err := s.CompareAndSwap("/foo", "wrong_value", 0, "baz", TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) + e, _err := s.CompareAndSwap("/foo", "wrong_value", 0, "baz", store.TTLOptionSet{ExpireTime: store.Permanent}) err := _err.(*etcdErr.Error) testutil.AssertEqual(t, err.ErrorCode, etcdErr.EcodeTestFailed) testutil.AssertEqual(t, err.Message, "Compare failed") @@ -576,10 +504,11 @@ func TestStoreCompareAndSwapPrevValueFailsIfNotMatch(t *testing.T) { // Ensure that the store can conditionally update a key if it has a previous index. func TestStoreCompareAndSwapPrevIndex(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 2 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) - e, err := s.CompareAndSwap("/foo", "", 1, "baz", TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) + e, err := s.CompareAndSwap("/foo", "", 1, "baz", store.TTLOptionSet{ExpireTime: store.Permanent}) testutil.AssertNil(t, err) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "compareAndSwap") @@ -598,10 +527,11 @@ func TestStoreCompareAndSwapPrevIndex(t *testing.T) { // Ensure that the store cannot conditionally update a key if it has the wrong previous index. func TestStoreCompareAndSwapPrevIndexFailsIfNotMatch(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 1 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) - e, _err := s.CompareAndSwap("/foo", "", 100, "baz", TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) + e, _err := s.CompareAndSwap("/foo", "", 100, "baz", store.TTLOptionSet{ExpireTime: store.Permanent}) err := _err.(*etcdErr.Error) testutil.AssertEqual(t, err.ErrorCode, etcdErr.EcodeTestFailed) testutil.AssertEqual(t, err.Message, "Compare failed") @@ -613,12 +543,13 @@ func TestStoreCompareAndSwapPrevIndexFailsIfNotMatch(t *testing.T) { // Ensure that the store can watch for key creation. func TestStoreWatchCreate(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 0 w, _ := s.Watch("/foo", false, false, 0) c := w.EventChan() testutil.AssertEqual(t, w.StartIndex(), eidx) - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) eidx = 1 e := nbselect(c) testutil.AssertEqual(t, e.EtcdIndex, eidx) @@ -630,12 +561,13 @@ func TestStoreWatchCreate(t *testing.T) { // Ensure that the store can watch for recursive key creation. func TestStoreWatchRecursiveCreate(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 0 w, _ := s.Watch("/foo", true, false, 0) testutil.AssertEqual(t, w.StartIndex(), eidx) eidx = 1 - s.Create("/foo/bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/bar", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent}) e := nbselect(w.EventChan()) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "create") @@ -644,13 +576,14 @@ func TestStoreWatchRecursiveCreate(t *testing.T) { // Ensure that the store can watch for key updates. func TestStoreWatchUpdate(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 1 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) w, _ := s.Watch("/foo", false, false, 0) testutil.AssertEqual(t, w.StartIndex(), eidx) eidx = 2 - s.Update("/foo", "baz", TTLOptionSet{ExpireTime: Permanent}) + s.Update("/foo", "baz", store.TTLOptionSet{ExpireTime: store.Permanent}) e := nbselect(w.EventChan()) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "update") @@ -659,13 +592,14 @@ func TestStoreWatchUpdate(t *testing.T) { // Ensure that the store can watch for recursive key updates. func TestStoreWatchRecursiveUpdate(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 1 - s.Create("/foo/bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/bar", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent}) w, _ := s.Watch("/foo", true, false, 0) testutil.AssertEqual(t, w.StartIndex(), eidx) eidx = 2 - s.Update("/foo/bar", "baz", TTLOptionSet{ExpireTime: Permanent}) + s.Update("/foo/bar", "baz", store.TTLOptionSet{ExpireTime: store.Permanent}) e := nbselect(w.EventChan()) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "update") @@ -674,9 +608,10 @@ func TestStoreWatchRecursiveUpdate(t *testing.T) { // Ensure that the store can watch for key deletions. func TestStoreWatchDelete(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 1 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) w, _ := s.Watch("/foo", false, false, 0) testutil.AssertEqual(t, w.StartIndex(), eidx) eidx = 2 @@ -689,9 +624,10 @@ func TestStoreWatchDelete(t *testing.T) { // Ensure that the store can watch for recursive key deletions. func TestStoreWatchRecursiveDelete(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 1 - s.Create("/foo/bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/bar", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent}) w, _ := s.Watch("/foo", true, false, 0) testutil.AssertEqual(t, w.StartIndex(), eidx) eidx = 2 @@ -704,13 +640,14 @@ func TestStoreWatchRecursiveDelete(t *testing.T) { // Ensure that the store can watch for CAS updates. func TestStoreWatchCompareAndSwap(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 1 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) w, _ := s.Watch("/foo", false, false, 0) testutil.AssertEqual(t, w.StartIndex(), eidx) eidx = 2 - s.CompareAndSwap("/foo", "bar", 0, "baz", TTLOptionSet{ExpireTime: Permanent}) + s.CompareAndSwap("/foo", "bar", 0, "baz", store.TTLOptionSet{ExpireTime: store.Permanent}) e := nbselect(w.EventChan()) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "compareAndSwap") @@ -719,172 +656,28 @@ func TestStoreWatchCompareAndSwap(t *testing.T) { // Ensure that the store can watch for recursive CAS updates. func TestStoreWatchRecursiveCompareAndSwap(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 1 - s.Create("/foo/bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/bar", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent}) w, _ := s.Watch("/foo", true, false, 0) testutil.AssertEqual(t, w.StartIndex(), eidx) eidx = 2 - s.CompareAndSwap("/foo/bar", "baz", 0, "bat", TTLOptionSet{ExpireTime: Permanent}) + s.CompareAndSwap("/foo/bar", "baz", 0, "bat", store.TTLOptionSet{ExpireTime: store.Permanent}) e := nbselect(w.EventChan()) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "compareAndSwap") testutil.AssertEqual(t, e.Node.Key, "/foo/bar") } -// Ensure that the store can watch for key expiration. -func TestStoreWatchExpire(t *testing.T) { - s := newStore() - fc := newFakeClock() - s.clock = fc - - var eidx uint64 = 3 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(400 * time.Millisecond)}) - s.Create("/foofoo", false, "barbarbar", false, TTLOptionSet{ExpireTime: fc.Now().Add(450 * time.Millisecond)}) - s.Create("/foodir", true, "", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)}) - - w, _ := s.Watch("/", true, false, 0) - testutil.AssertEqual(t, w.StartIndex(), eidx) - c := w.EventChan() - e := nbselect(c) - testutil.AssertNil(t, e) - fc.Advance(600 * time.Millisecond) - s.DeleteExpiredKeys(fc.Now()) - eidx = 4 - e = nbselect(c) - testutil.AssertEqual(t, e.EtcdIndex, eidx) - testutil.AssertEqual(t, e.Action, "expire") - testutil.AssertEqual(t, e.Node.Key, "/foo") - w, _ = s.Watch("/", true, false, 5) - eidx = 6 - testutil.AssertEqual(t, w.StartIndex(), eidx) - e = nbselect(w.EventChan()) - testutil.AssertEqual(t, e.EtcdIndex, eidx) - testutil.AssertEqual(t, e.Action, "expire") - testutil.AssertEqual(t, e.Node.Key, "/foofoo") - w, _ = s.Watch("/", true, false, 6) - e = nbselect(w.EventChan()) - testutil.AssertEqual(t, e.EtcdIndex, eidx) - testutil.AssertEqual(t, e.Action, "expire") - testutil.AssertEqual(t, e.Node.Key, "/foodir") - testutil.AssertEqual(t, e.Node.Dir, true) -} - -// Ensure that the store can watch for key expiration when refreshing. -func TestStoreWatchExpireRefresh(t *testing.T) { - s := newStore() - fc := newFakeClock() - s.clock = fc - - var eidx uint64 = 2 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) - s.Create("/foofoo", false, "barbarbar", false, TTLOptionSet{ExpireTime: fc.Now().Add(1200 * time.Millisecond), Refresh: true}) - - // Make sure we set watch updates when Refresh is true for newly created keys - w, _ := s.Watch("/", true, false, 0) - testutil.AssertEqual(t, w.StartIndex(), eidx) - c := w.EventChan() - e := nbselect(c) - testutil.AssertNil(t, e) - fc.Advance(600 * time.Millisecond) - s.DeleteExpiredKeys(fc.Now()) - eidx = 3 - e = nbselect(c) - testutil.AssertEqual(t, e.EtcdIndex, eidx) - testutil.AssertEqual(t, e.Action, "expire") - testutil.AssertEqual(t, e.Node.Key, "/foo") - - s.Update("/foofoo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) - w, _ = s.Watch("/", true, false, 4) - fc.Advance(700 * time.Millisecond) - s.DeleteExpiredKeys(fc.Now()) - eidx = 5 // We should skip 4 because a TTL update should occur with no watch notification if set `TTLOptionSet.Refresh` to true - testutil.AssertEqual(t, w.StartIndex(), eidx-1) - e = nbselect(w.EventChan()) - testutil.AssertEqual(t, e.EtcdIndex, eidx) - testutil.AssertEqual(t, e.Action, "expire") - testutil.AssertEqual(t, e.Node.Key, "/foofoo") -} - -// Ensure that the store can watch for key expiration when refreshing with an empty value. -func TestStoreWatchExpireEmptyRefresh(t *testing.T) { - s := newStore() - fc := newFakeClock() - s.clock = fc - - var eidx uint64 = 1 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) - // Should be no-op - fc.Advance(200 * time.Millisecond) - s.DeleteExpiredKeys(fc.Now()) - - s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) - w, _ := s.Watch("/", true, false, 2) - fc.Advance(700 * time.Millisecond) - s.DeleteExpiredKeys(fc.Now()) - eidx = 3 // We should skip 2 because a TTL update should occur with no watch notification if set `TTLOptionSet.Refresh` to true - testutil.AssertEqual(t, w.StartIndex(), eidx-1) - e := nbselect(w.EventChan()) - testutil.AssertEqual(t, e.EtcdIndex, eidx) - testutil.AssertEqual(t, e.Action, "expire") - testutil.AssertEqual(t, e.Node.Key, "/foo") - testutil.AssertEqual(t, *e.PrevNode.Value, "bar") -} - -// Update TTL of a key (set TTLOptionSet.Refresh to false) and send notification -func TestStoreWatchNoRefresh(t *testing.T) { - s := newStore() - fc := newFakeClock() - s.clock = fc - - var eidx uint64 = 1 - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) - // Should be no-op - fc.Advance(200 * time.Millisecond) - s.DeleteExpiredKeys(fc.Now()) - - // Update key's TTL with setting `TTLOptionSet.Refresh` to false will cause an update event - s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: false}) - w, _ := s.Watch("/", true, false, 2) - fc.Advance(700 * time.Millisecond) - s.DeleteExpiredKeys(fc.Now()) - eidx = 2 - testutil.AssertEqual(t, w.StartIndex(), eidx) - e := nbselect(w.EventChan()) - testutil.AssertEqual(t, e.EtcdIndex, eidx) - testutil.AssertEqual(t, e.Action, "update") - testutil.AssertEqual(t, e.Node.Key, "/foo") - testutil.AssertEqual(t, *e.PrevNode.Value, "bar") -} - -// Ensure that the store can update the TTL on a value with refresh. -func TestStoreRefresh(t *testing.T) { - s := newStore() - fc := newFakeClock() - s.clock = fc - - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)}) - s.Create("/bar", true, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)}) - _, err := s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) - testutil.AssertNil(t, err) - - _, err = s.Set("/foo", false, "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) - testutil.AssertNil(t, err) - - _, err = s.Update("/bar", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) - testutil.AssertNil(t, err) - - _, err = s.CompareAndSwap("/foo", "bar", 0, "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) - testutil.AssertNil(t, err) -} - // Ensure that the store can watch in streaming mode. func TestStoreWatchStream(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 1 w, _ := s.Watch("/foo", false, true, 0) // first modification - s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) e := nbselect(w.EventChan()) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "create") @@ -894,7 +687,7 @@ func TestStoreWatchStream(t *testing.T) { testutil.AssertNil(t, e) // second modification eidx = 2 - s.Update("/foo", "baz", TTLOptionSet{ExpireTime: Permanent}) + s.Update("/foo", "baz", store.TTLOptionSet{ExpireTime: store.Permanent}) e = nbselect(w.EventChan()) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "update") @@ -904,73 +697,13 @@ func TestStoreWatchStream(t *testing.T) { testutil.AssertNil(t, e) } -// Ensure that the store can recover from a previously saved state. -func TestStoreRecover(t *testing.T) { - s := newStore() - var eidx uint64 = 4 - s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/x", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) - s.Update("/foo/x", "barbar", TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/y", false, "baz", false, TTLOptionSet{ExpireTime: Permanent}) - b, err := s.Save() - testutil.AssertNil(t, err) - - s2 := newStore() - s2.Recovery(b) - - e, err := s.Get("/foo/x", false, false) - testutil.AssertEqual(t, e.Node.CreatedIndex, uint64(2)) - testutil.AssertEqual(t, e.Node.ModifiedIndex, uint64(3)) - testutil.AssertEqual(t, e.EtcdIndex, eidx) - testutil.AssertNil(t, err) - testutil.AssertEqual(t, *e.Node.Value, "barbar") - - e, err = s.Get("/foo/y", false, false) - testutil.AssertEqual(t, e.EtcdIndex, eidx) - testutil.AssertNil(t, err) - testutil.AssertEqual(t, *e.Node.Value, "baz") -} - -// Ensure that the store can recover from a previously saved state that includes an expiring key. -func TestStoreRecoverWithExpiration(t *testing.T) { - s := newStore() - s.clock = newFakeClock() - - fc := newFakeClock() - - var eidx uint64 = 4 - s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/x", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) - s.Create("/foo/y", false, "baz", false, TTLOptionSet{ExpireTime: fc.Now().Add(5 * time.Millisecond)}) - b, err := s.Save() - testutil.AssertNil(t, err) - - time.Sleep(10 * time.Millisecond) - - s2 := newStore() - s2.clock = fc - - s2.Recovery(b) - - fc.Advance(600 * time.Millisecond) - s.DeleteExpiredKeys(fc.Now()) - - e, err := s.Get("/foo/x", false, false) - testutil.AssertNil(t, err) - testutil.AssertEqual(t, e.EtcdIndex, eidx) - testutil.AssertEqual(t, *e.Node.Value, "bar") - - e, err = s.Get("/foo/y", false, false) - testutil.AssertNotNil(t, err) - testutil.AssertNil(t, e) -} - // Ensure that the store can watch for hidden keys as long as it's an exact path match. func TestStoreWatchCreateWithHiddenKey(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 1 w, _ := s.Watch("/_foo", false, false, 0) - s.Create("/_foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/_foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) e := nbselect(w.EventChan()) testutil.AssertEqual(t, e.EtcdIndex, eidx) testutil.AssertEqual(t, e.Action, "create") @@ -981,26 +714,28 @@ func TestStoreWatchCreateWithHiddenKey(t *testing.T) { // Ensure that the store doesn't see hidden key creates without an exact path match in recursive mode. func TestStoreWatchRecursiveCreateWithHiddenKey(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() w, _ := s.Watch("/foo", true, false, 0) - s.Create("/foo/_bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/_bar", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent}) e := nbselect(w.EventChan()) testutil.AssertNil(t, e) w, _ = s.Watch("/foo", true, false, 0) - s.Create("/foo/_baz", true, "", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/_baz", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}) e = nbselect(w.EventChan()) testutil.AssertNil(t, e) - s.Create("/foo/_baz/quux", false, "quux", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/_baz/quux", false, "quux", false, store.TTLOptionSet{ExpireTime: store.Permanent}) e = nbselect(w.EventChan()) testutil.AssertNil(t, e) } // Ensure that the store doesn't see hidden key updates. func TestStoreWatchUpdateWithHiddenKey(t *testing.T) { - s := newStore() - s.Create("/_foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s := newTestStore(t) + defer s.Close() + s.Create("/_foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) w, _ := s.Watch("/_foo", false, false, 0) - s.Update("/_foo", "baz", TTLOptionSet{ExpireTime: Permanent}) + s.Update("/_foo", "baz", store.TTLOptionSet{ExpireTime: store.Permanent}) e := nbselect(w.EventChan()) testutil.AssertEqual(t, e.Action, "update") testutil.AssertEqual(t, e.Node.Key, "/_foo") @@ -1010,19 +745,21 @@ func TestStoreWatchUpdateWithHiddenKey(t *testing.T) { // Ensure that the store doesn't see hidden key updates without an exact path match in recursive mode. func TestStoreWatchRecursiveUpdateWithHiddenKey(t *testing.T) { - s := newStore() - s.Create("/foo/_bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent}) + s := newTestStore(t) + defer s.Close() + s.Create("/foo/_bar", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent}) w, _ := s.Watch("/foo", true, false, 0) - s.Update("/foo/_bar", "baz", TTLOptionSet{ExpireTime: Permanent}) + s.Update("/foo/_bar", "baz", store.TTLOptionSet{ExpireTime: store.Permanent}) e := nbselect(w.EventChan()) testutil.AssertNil(t, e) } // Ensure that the store can watch for key deletions. func TestStoreWatchDeleteWithHiddenKey(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 2 - s.Create("/_foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/_foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) w, _ := s.Watch("/_foo", false, false, 0) s.Delete("/_foo", false, false) e := nbselect(w.EventChan()) @@ -1035,44 +772,22 @@ func TestStoreWatchDeleteWithHiddenKey(t *testing.T) { // Ensure that the store doesn't see hidden key deletes without an exact path match in recursive mode. func TestStoreWatchRecursiveDeleteWithHiddenKey(t *testing.T) { - s := newStore() - s.Create("/foo/_bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent}) + s := newTestStore(t) + defer s.Close() + s.Create("/foo/_bar", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent}) w, _ := s.Watch("/foo", true, false, 0) s.Delete("/foo/_bar", false, false) e := nbselect(w.EventChan()) testutil.AssertNil(t, e) } -// Ensure that the store doesn't see expirations of hidden keys. -func TestStoreWatchExpireWithHiddenKey(t *testing.T) { - s := newStore() - fc := newFakeClock() - s.clock = fc - - s.Create("/_foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)}) - s.Create("/foofoo", false, "barbarbar", false, TTLOptionSet{ExpireTime: fc.Now().Add(1000 * time.Millisecond)}) - - w, _ := s.Watch("/", true, false, 0) - c := w.EventChan() - e := nbselect(c) - testutil.AssertNil(t, e) - fc.Advance(600 * time.Millisecond) - s.DeleteExpiredKeys(fc.Now()) - e = nbselect(c) - testutil.AssertNil(t, e) - fc.Advance(600 * time.Millisecond) - s.DeleteExpiredKeys(fc.Now()) - e = nbselect(c) - testutil.AssertEqual(t, e.Action, "expire") - testutil.AssertEqual(t, e.Node.Key, "/foofoo") -} - // Ensure that the store does see hidden key creates if watching deeper than a hidden key in recursive mode. func TestStoreWatchRecursiveCreateDeeperThanHiddenKey(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() var eidx uint64 = 1 w, _ := s.Watch("/_foo/bar", true, false, 0) - s.Create("/_foo/bar/baz", false, "baz", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/_foo/bar/baz", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent}) e := nbselect(w.EventChan()) testutil.AssertNotNil(t, e) @@ -1089,21 +804,22 @@ func TestStoreWatchRecursiveCreateDeeperThanHiddenKey(t *testing.T) { // This test ensures that after closing the channel, the store can continue // to operate correctly. func TestStoreWatchSlowConsumer(t *testing.T) { - s := newStore() + s := newTestStore(t) + defer s.Close() s.Watch("/foo", true, true, 0) // stream must be true // Fill watch channel with 100 events for i := 1; i <= 100; i++ { - s.Set("/foo", false, string(i), TTLOptionSet{ExpireTime: Permanent}) // ok + s.Set("/foo", false, string(i), store.TTLOptionSet{ExpireTime: store.Permanent}) // ok } - testutil.AssertEqual(t, s.WatcherHub.count, int64(1)) - s.Set("/foo", false, "101", TTLOptionSet{ExpireTime: Permanent}) // ok + // testutil.AssertEqual(t, s.WatcherHub.count, int64(1)) + s.Set("/foo", false, "101", store.TTLOptionSet{ExpireTime: store.Permanent}) // ok // remove watcher - testutil.AssertEqual(t, s.WatcherHub.count, int64(0)) - s.Set("/foo", false, "102", TTLOptionSet{ExpireTime: Permanent}) // must not panic + // testutil.AssertEqual(t, s.WatcherHub.count, int64(0)) + s.Set("/foo", false, "102", store.TTLOptionSet{ExpireTime: store.Permanent}) // must not panic } // Performs a non-blocking select on an event channel. -func nbselect(c <-chan *Event) *Event { +func nbselect(c <-chan *store.Event) *store.Event { select { case e := <-c: return e @@ -1111,12 +827,3 @@ func nbselect(c <-chan *Event) *Event { return nil } } - -// newFakeClock creates a new FakeClock that has been advanced to at least minExpireTime -func newFakeClock() clockwork.FakeClock { - fc := clockwork.NewFakeClock() - for minExpireTime.After(fc.Now()) { - fc.Advance((0x1 << 62) * time.Nanosecond) - } - return fc -} diff --git a/store/store_ttl_test.go b/store/store_ttl_test.go new file mode 100644 index 00000000000..62c6a95ee7a --- /dev/null +++ b/store/store_ttl_test.go @@ -0,0 +1,360 @@ +// Copyright 2017 The etcd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package store + +import ( + "testing" + "time" + + etcdErr "github.com/coreos/etcd/error" + "github.com/coreos/etcd/pkg/testutil" + "github.com/jonboulle/clockwork" +) + +// Ensure that any TTL <= minExpireTime becomes Permanent +func TestMinExpireTime(t *testing.T) { + s := newStore() + fc := clockwork.NewFakeClock() + s.clock = fc + // FakeClock starts at 0, so minExpireTime should be far in the future.. but just in case + testutil.AssertTrue(t, minExpireTime.After(fc.Now()), "minExpireTime should be ahead of FakeClock!") + s.Create("/foo", false, "Y", false, TTLOptionSet{ExpireTime: fc.Now().Add(3 * time.Second)}) + fc.Advance(5 * time.Second) + // Ensure it hasn't expired + s.DeleteExpiredKeys(fc.Now()) + var eidx uint64 = 1 + e, err := s.Get("/foo", true, false) + testutil.AssertNil(t, err) + testutil.AssertEqual(t, e.EtcdIndex, eidx) + testutil.AssertEqual(t, e.Action, "get") + testutil.AssertEqual(t, e.Node.Key, "/foo") + testutil.AssertEqual(t, e.Node.TTL, int64(0)) +} + +// Ensure that the store can recursively retrieve a directory listing. +// Note that hidden files should not be returned. +func TestStoreGetDirectory(t *testing.T) { + s := newStore() + fc := newFakeClock() + s.clock = fc + s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/bar", false, "X", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/_hidden", false, "*", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/baz", true, "", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/baz/bat", false, "Y", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/baz/_hidden", false, "*", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/baz/ttl", false, "Y", false, TTLOptionSet{ExpireTime: fc.Now().Add(time.Second * 3)}) + var eidx uint64 = 7 + e, err := s.Get("/foo", true, false) + testutil.AssertNil(t, err) + testutil.AssertEqual(t, e.EtcdIndex, eidx) + testutil.AssertEqual(t, e.Action, "get") + testutil.AssertEqual(t, e.Node.Key, "/foo") + testutil.AssertEqual(t, len(e.Node.Nodes), 2) + var bazNodes NodeExterns + for _, node := range e.Node.Nodes { + switch node.Key { + case "/foo/bar": + testutil.AssertEqual(t, *node.Value, "X") + testutil.AssertEqual(t, node.Dir, false) + case "/foo/baz": + testutil.AssertEqual(t, node.Dir, true) + testutil.AssertEqual(t, len(node.Nodes), 2) + bazNodes = node.Nodes + default: + t.Errorf("key = %s, not matched", node.Key) + } + } + for _, node := range bazNodes { + switch node.Key { + case "/foo/baz/bat": + testutil.AssertEqual(t, *node.Value, "Y") + testutil.AssertEqual(t, node.Dir, false) + case "/foo/baz/ttl": + testutil.AssertEqual(t, *node.Value, "Y") + testutil.AssertEqual(t, node.Dir, false) + testutil.AssertEqual(t, node.TTL, int64(3)) + default: + t.Errorf("key = %s, not matched", node.Key) + } + } +} + +// Ensure that the store can update the TTL on a value. +func TestStoreUpdateValueTTL(t *testing.T) { + s := newStore() + fc := newFakeClock() + s.clock = fc + + var eidx uint64 = 2 + s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + _, err := s.Update("/foo", "baz", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)}) + testutil.AssertNil(t, err) + e, _ := s.Get("/foo", false, false) + testutil.AssertEqual(t, *e.Node.Value, "baz") + testutil.AssertEqual(t, e.EtcdIndex, eidx) + fc.Advance(600 * time.Millisecond) + s.DeleteExpiredKeys(fc.Now()) + e, err = s.Get("/foo", false, false) + testutil.AssertNil(t, e) + testutil.AssertEqual(t, err.(*etcdErr.Error).ErrorCode, etcdErr.EcodeKeyNotFound) +} + +// Ensure that the store can update the TTL on a directory. +func TestStoreUpdateDirTTL(t *testing.T) { + s := newStore() + fc := newFakeClock() + s.clock = fc + + var eidx uint64 = 3 + s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent}) + e, err := s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)}) + testutil.AssertNil(t, err) + testutil.AssertEqual(t, e.Node.Dir, true) + testutil.AssertEqual(t, e.EtcdIndex, eidx) + e, _ = s.Get("/foo/bar", false, false) + testutil.AssertEqual(t, *e.Node.Value, "baz") + testutil.AssertEqual(t, e.EtcdIndex, eidx) + + fc.Advance(600 * time.Millisecond) + s.DeleteExpiredKeys(fc.Now()) + e, err = s.Get("/foo/bar", false, false) + testutil.AssertNil(t, e) + testutil.AssertEqual(t, err.(*etcdErr.Error).ErrorCode, etcdErr.EcodeKeyNotFound) +} + +// Ensure that the store can watch for key expiration. +func TestStoreWatchExpire(t *testing.T) { + s := newStore() + fc := newFakeClock() + s.clock = fc + + var eidx uint64 = 3 + s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(400 * time.Millisecond)}) + s.Create("/foofoo", false, "barbarbar", false, TTLOptionSet{ExpireTime: fc.Now().Add(450 * time.Millisecond)}) + s.Create("/foodir", true, "", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)}) + + w, _ := s.Watch("/", true, false, 0) + testutil.AssertEqual(t, w.StartIndex(), eidx) + c := w.EventChan() + e := nbselect(c) + testutil.AssertNil(t, e) + fc.Advance(600 * time.Millisecond) + s.DeleteExpiredKeys(fc.Now()) + eidx = 4 + e = nbselect(c) + testutil.AssertEqual(t, e.EtcdIndex, eidx) + testutil.AssertEqual(t, e.Action, "expire") + testutil.AssertEqual(t, e.Node.Key, "/foo") + w, _ = s.Watch("/", true, false, 5) + eidx = 6 + testutil.AssertEqual(t, w.StartIndex(), eidx) + e = nbselect(w.EventChan()) + testutil.AssertEqual(t, e.EtcdIndex, eidx) + testutil.AssertEqual(t, e.Action, "expire") + testutil.AssertEqual(t, e.Node.Key, "/foofoo") + w, _ = s.Watch("/", true, false, 6) + e = nbselect(w.EventChan()) + testutil.AssertEqual(t, e.EtcdIndex, eidx) + testutil.AssertEqual(t, e.Action, "expire") + testutil.AssertEqual(t, e.Node.Key, "/foodir") + testutil.AssertEqual(t, e.Node.Dir, true) +} + +// Ensure that the store can watch for key expiration when refreshing. +func TestStoreWatchExpireRefresh(t *testing.T) { + s := newStore() + fc := newFakeClock() + s.clock = fc + + var eidx uint64 = 2 + s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) + s.Create("/foofoo", false, "barbarbar", false, TTLOptionSet{ExpireTime: fc.Now().Add(1200 * time.Millisecond), Refresh: true}) + + // Make sure we set watch updates when Refresh is true for newly created keys + w, _ := s.Watch("/", true, false, 0) + testutil.AssertEqual(t, w.StartIndex(), eidx) + c := w.EventChan() + e := nbselect(c) + testutil.AssertNil(t, e) + fc.Advance(600 * time.Millisecond) + s.DeleteExpiredKeys(fc.Now()) + eidx = 3 + e = nbselect(c) + testutil.AssertEqual(t, e.EtcdIndex, eidx) + testutil.AssertEqual(t, e.Action, "expire") + testutil.AssertEqual(t, e.Node.Key, "/foo") + + s.Update("/foofoo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) + w, _ = s.Watch("/", true, false, 4) + fc.Advance(700 * time.Millisecond) + s.DeleteExpiredKeys(fc.Now()) + eidx = 5 // We should skip 4 because a TTL update should occur with no watch notification if set `TTLOptionSet.Refresh` to true + testutil.AssertEqual(t, w.StartIndex(), eidx-1) + e = nbselect(w.EventChan()) + testutil.AssertEqual(t, e.EtcdIndex, eidx) + testutil.AssertEqual(t, e.Action, "expire") + testutil.AssertEqual(t, e.Node.Key, "/foofoo") +} + +// Ensure that the store can watch for key expiration when refreshing with an empty value. +func TestStoreWatchExpireEmptyRefresh(t *testing.T) { + s := newStore() + fc := newFakeClock() + s.clock = fc + + var eidx uint64 = 1 + s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) + // Should be no-op + fc.Advance(200 * time.Millisecond) + s.DeleteExpiredKeys(fc.Now()) + + s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) + w, _ := s.Watch("/", true, false, 2) + fc.Advance(700 * time.Millisecond) + s.DeleteExpiredKeys(fc.Now()) + eidx = 3 // We should skip 2 because a TTL update should occur with no watch notification if set `TTLOptionSet.Refresh` to true + testutil.AssertEqual(t, w.StartIndex(), eidx-1) + e := nbselect(w.EventChan()) + testutil.AssertEqual(t, e.EtcdIndex, eidx) + testutil.AssertEqual(t, e.Action, "expire") + testutil.AssertEqual(t, e.Node.Key, "/foo") + testutil.AssertEqual(t, *e.PrevNode.Value, "bar") +} + +// Update TTL of a key (set TTLOptionSet.Refresh to false) and send notification +func TestStoreWatchNoRefresh(t *testing.T) { + s := newStore() + fc := newFakeClock() + s.clock = fc + + var eidx uint64 = 1 + s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) + // Should be no-op + fc.Advance(200 * time.Millisecond) + s.DeleteExpiredKeys(fc.Now()) + + // Update key's TTL with setting `TTLOptionSet.Refresh` to false will cause an update event + s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: false}) + w, _ := s.Watch("/", true, false, 2) + fc.Advance(700 * time.Millisecond) + s.DeleteExpiredKeys(fc.Now()) + eidx = 2 + testutil.AssertEqual(t, w.StartIndex(), eidx) + e := nbselect(w.EventChan()) + testutil.AssertEqual(t, e.EtcdIndex, eidx) + testutil.AssertEqual(t, e.Action, "update") + testutil.AssertEqual(t, e.Node.Key, "/foo") + testutil.AssertEqual(t, *e.PrevNode.Value, "bar") +} + +// Ensure that the store can update the TTL on a value with refresh. +func TestStoreRefresh(t *testing.T) { + s := newStore() + fc := newFakeClock() + s.clock = fc + + s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)}) + s.Create("/bar", true, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)}) + _, err := s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) + testutil.AssertNil(t, err) + + _, err = s.Set("/foo", false, "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) + testutil.AssertNil(t, err) + + _, err = s.Update("/bar", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) + testutil.AssertNil(t, err) + + _, err = s.CompareAndSwap("/foo", "bar", 0, "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true}) + testutil.AssertNil(t, err) +} + +// Ensure that the store can recover from a previously saved state that includes an expiring key. +func TestStoreRecoverWithExpiration(t *testing.T) { + s := newStore() + s.clock = newFakeClock() + + fc := newFakeClock() + + var eidx uint64 = 4 + s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/x", false, "bar", false, TTLOptionSet{ExpireTime: Permanent}) + s.Create("/foo/y", false, "baz", false, TTLOptionSet{ExpireTime: fc.Now().Add(5 * time.Millisecond)}) + b, err := s.Save() + testutil.AssertNil(t, err) + + time.Sleep(10 * time.Millisecond) + + s2 := newStore() + s2.clock = fc + + s2.Recovery(b) + + fc.Advance(600 * time.Millisecond) + s.DeleteExpiredKeys(fc.Now()) + + e, err := s.Get("/foo/x", false, false) + testutil.AssertNil(t, err) + testutil.AssertEqual(t, e.EtcdIndex, eidx) + testutil.AssertEqual(t, *e.Node.Value, "bar") + + e, err = s.Get("/foo/y", false, false) + testutil.AssertNotNil(t, err) + testutil.AssertNil(t, e) +} + +// Ensure that the store doesn't see expirations of hidden keys. +func TestStoreWatchExpireWithHiddenKey(t *testing.T) { + s := newStore() + fc := newFakeClock() + s.clock = fc + + s.Create("/_foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)}) + s.Create("/foofoo", false, "barbarbar", false, TTLOptionSet{ExpireTime: fc.Now().Add(1000 * time.Millisecond)}) + + w, _ := s.Watch("/", true, false, 0) + c := w.EventChan() + e := nbselect(c) + testutil.AssertNil(t, e) + fc.Advance(600 * time.Millisecond) + s.DeleteExpiredKeys(fc.Now()) + e = nbselect(c) + testutil.AssertNil(t, e) + fc.Advance(600 * time.Millisecond) + s.DeleteExpiredKeys(fc.Now()) + e = nbselect(c) + testutil.AssertEqual(t, e.Action, "expire") + testutil.AssertEqual(t, e.Node.Key, "/foofoo") +} + +// newFakeClock creates a new FakeClock that has been advanced to at least minExpireTime +func newFakeClock() clockwork.FakeClock { + fc := clockwork.NewFakeClock() + for minExpireTime.After(fc.Now()) { + fc.Advance((0x1 << 62) * time.Nanosecond) + } + return fc +} + +// Performs a non-blocking select on an event channel. +func nbselect(c <-chan *Event) *Event { + select { + case e := <-c: + return e + default: + return nil + } +} diff --git a/store/store_v2_test.go b/store/store_v2_test.go new file mode 100644 index 00000000000..29569817731 --- /dev/null +++ b/store/store_v2_test.go @@ -0,0 +1,62 @@ +// Copyright 2017 The etcd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !v2v3 + +package store_test + +import ( + "testing" + + "github.com/coreos/etcd/pkg/testutil" + "github.com/coreos/etcd/store" +) + +type v2TestStore struct { + store.Store +} + +func (s *v2TestStore) Close() {} + +func newTestStore(t *testing.T, ns ...string) StoreCloser { + return &v2TestStore{store.New(ns...)} +} + +// Ensure that the store can recover from a previously saved state. +func TestStoreRecover(t *testing.T) { + s := newTestStore(t) + defer s.Close() + var eidx uint64 = 4 + s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}) + s.Create("/foo/x", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent}) + s.Update("/foo/x", "barbar", store.TTLOptionSet{ExpireTime: store.Permanent}) + s.Create("/foo/y", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent}) + b, err := s.Save() + testutil.AssertNil(t, err) + + s2 := newTestStore(t) + s2.Recovery(b) + + e, err := s.Get("/foo/x", false, false) + testutil.AssertEqual(t, e.Node.CreatedIndex, uint64(2)) + testutil.AssertEqual(t, e.Node.ModifiedIndex, uint64(3)) + testutil.AssertEqual(t, e.EtcdIndex, eidx) + testutil.AssertNil(t, err) + testutil.AssertEqual(t, *e.Node.Value, "barbar") + + e, err = s.Get("/foo/y", false, false) + testutil.AssertEqual(t, e.EtcdIndex, eidx) + testutil.AssertNil(t, err) + testutil.AssertEqual(t, *e.Node.Value, "baz") +}