Skip to content

Commit 2ff95f0

Browse files
committed
Add test coverage for throttledImageService
1 parent 22de8fc commit 2ff95f0

File tree

1 file changed

+98
-41
lines changed

1 file changed

+98
-41
lines changed

pkg/kubelet/images/image_manager_test.go

Lines changed: 98 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,69 +37,90 @@ type pullerExpects struct {
3737
}
3838

3939
type pullerTestCase struct {
40+
testName string
4041
containerImage string
4142
policy v1.PullPolicy
4243
inspectErr error
4344
pullerErr error
45+
qps float32
46+
burst int
4447
expected []pullerExpects
4548
}
4649

4750
func pullerTestCases() []pullerTestCase {
4851
return []pullerTestCase{
4952
{ // pull missing image
53+
testName: "image missing, pull",
5054
containerImage: "missing_image",
5155
policy: v1.PullIfNotPresent,
5256
inspectErr: nil,
5357
pullerErr: nil,
58+
qps: 0.0,
59+
burst: 0,
5460
expected: []pullerExpects{
5561
{[]string{"GetImageRef", "PullImage"}, nil},
5662
}},
5763

5864
{ // image present, don't pull
65+
testName: "image present, don't pull ",
5966
containerImage: "present_image",
6067
policy: v1.PullIfNotPresent,
6168
inspectErr: nil,
6269
pullerErr: nil,
70+
qps: 0.0,
71+
burst: 0,
6372
expected: []pullerExpects{
6473
{[]string{"GetImageRef"}, nil},
6574
{[]string{"GetImageRef"}, nil},
6675
{[]string{"GetImageRef"}, nil},
6776
}},
6877
// image present, pull it
6978
{containerImage: "present_image",
79+
testName: "image present, pull ",
7080
policy: v1.PullAlways,
7181
inspectErr: nil,
7282
pullerErr: nil,
83+
qps: 0.0,
84+
burst: 0,
7385
expected: []pullerExpects{
7486
{[]string{"GetImageRef", "PullImage"}, nil},
7587
{[]string{"GetImageRef", "PullImage"}, nil},
7688
{[]string{"GetImageRef", "PullImage"}, nil},
7789
}},
7890
// missing image, error PullNever
7991
{containerImage: "missing_image",
92+
testName: "image missing, never pull",
8093
policy: v1.PullNever,
8194
inspectErr: nil,
8295
pullerErr: nil,
96+
qps: 0.0,
97+
burst: 0,
8398
expected: []pullerExpects{
8499
{[]string{"GetImageRef"}, ErrImageNeverPull},
85100
{[]string{"GetImageRef"}, ErrImageNeverPull},
86101
{[]string{"GetImageRef"}, ErrImageNeverPull},
87102
}},
88103
// missing image, unable to inspect
89104
{containerImage: "missing_image",
105+
testName: "image missing, pull if not present",
90106
policy: v1.PullIfNotPresent,
91107
inspectErr: errors.New("unknown inspectError"),
92108
pullerErr: nil,
109+
qps: 0.0,
110+
burst: 0,
93111
expected: []pullerExpects{
94112
{[]string{"GetImageRef"}, ErrImageInspect},
95113
{[]string{"GetImageRef"}, ErrImageInspect},
96114
{[]string{"GetImageRef"}, ErrImageInspect},
97115
}},
98116
// missing image, unable to fetch
99117
{containerImage: "typo_image",
118+
testName: "image missing, unable to fetch",
100119
policy: v1.PullIfNotPresent,
101120
inspectErr: nil,
102121
pullerErr: errors.New("404"),
122+
qps: 0.0,
123+
burst: 0,
103124
expected: []pullerExpects{
104125
{[]string{"GetImageRef", "PullImage"}, ErrImagePull},
105126
{[]string{"GetImageRef", "PullImage"}, ErrImagePull},
@@ -108,6 +129,32 @@ func pullerTestCases() []pullerTestCase {
108129
{[]string{"GetImageRef"}, ErrImagePullBackOff},
109130
{[]string{"GetImageRef"}, ErrImagePullBackOff},
110131
}},
132+
// image present, non-zero qps, try to pull
133+
{containerImage: "present_image",
134+
testName: "image present and qps>0, pull",
135+
policy: v1.PullAlways,
136+
inspectErr: nil,
137+
pullerErr: nil,
138+
qps: 400.0,
139+
burst: 600,
140+
expected: []pullerExpects{
141+
{[]string{"GetImageRef", "PullImage"}, nil},
142+
{[]string{"GetImageRef", "PullImage"}, nil},
143+
{[]string{"GetImageRef", "PullImage"}, nil},
144+
}},
145+
// image present, non-zero qps, try to pull when qps exceeded
146+
{containerImage: "present_image",
147+
testName: "image present and excessive qps rate, pull",
148+
policy: v1.PullAlways,
149+
inspectErr: nil,
150+
pullerErr: nil,
151+
qps: 2000.0,
152+
burst: 0,
153+
expected: []pullerExpects{
154+
{[]string{"GetImageRef"}, ErrImagePull},
155+
{[]string{"GetImageRef"}, ErrImagePull},
156+
{[]string{"GetImageRef"}, ErrImagePullBackOff},
157+
}},
111158
}
112159
}
113160

@@ -129,7 +176,7 @@ func pullerTestEnv(c pullerTestCase, serialized bool) (puller ImageManager, fake
129176
fakeRuntime.Err = c.pullerErr
130177
fakeRuntime.InspectErr = c.inspectErr
131178

132-
puller = NewImageManager(fakeRecorder, fakeRuntime, backOff, serialized, 0, 0)
179+
puller = NewImageManager(fakeRecorder, fakeRuntime, backOff, serialized, c.qps, c.burst)
133180
return
134181
}
135182

@@ -146,16 +193,18 @@ func TestParallelPuller(t *testing.T) {
146193
cases := pullerTestCases()
147194

148195
useSerializedEnv := false
149-
for i, c := range cases {
196+
for _, c := range cases {
150197
puller, fakeClock, fakeRuntime, container := pullerTestEnv(c, useSerializedEnv)
151198

152-
for tick, expected := range c.expected {
153-
fakeRuntime.CalledFunctions = nil
154-
fakeClock.Step(time.Second)
155-
_, _, err := puller.EnsureImageExists(pod, container, nil, nil)
156-
assert.NoError(t, fakeRuntime.AssertCalls(expected.calls), "in test %d tick=%d", i, tick)
157-
assert.Equal(t, expected.err, err, "in test %d tick=%d", i, tick)
158-
}
199+
t.Run(c.testName, func(t *testing.T) {
200+
for _, expected := range c.expected {
201+
fakeRuntime.CalledFunctions = nil
202+
fakeClock.Step(time.Second)
203+
_, _, err := puller.EnsureImageExists(pod, container, nil, nil)
204+
assert.NoError(t, fakeRuntime.AssertCalls(expected.calls))
205+
assert.Equal(t, expected.err, err)
206+
}
207+
})
159208
}
160209
}
161210

@@ -172,34 +221,39 @@ func TestSerializedPuller(t *testing.T) {
172221
cases := pullerTestCases()
173222

174223
useSerializedEnv := true
175-
for i, c := range cases {
224+
for _, c := range cases {
176225
puller, fakeClock, fakeRuntime, container := pullerTestEnv(c, useSerializedEnv)
177226

178-
for tick, expected := range c.expected {
179-
fakeRuntime.CalledFunctions = nil
180-
fakeClock.Step(time.Second)
181-
_, _, err := puller.EnsureImageExists(pod, container, nil, nil)
182-
assert.NoError(t, fakeRuntime.AssertCalls(expected.calls), "in test %d tick=%d", i, tick)
183-
assert.Equal(t, expected.err, err, "in test %d tick=%d", i, tick)
184-
}
227+
t.Run(c.testName, func(t *testing.T) {
228+
for _, expected := range c.expected {
229+
fakeRuntime.CalledFunctions = nil
230+
fakeClock.Step(time.Second)
231+
_, _, err := puller.EnsureImageExists(pod, container, nil, nil)
232+
assert.NoError(t, fakeRuntime.AssertCalls(expected.calls))
233+
assert.Equal(t, expected.err, err)
234+
}
235+
})
185236
}
186237
}
187238

188239
func TestApplyDefaultImageTag(t *testing.T) {
189240
for _, testCase := range []struct {
190-
Input string
191-
Output string
241+
testName string
242+
Input string
243+
Output string
192244
}{
193-
{Input: "root", Output: "root:latest"},
194-
{Input: "root:tag", Output: "root:tag"},
195-
{Input: "root@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", Output: "root@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
245+
{testName: "root", Input: "root", Output: "root:latest"},
246+
{testName: "root:tag", Input: "root:tag", Output: "root:tag"},
247+
{testName: "root@sha", Input: "root@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", Output: "root@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
196248
} {
197-
image, err := applyDefaultImageTag(testCase.Input)
198-
if err != nil {
199-
t.Errorf("applyDefaultImageTag(%s) failed: %v", testCase.Input, err)
200-
} else if image != testCase.Output {
201-
t.Errorf("Expected image reference: %q, got %q", testCase.Output, image)
202-
}
249+
t.Run(testCase.testName, func(t *testing.T) {
250+
image, err := applyDefaultImageTag(testCase.Input)
251+
if err != nil {
252+
t.Errorf("applyDefaultImageTag(%s) failed: %v", testCase.Input, err)
253+
} else if image != testCase.Output {
254+
t.Errorf("Expected image reference: %q, got %q", testCase.Output, image)
255+
}
256+
})
203257
}
204258
}
205259

@@ -216,6 +270,7 @@ func TestPullAndListImageWithPodAnnotations(t *testing.T) {
216270
},
217271
}}
218272
c := pullerTestCase{ // pull missing image
273+
testName: "test pull and list image with pod annotations",
219274
containerImage: "missing_image",
220275
policy: v1.PullIfNotPresent,
221276
inspectErr: nil,
@@ -230,20 +285,22 @@ func TestPullAndListImageWithPodAnnotations(t *testing.T) {
230285
fakeRuntime.ImageList = []Image{}
231286
fakeClock.Step(time.Second)
232287

233-
_, _, err := puller.EnsureImageExists(pod, container, nil, nil)
234-
assert.NoError(t, fakeRuntime.AssertCalls(c.expected[0].calls), "tick=%d", 0)
235-
assert.Equal(t, c.expected[0].err, err, "tick=%d", 0)
288+
t.Run(c.testName, func(t *testing.T) {
289+
_, _, err := puller.EnsureImageExists(pod, container, nil, nil)
290+
assert.NoError(t, fakeRuntime.AssertCalls(c.expected[0].calls), "tick=%d", 0)
291+
assert.Equal(t, c.expected[0].err, err, "tick=%d", 0)
236292

237-
images, _ := fakeRuntime.ListImages()
238-
assert.Equal(t, 1, len(images), "ListImages() count")
293+
images, _ := fakeRuntime.ListImages()
294+
assert.Equal(t, 1, len(images), "ListImages() count")
239295

240-
image := images[0]
241-
assert.Equal(t, "missing_image:latest", image.ID, "Image ID")
296+
image := images[0]
297+
assert.Equal(t, "missing_image:latest", image.ID, "Image ID")
242298

243-
expectedAnnotations := []Annotation{
244-
{
245-
Name: "kubernetes.io/runtimehandler",
246-
Value: "handler_name",
247-
}}
248-
assert.Equal(t, expectedAnnotations, image.Spec.Annotations, "image spec annotations")
299+
expectedAnnotations := []Annotation{
300+
{
301+
Name: "kubernetes.io/runtimehandler",
302+
Value: "handler_name",
303+
}}
304+
assert.Equal(t, expectedAnnotations, image.Spec.Annotations, "image spec annotations")
305+
})
249306
}

0 commit comments

Comments
 (0)