store_v2_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // +build !v2v3
  15. package v2store_test
  16. import (
  17. "testing"
  18. "go.etcd.io/etcd/etcdserver/api/v2store"
  19. "go.etcd.io/etcd/pkg/testutil"
  20. )
  21. type v2TestStore struct {
  22. v2store.Store
  23. }
  24. func (s *v2TestStore) Close() {}
  25. func newTestStore(t *testing.T, ns ...string) StoreCloser {
  26. if len(ns) == 0 {
  27. t.Logf("new v2 store with no namespace")
  28. }
  29. return &v2TestStore{v2store.New(ns...)}
  30. }
  31. // Ensure that the store can recover from a previously saved state.
  32. func TestStoreRecover(t *testing.T) {
  33. s := newTestStore(t)
  34. defer s.Close()
  35. var eidx uint64 = 4
  36. s.Create("/foo", true, "", false, v2store.TTLOptionSet{ExpireTime: v2store.Permanent})
  37. s.Create("/foo/x", false, "bar", false, v2store.TTLOptionSet{ExpireTime: v2store.Permanent})
  38. s.Update("/foo/x", "barbar", v2store.TTLOptionSet{ExpireTime: v2store.Permanent})
  39. s.Create("/foo/y", false, "baz", false, v2store.TTLOptionSet{ExpireTime: v2store.Permanent})
  40. b, err := s.Save()
  41. testutil.AssertNil(t, err)
  42. s2 := newTestStore(t)
  43. s2.Recovery(b)
  44. e, err := s.Get("/foo/x", false, false)
  45. testutil.AssertEqual(t, e.Node.CreatedIndex, uint64(2))
  46. testutil.AssertEqual(t, e.Node.ModifiedIndex, uint64(3))
  47. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  48. testutil.AssertNil(t, err)
  49. testutil.AssertEqual(t, *e.Node.Value, "barbar")
  50. e, err = s.Get("/foo/y", false, false)
  51. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  52. testutil.AssertNil(t, err)
  53. testutil.AssertEqual(t, *e.Node.Value, "baz")
  54. }