From 3ef73bdb55f8e3c390da4318848a7d2f07688820 Mon Sep 17 00:00:00 2001 From: zhangxiaoyu-zidif Date: Mon, 5 Jun 2017 17:56:34 +0800 Subject: [PATCH] Add unittest for PodList --- pkg/printers/internalversion/printers_test.go | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/pkg/printers/internalversion/printers_test.go b/pkg/printers/internalversion/printers_test.go index 8d4c55e6c9b..d95346133e0 100644 --- a/pkg/printers/internalversion/printers_test.go +++ b/pkg/printers/internalversion/printers_test.go @@ -1459,6 +1459,57 @@ func TestPrintPod(t *testing.T) { } } +func TestPrintPodList(t *testing.T) { + tests := []struct { + pods api.PodList + expect []metav1alpha1.TableRow + }{ + // Test podList's pod: name, num of containers, restarts, container ready status + { + api.PodList{ + Items: []api.Pod{ + { + ObjectMeta: metav1.ObjectMeta{Name: "test1"}, + Spec: api.PodSpec{Containers: make([]api.Container, 2)}, + Status: api.PodStatus{ + Phase: "podPhase", + ContainerStatuses: []api.ContainerStatus{ + {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}}, + {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}}, + }, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{Name: "test2"}, + Spec: api.PodSpec{Containers: make([]api.Container, 1)}, + Status: api.PodStatus{ + Phase: "podPhase", + ContainerStatuses: []api.ContainerStatus{ + {Ready: true, RestartCount: 1, State: api.ContainerState{Running: &api.ContainerStateRunning{}}}, + }, + }, + }, + }, + }, + []metav1alpha1.TableRow{{Cells: []interface{}{"test1", "2/2", "podPhase", 6, ""}}, {Cells: []interface{}{"test2", "1/1", "podPhase", 1, ""}}}, + }, + } + + for _, test := range tests { + rows, err := printPodList(&test.pods, printers.PrintOptions{ShowAll: true}) + + if err != nil { + t.Fatal(err) + } + for i := range rows { + rows[i].Object.Object = nil + } + if !reflect.DeepEqual(test.expect, rows) { + t.Errorf("mismatch: %s", diff.ObjectReflectDiff(test.expect, rows)) + } + } +} + func TestPrintNonTerminatedPod(t *testing.T) { tests := []struct { pod api.Pod