@@ -37,69 +37,90 @@ type pullerExpects struct {
37
37
}
38
38
39
39
type pullerTestCase struct {
40
+ testName string
40
41
containerImage string
41
42
policy v1.PullPolicy
42
43
inspectErr error
43
44
pullerErr error
45
+ qps float32
46
+ burst int
44
47
expected []pullerExpects
45
48
}
46
49
47
50
func pullerTestCases () []pullerTestCase {
48
51
return []pullerTestCase {
49
52
{ // pull missing image
53
+ testName : "image missing, pull" ,
50
54
containerImage : "missing_image" ,
51
55
policy : v1 .PullIfNotPresent ,
52
56
inspectErr : nil ,
53
57
pullerErr : nil ,
58
+ qps : 0.0 ,
59
+ burst : 0 ,
54
60
expected : []pullerExpects {
55
61
{[]string {"GetImageRef" , "PullImage" }, nil },
56
62
}},
57
63
58
64
{ // image present, don't pull
65
+ testName : "image present, don't pull " ,
59
66
containerImage : "present_image" ,
60
67
policy : v1 .PullIfNotPresent ,
61
68
inspectErr : nil ,
62
69
pullerErr : nil ,
70
+ qps : 0.0 ,
71
+ burst : 0 ,
63
72
expected : []pullerExpects {
64
73
{[]string {"GetImageRef" }, nil },
65
74
{[]string {"GetImageRef" }, nil },
66
75
{[]string {"GetImageRef" }, nil },
67
76
}},
68
77
// image present, pull it
69
78
{containerImage : "present_image" ,
79
+ testName : "image present, pull " ,
70
80
policy : v1 .PullAlways ,
71
81
inspectErr : nil ,
72
82
pullerErr : nil ,
83
+ qps : 0.0 ,
84
+ burst : 0 ,
73
85
expected : []pullerExpects {
74
86
{[]string {"GetImageRef" , "PullImage" }, nil },
75
87
{[]string {"GetImageRef" , "PullImage" }, nil },
76
88
{[]string {"GetImageRef" , "PullImage" }, nil },
77
89
}},
78
90
// missing image, error PullNever
79
91
{containerImage : "missing_image" ,
92
+ testName : "image missing, never pull" ,
80
93
policy : v1 .PullNever ,
81
94
inspectErr : nil ,
82
95
pullerErr : nil ,
96
+ qps : 0.0 ,
97
+ burst : 0 ,
83
98
expected : []pullerExpects {
84
99
{[]string {"GetImageRef" }, ErrImageNeverPull },
85
100
{[]string {"GetImageRef" }, ErrImageNeverPull },
86
101
{[]string {"GetImageRef" }, ErrImageNeverPull },
87
102
}},
88
103
// missing image, unable to inspect
89
104
{containerImage : "missing_image" ,
105
+ testName : "image missing, pull if not present" ,
90
106
policy : v1 .PullIfNotPresent ,
91
107
inspectErr : errors .New ("unknown inspectError" ),
92
108
pullerErr : nil ,
109
+ qps : 0.0 ,
110
+ burst : 0 ,
93
111
expected : []pullerExpects {
94
112
{[]string {"GetImageRef" }, ErrImageInspect },
95
113
{[]string {"GetImageRef" }, ErrImageInspect },
96
114
{[]string {"GetImageRef" }, ErrImageInspect },
97
115
}},
98
116
// missing image, unable to fetch
99
117
{containerImage : "typo_image" ,
118
+ testName : "image missing, unable to fetch" ,
100
119
policy : v1 .PullIfNotPresent ,
101
120
inspectErr : nil ,
102
121
pullerErr : errors .New ("404" ),
122
+ qps : 0.0 ,
123
+ burst : 0 ,
103
124
expected : []pullerExpects {
104
125
{[]string {"GetImageRef" , "PullImage" }, ErrImagePull },
105
126
{[]string {"GetImageRef" , "PullImage" }, ErrImagePull },
@@ -108,6 +129,32 @@ func pullerTestCases() []pullerTestCase {
108
129
{[]string {"GetImageRef" }, ErrImagePullBackOff },
109
130
{[]string {"GetImageRef" }, ErrImagePullBackOff },
110
131
}},
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
+ }},
111
158
}
112
159
}
113
160
@@ -129,7 +176,7 @@ func pullerTestEnv(c pullerTestCase, serialized bool) (puller ImageManager, fake
129
176
fakeRuntime .Err = c .pullerErr
130
177
fakeRuntime .InspectErr = c .inspectErr
131
178
132
- puller = NewImageManager (fakeRecorder , fakeRuntime , backOff , serialized , 0 , 0 )
179
+ puller = NewImageManager (fakeRecorder , fakeRuntime , backOff , serialized , c . qps , c . burst )
133
180
return
134
181
}
135
182
@@ -146,16 +193,18 @@ func TestParallelPuller(t *testing.T) {
146
193
cases := pullerTestCases ()
147
194
148
195
useSerializedEnv := false
149
- for i , c := range cases {
196
+ for _ , c := range cases {
150
197
puller , fakeClock , fakeRuntime , container := pullerTestEnv (c , useSerializedEnv )
151
198
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
+ })
159
208
}
160
209
}
161
210
@@ -172,34 +221,39 @@ func TestSerializedPuller(t *testing.T) {
172
221
cases := pullerTestCases ()
173
222
174
223
useSerializedEnv := true
175
- for i , c := range cases {
224
+ for _ , c := range cases {
176
225
puller , fakeClock , fakeRuntime , container := pullerTestEnv (c , useSerializedEnv )
177
226
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
+ })
185
236
}
186
237
}
187
238
188
239
func TestApplyDefaultImageTag (t * testing.T ) {
189
240
for _ , testCase := range []struct {
190
- Input string
191
- Output string
241
+ testName string
242
+ Input string
243
+ Output string
192
244
}{
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" },
196
248
} {
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
+ })
203
257
}
204
258
}
205
259
@@ -216,6 +270,7 @@ func TestPullAndListImageWithPodAnnotations(t *testing.T) {
216
270
},
217
271
}}
218
272
c := pullerTestCase { // pull missing image
273
+ testName : "test pull and list image with pod annotations" ,
219
274
containerImage : "missing_image" ,
220
275
policy : v1 .PullIfNotPresent ,
221
276
inspectErr : nil ,
@@ -230,20 +285,22 @@ func TestPullAndListImageWithPodAnnotations(t *testing.T) {
230
285
fakeRuntime .ImageList = []Image {}
231
286
fakeClock .Step (time .Second )
232
287
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 )
236
292
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" )
239
295
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" )
242
298
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
+ })
249
306
}
0 commit comments