2014-07-15 14:52:39 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
|
2014-09-02 17:55:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-07-15 14:52:39 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
|
|
|
|
"github.com/coreos/go-etcd/etcd"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO(lavalamp): Use the etcd watcher from the tools package, and make sure all test cases here are tested there.
|
|
|
|
|
|
|
|
func TestGetEtcdData(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-07-15 14:52:39 +00:00
|
|
|
ch := make(chan interface{})
|
|
|
|
fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{
|
|
|
|
R: &etcd.Response{
|
|
|
|
Node: &etcd.Node{
|
2014-09-02 17:55:27 +00:00
|
|
|
Value: runtime.EncodeOrDie(&api.ContainerManifestList{
|
2014-07-23 01:53:41 +00:00
|
|
|
Items: []api.ContainerManifest{{ID: "foo"}},
|
|
|
|
}),
|
2014-07-15 14:52:39 +00:00
|
|
|
ModifiedIndex: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
E: nil,
|
|
|
|
}
|
2014-08-19 03:45:37 +00:00
|
|
|
NewSourceEtcd("/registry/hosts/machine/kubelet", fakeClient, ch)
|
2014-07-15 14:52:39 +00:00
|
|
|
|
|
|
|
//TODO: update FakeEtcdClient.Watch to handle receiver=nil with a given index
|
|
|
|
//returns an infinite stream of updates
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
update := (<-ch).(kubelet.PodUpdate)
|
|
|
|
expected := CreatePodUpdate(kubelet.SET, kubelet.Pod{Name: "foo", Manifest: api.ContainerManifest{ID: "foo"}})
|
|
|
|
if !reflect.DeepEqual(expected, update) {
|
|
|
|
t.Errorf("Expected %#v, Got %#v", expected, update)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetEtcdNoData(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-07-15 14:52:39 +00:00
|
|
|
ch := make(chan interface{}, 1)
|
|
|
|
fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{
|
|
|
|
R: &etcd.Response{},
|
|
|
|
E: nil,
|
|
|
|
}
|
2014-08-18 21:42:08 +00:00
|
|
|
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond, time.Minute}
|
2014-07-15 14:52:39 +00:00
|
|
|
_, err := c.fetchNextState(0)
|
2014-07-27 13:30:32 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error")
|
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
expectEmptyChannel(t, ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetEtcd(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-07-15 14:52:39 +00:00
|
|
|
ch := make(chan interface{}, 1)
|
2014-08-03 20:55:34 +00:00
|
|
|
manifest := api.ContainerManifest{ID: "foo", Version: "v1beta1", Containers: []api.Container{{Name: "1", Image: "foo"}}}
|
2014-07-15 14:52:39 +00:00
|
|
|
fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{
|
|
|
|
R: &etcd.Response{
|
|
|
|
Node: &etcd.Node{
|
2014-09-02 17:55:27 +00:00
|
|
|
Value: runtime.EncodeOrDie(&api.ContainerManifestList{
|
2014-08-03 20:55:34 +00:00
|
|
|
Items: []api.ContainerManifest{manifest},
|
2014-07-23 01:53:41 +00:00
|
|
|
}),
|
2014-07-15 14:52:39 +00:00
|
|
|
ModifiedIndex: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
E: nil,
|
|
|
|
}
|
2014-08-18 21:42:08 +00:00
|
|
|
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond, time.Minute}
|
2014-07-15 14:52:39 +00:00
|
|
|
lastIndex, err := c.fetchNextState(0)
|
2014-07-27 13:30:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
2014-07-23 00:21:41 +00:00
|
|
|
if lastIndex != 2 {
|
|
|
|
t.Errorf("Expected %#v, Got %#v", 2, lastIndex)
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
update := (<-ch).(kubelet.PodUpdate)
|
2014-08-03 20:55:34 +00:00
|
|
|
expected := CreatePodUpdate(kubelet.SET, kubelet.Pod{Name: "foo", Manifest: manifest})
|
2014-07-15 14:52:39 +00:00
|
|
|
if !reflect.DeepEqual(expected, update) {
|
|
|
|
t.Errorf("Expected %#v, Got %#v", expected, update)
|
|
|
|
}
|
2014-08-03 20:55:34 +00:00
|
|
|
for i := range update.Pods {
|
|
|
|
if errs := kubelet.ValidatePod(&update.Pods[i]); len(errs) != 0 {
|
|
|
|
t.Errorf("Expected no validation errors on %#v, Got %#v", update.Pods[i], errs)
|
|
|
|
}
|
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchEtcd(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-07-15 14:52:39 +00:00
|
|
|
ch := make(chan interface{}, 1)
|
|
|
|
fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{
|
|
|
|
R: &etcd.Response{
|
|
|
|
Node: &etcd.Node{
|
2014-09-02 17:55:27 +00:00
|
|
|
Value: runtime.EncodeOrDie(&api.ContainerManifestList{}),
|
2014-07-15 14:52:39 +00:00
|
|
|
ModifiedIndex: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
E: nil,
|
|
|
|
}
|
2014-08-18 21:42:08 +00:00
|
|
|
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond, time.Minute}
|
2014-07-15 14:52:39 +00:00
|
|
|
lastIndex, err := c.fetchNextState(1)
|
2014-07-27 13:30:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
2014-07-23 00:21:41 +00:00
|
|
|
if lastIndex != 3 {
|
|
|
|
t.Errorf("Expected %d, Got %d", 3, lastIndex)
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
update := (<-ch).(kubelet.PodUpdate)
|
|
|
|
expected := CreatePodUpdate(kubelet.SET)
|
|
|
|
if !reflect.DeepEqual(expected, update) {
|
|
|
|
t.Errorf("Expected %#v, Got %#v", expected, update)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetEtcdNotFound(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-07-15 14:52:39 +00:00
|
|
|
ch := make(chan interface{}, 1)
|
|
|
|
fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{
|
|
|
|
R: &etcd.Response{},
|
|
|
|
E: tools.EtcdErrorNotFound,
|
|
|
|
}
|
2014-08-18 21:42:08 +00:00
|
|
|
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond, time.Minute}
|
2014-07-15 14:52:39 +00:00
|
|
|
_, err := c.fetchNextState(0)
|
2014-07-27 13:30:32 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error")
|
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
expectEmptyChannel(t, ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetEtcdError(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := tools.NewFakeEtcdClient(t)
|
2014-07-15 14:52:39 +00:00
|
|
|
ch := make(chan interface{}, 1)
|
|
|
|
fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{
|
|
|
|
R: &etcd.Response{},
|
|
|
|
E: &etcd.EtcdError{
|
|
|
|
ErrorCode: 200, // non not found error
|
|
|
|
},
|
|
|
|
}
|
2014-08-18 21:42:08 +00:00
|
|
|
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond, time.Minute}
|
2014-07-15 14:52:39 +00:00
|
|
|
_, err := c.fetchNextState(0)
|
2014-07-27 13:30:32 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error")
|
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
expectEmptyChannel(t, ch)
|
|
|
|
}
|