kubelet: Make the test fail if (*FakeRuntime).Assert fails

This commit is contained in:
Geonju Kim 2021-02-11 16:37:07 +09:00
parent c198632a12
commit fc4a29da2c
3 changed files with 26 additions and 22 deletions

View File

@ -18,11 +18,11 @@ package testing
import ( import (
"context" "context"
"fmt"
"io" "io"
"net/url" "net/url"
"reflect" "reflect"
"sync" "sync"
"testing"
"time" "time"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
@ -58,6 +58,7 @@ type FakeRuntime struct {
Err error Err error
InspectErr error InspectErr error
StatusErr error StatusErr error
T *testing.T
} }
const FakeHost = "localhost:12345" const FakeHost = "localhost:12345"
@ -135,39 +136,40 @@ func (f *FakeRuntime) UpdatePodCIDR(c string) error {
return nil 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) { 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. // 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() f.Lock()
defer f.Unlock() defer f.Unlock()
return f.assertList(calls, f.CalledFunctions) return f.assertList(calls, f.CalledFunctions)
} }
func (f *FakeRuntime) AssertStartedPods(pods []string) error { func (f *FakeRuntime) AssertStartedPods(pods []string) bool {
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
return f.assertList(pods, f.StartedPods) return f.assertList(pods, f.StartedPods)
} }
func (f *FakeRuntime) AssertKilledPods(pods []string) error { func (f *FakeRuntime) AssertKilledPods(pods []string) bool {
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
return f.assertList(pods, f.KilledPods) return f.assertList(pods, f.KilledPods)
} }
func (f *FakeRuntime) AssertStartedContainers(containers []string) error { func (f *FakeRuntime) AssertStartedContainers(containers []string) bool {
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
return f.assertList(containers, f.StartedContainers) return f.assertList(containers, f.StartedContainers)
} }
func (f *FakeRuntime) AssertKilledContainers(containers []string) error { func (f *FakeRuntime) AssertKilledContainers(containers []string) bool {
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
return f.assertList(containers, f.KilledContainers) return f.assertList(containers, f.KilledContainers)

View File

@ -201,7 +201,7 @@ func TestParallelPuller(t *testing.T) {
fakeRuntime.CalledFunctions = nil fakeRuntime.CalledFunctions = nil
fakeClock.Step(time.Second) fakeClock.Step(time.Second)
_, _, err := puller.EnsureImageExists(pod, container, nil, nil) _, _, err := puller.EnsureImageExists(pod, container, nil, nil)
assert.NoError(t, fakeRuntime.AssertCalls(expected.calls)) fakeRuntime.AssertCalls(expected.calls)
assert.Equal(t, expected.err, err) assert.Equal(t, expected.err, err)
} }
}) })
@ -229,7 +229,7 @@ func TestSerializedPuller(t *testing.T) {
fakeRuntime.CalledFunctions = nil fakeRuntime.CalledFunctions = nil
fakeClock.Step(time.Second) fakeClock.Step(time.Second)
_, _, err := puller.EnsureImageExists(pod, container, nil, nil) _, _, err := puller.EnsureImageExists(pod, container, nil, nil)
assert.NoError(t, fakeRuntime.AssertCalls(expected.calls)) fakeRuntime.AssertCalls(expected.calls)
assert.Equal(t, expected.err, err) assert.Equal(t, expected.err, err)
} }
}) })
@ -287,7 +287,7 @@ func TestPullAndListImageWithPodAnnotations(t *testing.T) {
t.Run(c.testName, func(t *testing.T) { t.Run(c.testName, func(t *testing.T) {
_, _, err := puller.EnsureImageExists(pod, container, nil, nil) _, _, 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) assert.Equal(t, c.expected[0].err, err, "tick=%d", 0)
images, _ := fakeRuntime.ListImages() images, _ := fakeRuntime.ListImages()

View File

@ -144,16 +144,18 @@ func newTestKubeletWithImageList(
imageList []kubecontainer.Image, imageList []kubecontainer.Image,
controllerAttachDetachEnabled bool, controllerAttachDetachEnabled bool,
initFakeVolumePlugin bool) *TestKubelet { initFakeVolumePlugin bool) *TestKubelet {
fakeRuntime := &containertest.FakeRuntime{} fakeRuntime := &containertest.FakeRuntime{
fakeRuntime.RuntimeType = "test" ImageList: imageList,
fakeRuntime.VersionInfo = "1.5.0"
fakeRuntime.ImageList = imageList
// Set ready conditions by default. // Set ready conditions by default.
fakeRuntime.RuntimeStatus = &kubecontainer.RuntimeStatus{ RuntimeStatus: &kubecontainer.RuntimeStatus{
Conditions: []kubecontainer.RuntimeCondition{ Conditions: []kubecontainer.RuntimeCondition{
{Type: "RuntimeReady", Status: true}, {Type: "RuntimeReady", Status: true},
{Type: "NetworkReady", Status: true}, {Type: "NetworkReady", Status: true},
}, },
},
VersionInfo: "1.5.0",
RuntimeType: "test",
T: t,
} }
fakeRecorder := &record.FakeRecorder{} fakeRecorder := &record.FakeRecorder{}
@ -658,7 +660,7 @@ func TestKillPodFollwedByIsPodPendingTermination(t *testing.T) {
RunningPod: pod, 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") t.Fatal("Race condition: When KillPod is complete, the pod should be pending termination or be killed")
} }
} }