go.etcd.io/etcd@v3.3.27+incompatible/store/store_v2_test.go (about)

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