2019-11-28 20:56:04 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
2022-08-11 13:42:07 +00:00
|
|
|
"context"
|
2019-11-28 20:56:04 +00:00
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
2022-01-12 13:42:21 +00:00
|
|
|
"github.com/kvtools/valkeyrie/store"
|
2019-11-28 20:56:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func newProviderMock(kvPairs []*store.KVPair) *Provider {
|
|
|
|
return &Provider{
|
|
|
|
RootKey: "traefik",
|
|
|
|
kvClient: newKvClientMock(kvPairs, nil),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Override Get/List to return a error.
|
2019-11-28 20:56:04 +00:00
|
|
|
type KvError struct {
|
|
|
|
Get error
|
|
|
|
List error
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Extremely limited mock store so we can test initialization.
|
2019-11-28 20:56:04 +00:00
|
|
|
type Mock struct {
|
|
|
|
Error KvError
|
|
|
|
KVPairs []*store.KVPair
|
|
|
|
WatchTreeMethod func() <-chan []*store.KVPair
|
|
|
|
}
|
|
|
|
|
|
|
|
func newKvClientMock(kvPairs []*store.KVPair, err error) *Mock {
|
|
|
|
mock := &Mock{
|
|
|
|
KVPairs: kvPairs,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
mock.Error = KvError{
|
|
|
|
Get: err,
|
|
|
|
List: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mock
|
|
|
|
}
|
|
|
|
|
2022-08-11 13:42:07 +00:00
|
|
|
func (s *Mock) Put(ctx context.Context, key string, value []byte, opts *store.WriteOptions) error {
|
2019-11-28 20:56:04 +00:00
|
|
|
return errors.New("method Put not supported")
|
|
|
|
}
|
|
|
|
|
2022-08-11 13:42:07 +00:00
|
|
|
func (s *Mock) Get(ctx context.Context, key string, options *store.ReadOptions) (*store.KVPair, error) {
|
2019-11-28 20:56:04 +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
|
|
|
|
}
|
|
|
|
|
2022-08-11 13:42:07 +00:00
|
|
|
func (s *Mock) Delete(ctx context.Context, key string) error {
|
2019-11-28 20:56:04 +00:00
|
|
|
return errors.New("method Delete not supported")
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Exists mock.
|
2022-08-11 13:42:07 +00:00
|
|
|
func (s *Mock) Exists(ctx context.Context, key string, options *store.ReadOptions) (bool, error) {
|
2019-11-28 20:56:04 +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
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Watch mock.
|
2022-08-11 13:42:07 +00:00
|
|
|
func (s *Mock) Watch(ctx context.Context, key string, options *store.ReadOptions) (<-chan *store.KVPair, error) {
|
2019-11-28 20:56:04 +00:00
|
|
|
return nil, errors.New("method Watch not supported")
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// WatchTree mock.
|
2022-08-11 13:42:07 +00:00
|
|
|
func (s *Mock) WatchTree(ctx context.Context, prefix string, options *store.ReadOptions) (<-chan []*store.KVPair, error) {
|
2019-11-28 20:56:04 +00:00
|
|
|
return s.WatchTreeMethod(), nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// NewLock mock.
|
2022-08-11 13:42:07 +00:00
|
|
|
func (s *Mock) NewLock(ctx context.Context, key string, options *store.LockOptions) (store.Locker, error) {
|
2019-11-28 20:56:04 +00:00
|
|
|
return nil, errors.New("method NewLock not supported")
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// List mock.
|
2022-08-11 13:42:07 +00:00
|
|
|
func (s *Mock) List(ctx context.Context, prefix string, options *store.ReadOptions) ([]*store.KVPair, error) {
|
2019-11-28 20:56:04 +00:00
|
|
|
if err := s.Error.List; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var kv []*store.KVPair
|
|
|
|
for _, kvPair := range s.KVPairs {
|
2022-08-31 06:24:08 +00:00
|
|
|
if strings.HasPrefix(kvPair.Key, prefix) {
|
2019-11-28 20:56:04 +00:00
|
|
|
kv = append(kv, kvPair)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return kv, nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// DeleteTree mock.
|
2022-08-11 13:42:07 +00:00
|
|
|
func (s *Mock) DeleteTree(ctx context.Context, prefix string) error {
|
2019-11-28 20:56:04 +00:00
|
|
|
return errors.New("method DeleteTree not supported")
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// AtomicPut mock.
|
2022-08-11 13:42:07 +00:00
|
|
|
func (s *Mock) AtomicPut(ctx context.Context, key string, value []byte, previous *store.KVPair, opts *store.WriteOptions) (bool, *store.KVPair, error) {
|
2019-11-28 20:56:04 +00:00
|
|
|
return false, nil, errors.New("method AtomicPut not supported")
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// AtomicDelete mock.
|
2022-08-11 13:42:07 +00:00
|
|
|
func (s *Mock) AtomicDelete(ctx context.Context, key string, previous *store.KVPair) (bool, error) {
|
2019-11-28 20:56:04 +00:00
|
|
|
return false, errors.New("method AtomicDelete not supported")
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Close mock.
|
2022-08-11 13:42:07 +00:00
|
|
|
func (s *Mock) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|