mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-12-07 09:43:15 +00:00
The next index to watch should always be current+1 if we got a response from etcd, but otherwise it should always be current. Moved the increment into fetchNextState which returns the next index to watch for (or the same if an error occurs)
148 lines
4.7 KiB
Go
148 lines
4.7 KiB
Go
/*
|
|
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"
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
"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) {
|
|
fakeClient := tools.MakeFakeEtcdClient(t)
|
|
ch := make(chan interface{})
|
|
fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{
|
|
R: &etcd.Response{
|
|
Node: &etcd.Node{
|
|
Value: util.MakeJSONString([]api.ContainerManifest{api.ContainerManifest{ID: "foo"}}),
|
|
ModifiedIndex: 1,
|
|
},
|
|
},
|
|
E: nil,
|
|
}
|
|
NewSourceEtcd("/registry/hosts/machine/kubelet", fakeClient, time.Millisecond, ch)
|
|
|
|
//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) {
|
|
fakeClient := tools.MakeFakeEtcdClient(t)
|
|
ch := make(chan interface{}, 1)
|
|
fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{
|
|
R: &etcd.Response{},
|
|
E: nil,
|
|
}
|
|
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond}
|
|
_, err := c.fetchNextState(0)
|
|
expectError(t, err)
|
|
expectEmptyChannel(t, ch)
|
|
}
|
|
|
|
func TestGetEtcd(t *testing.T) {
|
|
fakeClient := tools.MakeFakeEtcdClient(t)
|
|
ch := make(chan interface{}, 1)
|
|
fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{
|
|
R: &etcd.Response{
|
|
Node: &etcd.Node{
|
|
Value: util.MakeJSONString([]api.ContainerManifest{api.ContainerManifest{ID: "foo"}}),
|
|
ModifiedIndex: 1,
|
|
},
|
|
},
|
|
E: nil,
|
|
}
|
|
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond}
|
|
lastIndex, err := c.fetchNextState(0)
|
|
expectNoError(t, err)
|
|
if lastIndex != 2 {
|
|
t.Errorf("Expected %#v, Got %#v", 2, lastIndex)
|
|
}
|
|
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 TestWatchEtcd(t *testing.T) {
|
|
fakeClient := tools.MakeFakeEtcdClient(t)
|
|
ch := make(chan interface{}, 1)
|
|
fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{
|
|
R: &etcd.Response{
|
|
Node: &etcd.Node{
|
|
Value: util.MakeJSONString([]api.Container{}),
|
|
ModifiedIndex: 2,
|
|
},
|
|
},
|
|
E: nil,
|
|
}
|
|
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond}
|
|
lastIndex, err := c.fetchNextState(1)
|
|
expectNoError(t, err)
|
|
if lastIndex != 3 {
|
|
t.Errorf("Expected %d, Got %d", 3, lastIndex)
|
|
}
|
|
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) {
|
|
fakeClient := tools.MakeFakeEtcdClient(t)
|
|
ch := make(chan interface{}, 1)
|
|
fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{
|
|
R: &etcd.Response{},
|
|
E: tools.EtcdErrorNotFound,
|
|
}
|
|
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond}
|
|
_, err := c.fetchNextState(0)
|
|
expectError(t, err)
|
|
expectEmptyChannel(t, ch)
|
|
}
|
|
|
|
func TestGetEtcdError(t *testing.T) {
|
|
fakeClient := tools.MakeFakeEtcdClient(t)
|
|
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
|
|
},
|
|
}
|
|
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond}
|
|
_, err := c.fetchNextState(0)
|
|
expectError(t, err)
|
|
expectEmptyChannel(t, ch)
|
|
}
|