作者:algoad
项目:etc
// 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, "")
}
作者:algoad
项目:etc
// 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", "")
}
作者:algoad
项目:etc
// 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, Permanent)
e, _err := s.Delete("/foo", false, false)
err := _err.(*etcdErr.Error)
assert.Equal(t, err.ErrorCode, etcdErr.EcodeNotFile, "")
assert.Equal(t, err.Message, "Not a file", "")
assert.Nil(t, e, "")
}
作者:algoad
项目:etc
// Ensure that the store cannot update a directory.
func TestStoreUpdateFailsIfDirectory(t *testing.T) {
s := newStore()
s.Create("/foo", true, "", false, Permanent)
e, _err := s.Update("/foo", "baz", Permanent)
err := _err.(*etcdErr.Error)
assert.Equal(t, err.ErrorCode, etcdErr.EcodeNotFile, "")
assert.Equal(t, err.Message, "Not a file", "")
assert.Equal(t, err.Cause, "/foo", "")
assert.Nil(t, e, "")
}
作者:algoad
项目:etc
// Ensure that the store can create a new directory if it doesn't already exist.
func TestStoreCreateDirectory(t *testing.T) {
s := newStore()
var eidx uint64 = 1
e, err := s.Create("/foo", true, "", false, Permanent)
assert.Nil(t, err, "")
assert.Equal(t, e.EtcdIndex, eidx, "")
assert.Equal(t, e.Action, "create", "")
assert.Equal(t, e.Node.Key, "/foo", "")
assert.True(t, e.Node.Dir, "")
}
作者:algoad
项目:etc
// Ensure that the store doesn't see hidden key updates.
func TestStoreWatchUpdateWithHiddenKey(t *testing.T) {
s := newStore()
s.Create("/_foo", false, "bar", false, Permanent)
w, _ := s.Watch("/_foo", false, false, 0)
s.Update("/_foo", "baz", Permanent)
e := nbselect(w.EventChan())
assert.Equal(t, e.Action, "update", "")
assert.Equal(t, e.Node.Key, "/_foo", "")
e = nbselect(w.EventChan())
assert.Nil(t, e, "")
}
作者:algoad
项目:etc
// Ensure that the store can retrieve an existing value.
func TestStoreGetValue(t *testing.T) {
s := newStore()
s.Create("/foo", false, "bar", false, Permanent)
var eidx uint64 = 1
e, err := s.Get("/foo", false, false)
assert.Nil(t, err, "")
assert.Equal(t, e.EtcdIndex, eidx, "")
assert.Equal(t, e.Action, "get", "")
assert.Equal(t, e.Node.Key, "/foo", "")
assert.Equal(t, *e.Node.Value, "bar", "")
}
作者:algoad
项目:etc
//Ensure that the number of expirations is recorded in the stats.
func TestStoreStatsExpireCount(t *testing.T) {
s := newStore()
fc := newFakeClock()
s.clock = fc
s.Create("/foo", false, "bar", false, fc.Now().Add(500*time.Millisecond))
assert.Equal(t, uint64(0), s.Stats.ExpireCount, "")
fc.Advance(600 * time.Millisecond)
s.DeleteExpiredKeys(fc.Now())
assert.Equal(t, uint64(1), s.Stats.ExpireCount, "")
}
作者:algoad
项目:etc
// 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", "")
}
作者:algoad
项目:etc
// 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()
var eidx uint64 = 1
w, _ := s.Watch("/_foo", false, false, 0)
s.Create("/_foo", false, "bar", false, Permanent)
e := nbselect(w.EventChan())
assert.Equal(t, e.EtcdIndex, eidx, "")
assert.Equal(t, e.Action, "create", "")
assert.Equal(t, e.Node.Key, "/_foo", "")
e = nbselect(w.EventChan())
assert.Nil(t, e, "")
}
作者:algoad
项目:etc
// Ensure that the store can watch for recursive key creation.
func TestStoreWatchRecursiveCreate(t *testing.T) {
s := newStore()
var eidx uint64 = 0
w, _ := s.Watch("/foo", true, false, 0)
assert.Equal(t, w.StartIndex(), eidx, "")
eidx = 1
s.Create("/foo/bar", false, "baz", false, Permanent)
e := nbselect(w.EventChan())
assert.Equal(t, e.EtcdIndex, eidx, "")
assert.Equal(t, e.Action, "create", "")
assert.Equal(t, e.Node.Key, "/foo/bar", "")
}
作者:algoad
项目:etc
// Ensure that the store cannot conditionally update a key if it has the wrong previous index.
func TestStoreCompareAndSwapPrevIndexFailsIfNotMatch(t *testing.T) {
s := newStore()
var eidx uint64 = 1
s.Create("/foo", false, "bar", false, Permanent)
e, _err := s.CompareAndSwap("/foo", "", 100, "baz", Permanent)
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", "")
}
作者:algoad
项目:etc
// 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", "")
}
作者:algoad
项目:etc
// Ensure that the store can watch for CAS updates.
func TestStoreWatchCompareAndSwap(t *testing.T) {
s := newStore()
var eidx uint64 = 1
s.Create("/foo", false, "bar", false, Permanent)
w, _ := s.Watch("/foo", false, false, 0)
assert.Equal(t, w.StartIndex(), eidx, "")
eidx = 2
s.CompareAndSwap("/foo", "bar", 0, "baz", Permanent)
e := nbselect(w.EventChan())
assert.Equal(t, e.EtcdIndex, eidx, "")
assert.Equal(t, e.Action, "compareAndSwap", "")
assert.Equal(t, e.Node.Key, "/foo", "")
}
作者:algoad
项目:etc
// Ensure that the store can watch for key expiration.
func TestStoreWatchExpire(t *testing.T) {
s := newStore()
fc := newFakeClock()
s.clock = fc
var eidx uint64 = 2
s.Create("/foo", false, "bar", false, fc.Now().Add(500*time.Millisecond))
s.Create("/foofoo", false, "barbarbar", false, fc.Now().Add(500*time.Millisecond))
w, _ := s.Watch("/", true, false, 0)
assert.Equal(t, w.StartIndex(), eidx, "")
c := w.EventChan()
e := nbselect(c)
assert.Nil(t, e, "")
fc.Advance(600 * time.Millisecond)
s.DeleteExpiredKeys(fc.Now())
eidx = 3
e = nbselect(c)
assert.Equal(t, e.EtcdIndex, eidx, "")
assert.Equal(t, e.Action, "expire", "")
assert.Equal(t, e.Node.Key, "/foo", "")
w, _ = s.Watch("/", true, false, 4)
eidx = 4
assert.Equal(t, w.StartIndex(), eidx, "")
e = nbselect(w.EventChan())
assert.Equal(t, e.EtcdIndex, eidx, "")
assert.Equal(t, e.Action, "expire", "")
assert.Equal(t, e.Node.Key, "/foofoo", "")
}
作者:algoad
项目:etc
// Ensure that the store fails to create a key if it already exists.
func TestStoreCreateFailsIfExists(t *testing.T) {
s := newStore()
// create /foo as dir
s.Create("/foo", true, "", false, Permanent)
// create /foo as dir again
e, _err := s.Create("/foo", true, "", false, Permanent)
err := _err.(*etcdErr.Error)
assert.Equal(t, err.ErrorCode, etcdErr.EcodeNodeExist, "")
assert.Equal(t, err.Message, "Key already exists", "")
assert.Equal(t, err.Cause, "/foo", "")
assert.Equal(t, err.Index, uint64(1), "")
assert.Nil(t, e, 0, "")
}
作者:algoad
项目:etc
// Ensure that the store can watch for key creation.
func TestStoreWatchCreate(t *testing.T) {
s := newStore()
var eidx uint64 = 0
w, _ := s.Watch("/foo", false, false, 0)
c := w.EventChan()
assert.Equal(t, w.StartIndex(), eidx, "")
s.Create("/foo", false, "bar", false, Permanent)
eidx = 1
e := nbselect(c)
assert.Equal(t, e.EtcdIndex, eidx, "")
assert.Equal(t, e.Action, "create", "")
assert.Equal(t, e.Node.Key, "/foo", "")
e = nbselect(c)
assert.Nil(t, e, "")
}
作者:algoad
项目:etc
// Ensure that the store can retrieve a directory in sorted order.
func TestStoreGetSorted(t *testing.T) {
s := newStore()
s.Create("/foo", true, "", false, Permanent)
s.Create("/foo/x", false, "0", false, Permanent)
s.Create("/foo/z", false, "0", false, Permanent)
s.Create("/foo/y", true, "", false, Permanent)
s.Create("/foo/y/a", false, "0", false, Permanent)
s.Create("/foo/y/b", false, "0", false, Permanent)
var eidx uint64 = 6
e, err := s.Get("/foo", true, true)
assert.Nil(t, err, "")
assert.Equal(t, e.EtcdIndex, eidx, "")
var yNodes NodeExterns
for _, node := range e.Node.Nodes {
switch node.Key {
case "/foo/x":
case "/foo/y":
yNodes = node.Nodes
case "/foo/z":
default:
t.Errorf("key = %s, not matched", node.Key)
}
}
for _, node := range yNodes {
switch node.Key {
case "/foo/y/a":
case "/foo/y/b":
default:
t.Errorf("key = %s, not matched", node.Key)
}
}
}
作者:algoad
项目:etc
// 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, "")
}
作者:algoad
项目:etc
// 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, Permanent)
_, err := s.Update("/foo", "baz", fc.Now().Add(500*time.Millisecond))
e, _ := s.Get("/foo", false, false)
assert.Equal(t, *e.Node.Value, "baz", "")
assert.Equal(t, e.EtcdIndex, eidx, "")
fc.Advance(600 * time.Millisecond)
s.DeleteExpiredKeys(fc.Now())
e, err = s.Get("/foo", false, false)
assert.Nil(t, e, "")
assert.Equal(t, err.(*etcdErr.Error).ErrorCode, etcdErr.EcodeKeyNotFound, "")
}