// Ensure that the store can delete a directory if recursive is specified. func TestStoreDeleteDiretory(t *testing.T) { s := newStore() // create directory /foo var eidx uint64 = 2 s.Create("/foo", true, "", false, Permanent) // delete /foo with dir = true and recursive = false // this should succeed, since the directory is empty e, err := s.Delete("/foo", true, false) assert.Nil(t, err, "") assert.Equal(t, e.EtcdIndex, eidx, "") assert.Equal(t, e.Action, "delete", "") // check prevNode assert.NotNil(t, e.PrevNode, "") assert.Equal(t, e.PrevNode.Key, "/foo", "") assert.Equal(t, e.PrevNode.Dir, true, "") // create directory /foo and directory /foo/bar s.Create("/foo/bar", true, "", false, Permanent) // delete /foo with dir = true and recursive = false // this should fail, since the directory is not empty _, err = s.Delete("/foo", true, false) assert.NotNil(t, err, "") // delete /foo with dir=false and recursive = true // this should succeed, since recursive implies dir=true // and recursively delete should be able to delete all // items under the given directory e, err = s.Delete("/foo", false, true) assert.Nil(t, err, "") assert.Equal(t, e.Action, "delete", "") }
// 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, Permanent) s.Create("/foo/x", false, "bar", false, Permanent) s.Create("/foo/y", false, "baz", false, fc.Now().Add(5*time.Millisecond)) b, err := s.Save() 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) assert.Nil(t, err, "") assert.Equal(t, e.EtcdIndex, eidx, "") assert.Equal(t, *e.Node.Value, "bar", "") e, err = s.Get("/foo/y", false, false) assert.NotNil(t, err, "") assert.Nil(t, e, "") }
// Ensure that the store cannot delete a directory. func TestStoreCompareAndDeleteDiretoryFail(t *testing.T) { s := newStore() s.Create("/foo", true, "", false, Permanent) _, _err := s.CompareAndDelete("/foo", "", 0) assert.NotNil(t, _err, "") err := _err.(*etcdErr.Error) assert.Equal(t, err.ErrorCode, etcdErr.EcodeNotFile, "") }
func TestRootRdOnly(t *testing.T) { s := newStore("/0") for _, tt := range []string{"/", "/0"} { _, err := s.Set(tt, true, "", Permanent) assert.NotNil(t, err, "") _, err = s.Delete(tt, true, true) assert.NotNil(t, err, "") _, err = s.Create(tt, true, "", false, Permanent) assert.NotNil(t, err, "") _, err = s.Update(tt, "", Permanent) assert.NotNil(t, err, "") _, err = s.CompareAndSwap(tt, "", 0, "", Permanent) assert.NotNil(t, err, "") } }
// 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() var eidx uint64 = 1 w, _ := s.Watch("/_foo/bar", true, false, 0) s.Create("/_foo/bar/baz", false, "baz", false, Permanent) e := nbselect(w.EventChan()) assert.NotNil(t, e, "") assert.Equal(t, e.EtcdIndex, eidx, "") assert.Equal(t, e.Action, "create", "") assert.Equal(t, e.Node.Key, "/_foo/bar/baz", "") }
// Ensure that the store can delete a value. func TestStoreDeleteValue(t *testing.T) { s := newStore() var eidx uint64 = 2 s.Create("/foo", false, "bar", false, Permanent) e, err := s.Delete("/foo", false, false) assert.Nil(t, err, "") assert.Equal(t, e.EtcdIndex, eidx, "") assert.Equal(t, e.Action, "delete", "") // check prevNode assert.NotNil(t, e.PrevNode, "") assert.Equal(t, e.PrevNode.Key, "/foo", "") assert.Equal(t, *e.PrevNode.Value, "bar", "") }
func TestStoreCompareAndDeletePrevIndexFailsIfNotMatch(t *testing.T) { s := newStore() var eidx uint64 = 1 s.Create("/foo", false, "bar", false, Permanent) e, _err := s.CompareAndDelete("/foo", "", 100) assert.NotNil(t, _err, "") err := _err.(*etcdErr.Error) assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "") assert.Equal(t, err.Message, "Compare failed", "") assert.Nil(t, e, "") e, _ = s.Get("/foo", false, false) assert.Equal(t, e.EtcdIndex, eidx, "") assert.Equal(t, *e.Node.Value, "bar", "") }
func TestStoreCompareAndDeletePrevIndex(t *testing.T) { s := newStore() var eidx uint64 = 2 s.Create("/foo", false, "bar", false, Permanent) e, err := s.CompareAndDelete("/foo", "", 1) assert.Nil(t, err, "") assert.Equal(t, e.EtcdIndex, eidx, "") assert.Equal(t, e.Action, "compareAndDelete", "") // check pervNode assert.NotNil(t, e.PrevNode, "") assert.Equal(t, e.PrevNode.Key, "/foo", "") assert.Equal(t, *e.PrevNode.Value, "bar", "") assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "") assert.Equal(t, e.PrevNode.CreatedIndex, uint64(1), "") }
func TestSet(t *testing.T) { s := newStore() // Set /foo="" var eidx uint64 = 1 e, err := s.Set("/foo", false, "", Permanent) assert.Nil(t, err, "") assert.Equal(t, e.EtcdIndex, eidx, "") assert.Equal(t, e.Action, "set", "") assert.Equal(t, e.Node.Key, "/foo", "") assert.False(t, e.Node.Dir, "") assert.Equal(t, *e.Node.Value, "", "") assert.Nil(t, e.Node.Nodes, "") assert.Nil(t, e.Node.Expiration, "") assert.Equal(t, e.Node.TTL, 0, "") assert.Equal(t, e.Node.ModifiedIndex, uint64(1), "") // Set /foo="bar" eidx = 2 e, err = s.Set("/foo", false, "bar", Permanent) assert.Nil(t, err, "") assert.Equal(t, e.EtcdIndex, eidx, "") assert.Equal(t, e.Action, "set", "") assert.Equal(t, e.Node.Key, "/foo", "") assert.False(t, e.Node.Dir, "") assert.Equal(t, *e.Node.Value, "bar", "") assert.Nil(t, e.Node.Nodes, "") assert.Nil(t, e.Node.Expiration, "") assert.Equal(t, e.Node.TTL, 0, "") assert.Equal(t, e.Node.ModifiedIndex, uint64(2), "") // check prevNode assert.NotNil(t, e.PrevNode, "") assert.Equal(t, e.PrevNode.Key, "/foo", "") assert.Equal(t, *e.PrevNode.Value, "", "") assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "") // Set /foo="baz" (for testing prevNode) eidx = 3 e, err = s.Set("/foo", false, "baz", Permanent) assert.Nil(t, err, "") assert.Equal(t, e.EtcdIndex, eidx, "") assert.Equal(t, e.Action, "set", "") assert.Equal(t, e.Node.Key, "/foo", "") assert.False(t, e.Node.Dir, "") assert.Equal(t, *e.Node.Value, "baz", "") assert.Nil(t, e.Node.Nodes, "") assert.Nil(t, e.Node.Expiration, "") assert.Equal(t, e.Node.TTL, 0, "") assert.Equal(t, e.Node.ModifiedIndex, uint64(3), "") // check prevNode assert.NotNil(t, e.PrevNode, "") assert.Equal(t, e.PrevNode.Key, "/foo", "") assert.Equal(t, *e.PrevNode.Value, "bar", "") assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(2), "") // Set /dir as a directory eidx = 4 e, err = s.Set("/dir", true, "", Permanent) assert.Nil(t, err, "") assert.Equal(t, e.EtcdIndex, eidx, "") assert.Equal(t, e.Action, "set", "") assert.Equal(t, e.Node.Key, "/dir", "") assert.True(t, e.Node.Dir, "") assert.Nil(t, e.Node.Value) assert.Nil(t, e.Node.Nodes, "") assert.Nil(t, e.Node.Expiration, "") assert.Equal(t, e.Node.TTL, 0, "") assert.Equal(t, e.Node.ModifiedIndex, uint64(4), "") }