github.com/wynshop-open-source/gomplate@v3.5.0+incompatible/libkv/libkv_test.go (about)

     1  package libkv
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/docker/libkv/store"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestRead(t *testing.T) {
    12  	s := &FakeStore{data: []*store.KVPair{
    13  		{Key: "foo", Value: []byte("bar")},
    14  	}}
    15  	kv := &LibKV{s}
    16  	_, err := kv.Read("foo")
    17  
    18  	assert.NoError(t, err)
    19  
    20  	s = &FakeStore{err: errors.New("fail")}
    21  	kv = &LibKV{s}
    22  	_, err = kv.Read("foo")
    23  
    24  	assert.Error(t, err)
    25  }
    26  
    27  type FakeStore struct {
    28  	data []*store.KVPair
    29  	err  error
    30  }
    31  
    32  func (s *FakeStore) Put(key string, value []byte, options *store.WriteOptions) error {
    33  	return nil
    34  }
    35  
    36  func (s *FakeStore) Get(key string) (*store.KVPair, error) {
    37  	if s.err != nil {
    38  		return nil, s.err
    39  	}
    40  
    41  	for _, v := range s.data {
    42  		if v.Key == key {
    43  			return v, nil
    44  		}
    45  	}
    46  	return nil, nil
    47  }
    48  
    49  func (s *FakeStore) Delete(key string) error {
    50  	return nil
    51  }
    52  
    53  func (s *FakeStore) Exists(key string) (bool, error) {
    54  	return false, nil
    55  }
    56  
    57  func (s *FakeStore) Watch(key string, stopCh <-chan struct{}) (<-chan *store.KVPair, error) {
    58  	return nil, nil
    59  }
    60  
    61  func (s *FakeStore) WatchTree(directory string, stopCh <-chan struct{}) (<-chan []*store.KVPair, error) {
    62  	return nil, nil
    63  }
    64  
    65  func (s *FakeStore) NewLock(key string, options *store.LockOptions) (store.Locker, error) {
    66  	return nil, nil
    67  }
    68  
    69  func (s *FakeStore) List(directory string) ([]*store.KVPair, error) {
    70  	return nil, nil
    71  }
    72  
    73  func (s *FakeStore) DeleteTree(directory string) error {
    74  	return nil
    75  }
    76  
    77  func (s *FakeStore) AtomicPut(key string, value []byte, previous *store.KVPair, options *store.WriteOptions) (bool, *store.KVPair, error) {
    78  	return false, nil, nil
    79  }
    80  
    81  func (s *FakeStore) AtomicDelete(key string, previous *store.KVPair) (bool, error) {
    82  	return false, nil
    83  }
    84  
    85  func (s *FakeStore) Close() {}