From c72c2a0d1e654d9b6c418a7f4fc5a5e827b0e18c Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Sun, 8 Feb 2015 13:22:19 -0800 Subject: [PATCH] added tests --- pkg/kubelet/kubelet_test.go | 167 ++++++++++++++++++++++++++++++------ pkg/kubelet/probe_test.go | 114 ++++++++++++++++++++++++ 2 files changed, 256 insertions(+), 25 deletions(-) diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index 52cd14865a0..ffc329a9f01 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -255,31 +255,7 @@ func TestKubeletDirsCompat(t *testing.T) { } func TestKillContainerWithError(t *testing.T) { - fakeDocker := &dockertools.FakeDockerClient{ - Err: fmt.Errorf("sample error"), - ContainerList: []docker.APIContainers{ - { - ID: "1234", - Names: []string{"/k8s_foo_qux_1234_42"}, - }, - { - ID: "5678", - Names: []string{"/k8s_bar_qux_5678_42"}, - }, - }, - } - kubelet, _ := newTestKubelet(t) - kubelet.dockerClient = fakeDocker - err := kubelet.killContainer(&fakeDocker.ContainerList[0]) - if err == nil { - t.Errorf("expected error, found nil") - } - verifyCalls(t, fakeDocker, []string{"stop"}) -} - -func TestKillContainer(t *testing.T) { - kubelet, fakeDocker := newTestKubelet(t) - fakeDocker.ContainerList = []docker.APIContainers{ + containers := []docker.APIContainers{ { ID: "1234", Names: []string{"/k8s_foo_qux_1234_42"}, @@ -289,15 +265,63 @@ func TestKillContainer(t *testing.T) { Names: []string{"/k8s_bar_qux_5678_42"}, }, } + fakeDocker := &dockertools.FakeDockerClient{ + Err: fmt.Errorf("sample error"), + ContainerList: append([]docker.APIContainers{}, containers...), + } + kubelet, _ := newTestKubelet(t) + for _, c := range fakeDocker.ContainerList { + kubelet.readiness.set(c.ID, true) + } + kubelet.dockerClient = fakeDocker + err := kubelet.killContainer(&fakeDocker.ContainerList[0]) + if err == nil { + t.Errorf("expected error, found nil") + } + verifyCalls(t, fakeDocker, []string{"stop"}) + killedContainer := containers[0] + liveContainer := containers[1] + if _, found := kubelet.readiness.states[killedContainer.ID]; found { + t.Errorf("exepcted container entry ID '%v' to not be found. states: %+v", killedContainer.ID, kubelet.readiness.states) + } + if _, found := kubelet.readiness.states[liveContainer.ID]; !found { + t.Errorf("exepcted container entry ID '%v' to be found. states: %+v", liveContainer.ID, kubelet.readiness.states) + } +} + +func TestKillContainer(t *testing.T) { + containers := []docker.APIContainers{ + { + ID: "1234", + Names: []string{"/k8s_foo_qux_1234_42"}, + }, + { + ID: "5678", + Names: []string{"/k8s_bar_qux_5678_42"}, + }, + } + kubelet, fakeDocker := newTestKubelet(t) + fakeDocker.ContainerList = append([]docker.APIContainers{}, containers...) fakeDocker.Container = &docker.Container{ Name: "foobar", } + for _, c := range fakeDocker.ContainerList { + kubelet.readiness.set(c.ID, true) + } err := kubelet.killContainer(&fakeDocker.ContainerList[0]) if err != nil { t.Errorf("unexpected error: %v", err) } verifyCalls(t, fakeDocker, []string{"stop"}) + killedContainer := containers[0] + liveContainer := containers[1] + if _, found := kubelet.readiness.states[killedContainer.ID]; found { + t.Errorf("exepcted container entry ID '%v' to not be found. states: %+v", killedContainer.ID, kubelet.readiness.states) + } + if _, found := kubelet.readiness.states[liveContainer.ID]; !found { + t.Errorf("exepcted container entry ID '%v' to be found. states: %+v", liveContainer.ID, kubelet.readiness.states) + } } type channelReader struct { @@ -2560,3 +2584,96 @@ func TestPodPhaseWithRestartOnFailure(t *testing.T) { } } } + +func TestGetPodReadyCondition(t *testing.T) { + ready := []api.PodCondition{{ + Kind: api.PodReady, + Status: api.ConditionFull, + }} + unready := []api.PodCondition{{ + Kind: api.PodReady, + Status: api.ConditionNone, + }} + tests := []struct { + spec *api.PodSpec + info api.PodInfo + expected []api.PodCondition + }{ + { + spec: nil, + info: nil, + expected: unready, + }, + { + spec: &api.PodSpec{}, + info: api.PodInfo{}, + expected: ready, + }, + { + spec: &api.PodSpec{ + Containers: []api.Container{ + {Name: "1234"}, + }, + }, + info: api.PodInfo{}, + expected: unready, + }, + { + spec: &api.PodSpec{ + Containers: []api.Container{ + {Name: "1234"}, + }, + }, + info: api.PodInfo{ + "1234": api.ContainerStatus{Ready: true}, + }, + expected: ready, + }, + { + spec: &api.PodSpec{ + Containers: []api.Container{ + {Name: "1234"}, + {Name: "5678"}, + }, + }, + info: api.PodInfo{ + "1234": api.ContainerStatus{Ready: true}, + "5678": api.ContainerStatus{Ready: true}, + }, + expected: ready, + }, + { + spec: &api.PodSpec{ + Containers: []api.Container{ + {Name: "1234"}, + {Name: "5678"}, + }, + }, + info: api.PodInfo{ + "1234": api.ContainerStatus{Ready: true}, + }, + expected: unready, + }, + { + spec: &api.PodSpec{ + Containers: []api.Container{ + {Name: "1234"}, + {Name: "5678"}, + }, + }, + info: api.PodInfo{ + "1234": api.ContainerStatus{Ready: true}, + "5678": api.ContainerStatus{Ready: false}, + }, + expected: unready, + }, + } + + for i, test := range tests { + condition := getPodReadyCondition(test.spec, test.info) + if !reflect.DeepEqual(condition, test.expected) { + t.Errorf("On test case %v, expected:\n%+v\ngot\n%+v\n", i, test.expected, condition) + } + } + +} diff --git a/pkg/kubelet/probe_test.go b/pkg/kubelet/probe_test.go index e7207777daf..4a22e5e8a64 100644 --- a/pkg/kubelet/probe_test.go +++ b/pkg/kubelet/probe_test.go @@ -17,10 +17,17 @@ limitations under the License. package kubelet import ( + "errors" "testing" + "time" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" + "github.com/GoogleCloudPlatform/kubernetes/pkg/probe" + "github.com/GoogleCloudPlatform/kubernetes/pkg/types" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" + "github.com/GoogleCloudPlatform/kubernetes/pkg/util/exec" + + "github.com/fsouza/go-dockerclient" ) func TestFindPortByName(t *testing.T) { @@ -128,3 +135,110 @@ func TestGetTCPAddrParts(t *testing.T) { } } } + +type fakeExecProber struct { + result probe.Result + err error +} + +func (p fakeExecProber) Probe(_ exec.Cmd) (probe.Result, error) { + return p.result, p.err +} + +func makeTestKubelet(result probe.Result, err error) *Kubelet { + return &Kubelet{ + prober: probeHolder{ + exec: fakeExecProber{ + result: result, + err: err, + }, + }, + } +} + +func TestProbeContainer(t *testing.T) { + dc := &docker.APIContainers{Created: time.Now().Unix()} + tests := []struct { + p *api.Probe + defaultResult probe.Result + expectError bool + expectedResult probe.Result + }{ + { + defaultResult: probe.Success, + expectedResult: probe.Success, + }, + { + defaultResult: probe.Failure, + expectedResult: probe.Success, + }, + { + p: &api.Probe{InitialDelaySeconds: 100}, + defaultResult: probe.Failure, + expectError: false, + expectedResult: probe.Failure, + }, + { + p: &api.Probe{ + InitialDelaySeconds: -100, + }, + defaultResult: probe.Failure, + expectError: false, + expectedResult: probe.Unknown, + }, + { + p: &api.Probe{ + InitialDelaySeconds: -100, + Handler: api.Handler{ + Exec: &api.ExecAction{}, + }, + }, + defaultResult: probe.Failure, + expectError: false, + expectedResult: probe.Success, + }, + { + p: &api.Probe{ + InitialDelaySeconds: -100, + Handler: api.Handler{ + Exec: &api.ExecAction{}, + }, + }, + defaultResult: probe.Failure, + expectError: true, + expectedResult: probe.Unknown, + }, + { + p: &api.Probe{ + InitialDelaySeconds: -100, + Handler: api.Handler{ + Exec: &api.ExecAction{}, + }, + }, + defaultResult: probe.Success, + expectError: false, + expectedResult: probe.Failure, + }, + } + + for _, test := range tests { + var kl *Kubelet + + if test.expectError { + kl = makeTestKubelet(test.expectedResult, errors.New("error")) + } else { + kl = makeTestKubelet(test.expectedResult, nil) + } + + result, err := kl.probeContainer(test.p, "", types.UID(""), api.PodStatus{}, api.Container{}, dc, test.defaultResult) + if test.expectError && err == nil { + t.Error("Expected error but did no error was returned.") + } + if !test.expectError && err != nil { + t.Errorf("Expected error but got: %v", err) + } + if test.expectedResult != result { + t.Errorf("Expected result was %v but probeContainer() returned %v", test.expectedResult, result) + } + } +}