From fc4a29da2ca5a47cb5c2a1aa5eddc3ad55d0c70b Mon Sep 17 00:00:00 2001 From: Geonju Kim Date: Thu, 11 Feb 2021 16:37:07 +0900 Subject: [PATCH] kubelet: Make the test fail if (*FakeRuntime).Assert fails --- pkg/kubelet/container/testing/fake_runtime.go | 20 +++++++++-------- pkg/kubelet/images/image_manager_test.go | 6 ++--- pkg/kubelet/kubelet_test.go | 22 ++++++++++--------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/pkg/kubelet/container/testing/fake_runtime.go b/pkg/kubelet/container/testing/fake_runtime.go index 6598c727920..4e50b2c53c7 100644 --- a/pkg/kubelet/container/testing/fake_runtime.go +++ b/pkg/kubelet/container/testing/fake_runtime.go @@ -18,11 +18,11 @@ package testing import ( "context" - "fmt" "io" "net/url" "reflect" "sync" + "testing" "time" v1 "k8s.io/api/core/v1" @@ -58,6 +58,7 @@ type FakeRuntime struct { Err error InspectErr error StatusErr error + T *testing.T } const FakeHost = "localhost:12345" @@ -135,39 +136,40 @@ func (f *FakeRuntime) UpdatePodCIDR(c string) error { return nil } -func (f *FakeRuntime) assertList(expect []string, test []string) error { +func (f *FakeRuntime) assertList(expect []string, test []string) bool { if !reflect.DeepEqual(expect, test) { - return fmt.Errorf("expected %#v, got %#v", expect, test) + f.T.Errorf("AssertList: expected %#v, got %#v", expect, test) + return false } - return nil + return true } // AssertCalls test if the invoked functions are as expected. -func (f *FakeRuntime) AssertCalls(calls []string) error { +func (f *FakeRuntime) AssertCalls(calls []string) bool { f.Lock() defer f.Unlock() return f.assertList(calls, f.CalledFunctions) } -func (f *FakeRuntime) AssertStartedPods(pods []string) error { +func (f *FakeRuntime) AssertStartedPods(pods []string) bool { f.Lock() defer f.Unlock() return f.assertList(pods, f.StartedPods) } -func (f *FakeRuntime) AssertKilledPods(pods []string) error { +func (f *FakeRuntime) AssertKilledPods(pods []string) bool { f.Lock() defer f.Unlock() return f.assertList(pods, f.KilledPods) } -func (f *FakeRuntime) AssertStartedContainers(containers []string) error { +func (f *FakeRuntime) AssertStartedContainers(containers []string) bool { f.Lock() defer f.Unlock() return f.assertList(containers, f.StartedContainers) } -func (f *FakeRuntime) AssertKilledContainers(containers []string) error { +func (f *FakeRuntime) AssertKilledContainers(containers []string) bool { f.Lock() defer f.Unlock() return f.assertList(containers, f.KilledContainers) diff --git a/pkg/kubelet/images/image_manager_test.go b/pkg/kubelet/images/image_manager_test.go index 9ce3dcf3ef4..b5b926e0399 100644 --- a/pkg/kubelet/images/image_manager_test.go +++ b/pkg/kubelet/images/image_manager_test.go @@ -201,7 +201,7 @@ func TestParallelPuller(t *testing.T) { fakeRuntime.CalledFunctions = nil fakeClock.Step(time.Second) _, _, err := puller.EnsureImageExists(pod, container, nil, nil) - assert.NoError(t, fakeRuntime.AssertCalls(expected.calls)) + fakeRuntime.AssertCalls(expected.calls) assert.Equal(t, expected.err, err) } }) @@ -229,7 +229,7 @@ func TestSerializedPuller(t *testing.T) { fakeRuntime.CalledFunctions = nil fakeClock.Step(time.Second) _, _, err := puller.EnsureImageExists(pod, container, nil, nil) - assert.NoError(t, fakeRuntime.AssertCalls(expected.calls)) + fakeRuntime.AssertCalls(expected.calls) assert.Equal(t, expected.err, err) } }) @@ -287,7 +287,7 @@ func TestPullAndListImageWithPodAnnotations(t *testing.T) { t.Run(c.testName, func(t *testing.T) { _, _, err := puller.EnsureImageExists(pod, container, nil, nil) - assert.NoError(t, fakeRuntime.AssertCalls(c.expected[0].calls), "tick=%d", 0) + fakeRuntime.AssertCalls(c.expected[0].calls) assert.Equal(t, c.expected[0].err, err, "tick=%d", 0) images, _ := fakeRuntime.ListImages() diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index aaf74b400d0..7bec7e23564 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -144,16 +144,18 @@ func newTestKubeletWithImageList( imageList []kubecontainer.Image, controllerAttachDetachEnabled bool, initFakeVolumePlugin bool) *TestKubelet { - fakeRuntime := &containertest.FakeRuntime{} - fakeRuntime.RuntimeType = "test" - fakeRuntime.VersionInfo = "1.5.0" - fakeRuntime.ImageList = imageList - // Set ready conditions by default. - fakeRuntime.RuntimeStatus = &kubecontainer.RuntimeStatus{ - Conditions: []kubecontainer.RuntimeCondition{ - {Type: "RuntimeReady", Status: true}, - {Type: "NetworkReady", Status: true}, + fakeRuntime := &containertest.FakeRuntime{ + ImageList: imageList, + // Set ready conditions by default. + RuntimeStatus: &kubecontainer.RuntimeStatus{ + Conditions: []kubecontainer.RuntimeCondition{ + {Type: "RuntimeReady", Status: true}, + {Type: "NetworkReady", Status: true}, + }, }, + VersionInfo: "1.5.0", + RuntimeType: "test", + T: t, } fakeRecorder := &record.FakeRecorder{} @@ -658,7 +660,7 @@ func TestKillPodFollwedByIsPodPendingTermination(t *testing.T) { RunningPod: pod, }) - if !(kl.podKiller.IsPodPendingTerminationByUID(pod.ID) || fakeRuntime.AssertKilledPods([]string{"12345678"}) == nil) { + if !(kl.podKiller.IsPodPendingTerminationByUID(pod.ID) || fakeRuntime.AssertKilledPods([]string{"12345678"})) { t.Fatal("Race condition: When KillPod is complete, the pod should be pending termination or be killed") } }