Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AssertCalls usage for kubelet fake runtimes unit tests #45571

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
115 changes: 70 additions & 45 deletions pkg/kubelet/images/image_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,60 +31,83 @@ import (
ctest "k8s.io/kubernetes/pkg/kubelet/container/testing"
)

type pullerExpects struct {
calls []string
err error
}

type pullerTestCase struct {
containerImage string
policy v1.PullPolicy
calledFunctions []string
inspectErr error
pullerErr error
expectedErr []error
containerImage string
policy v1.PullPolicy
inspectErr error
pullerErr error
expected []pullerExpects
}

func pullerTestCases() []pullerTestCase {
return []pullerTestCase{
{ // pull missing image
containerImage: "missing_image",
policy: v1.PullIfNotPresent,
calledFunctions: []string{"GetImageRef", "PullImage"},
inspectErr: nil,
pullerErr: nil,
expectedErr: []error{nil}},
containerImage: "missing_image",
policy: v1.PullIfNotPresent,
inspectErr: nil,
pullerErr: nil,
expected: []pullerExpects{
{[]string{"GetImageRef", "PullImage"}, nil},
}},

{ // image present, don't pull
containerImage: "present_image",
policy: v1.PullIfNotPresent,
calledFunctions: []string{"GetImageRef"},
inspectErr: nil,
pullerErr: nil,
expectedErr: []error{nil, nil, nil}},
containerImage: "present_image",
policy: v1.PullIfNotPresent,
inspectErr: nil,
pullerErr: nil,
expected: []pullerExpects{
{[]string{"GetImageRef"}, nil},
{[]string{"GetImageRef"}, nil},
{[]string{"GetImageRef"}, nil},
}},
// image present, pull it
{containerImage: "present_image",
policy: v1.PullAlways,
calledFunctions: []string{"GetImageRef", "PullImage"},
inspectErr: nil,
pullerErr: nil,
expectedErr: []error{nil, nil, nil}},
policy: v1.PullAlways,
inspectErr: nil,
pullerErr: nil,
expected: []pullerExpects{
{[]string{"GetImageRef", "PullImage"}, nil},
{[]string{"GetImageRef", "PullImage"}, nil},
{[]string{"GetImageRef", "PullImage"}, nil},
}},
// missing image, error PullNever
{containerImage: "missing_image",
policy: v1.PullNever,
calledFunctions: []string{"GetImageRef"},
inspectErr: nil,
pullerErr: nil,
expectedErr: []error{ErrImageNeverPull, ErrImageNeverPull, ErrImageNeverPull}},
policy: v1.PullNever,
inspectErr: nil,
pullerErr: nil,
expected: []pullerExpects{
{[]string{"GetImageRef"}, ErrImageNeverPull},
{[]string{"GetImageRef"}, ErrImageNeverPull},
{[]string{"GetImageRef"}, ErrImageNeverPull},
}},
// missing image, unable to inspect
{containerImage: "missing_image",
policy: v1.PullIfNotPresent,
calledFunctions: []string{"GetImageRef"},
inspectErr: errors.New("unknown inspectError"),
pullerErr: nil,
expectedErr: []error{ErrImageInspect, ErrImageInspect, ErrImageInspect}},
policy: v1.PullIfNotPresent,
inspectErr: errors.New("unknown inspectError"),
pullerErr: nil,
expected: []pullerExpects{
{[]string{"GetImageRef"}, ErrImageInspect},
{[]string{"GetImageRef"}, ErrImageInspect},
{[]string{"GetImageRef"}, ErrImageInspect},
}},
// missing image, unable to fetch
{containerImage: "typo_image",
policy: v1.PullIfNotPresent,
calledFunctions: []string{"GetImageRef", "PullImage"},
inspectErr: nil,
pullerErr: errors.New("404"),
expectedErr: []error{ErrImagePull, ErrImagePull, ErrImagePullBackOff, ErrImagePull, ErrImagePullBackOff, ErrImagePullBackOff}},
policy: v1.PullIfNotPresent,
inspectErr: nil,
pullerErr: errors.New("404"),
expected: []pullerExpects{
{[]string{"GetImageRef", "PullImage"}, ErrImagePull},
{[]string{"GetImageRef", "PullImage"}, ErrImagePull},
{[]string{"GetImageRef"}, ErrImagePullBackOff},
{[]string{"GetImageRef", "PullImage"}, ErrImagePull},
{[]string{"GetImageRef"}, ErrImagePullBackOff},
{[]string{"GetImageRef"}, ErrImagePullBackOff},
}},
}
}

