2017-10-12 15:50:03 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
2018-01-24 16:52:03 +00:00
|
|
|
"github.com/abronan/valkeyrie/store"
|
2017-10-12 15:50:03 +00:00
|
|
|
)
|
|
|
|
|
2018-01-03 15:37:31 +00:00
|
|
|
func newProviderMock(kvPairs []*store.KVPair) *Provider {
|
|
|
|
return &Provider{
|
|
|
|
Prefix: "traefik",
|
|
|
|
kvClient: &Mock{
|
|
|
|
KVPairs: kvPairs,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 15:50:03 +00:00
|
|
|
// Override Get/List to return a error
|
|
|
|
type KvError struct {
|
|
|
|
Get error
|
|
|
|
List error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extremely limited mock store so we can test initialization
|
|
|
|
type Mock struct {
|
|
|
|
Error KvError
|
|
|
|
KVPairs []*store.KVPair
|
|
|
|
WatchTreeMethod func() <-chan []*store.KVPair
|
|
|
|
}
|
|
|
|
|
2017-12-23 16:45:25 +00:00
|
|
|
func newKvClientMock(kvPairs []*store.KVPair, err error) *Mock {
|
|
|
|
mock := &Mock{
|
|
|
|
KVPairs: kvPairs,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
mock.Error = KvError{
|
|
|
|
Get: err,
|
|
|
|
List: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mock
|
|
|
|
}
|
|
|
|
|
2017-10-12 15:50:03 +00:00
|
|
|
func (s *Mock) Put(key string, value []byte, opts *store.WriteOptions) error {
|
2018-08-06 18:00:03 +00:00
|
|
|
return errors.New("put not supported")
|
2017-10-12 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 16:22:03 +00:00
|
|
|
func (s *Mock) Get(key string, options *store.ReadOptions) (*store.KVPair, error) {
|
2017-10-12 15:50:03 +00:00
|
|
|
if err := s.Error.Get; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, kvPair := range s.KVPairs {
|
|
|
|
if kvPair.Key == key {
|
|
|
|
return kvPair, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, store.ErrKeyNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Mock) Delete(key string) error {
|
2018-08-06 18:00:03 +00:00
|
|
|
return errors.New("delete not supported")
|
2017-10-12 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exists mock
|
2017-11-17 16:22:03 +00:00
|
|
|
func (s *Mock) Exists(key string, options *store.ReadOptions) (bool, error) {
|
2017-10-12 15:50:03 +00:00
|
|
|
if err := s.Error.Get; err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, kvPair := range s.KVPairs {
|
|
|
|
if strings.HasPrefix(kvPair.Key, key) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, store.ErrKeyNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch mock
|
2017-11-17 16:22:03 +00:00
|
|
|
func (s *Mock) Watch(key string, stopCh <-chan struct{}, options *store.ReadOptions) (<-chan *store.KVPair, error) {
|
2018-08-06 18:00:03 +00:00
|
|
|
return nil, errors.New("watch not supported")
|
2017-10-12 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WatchTree mock
|
2017-11-17 16:22:03 +00:00
|
|
|
func (s *Mock) WatchTree(prefix string, stopCh <-chan struct{}, options *store.ReadOptions) (<-chan []*store.KVPair, error) {
|
2017-10-12 15:50:03 +00:00
|
|
|
return s.WatchTreeMethod(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLock mock
|
|
|
|
func (s *Mock) NewLock(key string, options *store.LockOptions) (store.Locker, error) {
|
|
|
|
return nil, errors.New("NewLock not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// List mock
|
2017-11-17 16:22:03 +00:00
|
|
|
func (s *Mock) List(prefix string, options *store.ReadOptions) ([]*store.KVPair, error) {
|
2017-10-12 15:50:03 +00:00
|
|
|
if err := s.Error.List; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-23 16:45:25 +00:00
|
|
|
var kv []*store.KVPair
|
2017-10-12 15:50:03 +00:00
|
|
|
for _, kvPair := range s.KVPairs {
|
2017-12-23 16:45:25 +00:00
|
|
|
if strings.HasPrefix(kvPair.Key, prefix) && !strings.ContainsAny(strings.TrimPrefix(kvPair.Key, prefix), pathSeparator) {
|
2017-10-12 15:50:03 +00:00
|
|
|
kv = append(kv, kvPair)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return kv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTree mock
|
|
|
|
func (s *Mock) DeleteTree(prefix string) error {
|
|
|
|
return errors.New("DeleteTree not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// AtomicPut mock
|
|
|
|
func (s *Mock) AtomicPut(key string, value []byte, previous *store.KVPair, opts *store.WriteOptions) (bool, *store.KVPair, error) {
|
|
|
|
return false, nil, errors.New("AtomicPut not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// AtomicDelete mock
|
|
|
|
func (s *Mock) AtomicDelete(key string, previous *store.KVPair) (bool, error) {
|
|
|
|
return false, errors.New("AtomicDelete not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close mock
|
|
|
|
func (s *Mock) Close() {}
|