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 (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
2014-06-17 02:10:43 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-09-23 20:43:48 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/pod"
|
2014-08-11 07:34:59 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
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 {
|
2014-09-23 20:43:48 +00:00
|
|
|
registry := NewRegistry(tools.EtcdHelper{client, latest.Codec, latest.ResourceVersioner},
|
|
|
|
&pod.BasicManifestFactory{
|
|
|
|
ServiceRegistry: ®istrytest.ServiceRegistry{},
|
|
|
|
})
|
2014-06-17 23:23:52 +00:00
|
|
|
return registry
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestEtcdGetPod(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-09-11 17:02:53 +00:00
|
|
|
fakeClient.Set("/registry/pods/foo", runtime.EncodeOrDie(latest.Codec, &api.Pod{JSONBase: api.JSONBase{ID: "foo"}}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-06-09 05:38:45 +00:00
|
|
|
pod, err := registry.GetPod("foo")
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
if pod.ID != "foo" {
|
|
|
|
t.Errorf("Unexpected pod: %#v", pod)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestEtcdGetPodNotFound(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-07-22 04:26:03 +00:00
|
|
|
fakeClient.Data["/registry/pods/foo"] = 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-06-09 05:38:45 +00:00
|
|
|
_, err := registry.GetPod("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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestEtcdCreatePod(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-01 21:02:37 +00:00
|
|
|
fakeClient.TestIndex = true
|
2014-07-22 04:26:03 +00:00
|
|
|
fakeClient.Data["/registry/pods/foo"] = 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-09-11 17:02:53 +00:00
|
|
|
fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(latest.Codec, &api.ContainerManifestList{}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-08 04:14:18 +00:00
|
|
|
err := registry.CreatePod(&api.Pod{
|
2014-06-12 20:17:34 +00:00
|
|
|
JSONBase: api.JSONBase{
|
2014-06-06 23:40:48 +00:00
|
|
|
ID: "foo",
|
|
|
|
},
|
2014-06-12 20:17:34 +00:00
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
|
|
|
Containers: []api.Container{
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
2014-06-06 23:40:48 +00:00
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
2014-08-19 23:41:51 +00:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Suddenly, a wild scheduler appears:
|
|
|
|
err = registry.ApplyBinding(&api.Binding{PodID: "foo", Host: "machine"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2014-08-03 23:51:34 +00:00
|
|
|
}
|
|
|
|
|
2014-07-22 04:26:03 +00:00
|
|
|
resp, err := fakeClient.Get("/registry/pods/foo", 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 pod api.Pod
|
2014-09-16 19:56:26 +00:00
|
|
|
err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &pod)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
if pod.ID != "foo" {
|
|
|
|
t.Errorf("Unexpected pod: %#v %s", pod, resp.Node.Value)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-07-23 01:53:41 +00:00
|
|
|
var manifests api.ContainerManifestList
|
2014-06-06 23:40:48 +00:00
|
|
|
resp, err = fakeClient.Get("/registry/hosts/machine/kubelet", false, false)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-09-16 19:56:26 +00:00
|
|
|
err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &manifests)
|
2014-07-23 01:53:41 +00:00
|
|
|
if len(manifests.Items) != 1 || manifests.Items[0].ID != "foo" {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Unexpected manifest list: %#v", manifests)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestEtcdCreatePodAlreadyExisting(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-07-22 04:26:03 +00:00
|
|
|
fakeClient.Data["/registry/pods/foo"] = tools.EtcdResponseWithError{
|
2014-06-06 23:40:48 +00:00
|
|
|
R: &etcd.Response{
|
|
|
|
Node: &etcd.Node{
|
2014-09-11 17:02:53 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{JSONBase: api.JSONBase{ID: "foo"}}),
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
E: nil,
|
|
|
|
}
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-08 04:14:18 +00:00
|
|
|
err := registry.CreatePod(&api.Pod{
|
2014-06-12 20:17:34 +00:00
|
|
|
JSONBase: api.JSONBase{
|
2014-06-06 23:40:48 +00:00
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
})
|
2014-09-05 19:30:35 +00:00
|
|
|
if !errors.IsAlreadyExists(err) {
|
|
|
|
t.Errorf("Unexpected error returned: %#v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestEtcdCreatePodWithContainersError(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-08 20:25:36 +00:00
|
|
|
fakeClient.TestIndex = true
|
2014-07-22 04:26:03 +00:00
|
|
|
fakeClient.Data["/registry/pods/foo"] = 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-06-30 19:00:14 +00:00
|
|
|
fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{
|
2014-06-06 23:40:48 +00:00
|
|
|
R: &etcd.Response{
|
|
|
|
Node: nil,
|
|
|
|
},
|
2014-09-05 19:30:35 +00:00
|
|
|
E: tools.EtcdErrorNodeExist, // validate that ApplyBinding is translating Create errors
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-08 04:14:18 +00:00
|
|
|
err := registry.CreatePod(&api.Pod{
|
2014-06-12 20:17:34 +00:00
|
|
|
JSONBase: api.JSONBase{
|
2014-06-06 23:40:48 +00:00
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
})
|
2014-08-19 23:41:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-19 23:41:51 +00:00
|
|
|
|
|
|
|
// Suddenly, a wild scheduler appears:
|
|
|
|
err = registry.ApplyBinding(&api.Binding{PodID: "foo", Host: "machine"})
|
2014-09-05 19:30:35 +00:00
|
|
|
if !errors.IsAlreadyExists(err) {
|
|
|
|
t.Fatalf("Unexpected error returned: %#v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-19 23:41:51 +00:00
|
|
|
|
|
|
|
existingPod, err := registry.GetPod("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if existingPod.DesiredState.Host == "machine" {
|
2014-09-05 19:30:35 +00:00
|
|
|
t.Fatal("Pod's host changed in response to an non-apply-able binding.")
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestEtcdCreatePodWithContainersNotFound(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-08 20:25:36 +00:00
|
|
|
fakeClient.TestIndex = true
|
2014-07-22 04:26:03 +00:00
|
|
|
fakeClient.Data["/registry/pods/foo"] = 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-06-30 19:00:14 +00:00
|
|
|
fakeClient.Data["/registry/hosts/machine/kubelet"] = 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-08 04:14:18 +00:00
|
|
|
err := registry.CreatePod(&api.Pod{
|
2014-06-12 20:17:34 +00:00
|
|
|
JSONBase: api.JSONBase{
|
2014-06-06 23:40:48 +00:00
|
|
|
ID: "foo",
|
|
|
|
},
|
2014-06-12 20:17:34 +00:00
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
2014-06-30 19:41:48 +00:00
|
|
|
ID: "foo",
|
2014-06-12 20:17:34 +00:00
|
|
|
Containers: []api.Container{
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
2014-06-06 23:40:48 +00:00
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
2014-08-08 20:25:36 +00:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2014-08-03 23:51:34 +00:00
|
|
|
}
|
|
|
|
|
2014-08-19 23:41:51 +00:00
|
|
|
// Suddenly, a wild scheduler appears:
|
|
|
|
err = registry.ApplyBinding(&api.Binding{PodID: "foo", Host: "machine"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-22 04:26:03 +00:00
|
|
|
resp, err := fakeClient.Get("/registry/pods/foo", 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 pod api.Pod
|
2014-09-16 19:56:26 +00:00
|
|
|
err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &pod)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
if pod.ID != "foo" {
|
|
|
|
t.Errorf("Unexpected pod: %#v %s", pod, resp.Node.Value)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-07-23 01:53:41 +00:00
|
|
|
var manifests api.ContainerManifestList
|
2014-06-06 23:40:48 +00:00
|
|
|
resp, err = fakeClient.Get("/registry/hosts/machine/kubelet", false, false)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-09-16 19:56:26 +00:00
|
|
|
err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &manifests)
|
2014-07-23 01:53:41 +00:00
|
|
|
if len(manifests.Items) != 1 || manifests.Items[0].ID != "foo" {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Unexpected manifest list: %#v", manifests)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestEtcdCreatePodWithExistingContainers(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-01 21:02:37 +00:00
|
|
|
fakeClient.TestIndex = true
|
2014-07-22 04:26:03 +00:00
|
|
|
fakeClient.Data["/registry/pods/foo"] = 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-09-11 17:02:53 +00:00
|
|
|
fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(latest.Codec, &api.ContainerManifestList{
|
2014-07-23 01:53:41 +00:00
|
|
|
Items: []api.ContainerManifest{
|
|
|
|
{ID: "bar"},
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-08 04:14:18 +00:00
|
|
|
err := registry.CreatePod(&api.Pod{
|
2014-06-12 20:17:34 +00:00
|
|
|
JSONBase: api.JSONBase{
|
2014-06-06 23:40:48 +00:00
|
|
|
ID: "foo",
|
|
|
|
},
|
2014-06-12 20:17:34 +00:00
|
|
|
DesiredState: api.PodState{
|
|
|
|
Manifest: api.ContainerManifest{
|
2014-06-30 19:41:48 +00:00
|
|
|
ID: "foo",
|
2014-06-12 20:17:34 +00:00
|
|
|
Containers: []api.Container{
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
2014-06-06 23:40:48 +00:00
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
2014-08-19 23:41:51 +00:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Suddenly, a wild scheduler appears:
|
|
|
|
err = registry.ApplyBinding(&api.Binding{PodID: "foo", Host: "machine"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2014-08-03 23:51:34 +00:00
|
|
|
}
|
|
|
|
|
2014-07-22 04:26:03 +00:00
|
|
|
resp, err := fakeClient.Get("/registry/pods/foo", 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 pod api.Pod
|
2014-09-16 19:56:26 +00:00
|
|
|
err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &pod)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
if pod.ID != "foo" {
|
|
|
|
t.Errorf("Unexpected pod: %#v %s", pod, resp.Node.Value)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-07-23 01:53:41 +00:00
|
|
|
var manifests api.ContainerManifestList
|
2014-06-06 23:40:48 +00:00
|
|
|
resp, err = fakeClient.Get("/registry/hosts/machine/kubelet", false, false)
|
2014-08-03 23:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-09-16 19:56:26 +00:00
|
|
|
err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &manifests)
|
2014-07-23 01:53:41 +00:00
|
|
|
if len(manifests.Items) != 2 || manifests.Items[1].ID != "foo" {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Unexpected manifest list: %#v", manifests)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestEtcdDeletePod(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-01 21:02:37 +00:00
|
|
|
fakeClient.TestIndex = true
|
|
|
|
|
2014-07-22 04:26:03 +00:00
|
|
|
key := "/registry/pods/foo"
|
2014-09-11 17:02:53 +00:00
|
|
|
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Pod{
|
2014-07-22 04:26:03 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo"},
|
2014-08-11 22:12:51 +00:00
|
|
|
DesiredState: api.PodState{Host: "machine"},
|
2014-07-22 04:26:03 +00:00
|
|
|
}), 0)
|
2014-09-11 17:02:53 +00:00
|
|
|
fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(latest.Codec, &api.ContainerManifestList{
|
2014-07-23 01:53:41 +00:00
|
|
|
Items: []api.ContainerManifest{
|
|
|
|
{ID: "foo"},
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-06-09 05:38:45 +00:00
|
|
|
err := registry.DeletePod("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-27 19:54:45 +00:00
|
|
|
} else if fakeClient.DeletedKeys[0] != key {
|
2014-06-17 23:23:52 +00:00
|
|
|
t.Errorf("Unexpected key: %s, expected %s", fakeClient.DeletedKeys[0], key)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-07-23 01:53:41 +00:00
|
|
|
response, err := fakeClient.Get("/registry/hosts/machine/kubelet", false, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
var manifests api.ContainerManifestList
|
2014-09-16 19:56:26 +00:00
|
|
|
latest.Codec.DecodeInto([]byte(response.Node.Value), &manifests)
|
2014-07-23 01:53:41 +00:00
|
|
|
if len(manifests.Items) != 0 {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Unexpected container set: %s, expected empty", response.Node.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestEtcdDeletePodMultipleContainers(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-01 21:02:37 +00:00
|
|
|
fakeClient.TestIndex = true
|
|
|
|
|
2014-07-22 04:26:03 +00:00
|
|
|
key := "/registry/pods/foo"
|
2014-09-11 17:02:53 +00:00
|
|
|
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Pod{
|
2014-07-22 04:26:03 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo"},
|
2014-08-11 22:12:51 +00:00
|
|
|
DesiredState: api.PodState{Host: "machine"},
|
2014-07-22 04:26:03 +00:00
|
|
|
}), 0)
|
2014-09-11 17:02:53 +00:00
|
|
|
fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(latest.Codec, &api.ContainerManifestList{
|
2014-07-23 01:53:41 +00:00
|
|
|
Items: []api.ContainerManifest{
|
|
|
|
{ID: "foo"},
|
|
|
|
{ID: "bar"},
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-06-09 05:38:45 +00:00
|
|
|
err := registry.DeletePod("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
|
|
|
}
|
2014-07-23 01:53:41 +00:00
|
|
|
response, err := fakeClient.Get("/registry/hosts/machine/kubelet", false, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-07-23 01:53:41 +00:00
|
|
|
var manifests api.ContainerManifestList
|
2014-09-16 19:56:26 +00:00
|
|
|
latest.Codec.DecodeInto([]byte(response.Node.Value), &manifests)
|
2014-07-23 01:53:41 +00:00
|
|
|
if len(manifests.Items) != 1 {
|
|
|
|
t.Fatalf("Unexpected manifest set: %#v, expected empty", manifests)
|
|
|
|
}
|
|
|
|
if manifests.Items[0].ID != "bar" {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Deleted wrong manifest: %#v", manifests)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestEtcdEmptyListPods(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-07-22 04:26:03 +00:00
|
|
|
key := "/registry/pods"
|
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{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
E: nil,
|
|
|
|
}
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-25 18:35:10 +00:00
|
|
|
ctx := api.NewContext()
|
|
|
|
pods, err := registry.ListPods(ctx, labels.Everything())
|
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(pods.Items) != 0 {
|
2014-06-09 05:38:45 +00:00
|
|
|
t.Errorf("Unexpected pod list: %#v", pods)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestEtcdListPodsNotFound(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-07-22 04:26:03 +00:00
|
|
|
key := "/registry/pods"
|
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-25 18:35:10 +00:00
|
|
|
ctx := api.NewContext()
|
|
|
|
pods, err := registry.ListPods(ctx, labels.Everything())
|
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(pods.Items) != 0 {
|
2014-06-09 05:38:45 +00:00
|
|
|
t.Errorf("Unexpected pod list: %#v", pods)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
func TestEtcdListPods(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-07-22 04:26:03 +00:00
|
|
|
key := "/registry/pods"
|
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-09-11 17:02:53 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{
|
2014-07-22 04:26:03 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo"},
|
2014-08-11 22:12:51 +00:00
|
|
|
DesiredState: api.PodState{Host: "machine"},
|
2014-07-22 04:26:03 +00:00
|
|
|
}),
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
2014-09-11 17:02:53 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{
|
2014-07-22 04:26:03 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "bar"},
|
2014-08-11 22:12:51 +00:00
|
|
|
DesiredState: api.PodState{Host: "machine"},
|
2014-07-22 04:26:03 +00:00
|
|
|
}),
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
E: nil,
|
|
|
|
}
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-25 18:35:10 +00:00
|
|
|
ctx := api.NewContext()
|
|
|
|
pods, err := registry.ListPods(ctx, labels.Everything())
|
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(pods.Items) != 2 || pods.Items[0].ID != "foo" || pods.Items[1].ID != "bar" {
|
2014-06-09 05:38:45 +00:00
|
|
|
t.Errorf("Unexpected pod list: %#v", pods)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-08-29 00:48:07 +00:00
|
|
|
if pods.Items[0].CurrentState.Host != "machine" ||
|
|
|
|
pods.Items[1].CurrentState.Host != "machine" {
|
2014-06-18 17:37:27 +00:00
|
|
|
t.Errorf("Failed to populate host name.")
|
|
|
|
}
|
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-06-06 23:40:48 +00:00
|
|
|
key := "/registry/controllers"
|
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-06-06 23:40:48 +00:00
|
|
|
controllers, err := registry.ListControllers()
|
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-06-06 23:40:48 +00:00
|
|
|
key := "/registry/services/specs"
|
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-06-06 23:40:48 +00:00
|
|
|
services, err := registry.ListServices()
|
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-06-06 23:40:48 +00:00
|
|
|
key := "/registry/controllers"
|
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-09-11 17:02:53 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}),
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
2014-09-11 17:02:53 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{JSONBase: api.JSONBase{ID: "bar"}}),
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
E: nil,
|
|
|
|
}
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-06-06 23:40:48 +00:00
|
|
|
controllers, err := registry.ListControllers()
|
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) != 2 || controllers.Items[0].ID != "foo" || controllers.Items[1].ID != "bar" {
|
2014-06-06 23:40:48 +00:00
|
|
|
t.Errorf("Unexpected controller list: %#v", controllers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdGetController(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-09-11 17:02:53 +00:00
|
|
|
fakeClient.Set("/registry/controllers/foo", runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-06-06 23:40:48 +00:00
|
|
|
ctrl, err := registry.GetController("foo")
|
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 ctrl.ID != "foo" {
|
|
|
|
t.Errorf("Unexpected controller: %#v", ctrl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdGetControllerNotFound(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-06-30 19:00:14 +00:00
|
|
|
fakeClient.Data["/registry/controllers/foo"] = 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-06-06 23:40:48 +00:00
|
|
|
ctrl, err := registry.GetController("foo")
|
|
|
|
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-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-06-06 23:40:48 +00:00
|
|
|
err := registry.DeleteController("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
|
|
|
}
|
|
|
|
key := "/registry/controllers/foo"
|
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-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-08 04:14:18 +00:00
|
|
|
err := registry.CreateController(&api.ReplicationController{
|
2014-06-12 20:17:34 +00:00
|
|
|
JSONBase: api.JSONBase{
|
2014-06-06 23:40:48 +00:00
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
})
|
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
|
|
|
resp, err := fakeClient.Get("/registry/controllers/foo", 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-06-06 23:40:48 +00:00
|
|
|
if ctrl.ID != "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-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-09-11 17:02:53 +00:00
|
|
|
fakeClient.Set("/registry/controllers/foo", runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0)
|
2014-08-03 17:07:40 +00:00
|
|
|
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-08 04:14:18 +00:00
|
|
|
err := registry.CreateController(&api.ReplicationController{
|
2014-08-03 17:07:40 +00:00
|
|
|
JSONBase: api.JSONBase{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
})
|
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-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-01 21:02:37 +00:00
|
|
|
fakeClient.TestIndex = true
|
|
|
|
|
2014-09-11 17:02:53 +00:00
|
|
|
resp, _ := fakeClient.Set("/registry/controllers/foo", runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-08 04:14:18 +00:00
|
|
|
err := registry.UpdateController(&api.ReplicationController{
|
2014-08-01 21:02:37 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo", ResourceVersion: resp.Node.ModifiedIndex},
|
2014-06-12 20:17:34 +00:00
|
|
|
DesiredState: api.ReplicationControllerState{
|
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-06-06 23:40:48 +00:00
|
|
|
ctrl, err := registry.GetController("foo")
|
|
|
|
if ctrl.DesiredState.Replicas != 2 {
|
|
|
|
t.Errorf("Unexpected controller: %#v", ctrl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdListServices(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-06-06 23:40:48 +00:00
|
|
|
key := "/registry/services/specs"
|
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-09-11 17:02:53 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.Service{JSONBase: api.JSONBase{ID: "foo"}}),
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
2014-06-12 21:09:40 +00:00
|
|
|
{
|
2014-09-11 17:02:53 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.Service{JSONBase: api.JSONBase{ID: "bar"}}),
|
2014-06-06 23:40:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
E: nil,
|
|
|
|
}
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-06-06 23:40:48 +00:00
|
|
|
services, err := registry.ListServices()
|
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) != 2 || services.Items[0].ID != "foo" || services.Items[1].ID != "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-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-08 04:14:18 +00:00
|
|
|
err := registry.CreateService(&api.Service{
|
2014-06-12 20:17:34 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "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-06-06 23:40:48 +00:00
|
|
|
resp, err := fakeClient.Get("/registry/services/specs/foo", 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-06-06 23:40:48 +00:00
|
|
|
if service.ID != "foo" {
|
|
|
|
t.Errorf("Unexpected service: %#v %s", service, resp.Node.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 17:07:40 +00:00
|
|
|
func TestEtcdCreateServiceAlreadyExisting(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-09-11 17:02:53 +00:00
|
|
|
fakeClient.Set("/registry/services/specs/foo", runtime.EncodeOrDie(latest.Codec, &api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-09-08 04:14:18 +00:00
|
|
|
err := registry.CreateService(&api.Service{
|
2014-08-03 17:07:40 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo"},
|
|
|
|
})
|
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 TestEtcdGetService(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-09-11 17:02:53 +00:00
|
|
|
fakeClient.Set("/registry/services/specs/foo", runtime.EncodeOrDie(latest.Codec, &api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-06-06 23:40:48 +00:00
|
|
|
service, err := registry.GetService("foo")
|
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 service.ID != "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-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-06-30 19:00:14 +00:00
|
|
|
fakeClient.Data["/registry/services/specs/foo"] = 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-06-06 23:40:48 +00:00
|
|
|
_, err := registry.GetService("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-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-06-06 23:40:48 +00:00
|
|
|
err := registry.DeleteService("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
|
|
|
}
|
|
|
|
key := "/registry/services/specs/foo"
|
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
|
|
|
}
|
|
|
|
key = "/registry/services/endpoints/foo"
|
2014-06-17 23:23:52 +00:00
|
|
|
if fakeClient.DeletedKeys[1] != key {
|
|
|
|
t.Errorf("Unexpected key: %s, expected %s", fakeClient.DeletedKeys[1], key)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdUpdateService(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-08-01 21:02:37 +00:00
|
|
|
fakeClient.TestIndex = true
|
|
|
|
|
2014-09-11 17:02:53 +00:00
|
|
|
resp, _ := fakeClient.Set("/registry/services/specs/foo", runtime.EncodeOrDie(latest.Codec, &api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0)
|
2014-08-28 23:12:14 +00:00
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
2014-06-18 23:01:49 +00:00
|
|
|
testService := api.Service{
|
2014-08-01 21:02:37 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo", ResourceVersion: resp.Node.ModifiedIndex},
|
2014-06-06 23:40:48 +00:00
|
|
|
Labels: map[string]string{
|
|
|
|
"baz": "bar",
|
|
|
|
},
|
2014-06-18 23:01:49 +00:00
|
|
|
Selector: map[string]string{
|
|
|
|
"baz": "bar",
|
|
|
|
},
|
|
|
|
}
|
2014-09-08 04:14:18 +00:00
|
|
|
err := registry.UpdateService(&testService)
|
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
|
|
|
svc, err := registry.GetService("foo")
|
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.
|
|
|
|
svc.ResourceVersion = 0
|
|
|
|
testService.ResourceVersion = 0
|
2014-06-18 23:01:49 +00:00
|
|
|
if !reflect.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) {
|
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
|
|
|
key := "/registry/services/endpoints"
|
|
|
|
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
|
|
|
R: &etcd.Response{
|
|
|
|
Node: &etcd.Node{
|
|
|
|
Nodes: []*etcd.Node{
|
|
|
|
{
|
2014-09-11 17:02:53 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.Endpoints{JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{"127.0.0.1:8345"}}),
|
2014-08-29 02:31:41 +00:00
|
|
|
},
|
|
|
|
{
|
2014-09-11 17:02:53 +00:00
|
|
|
Value: runtime.EncodeOrDie(latest.Codec, &api.Endpoints{JSONBase: api.JSONBase{ID: "bar"}}),
|
2014-08-29 02:31:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
E: nil,
|
|
|
|
}
|
|
|
|
registry := NewTestEtcdRegistry(fakeClient)
|
|
|
|
services, err := registry.ListEndpoints()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(services.Items) != 2 || services.Items[0].ID != "foo" || services.Items[1].ID != "bar" {
|
|
|
|
t.Errorf("Unexpected endpoints list: %#v", services)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 19:48:34 +00:00
|
|
|
func TestEtcdGetEndpoints(t *testing.T) {
|
|
|
|
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-08-14 19:48:34 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo"},
|
|
|
|
Endpoints: []string{"127.0.0.1:34855"},
|
2014-08-25 21:36:15 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 17:02:53 +00:00
|
|
|
fakeClient.Set("/registry/services/endpoints/foo", runtime.EncodeOrDie(latest.Codec, endpoints), 0)
|
2014-08-25 21:36:15 +00:00
|
|
|
|
|
|
|
got, err := registry.GetEndpoints("foo")
|
2014-08-14 19:48:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-08-25 21:36:15 +00:00
|
|
|
if e, a := endpoints, got; !reflect.DeepEqual(e, a) {
|
|
|
|
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-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-07-23 01:53:41 +00:00
|
|
|
JSONBase: api.JSONBase{ID: "foo"},
|
2014-06-06 23:40:48 +00:00
|
|
|
Endpoints: []string{"baz", "bar"},
|
|
|
|
}
|
2014-08-01 21:02:37 +00:00
|
|
|
|
2014-09-11 17:02:53 +00:00
|
|
|
fakeClient.Set("/registry/services/endpoints/foo", runtime.EncodeOrDie(latest.Codec, &api.Endpoints{}), 0)
|
2014-08-01 21:02:37 +00:00
|
|
|
|
2014-09-08 04:14:18 +00:00
|
|
|
err := registry.UpdateEndpoints(&endpoints)
|
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
|
|
|
response, err := fakeClient.Get("/registry/services/endpoints/foo", 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)
|
2014-06-06 23:40:48 +00:00
|
|
|
if !reflect.DeepEqual(endpoints, endpointsOut) {
|
|
|
|
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) {
|
|
|
|
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.WatchServices(
|
|
|
|
labels.Everything(),
|
|
|
|
labels.SelectorFromSet(labels.Set{"ID": "foo"}),
|
|
|
|
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 TestEtcdWatchServicesBadSelector(t *testing.T) {
|
|
|
|
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(
|
|
|
|
labels.Everything(),
|
|
|
|
labels.SelectorFromSet(labels.Set{"Field.Selector": "foo"}),
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("unexpected non-error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = registry.WatchServices(
|
|
|
|
labels.SelectorFromSet(labels.Set{"Label.Selector": "foo"}),
|
|
|
|
labels.Everything(),
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("unexpected non-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdWatchEndpoints(t *testing.T) {
|
|
|
|
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(
|
|
|
|
labels.Everything(),
|
|
|
|
labels.SelectorFromSet(labels.Set{"ID": "foo"}),
|
|
|
|
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 TestEtcdWatchEndpointsBadSelector(t *testing.T) {
|
|
|
|
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(
|
|
|
|
labels.Everything(),
|
|
|
|
labels.SelectorFromSet(labels.Set{"Field.Selector": "foo"}),
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("unexpected non-error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = registry.WatchEndpoints(
|
|
|
|
labels.SelectorFromSet(labels.Set{"Label.Selector": "foo"}),
|
|
|
|
labels.Everything(),
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("unexpected non-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|