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
15// +build !v2v3
16
17package v2store_test
18
19import (
20	"testing"
21
22	"go.etcd.io/etcd/etcdserver/api/v2store"
23	"go.etcd.io/etcd/pkg/testutil"
24)
25
26type v2TestStore struct {
27	v2store.Store
28}
29
30func (s *v2TestStore) Close() {}
31
32func newTestStore(t *testing.T, ns ...string) StoreCloser {
33	if len(ns) == 0 {
34		t.Logf("new v2 store with no namespace")
35	}
36	return &v2TestStore{v2store.New(ns...)}
37}
38
39// Ensure that the store can recover from a previously saved state.
40func TestStoreRecover(t *testing.T) {
41	s := newTestStore(t)
42	defer s.Close()
43	var eidx uint64 = 4
44	s.Create("/foo", true, "", false, v2store.TTLOptionSet{ExpireTime: v2store.Permanent})
45	s.Create("/foo/x", false, "bar", false, v2store.TTLOptionSet{ExpireTime: v2store.Permanent})
46	s.Update("/foo/x", "barbar", v2store.TTLOptionSet{ExpireTime: v2store.Permanent})
47	s.Create("/foo/y", false, "baz", false, v2store.TTLOptionSet{ExpireTime: v2store.Permanent})
48	b, err := s.Save()
49	testutil.AssertNil(t, err)
50
51	s2 := newTestStore(t)
52	s2.Recovery(b)
53
54	e, err := s.Get("/foo/x", false, false)
55	testutil.AssertEqual(t, e.Node.CreatedIndex, uint64(2))
56	testutil.AssertEqual(t, e.Node.ModifiedIndex, uint64(3))
57	testutil.AssertEqual(t, e.EtcdIndex, eidx)
58	testutil.AssertNil(t, err)
59	testutil.AssertEqual(t, *e.Node.Value, "barbar")
60
61	e, err = s.Get("/foo/y", false, false)
62	testutil.AssertEqual(t, e.EtcdIndex, eidx)
63	testutil.AssertNil(t, err)
64	testutil.AssertEqual(t, *e.Node.Value, "baz")
65}
66