2017-04-17 10:50:02 +00:00
|
|
|
package kv
|
2015-11-13 10:50:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-12-30 08:21:13 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2015-11-13 10:50:32 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-03-04 22:52:08 +00:00
|
|
|
"time"
|
2015-11-13 10:50:32 +00:00
|
|
|
|
2016-12-30 08:21:13 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2015-11-13 10:50:32 +00:00
|
|
|
"github.com/docker/libkv/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestKvList(t *testing.T) {
|
|
|
|
cases := []struct {
|
2017-04-17 10:50:02 +00:00
|
|
|
provider *Provider
|
2015-11-13 10:50:32 +00:00
|
|
|
keys []string
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{
|
2017-04-17 10:50:02 +00:00
|
|
|
provider: &Provider{
|
|
|
|
Kvclient: &Mock{},
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
keys: []string{},
|
|
|
|
expected: []string{},
|
|
|
|
},
|
|
|
|
{
|
2017-04-17 10:50:02 +00:00
|
|
|
provider: &Provider{
|
|
|
|
Kvclient: &Mock{},
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
keys: []string{"traefik"},
|
|
|
|
expected: []string{},
|
|
|
|
},
|
|
|
|
{
|
2017-04-17 10:50:02 +00:00
|
|
|
provider: &Provider{
|
|
|
|
Kvclient: &Mock{
|
2015-11-13 10:50:32 +00:00
|
|
|
KVPairs: []*store.KVPair{
|
|
|
|
{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
keys: []string{"bar"},
|
|
|
|
expected: []string{},
|
|
|
|
},
|
|
|
|
{
|
2017-04-17 10:50:02 +00:00
|
|
|
provider: &Provider{
|
|
|
|
Kvclient: &Mock{
|
2015-11-13 10:50:32 +00:00
|
|
|
KVPairs: []*store.KVPair{
|
|
|
|
{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
keys: []string{"foo"},
|
|
|
|
expected: []string{"foo"},
|
|
|
|
},
|
|
|
|
{
|
2017-04-17 10:50:02 +00:00
|
|
|
provider: &Provider{
|
|
|
|
Kvclient: &Mock{
|
2015-11-13 10:50:32 +00:00
|
|
|
KVPairs: []*store.KVPair{
|
|
|
|
{
|
|
|
|
Key: "foo/baz/1",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "foo/baz/2",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "foo/baz/biz/1",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
keys: []string{"foo", "/baz/"},
|
2016-04-19 17:23:08 +00:00
|
|
|
expected: []string{"foo/baz/1", "foo/baz/2"},
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
actual := c.provider.list(c.keys...)
|
|
|
|
sort.Strings(actual)
|
|
|
|
sort.Strings(c.expected)
|
|
|
|
if !reflect.DeepEqual(actual, c.expected) {
|
|
|
|
t.Fatalf("expected %v, got %v for %v and %v", c.expected, actual, c.keys, c.provider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error case
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := &Provider{
|
|
|
|
Kvclient: &Mock{
|
2017-03-11 23:22:39 +00:00
|
|
|
Error: KvError{
|
|
|
|
List: store.ErrKeyNotFound,
|
|
|
|
},
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
actual := provider.list("anything")
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("Should have return nil, got %v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKvGet(t *testing.T) {
|
|
|
|
cases := []struct {
|
2017-04-17 10:50:02 +00:00
|
|
|
provider *Provider
|
2015-11-13 10:50:32 +00:00
|
|
|
keys []string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2017-04-17 10:50:02 +00:00
|
|
|
provider: &Provider{
|
|
|
|
Kvclient: &Mock{},
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
keys: []string{},
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
2017-04-17 10:50:02 +00:00
|
|
|
provider: &Provider{
|
|
|
|
Kvclient: &Mock{},
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
keys: []string{"traefik"},
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
2017-04-17 10:50:02 +00:00
|
|
|
provider: &Provider{
|
|
|
|
Kvclient: &Mock{
|
2015-11-13 10:50:32 +00:00
|
|
|
KVPairs: []*store.KVPair{
|
|
|
|
{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
keys: []string{"bar"},
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
2017-04-17 10:50:02 +00:00
|
|
|
provider: &Provider{
|
|
|
|
Kvclient: &Mock{
|
2015-11-13 10:50:32 +00:00
|
|
|
KVPairs: []*store.KVPair{
|
|
|
|
{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
keys: []string{"foo"},
|
|
|
|
expected: "bar",
|
|
|
|
},
|
|
|
|
{
|
2017-04-17 10:50:02 +00:00
|
|
|
provider: &Provider{
|
|
|
|
Kvclient: &Mock{
|
2015-11-13 10:50:32 +00:00
|
|
|
KVPairs: []*store.KVPair{
|
|
|
|
{
|
|
|
|
Key: "foo/baz/1",
|
|
|
|
Value: []byte("bar1"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "foo/baz/2",
|
|
|
|
Value: []byte("bar2"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "foo/baz/biz/1",
|
|
|
|
Value: []byte("bar3"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
keys: []string{"foo", "/baz/", "2"},
|
|
|
|
expected: "bar2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2016-02-01 10:07:05 +00:00
|
|
|
actual := c.provider.get("", c.keys...)
|
2015-11-13 10:50:32 +00:00
|
|
|
if actual != c.expected {
|
|
|
|
t.Fatalf("expected %v, got %v for %v and %v", c.expected, actual, c.keys, c.provider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error case
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := &Provider{
|
|
|
|
Kvclient: &Mock{
|
2017-03-11 23:22:39 +00:00
|
|
|
Error: KvError{
|
|
|
|
Get: store.ErrKeyNotFound,
|
|
|
|
},
|
2015-11-13 10:50:32 +00:00
|
|
|
},
|
|
|
|
}
|
2016-02-01 10:07:05 +00:00
|
|
|
actual := provider.get("", "anything")
|
2015-11-13 10:50:32 +00:00
|
|
|
if actual != "" {
|
|
|
|
t.Fatalf("Should have return nil, got %v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKvLast(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
key string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
key: "",
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "foo",
|
|
|
|
expected: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "foo/bar",
|
|
|
|
expected: "bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "foo/bar/baz",
|
|
|
|
expected: "baz",
|
|
|
|
},
|
|
|
|
// FIXME is this wanted ?
|
|
|
|
{
|
|
|
|
key: "foo/bar/",
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := &Provider{}
|
2015-11-13 10:50:32 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
actual := provider.last(c.key)
|
|
|
|
if actual != c.expected {
|
|
|
|
t.Fatalf("expected %s, got %s", c.expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-04 22:52:08 +00:00
|
|
|
type KvMock struct {
|
2017-04-17 10:50:02 +00:00
|
|
|
Provider
|
2016-03-04 22:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *KvMock) loadConfig() *types.Configuration {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKvWatchTree(t *testing.T) {
|
|
|
|
returnedChans := make(chan chan []*store.KVPair)
|
|
|
|
provider := &KvMock{
|
2017-04-17 10:50:02 +00:00
|
|
|
Provider{
|
|
|
|
Kvclient: &Mock{
|
2016-03-04 22:52:08 +00:00
|
|
|
WatchTreeMethod: func() <-chan []*store.KVPair {
|
|
|
|
c := make(chan []*store.KVPair, 10)
|
|
|
|
returnedChans <- c
|
|
|
|
return c
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
configChan := make(chan types.ConfigMessage)
|
2016-04-19 17:23:08 +00:00
|
|
|
go func() {
|
2016-04-13 18:36:23 +00:00
|
|
|
provider.watchKv(configChan, "prefix", make(chan bool, 1))
|
2016-04-19 17:23:08 +00:00
|
|
|
}()
|
2016-03-04 22:52:08 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case c1 := <-returnedChans:
|
|
|
|
c1 <- []*store.KVPair{}
|
|
|
|
<-configChan
|
|
|
|
close(c1) // WatchTree chans can close due to error
|
|
|
|
case <-time.After(1 * time.Second):
|
2016-03-05 20:43:44 +00:00
|
|
|
t.Fatalf("Failed to create a new WatchTree chan")
|
2016-03-04 22:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case c2 := <-returnedChans:
|
|
|
|
c2 <- []*store.KVPair{}
|
|
|
|
<-configChan
|
|
|
|
case <-time.After(1 * time.Second):
|
2016-03-05 20:43:44 +00:00
|
|
|
t.Fatalf("Failed to create a new WatchTree chan")
|
2016-03-04 22:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case _ = <-configChan:
|
|
|
|
t.Fatalf("configChan should be empty")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-11 23:22:39 +00:00
|
|
|
// Override Get/List to return a error
|
|
|
|
type KvError struct {
|
|
|
|
Get error
|
|
|
|
List error
|
|
|
|
}
|
|
|
|
|
2015-11-13 10:50:32 +00:00
|
|
|
// Extremely limited mock store so we can test initialization
|
|
|
|
type Mock struct {
|
2017-03-11 23:22:39 +00:00
|
|
|
Error KvError
|
2016-03-04 22:52:08 +00:00
|
|
|
KVPairs []*store.KVPair
|
|
|
|
WatchTreeMethod func() <-chan []*store.KVPair
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Mock) Put(key string, value []byte, opts *store.WriteOptions) error {
|
|
|
|
return errors.New("Put not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Mock) Get(key string) (*store.KVPair, error) {
|
2017-03-11 23:22:39 +00:00
|
|
|
if err := s.Error.Get; err != nil {
|
|
|
|
return nil, err
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
for _, kvPair := range s.KVPairs {
|
|
|
|
if kvPair.Key == key {
|
|
|
|
return kvPair, nil
|
|
|
|
}
|
|
|
|
}
|
2017-03-07 14:56:35 +00:00
|
|
|
return nil, store.ErrKeyNotFound
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Mock) Delete(key string) error {
|
|
|
|
return errors.New("Delete not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exists mock
|
|
|
|
func (s *Mock) Exists(key string) (bool, error) {
|
|
|
|
return false, errors.New("Exists not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch mock
|
|
|
|
func (s *Mock) Watch(key string, stopCh <-chan struct{}) (<-chan *store.KVPair, error) {
|
|
|
|
return nil, errors.New("Watch not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// WatchTree mock
|
|
|
|
func (s *Mock) WatchTree(prefix string, stopCh <-chan struct{}) (<-chan []*store.KVPair, error) {
|
2016-03-04 22:52:08 +00:00
|
|
|
return s.WatchTreeMethod(), nil
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLock mock
|
|
|
|
func (s *Mock) NewLock(key string, options *store.LockOptions) (store.Locker, error) {
|
|
|
|
return nil, errors.New("NewLock not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// List mock
|
|
|
|
func (s *Mock) List(prefix string) ([]*store.KVPair, error) {
|
2017-03-11 23:22:39 +00:00
|
|
|
if err := s.Error.List; err != nil {
|
|
|
|
return nil, err
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|
|
|
|
kv := []*store.KVPair{}
|
|
|
|
for _, kvPair := range s.KVPairs {
|
2016-04-19 17:23:08 +00:00
|
|
|
if strings.HasPrefix(kvPair.Key, prefix) && !strings.ContainsAny(strings.TrimPrefix(kvPair.Key, prefix), "/") {
|
2015-11-13 10:50:32 +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() {
|
|
|
|
return
|
|
|
|
}
|
2016-04-19 17:23:08 +00:00
|
|
|
|
|
|
|
func TestKVLoadConfig(t *testing.T) {
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := &Provider{
|
2016-04-19 17:23:08 +00:00
|
|
|
Prefix: "traefik",
|
2017-04-17 10:50:02 +00:00
|
|
|
Kvclient: &Mock{
|
2016-04-19 17:23:08 +00:00
|
|
|
KVPairs: []*store.KVPair{
|
|
|
|
{
|
|
|
|
Key: "traefik/frontends/frontend.with.dot",
|
|
|
|
Value: []byte(""),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "traefik/frontends/frontend.with.dot/backend",
|
|
|
|
Value: []byte("backend.with.dot.too"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "traefik/frontends/frontend.with.dot/routes",
|
|
|
|
Value: []byte(""),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "traefik/frontends/frontend.with.dot/routes/route.with.dot",
|
|
|
|
Value: []byte(""),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "traefik/frontends/frontend.with.dot/routes/route.with.dot/rule",
|
|
|
|
Value: []byte("Host:test.localhost"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "traefik/backends/backend.with.dot.too",
|
|
|
|
Value: []byte(""),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "traefik/backends/backend.with.dot.too/servers",
|
|
|
|
Value: []byte(""),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "traefik/backends/backend.with.dot.too/servers/server.with.dot",
|
|
|
|
Value: []byte(""),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "traefik/backends/backend.with.dot.too/servers/server.with.dot/url",
|
|
|
|
Value: []byte("http://172.17.0.2:80"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "traefik/backends/backend.with.dot.too/servers/server.with.dot/weight",
|
2016-11-23 13:49:55 +00:00
|
|
|
Value: []byte("0"),
|
2016-04-19 17:23:08 +00:00
|
|
|
},
|
2017-03-07 14:56:35 +00:00
|
|
|
{
|
|
|
|
Key: "traefik/backends/backend.with.dot.too/servers/server.with.dot.without.url",
|
|
|
|
Value: []byte(""),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "traefik/backends/backend.with.dot.too/servers/server.with.dot.without.url/weight",
|
|
|
|
Value: []byte("0"),
|
|
|
|
},
|
2016-04-19 17:23:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actual := provider.loadConfig()
|
|
|
|
expected := &types.Configuration{
|
|
|
|
Backends: map[string]*types.Backend{
|
|
|
|
"backend.with.dot.too": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"server.with.dot": {
|
|
|
|
URL: "http://172.17.0.2:80",
|
2016-11-23 13:49:55 +00:00
|
|
|
Weight: 0,
|
2016-04-19 17:23:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
LoadBalancer: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Frontends: map[string]*types.Frontend{
|
|
|
|
"frontend.with.dot": {
|
|
|
|
Backend: "backend.with.dot.too",
|
2016-05-10 11:43:24 +00:00
|
|
|
PassHostHeader: true,
|
2016-04-19 17:23:08 +00:00
|
|
|
EntryPoints: []string{},
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"route.with.dot": {
|
|
|
|
Rule: "Host:test.localhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual.Backends, expected.Backends) {
|
|
|
|
t.Fatalf("expected %+v, got %+v", expected.Backends, actual.Backends)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual.Frontends, expected.Frontends) {
|
|
|
|
t.Fatalf("expected %+v, got %+v", expected.Frontends, actual.Frontends)
|
|
|
|
}
|
|
|
|
}
|