Expand All @@ -102,7 +125,7 @@ func pullerTestEnv(c pullerTestCase, serialized bool) (puller ImageManager, fake
fakeRuntime = &ctest.FakeRuntime{}
fakeRecorder := &record.FakeRecorder{}

fakeRuntime.ImageList = []Image{{ID: "present_image"}}
fakeRuntime.ImageList = []Image{{ID: "present_image:latest"}}
fakeRuntime.Err = c.pullerErr
fakeRuntime.InspectErr = c.inspectErr

Expand All @@ -125,11 +148,12 @@ func TestParallelPuller(t *testing.T) {
for i, c := range cases {
puller, fakeClock, fakeRuntime, container := pullerTestEnv(c, false)

for tick, expected := range c.expectedErr {
for tick, expected := range c.expected {
fakeRuntime.CalledFunctions = nil
fakeClock.Step(time.Second)
_, _, err := puller.EnsureImageExists(pod, container, nil)
fakeRuntime.AssertCalls(c.calledFunctions)
assert.Equal(t, expected, err, "in test %d tick=%d", i, tick)
assert.NoError(t, fakeRuntime.AssertCalls(expected.calls), "in test %d tick=%d", i, tick)
assert.Equal(t, expected.err, err, "in test %d tick=%d", i, tick)
}
}
}
Expand All @@ -149,11 +173,12 @@ func TestSerializedPuller(t *testing.T) {
for i, c := range cases {
puller, fakeClock, fakeRuntime, container := pullerTestEnv(c, true)

for tick, expected := range c.expectedErr {
for tick, expected := range c.expected {
fakeRuntime.CalledFunctions = nil
fakeClock.Step(time.Second)
_, _, err := puller.EnsureImageExists(pod, container, nil)
fakeRuntime.AssertCalls(c.calledFunctions)
assert.Equal(t, expected, err, "in test %d tick=%d", i, tick)
assert.NoError(t, fakeRuntime.AssertCalls(expected.calls), "in test %d tick=%d", i, tick)
assert.Equal(t, expected.err, err, "in test %d tick=%d", i, tick)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/kuberuntime/kuberuntime_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestRemoveContainer(t *testing.T) {
expectedContainerLogSymlink := legacyLogSymlink(containerId, "foo", "bar", "new")
assert.Equal(t, fakeOS.Removes, []string{expectedContainerLogPath, expectedContainerLogSymlink})
// Verify container is removed
fakeRuntime.AssertCalls([]string{"RemoveContainer"})
assert.Contains(t, fakeRuntime.Called, "RemoveContainer")
containers, err := fakeRuntime.ListContainers(&runtimeapi.ContainerFilter{Id: containerId})
assert.NoError(t, err)
assert.Empty(t, containers)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/kuberuntime/kuberuntime_sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestCreatePodSandbox(t *testing.T) {
}
id, _, err := m.createPodSandbox(pod, 1)
assert.NoError(t, err)
fakeRuntime.AssertCalls([]string{"RunPodSandbox"})
assert.Contains(t, fakeRuntime.Called, "RunPodSandbox")
sandboxes, err := fakeRuntime.ListPodSandbox(&runtimeapi.PodSandboxFilter{Id: id})
assert.NoError(t, err)
assert.Equal(t, len(sandboxes), 1)
Expand Down