2014-06-06 23:40:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2014-06-23 18:32:11 +00:00
|
|
|
|
2014-08-11 07:34:59 +00:00
|
|
|
package etcd
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
import (
|
2014-10-07 20:51:28 +00:00
|
|
|
"strconv"
|
2014-06-06 23:40:48 +00:00
|
|
|
"testing"
|
2014-11-28 22:28:42 +00:00
|
|
|
"time"
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2014-06-12 20:17:34 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-09-03 21:16:00 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
2014-09-11 17:02:53 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
2015-03-06 23:29:03 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
2014-06-17 02:10:43 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2015-02-12 00:07:54 +00:00
|
|
|
etcdgeneric "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic/etcd"
|
2014-09-23 20:43:48 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/pod"
|
2015-02-12 00:07:54 +00:00
|
|
|
podetcd "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/pod/etcd"
|
2014-09-02 17:55:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-06-30 19:00:14 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
|
2014-08-11 07:34:59 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
"github.com/coreos/go-etcd/etcd"
|
|
|
|
)
|
|
|
|
|
2014-08-28 23:12:14 +00:00
|
|
|
func NewTestEtcdRegistry(client tools.EtcdClient) *Registry {
|
2015-02-12 00:07:54 +00:00
|
|
|
registry := NewRegistry(tools.EtcdHelper{client, latest.Codec, tools.RuntimeVersionAdapter{latest.ResourceVersioner}}, nil)
|
2014-06-17 23:23:52 +00:00
|
|
|
return registry
|
|
|
|
}
|
|
|
|
|
2015-02-12 00:07:54 +00:00
|
|
|
func NewTestEtcdRegistryWithPods(client tools.EtcdClient) *Registry {
|
|
|
|
helper := tools.EtcdHelper{client, latest.Codec, tools.RuntimeVersionAdapter{latest.ResourceVersioner}}
|
2015-03-13 08:58:00 +00:00
|
|
|
podStorage, _, _ := podetcd.NewREST(helper)
|
2015-02-12 00:07:54 +00:00
|
|
|
registry := NewRegistry(helper, pod.NewRegistry(podStorage))
|
|
|
|
return registry
|
2014-11-28 22:28:42 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
func TestEtcdListControllersNotFound(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
|
|
|
key := makeControllerListKey(ctx)
|
2014-06-30 19:00:14 +00:00
|
|
|
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
2014-06-06 23:40:48 +00:00
|
|
|
R: &etcd.Response{},
|
2014-07-15 11:48:06 +00:00
|
|
|
E: tools.EtcdErrorNotFound,
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-26 19:19:22 +00:00
|
|
|
controllers, err := registry.ListControllers(ctx)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-08-29 00:48:07 +00:00
|
|
|
if len(controllers.Items) != 0 {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Unexpected controller list: %#v", controllers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdListServicesNotFound(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
|
|
|
key := makeServiceListKey(ctx)
|
2014-06-30 19:00:14 +00:00
|
|
|
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
2014-06-06 23:40:48 +00:00
|
|
|
R: &etcd.Response{},
|
2014-07-15 11:48:06 +00:00
|
|
|
E: tools.EtcdErrorNotFound,
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-26 19:19:22 +00:00
|
|
|
services, err := registry.ListServices(ctx)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
if len(services.Items) != 0 {
|
|
|
|
t.Errorf("Unexpected controller list: %#v", services)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdListControllers(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
|
|
|
key := makeControllerListKey(ctx)
|
2014-06-30 19:00:14 +00:00
|
|
|
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
2014-06-06 23:40:48 +00:00
|
|
|
R: &etcd.Response{
|
|
|
|
Node: &etcd.Node{
|
|
|
|
Nodes: []*etcd.Node{
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Name: "foo"}}),
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Name: "bar"}}),
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
E: nil,
|
|
|
|
}
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-26 19:19:22 +00:00
|
|
|
controllers, err := registry.ListControllers(ctx)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
if len(controllers.Items) != 2 || controllers.Items[0].Name != "foo" || controllers.Items[1].Name != "bar" {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Unexpected controller list: %#v", controllers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-03 15:44:06 +00:00
|
|
|
// TestEtcdGetControllerDifferentNamespace ensures same-name controllers in different namespaces do not clash
|
|
|
|
func TestEtcdGetControllerDifferentNamespace(t *testing.T) {
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
|
|
|
|
|
|
|
ctx1 := api.NewDefaultContext()
|
|
|
|
ctx2 := api.WithNamespace(api.NewContext(), "other")
|
|
|
|
|
|
|
|
key1, _ := makeControllerKey(ctx1, "foo")
|
|
|
|
key2, _ := makeControllerKey(ctx2, "foo")
|
|
|
|
|
2014-10-23 20:51:34 +00:00
|
|
|
fakeClient.Set(key1, runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Namespace: "default", Name: "foo"}}), 0)
|
|
|
|
fakeClient.Set(key2, runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Namespace: "other", Name: "foo"}}), 0)
|
2014-10-03 15:44:06 +00:00
|
|
|
|
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
|
|
|
|
|
|
|
ctrl1, err := registry.GetController(ctx1, "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-10-22 17:02:02 +00:00
|
|
|
if ctrl1.Name != "foo" {
|
2014-10-03 15:44:06 +00:00
|
|
|
t.Errorf("Unexpected controller: %#v", ctrl1)
|
|
|
|
}
|
|
|
|
if ctrl1.Namespace != "default" {
|
|
|
|
t.Errorf("Unexpected controller: %#v", ctrl1)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctrl2, err := registry.GetController(ctx2, "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-10-22 17:02:02 +00:00
|
|
|
if ctrl2.Name != "foo" {
|
2014-10-03 15:44:06 +00:00
|
|
|
t.Errorf("Unexpected controller: %#v", ctrl2)
|
|
|
|
}
|
|
|
|
if ctrl2.Namespace != "other" {
|
|
|
|
t.Errorf("Unexpected controller: %#v", ctrl2)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
func TestEtcdGetController(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-10-03 15:44:06 +00:00
|
|
|
key, _ := makeControllerKey(ctx, "foo")
|
2014-10-23 20:51:34 +00:00
|
|
|
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-26 19:19:22 +00:00
|
|
|
ctrl, err := registry.GetController(ctx, "foo")
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
if ctrl.Name != "foo" {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Unexpected controller: %#v", ctrl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdGetControllerNotFound(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-10-03 15:44:06 +00:00
|
|
|
key, _ := makeControllerKey(ctx, "foo")
|
|
|
|
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
2014-06-06 23:40:48 +00:00
|
|
|
R: &etcd.Response{
|
|
|
|
Node: nil,
|
|
|
|
},
|
2014-07-15 11:48:06 +00:00
|
|
|
E: tools.EtcdErrorNotFound,
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-26 19:19:22 +00:00
|
|
|
ctrl, err := registry.GetController(ctx, "foo")
|
2014-06-06 23:40:48 +00:00
|
|
|
if ctrl != nil {
|
|
|
|
t.Errorf("Unexpected non-nil controller: %#v", ctrl)
|
|
|
|
}
|
2014-09-05 19:30:35 +00:00
|
|
|
if !errors.IsNotFound(err) {
|
|
|
|
t.Errorf("Unexpected error returned: %#v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdDeleteController(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-10-03 15:44:06 +00:00
|
|
|
key, _ := makeControllerKey(ctx, "foo")
|
2015-03-05 03:34:16 +00:00
|
|
|
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
|
2014-09-26 19:19:22 +00:00
|
|
|
err := registry.DeleteController(ctx, "foo")
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-17 23:23:52 +00:00
|
|
|
if len(fakeClient.DeletedKeys) != 1 {
|
|
|
|
t.Errorf("Expected 1 delete, found %#v", fakeClient.DeletedKeys)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-17 23:23:52 +00:00
|
|
|
if fakeClient.DeletedKeys[0] != key {
|
|
|
|
t.Errorf("Unexpected key: %s, expected %s", fakeClient.DeletedKeys[0], key)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdCreateController(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-10-03 15:44:06 +00:00
|
|
|
key, _ := makeControllerKey(ctx, "foo")
|
2015-02-28 00:37:04 +00:00
|
|
|
_, err := registry.CreateController(ctx, &api.ReplicationController{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
2014-10-22 17:02:02 +00:00
|
|
|
Name: "foo",
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
})
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-10-03 15:44:06 +00:00
|
|
|
resp, err := fakeClient.Get(key, false, false)
|
2014-07-23 01:53:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %v", err)
|
|
|
|
}
|
2014-06-12 20:17:34 +00:00
|
|
|
var ctrl api.ReplicationController
|
2014-09-16 19:56:26 +00:00
|
|
|
err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &ctrl)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
if ctrl.Name != "foo" {
|
2014-06-09 05:38:45 +00:00
|
|
|
t.Errorf("Unexpected pod: %#v %s", ctrl, resp.Node.Value)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 17:07:40 +00:00
|
|
|
func TestEtcdCreateControllerAlreadyExisting(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-10-03 15:44:06 +00:00
|
|
|
key, _ := makeControllerKey(ctx, "foo")
|
2014-10-23 20:51:34 +00:00
|
|
|
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
|
2014-08-03 17:07:40 +00:00
|
|
|
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2015-02-28 00:37:04 +00:00
|
|
|
_, err := registry.CreateController(ctx, &api.ReplicationController{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
2014-10-22 17:02:02 +00:00
|
|
|
Name: "foo",
|
2014-08-03 17:07:40 +00:00
|
|
|
},
|
|
|
|
})
|
2014-09-03 21:16:00 +00:00
|
|
|
if !errors.IsAlreadyExists(err) {
|
2014-08-03 17:07:40 +00:00
|
|
|
t.Errorf("expected already exists err, got %#v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
func TestEtcdUpdateController(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-01 21:02:37 +00:00
|
|
|
fakeClient.TestIndex = true
|
2014-10-03 15:44:06 +00:00
|
|
|
key, _ := makeControllerKey(ctx, "foo")
|
2014-10-23 20:51:34 +00:00
|
|
|
resp, _ := fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2015-02-28 00:37:04 +00:00
|
|
|
_, err := registry.UpdateController(ctx, &api.ReplicationController{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: strconv.FormatUint(resp.Node.ModifiedIndex, 10)},
|
2014-11-07 02:09:46 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
2014-06-06 23:40:48 +00:00
|
|
|
Replicas: 2,
|
|
|
|
},
|
|
|
|
})
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-09-26 19:19:22 +00:00
|
|
|
ctrl, err := registry.GetController(ctx, "foo")
|
2014-11-07 02:09:46 +00:00
|
|
|
if ctrl.Spec.Replicas != 2 {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Unexpected controller: %#v", ctrl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-28 22:28:42 +00:00
|
|
|
func TestEtcdWatchController(t *testing.T) {
|
|
|
|
ctx := api.NewDefaultContext()
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
|
|
|
watching, err := registry.WatchControllers(ctx,
|
|
|
|
labels.Everything(),
|
2015-03-06 23:29:03 +00:00
|
|
|
fields.Everything(),
|
2014-11-28 22:28:42 +00:00
|
|
|
"1",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
fakeClient.WaitForWatchCompletion()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case _, ok := <-watching.ResultChan():
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("watching channel should be open")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
fakeClient.WatchInjectError <- nil
|
|
|
|
if _, ok := <-watching.ResultChan(); ok {
|
|
|
|
t.Errorf("watching channel should be closed")
|
|
|
|
}
|
|
|
|
watching.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdWatchControllersMatch(t *testing.T) {
|
|
|
|
ctx := api.NewDefaultContext()
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2015-02-12 00:07:54 +00:00
|
|
|
fakeClient.ExpectNotFoundGet(etcdgeneric.NamespaceKeyRootFunc(ctx, "/registry/pods"))
|
|
|
|
registry := NewTestEtcdRegistryWithPods(fakeClient)
|
2014-11-28 22:28:42 +00:00
|
|
|
watching, err := registry.WatchControllers(ctx,
|
|
|
|
labels.SelectorFromSet(labels.Set{"name": "foo"}),
|
2015-03-06 23:29:03 +00:00
|
|
|
fields.Everything(),
|
2014-11-28 22:28:42 +00:00
|
|
|
"1",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
fakeClient.WaitForWatchCompletion()
|
|
|
|
|
|
|
|
controller := &api.ReplicationController{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
controllerBytes, _ := latest.Codec.Encode(controller)
|
|
|
|
fakeClient.WatchResponse <- &etcd.Response{
|
|
|
|
Action: "create",
|
|
|
|
Node: &etcd.Node{
|
|
|
|
Value: string(controllerBytes),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case _, ok := <-watching.ResultChan():
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("watching channel should be open")
|
|
|
|
}
|
|
|
|
case <-time.After(time.Millisecond * 100):
|
|
|
|
t.Error("unexpected timeout from result channel")
|
|
|
|
}
|
|
|
|
watching.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdWatchControllersNotMatch(t *testing.T) {
|
|
|
|
ctx := api.NewDefaultContext()
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2015-02-12 00:07:54 +00:00
|
|
|
fakeClient.ExpectNotFoundGet(etcdgeneric.NamespaceKeyRootFunc(ctx, "/registry/pods"))
|
|
|
|
registry := NewTestEtcdRegistryWithPods(fakeClient)
|
2014-11-28 22:28:42 +00:00
|
|
|
watching, err := registry.WatchControllers(ctx,
|
|
|
|
labels.SelectorFromSet(labels.Set{"name": "foo"}),
|
2015-03-06 23:29:03 +00:00
|
|
|
fields.Everything(),
|
2014-11-28 22:28:42 +00:00
|
|
|
"1",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
fakeClient.WaitForWatchCompletion()
|
|
|
|
|
|
|
|
controller := &api.ReplicationController{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "bar",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
controllerBytes, _ := latest.Codec.Encode(controller)
|
|
|
|
fakeClient.WatchResponse <- &etcd.Response{
|
|
|
|
Action: "create",
|
|
|
|
Node: &etcd.Node{
|
|
|
|
Value: string(controllerBytes),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-watching.ResultChan():
|
|
|
|
t.Error("unexpected result from result channel")
|
|
|
|
case <-time.After(time.Millisecond * 100):
|
|
|
|
// expected case
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
func TestEtcdListServices(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-10-03 15:44:06 +00:00
|
|
|
key := makeServiceListKey(ctx)
|
2014-06-30 19:00:14 +00:00
|
|
|
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
2014-06-06 23:40:48 +00:00
|
|
|
R: &etcd.Response{
|
|
|
|
Node: &etcd.Node{
|
|
|
|
Nodes: []*etcd.Node{
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.Service{ObjectMeta: api.ObjectMeta{Name: "foo"}}),
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
2014-10-23 20:51:34 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.Service{ObjectMeta: api.ObjectMeta{Name: "bar"}}),
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
E: nil,
|
|
|
|
}
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-26 19:19:22 +00:00
|
|
|
services, err := registry.ListServices(ctx)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
if len(services.Items) != 2 || services.Items[0].Name != "foo" || services.Items[1].Name != "bar" {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("Unexpected service list: %#v", services)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdCreateService(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2015-02-28 00:37:04 +00:00
|
|
|
_, err := registry.CreateService(ctx, &api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-06-06 23:40:48 +00:00
|
|
|
})
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-03 15:44:06 +00:00
|
|
|
key, _ := makeServiceKey(ctx, "foo")
|
|
|
|
resp, err := fakeClient.Get(key, false, false)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-12 20:17:34 +00:00
|
|
|
var service api.Service
|
2014-09-16 19:56:26 +00:00
|
|
|
err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &service)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
if service.Name != "foo" {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Unexpected service: %#v %s", service, resp.Node.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 17:07:40 +00:00
|
|
|
func TestEtcdCreateServiceAlreadyExisting(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-10-03 15:44:06 +00:00
|
|
|
key, _ := makeServiceKey(ctx, "foo")
|
2014-10-23 20:51:34 +00:00
|
|
|
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Service{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2015-02-28 00:37:04 +00:00
|
|
|
_, err := registry.CreateService(ctx, &api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-08-03 17:07:40 +00:00
|
|
|
})
|
2014-09-03 21:16:00 +00:00
|
|
|
if !errors.IsAlreadyExists(err) {
|
2014-08-03 17:07:40 +00:00
|
|
|
t.Errorf("expected already exists err, got %#v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-03 15:44:06 +00:00
|
|
|
// TestEtcdGetServiceDifferentNamespace ensures same-name services in different namespaces do not clash
|
|
|
|
func TestEtcdGetServiceDifferentNamespace(t *testing.T) {
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
|
|
|
|
|
|
|
ctx1 := api.NewDefaultContext()
|
|
|
|
ctx2 := api.WithNamespace(api.NewContext(), "other")
|
|
|
|
|
|
|
|
key1, _ := makeServiceKey(ctx1, "foo")
|
|
|
|
key2, _ := makeServiceKey(ctx2, "foo")
|
|
|
|
|
2014-10-23 20:51:34 +00:00
|
|
|
fakeClient.Set(key1, runtime.EncodeOrDie(latest.Codec, &api.Service{ObjectMeta: api.ObjectMeta{Namespace: "default", Name: "foo"}}), 0)
|
|
|
|
fakeClient.Set(key2, runtime.EncodeOrDie(latest.Codec, &api.Service{ObjectMeta: api.ObjectMeta{Namespace: "other", Name: "foo"}}), 0)
|
2014-10-03 15:44:06 +00:00
|
|
|
|
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
|
|
|
|
|
|
|
service1, err := registry.GetService(ctx1, "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-10-22 17:02:02 +00:00
|
|
|
if service1.Name != "foo" {
|
2014-10-03 15:44:06 +00:00
|
|
|
t.Errorf("Unexpected service: %#v", service1)
|
|
|
|
}
|
|
|
|
if service1.Namespace != "default" {
|
|
|
|
t.Errorf("Unexpected service: %#v", service1)
|
|
|
|
}
|
|
|
|
|
|
|
|
service2, err := registry.GetService(ctx2, "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-10-22 17:02:02 +00:00
|
|
|
if service2.Name != "foo" {
|
2014-10-03 15:44:06 +00:00
|
|
|
t.Errorf("Unexpected service: %#v", service2)
|
|
|
|
}
|
|
|
|
if service2.Namespace != "other" {
|
|
|
|
t.Errorf("Unexpected service: %#v", service2)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
func TestEtcdGetService(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-10-03 15:44:06 +00:00
|
|
|
key, _ := makeServiceKey(ctx, "foo")
|
2014-10-23 20:51:34 +00:00
|
|
|
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Service{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-26 19:19:22 +00:00
|
|
|
service, err := registry.GetService(ctx, "foo")
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
if service.Name != "foo" {
|
2014-08-14 19:48:34 +00:00
|
|
|
t.Errorf("Unexpected service: %#v", service)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdGetServiceNotFound(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-10-03 15:44:06 +00:00
|
|
|
key, _ := makeServiceKey(ctx, "foo")
|
|
|
|
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
2014-06-06 23:40:48 +00:00
|
|
|
R: &etcd.Response{
|
|
|
|
Node: nil,
|
|
|
|
},
|
2014-07-15 11:48:06 +00:00
|
|
|
E: tools.EtcdErrorNotFound,
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-26 19:19:22 +00:00
|
|
|
_, err := registry.GetService(ctx, "foo")
|
2014-09-05 19:30:35 +00:00
|
|
|
if !errors.IsNotFound(err) {
|
|
|
|
t.Errorf("Unexpected error returned: %#v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdDeleteService(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2015-03-05 03:34:16 +00:00
|
|
|
key, _ := makeServiceKey(ctx, "foo")
|
|
|
|
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Service{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
|
|
|
|
endpointsKey, _ := makeServiceEndpointsKey(ctx, "foo")
|
|
|
|
fakeClient.Set(endpointsKey, runtime.EncodeOrDie(latest.Codec, &api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "foo"}, Protocol: "TCP"}), 0)
|
|
|
|
|
2014-09-26 19:19:22 +00:00
|
|
|
err := registry.DeleteService(ctx, "foo")
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-17 23:23:52 +00:00
|
|
|
if len(fakeClient.DeletedKeys) != 2 {
|
|
|
|
t.Errorf("Expected 2 delete, found %#v", fakeClient.DeletedKeys)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-17 23:23:52 +00:00
|
|
|
if fakeClient.DeletedKeys[0] != key {
|
|
|
|
t.Errorf("Unexpected key: %s, expected %s", fakeClient.DeletedKeys[0], key)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2015-03-05 03:34:16 +00:00
|
|
|
if fakeClient.DeletedKeys[1] != endpointsKey {
|
|
|
|
t.Errorf("Unexpected key: %s, expected %s", fakeClient.DeletedKeys[1], endpointsKey)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdUpdateService(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-01 21:02:37 +00:00
|
|
|
fakeClient.TestIndex = true
|
2014-10-30 13:29:11 +00:00
|
|
|
key, _ := makeServiceKey(ctx, "uniquefoo")
|
|
|
|
resp, _ := fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Service{ObjectMeta: api.ObjectMeta{Name: "uniquefoo"}}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-06-18 23:01:49 +00:00
|
|
|
testService := api.Service{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
2014-10-30 13:29:11 +00:00
|
|
|
Name: "uniquefoo",
|
2014-10-23 20:51:34 +00:00
|
|
|
ResourceVersion: strconv.FormatUint(resp.Node.ModifiedIndex, 10),
|
|
|
|
Labels: map[string]string{
|
|
|
|
"baz": "bar",
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
2014-10-30 13:29:11 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Selector: map[string]string{
|
|
|
|
"baz": "bar",
|
|
|
|
},
|
2015-01-26 17:52:50 +00:00
|
|
|
Protocol: "TCP",
|
|
|
|
SessionAffinity: "None",
|
2014-06-18 23:01:49 +00:00
|
|
|
},
|
|
|
|
}
|
2015-02-28 00:37:04 +00:00
|
|
|
_, err := registry.UpdateService(ctx, &testService)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-10-30 13:29:11 +00:00
|
|
|
svc, err := registry.GetService(ctx, "uniquefoo")
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-08-01 21:02:37 +00:00
|
|
|
// Clear modified indices before the equality test.
|
2014-10-07 20:51:28 +00:00
|
|
|
svc.ResourceVersion = ""
|
|
|
|
testService.ResourceVersion = ""
|
2015-01-26 17:52:50 +00:00
|
|
|
if !api.Semantic.DeepEqual(*svc, testService) {
|
2014-08-01 21:02:37 +00:00
|
|
|
t.Errorf("Unexpected service: got\n %#v\n, wanted\n %#v", svc, testService)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-29 02:31:41 +00:00
|
|
|
func TestEtcdListEndpoints(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-29 02:31:41 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-10-03 15:44:06 +00:00
|
|
|
key := makeServiceEndpointsListKey(ctx)
|
2014-08-29 02:31:41 +00:00
|
|
|
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
|
|
|
R: &etcd.Response{
|
|
|
|
Node: &etcd.Node{
|
|
|
|
Nodes: []*etcd.Node{
|
|
|
|
{
|
2015-02-23 21:53:21 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "foo"}, Protocol: "TCP", Endpoints: []api.Endpoint{{IP: "127.0.0.1", Port: 8345}}}),
|
2014-08-29 02:31:41 +00:00
|
|
|
},
|
|
|
|
{
|
2015-02-23 21:53:21 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "bar"}, Protocol: "TCP"}),
|
2014-08-29 02:31:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
E: nil,
|
|
|
|
}
|
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-26 19:19:22 +00:00
|
|
|
services, err := registry.ListEndpoints(ctx)
|
2014-08-29 02:31:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
if len(services.Items) != 2 || services.Items[0].Name != "foo" || services.Items[1].Name != "bar" {
|
2014-08-29 02:31:41 +00:00
|
|
|
t.Errorf("Unexpected endpoints list: %#v", services)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 19:48:34 +00:00
|
|
|
func TestEtcdGetEndpoints(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-14 19:48:34 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-08-25 21:36:15 +00:00
|
|
|
endpoints := &api.Endpoints{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2015-02-23 21:53:21 +00:00
|
|
|
Protocol: "TCP",
|
|
|
|
Endpoints: []api.Endpoint{{IP: "127.0.0.1", Port: 34855}},
|
2014-08-25 21:36:15 +00:00
|
|
|
}
|
|
|
|
|
2014-10-03 15:44:06 +00:00
|
|
|
key, _ := makeServiceEndpointsKey(ctx, "foo")
|
|
|
|
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, endpoints), 0)
|
2014-08-25 21:36:15 +00:00
|
|
|
|
2014-09-26 19:19:22 +00:00
|
|
|
got, err := registry.GetEndpoints(ctx, "foo")
|
2014-08-14 19:48:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-01-26 17:52:50 +00:00
|
|
|
if e, a := endpoints, got; !api.Semantic.DeepEqual(e, a) {
|
2014-08-25 21:36:15 +00:00
|
|
|
t.Errorf("Unexpected endpoints: %#v, expected %#v", e, a)
|
2014-08-14 19:48:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
func TestEtcdUpdateEndpoints(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-01 21:02:37 +00:00
|
|
|
fakeClient.TestIndex = true
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-06-12 20:17:34 +00:00
|
|
|
endpoints := api.Endpoints{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2015-02-23 21:53:21 +00:00
|
|
|
Protocol: "TCP",
|
|
|
|
Endpoints: []api.Endpoint{{IP: "baz"}, {IP: "bar"}},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-01 21:02:37 +00:00
|
|
|
|
2014-10-03 15:44:06 +00:00
|
|
|
key, _ := makeServiceEndpointsKey(ctx, "foo")
|
|
|
|
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Endpoints{}), 0)
|
2014-08-01 21:02:37 +00:00
|
|
|
|
2014-09-26 19:19:22 +00:00
|
|
|
err := registry.UpdateEndpoints(ctx, &endpoints)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-03 15:44:06 +00:00
|
|
|
response, err := fakeClient.Get(key, false, false)
|
2014-07-23 01:53:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %v", err)
|
|
|
|
}
|
2014-06-12 20:17:34 +00:00
|
|
|
var endpointsOut api.Endpoints
|
2014-09-16 19:56:26 +00:00
|
|
|
err = latest.Codec.DecodeInto([]byte(response.Node.Value), &endpointsOut)
|
2015-01-26 17:52:50 +00:00
|
|
|
if !api.Semantic.DeepEqual(endpoints, endpointsOut) {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Unexpected endpoints: %#v, expected %#v", endpointsOut, endpoints)
|
|
|
|
}
|
|
|
|
}
|
2014-06-27 03:24:10 +00:00
|
|
|
|
2014-08-14 19:48:34 +00:00
|
|
|
func TestEtcdWatchServices(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-14 19:48:34 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-26 19:19:22 +00:00
|
|
|
watching, err := registry.WatchServices(ctx,
|
2014-08-14 19:48:34 +00:00
|
|
|
labels.Everything(),
|
2015-03-06 23:29:03 +00:00
|
|
|
fields.SelectorFromSet(fields.Set{"name": "foo"}),
|
2014-10-07 20:51:28 +00:00
|
|
|
"1",
|
2014-08-14 19:48:34 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
fakeClient.WaitForWatchCompletion()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case _, ok := <-watching.ResultChan():
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("watching channel should be open")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
fakeClient.WatchInjectError <- nil
|
|
|
|
if _, ok := <-watching.ResultChan(); ok {
|
|
|
|
t.Errorf("watching channel should be closed")
|
|
|
|
}
|
|
|
|
watching.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdWatchServicesBadSelector(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-14 19:48:34 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-08-14 19:48:34 +00:00
|
|
|
_, err := registry.WatchServices(
|
2014-09-26 19:19:22 +00:00
|
|
|
ctx,
|
2014-08-14 19:48:34 +00:00
|
|
|
labels.Everything(),
|
2015-03-06 23:29:03 +00:00
|
|
|
fields.SelectorFromSet(fields.Set{"Field.Selector": "foo"}),
|
2014-10-07 20:51:28 +00:00
|
|
|
"",
|
2014-08-14 19:48:34 +00:00
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("unexpected non-error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = registry.WatchServices(
|
2014-09-26 19:19:22 +00:00
|
|
|
ctx,
|
2014-08-14 19:48:34 +00:00
|
|
|
labels.SelectorFromSet(labels.Set{"Label.Selector": "foo"}),
|
2015-03-06 23:29:03 +00:00
|
|
|
fields.Everything(),
|
2014-10-07 20:51:28 +00:00
|
|
|
"",
|
2014-08-14 19:48:34 +00:00
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("unexpected non-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdWatchEndpoints(t *testing.T) {
|
2014-10-03 15:44:06 +00:00
|
|
|
ctx := api.NewDefaultContext()
|
2014-08-14 19:48:34 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-08-14 19:48:34 +00:00
|
|
|
watching, err := registry.WatchEndpoints(
|
2014-09-26 19:19:22 +00:00
|
|
|
ctx,
|
2014-08-14 19:48:34 +00:00
|
|
|
labels.Everything(),
|
2015-03-06 23:29:03 +00:00
|
|
|
fields.SelectorFromSet(fields.Set{"name": "foo"}),
|
2014-10-07 20:51:28 +00:00
|
|
|
"1",
|
2014-08-14 19:48:34 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
fakeClient.WaitForWatchCompletion()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case _, ok := <-watching.ResultChan():
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("watching channel should be open")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
fakeClient.WatchInjectError <- nil
|
|
|
|
if _, ok := <-watching.ResultChan(); ok {
|
|
|
|
t.Errorf("watching channel should be closed")
|
|
|
|
}
|
|
|
|
watching.Stop()
|
|
|
|
}
|
|
|
|
|
2014-10-17 17:18:18 +00:00
|
|
|
func TestEtcdWatchEndpointsAcrossNamespaces(t *testing.T) {
|
|
|
|
ctx := api.NewContext()
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
|
|
|
watching, err := registry.WatchEndpoints(
|
|
|
|
ctx,
|
|
|
|
labels.Everything(),
|
2015-03-06 23:29:03 +00:00
|
|
|
fields.Everything(),
|
2014-10-17 17:18:18 +00:00
|
|
|
"1",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
fakeClient.WaitForWatchCompletion()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case _, ok := <-watching.ResultChan():
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("watching channel should be open")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
fakeClient.WatchInjectError <- nil
|
|
|
|
if _, ok := <-watching.ResultChan(); ok {
|
|
|
|
t.Errorf("watching channel should be closed")
|
|
|
|
}
|
|
|
|
watching.Stop()
|
|
|
|
}
|
|
|
|
|
2014-08-14 19:48:34 +00:00
|
|
|
func TestEtcdWatchEndpointsBadSelector(t *testing.T) {
|
2014-09-26 19:19:22 +00:00
|
|
|
ctx := api.NewContext()
|
2014-08-14 19:48:34 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-08-14 19:48:34 +00:00
|
|
|
_, err := registry.WatchEndpoints(
|
2014-09-26 19:19:22 +00:00
|
|
|
ctx,
|
2014-08-14 19:48:34 +00:00
|
|
|
labels.Everything(),
|
2015-03-06 23:29:03 +00:00
|
|
|
fields.SelectorFromSet(fields.Set{"Field.Selector": "foo"}),
|
2014-10-07 20:51:28 +00:00
|
|
|
"",
|
2014-08-14 19:48:34 +00:00
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("unexpected non-error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = registry.WatchEndpoints(
|
2014-09-26 19:19:22 +00:00
|
|
|
ctx,
|
2014-08-14 19:48:34 +00:00
|
|
|
labels.SelectorFromSet(labels.Set{"Label.Selector": "foo"}),
|
2015-03-06 23:29:03 +00:00
|
|
|
fields.Everything(),
|
2014-10-07 20:51:28 +00:00
|
|
|
"",
|
2014-08-14 19:48:34 +00:00
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("unexpected non-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-09 03:02:21 +00:00
|
|
|
func TestEtcdListMinions(t *testing.T) {
|
|
|
|
ctx := api.NewContext()
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
|
|
|
key := "/registry/minions"
|
|
|
|
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
|
|
|
R: &etcd.Response{
|
|
|
|
Node: &etcd.Node{
|
|
|
|
Nodes: []*etcd.Node{
|
|
|
|
{
|
2014-12-08 03:44:27 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.Node{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-09-09 03:02:21 +00:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
2014-12-08 03:44:27 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.Node{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "bar"},
|
2014-09-09 03:02:21 +00:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
E: nil,
|
|
|
|
}
|
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
|
|
|
minions, err := registry.ListMinions(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
if len(minions.Items) != 2 || minions.Items[0].Name != "foo" || minions.Items[1].Name != "bar" {
|
2014-09-09 03:02:21 +00:00
|
|
|
t.Errorf("Unexpected minion list: %#v", minions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 20:37:46 +00:00
|
|
|
func TestEtcdCreateMinion(t *testing.T) {
|
2014-09-09 03:02:21 +00:00
|
|
|
ctx := api.NewContext()
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-12-08 03:44:27 +00:00
|
|
|
err := registry.CreateMinion(ctx, &api.Node{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-09-09 03:02:21 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := fakeClient.Get("/registry/minions/foo", false, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-12-08 03:44:27 +00:00
|
|
|
var minion api.Node
|
2014-09-09 03:02:21 +00:00
|
|
|
err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &minion)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
if minion.Name != "foo" {
|
2014-09-09 03:02:21 +00:00
|
|
|
t.Errorf("Unexpected minion: %#v %s", minion, resp.Node.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 03:13:04 +00:00
|
|
|
func TestEtcdGetMinion(t *testing.T) {
|
2014-09-09 03:02:21 +00:00
|
|
|
ctx := api.NewContext()
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-12-08 03:44:27 +00:00
|
|
|
fakeClient.Set("/registry/minions/foo", runtime.EncodeOrDie(latest.Codec, &api.Node{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
|
2014-09-09 03:02:21 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-10-09 03:13:04 +00:00
|
|
|
minion, err := registry.GetMinion(ctx, "foo")
|
2014-09-09 03:02:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
if minion.Name != "foo" {
|
2014-10-09 03:13:04 +00:00
|
|
|
t.Errorf("Unexpected minion: %#v", minion)
|
2014-09-09 03:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 03:13:04 +00:00
|
|
|
func TestEtcdGetMinionNotFound(t *testing.T) {
|
2014-09-09 03:02:21 +00:00
|
|
|
ctx := api.NewContext()
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
|
|
|
fakeClient.Data["/registry/minions/foo"] = tools.EtcdResponseWithError{
|
|
|
|
R: &etcd.Response{
|
|
|
|
Node: nil,
|
|
|
|
},
|
|
|
|
E: tools.EtcdErrorNotFound,
|
|
|
|
}
|
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-10-09 03:13:04 +00:00
|
|
|
_, err := registry.GetMinion(ctx, "foo")
|
2014-09-09 03:02:21 +00:00
|
|
|
|
2014-10-09 03:13:04 +00:00
|
|
|
if !errors.IsNotFound(err) {
|
|
|
|
t.Errorf("Unexpected error returned: %#v", err)
|
2014-09-09 03:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdDeleteMinion(t *testing.T) {
|
|
|
|
ctx := api.NewContext()
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2015-03-05 03:34:16 +00:00
|
|
|
key := "/registry/minions/foo"
|
|
|
|
fakeClient.Set("/registry/minions/foo", runtime.EncodeOrDie(latest.Codec, &api.Node{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
|
|
|
|
|
2014-09-09 03:02:21 +00:00
|
|
|
err := registry.DeleteMinion(ctx, "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(fakeClient.DeletedKeys) != 1 {
|
|
|
|
t.Errorf("Expected 1 delete, found %#v", fakeClient.DeletedKeys)
|
|
|
|
}
|
|
|
|
if fakeClient.DeletedKeys[0] != key {
|
|
|
|
t.Errorf("Unexpected key: %s, expected %s", fakeClient.DeletedKeys[0], key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 00:25:23 +00:00
|
|
|
func TestEtcdWatchMinion(t *testing.T) {
|
|
|
|
ctx := api.NewDefaultContext()
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
|
|
|
watching, err := registry.WatchMinions(ctx,
|
|
|
|
labels.Everything(),
|
2015-03-06 23:29:03 +00:00
|
|
|
fields.Everything(),
|
2014-11-26 00:25:23 +00:00
|
|
|
"1",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
fakeClient.WaitForWatchCompletion()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case _, ok := <-watching.ResultChan():
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("watching channel should be open")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
fakeClient.WatchInjectError <- nil
|
|
|
|
if _, ok := <-watching.ResultChan(); ok {
|
|
|
|
t.Errorf("watching channel should be closed")
|
|
|
|
}
|
|
|
|
watching.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdWatchMinionsMatch(t *testing.T) {
|
|
|
|
ctx := api.NewDefaultContext()
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
|
|
|
watching, err := registry.WatchMinions(ctx,
|
|
|
|
labels.SelectorFromSet(labels.Set{"name": "foo"}),
|
2015-03-06 23:29:03 +00:00
|
|
|
fields.Everything(),
|
2014-11-26 00:25:23 +00:00
|
|
|
"1",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
fakeClient.WaitForWatchCompletion()
|
|
|
|
|
|
|
|
node := &api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
nodeBytes, _ := latest.Codec.Encode(node)
|
|
|
|
fakeClient.WatchResponse <- &etcd.Response{
|
|
|
|
Action: "create",
|
|
|
|
Node: &etcd.Node{
|
|
|
|
Value: string(nodeBytes),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case _, ok := <-watching.ResultChan():
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("watching channel should be open")
|
|
|
|
}
|
|
|
|
case <-time.After(time.Millisecond * 100):
|
|
|
|
t.Error("unexpected timeout from result channel")
|
|
|
|
}
|
|
|
|
watching.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdWatchMinionsNotMatch(t *testing.T) {
|
|
|
|
ctx := api.NewDefaultContext()
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
|
|
|
watching, err := registry.WatchMinions(ctx,
|
|
|
|
labels.SelectorFromSet(labels.Set{"name": "foo"}),
|
2015-03-06 23:29:03 +00:00
|
|
|
fields.Everything(),
|
2014-11-26 00:25:23 +00:00
|
|
|
"1",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
fakeClient.WaitForWatchCompletion()
|
|
|
|
|
|
|
|
node := &api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "bar",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
nodeBytes, _ := latest.Codec.Encode(node)
|
|
|
|
fakeClient.WatchResponse <- &etcd.Response{
|
|
|
|
Action: "create",
|
|
|
|
Node: &etcd.Node{
|
|
|
|
Value: string(nodeBytes),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-watching.ResultChan():
|
|
|
|
t.Error("unexpected result from result channel")
|
|
|
|
case <-time.After(time.Millisecond * 100):
|
|
|
|
// expected case
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 03:24:10 +00:00
|
|
|
// TODO We need a test for the compare and swap behavior. This basically requires two things:
|
|
|
|
// 1) Add a per-operation synchronization channel to the fake etcd client, such that any operation waits on that
|
|
|
|
// channel, this will enable us to orchestrate the flow of etcd requests in the test.
|
|
|
|
// 2) We need to make the map from key to (response, error) actually be a [](response, error) and pop
|
|
|
|
// our way through the responses. That will enable us to hand back multiple different responses for
|
|
|
|
// the same key.
|
|
|
|
// Once that infrastructure is in place, the test looks something like:
|
|
|
|
// Routine #1 Routine #2
|
|
|
|
// Read
|
|
|
|
// Wait for sync on update Read
|
|
|
|
// Update
|
|
|
|
// Update
|
|
|
|
// In the buggy case, this will result in lost data. In the correct case, the second update should fail
|
|
|
|
// and be retried.
|