@@ -40,6 +40,7 @@ import (
40
40
"github.com/aws/amazon-ecs-agent/agent/engine/dockeriface/mocks"
41
41
"github.com/aws/amazon-ecs-agent/agent/engine/emptyvolume"
42
42
"github.com/aws/amazon-ecs-agent/agent/utils/ttime/mocks"
43
+ "github.com/stretchr/testify/require"
43
44
)
44
45
45
46
// xContainerShortTimeout is a short duration intended to be used by the
@@ -90,15 +91,16 @@ func TestPullImageOutputTimeout(t *testing.T) {
90
91
defer done ()
91
92
92
93
pullBeginTimeout := make (chan time.Time )
93
- testTime .EXPECT ().After (dockerPullBeginTimeout ).Return (pullBeginTimeout )
94
- testTime .EXPECT ().After (pullImageTimeout )
94
+ testTime .EXPECT ().After (dockerPullBeginTimeout ).Return (pullBeginTimeout ). MinTimes ( 1 )
95
+ testTime .EXPECT ().After (pullImageTimeout ). MinTimes ( 1 )
95
96
wait := sync.WaitGroup {}
96
97
wait .Add (1 )
98
+ // multiple invocations will happen due to retries, but all should timeout
97
99
mockDocker .EXPECT ().PullImage (& pullImageOptsMatcher {"image:latest" }, gomock .Any ()).Do (func (x , y interface {}) {
98
100
pullBeginTimeout <- time .Now ()
99
101
wait .Wait ()
100
102
// Don't return, verify timeout happens
101
- })
103
+ }). Times ( maximumPullRetries ) // expected number of retries
102
104
103
105
metadata := client .PullImage ("image" , nil )
104
106
if metadata .Error == nil {
@@ -159,9 +161,7 @@ func TestPullImage(t *testing.T) {
159
161
mockDocker .EXPECT ().PullImage (& pullImageOptsMatcher {"image:latest" }, gomock .Any ()).Return (nil )
160
162
161
163
metadata := client .PullImage ("image" , nil )
162
- if metadata .Error != nil {
163
- t .Error ("Expected pull to succeed" )
164
- }
164
+ assert .NoError (t , metadata .Error , "Expected pull to succeed" )
165
165
}
166
166
167
167
func TestPullImageTag (t * testing.T ) {
@@ -172,9 +172,7 @@ func TestPullImageTag(t *testing.T) {
172
172
mockDocker .EXPECT ().PullImage (& pullImageOptsMatcher {"image:mytag" }, gomock .Any ()).Return (nil )
173
173
174
174
metadata := client .PullImage ("image:mytag" , nil )
175
- if metadata .Error != nil {
176
- t .Error ("Expected pull to succeed" )
177
- }
175
+ assert .NoError (t , metadata .Error , "Expected pull to succeed" )
178
176
}
179
177
180
178
func TestPullImageDigest (t * testing.T ) {
@@ -188,9 +186,7 @@ func TestPullImageDigest(t *testing.T) {
188
186
).Return (nil )
189
187
190
188
metadata := client .PullImage ("image@sha256:bc8813ea7b3603864987522f02a76101c17ad122e1c46d790efc0fca78ca7bfb" , nil )
191
- if metadata .Error != nil {
192
- t .Error ("Expected pull to succeed" )
193
- }
189
+ assert .NoError (t , metadata .Error , "Expected pull to succeed" )
194
190
}
195
191
196
192
func TestPullEmptyvolumeImage (t * testing.T ) {
@@ -203,19 +199,13 @@ func TestPullEmptyvolumeImage(t *testing.T) {
203
199
mockDocker .EXPECT ().InspectImage (emptyvolume .Image + ":" + emptyvolume .Tag ).Return (nil , errors .New ("Does not exist" )),
204
200
mockDocker .EXPECT ().ImportImage (gomock .Any ()).Do (func (x interface {}) {
205
201
req := x .(docker.ImportImageOptions )
206
- if req .Repository != emptyvolume .Image {
207
- t .Fatal ("Expected empty volume repository" )
208
- }
209
- if req .Tag != emptyvolume .Tag {
210
- t .Fatal ("Expected empty volume repository" )
211
- }
202
+ require .Equal (t , emptyvolume .Image , req .Repository , "expected empty volume repository" )
203
+ require .Equal (t , emptyvolume .Tag , req .Tag , "expected empty volume tag" )
212
204
}),
213
205
)
214
206
215
207
metadata := client .PullImage (emptyvolume .Image + ":" + emptyvolume .Tag , nil )
216
- if metadata .Error != nil {
217
- t .Error (metadata .Error )
218
- }
208
+ assert .NoError (t , metadata .Error , "Expected pull to succeed" )
219
209
}
220
210
221
211
func TestPullExistingEmptyvolumeImage (t * testing.T ) {
@@ -229,9 +219,7 @@ func TestPullExistingEmptyvolumeImage(t *testing.T) {
229
219
)
230
220
231
221
metadata := client .PullImage (emptyvolume .Image + ":" + emptyvolume .Tag , nil )
232
- if metadata .Error != nil {
233
- t .Error (metadata .Error )
234
- }
222
+ assert .NoError (t , metadata .Error , "Expected pull to succeed" )
235
223
}
236
224
237
225
func TestPullImageECRSuccess (t * testing.T ) {
@@ -285,9 +273,7 @@ func TestPullImageECRSuccess(t *testing.T) {
285
273
).Return (nil )
286
274
287
275
metadata := client .PullImage (image , authData )
288
- if metadata .Error != nil {
289
- t .Error ("Expected pull to succeed" )
290
- }
276
+ assert .NoError (t , metadata .Error , "Expected pull to succeed" )
291
277
}
292
278
293
279
func TestPullImageECRAuthFail (t * testing.T ) {
@@ -322,12 +308,11 @@ func TestPullImageECRAuthFail(t *testing.T) {
322
308
image := imageEndpoint + "/myimage:tag"
323
309
324
310
ecrClientFactory .EXPECT ().GetClient (region , endpointOverride ).Return (ecrClient )
311
+ // no retries for this error
325
312
ecrClient .EXPECT ().GetAuthorizationToken (gomock .Any ()).Return (nil , errors .New ("test error" ))
326
313
327
314
metadata := client .PullImage (image , authData )
328
- if metadata .Error == nil {
329
- t .Error ("Expected pull to fail" )
330
- }
315
+ assert .Error (t , metadata .Error , "expected pull to fail" )
331
316
}
332
317
333
318
func TestCreateContainerTimeout (t * testing.T ) {
@@ -344,12 +329,8 @@ func TestCreateContainerTimeout(t *testing.T) {
344
329
// Don't return, verify timeout happens
345
330
})
346
331
metadata := client .CreateContainer (config .Config , nil , config .Name , xContainerShortTimeout )
347
- if metadata .Error == nil {
348
- t .Error ("Expected error for pull timeout" )
349
- }
350
- if metadata .Error .(api.NamedError ).ErrorName () != "DockerTimeoutError" {
351
- t .Error ("Wrong error type" )
352
- }
332
+ assert .Error (t , metadata .Error , "expected error for pull timeout" )
333
+ assert .Equal (t , "DockerTimeoutError" , metadata .Error .(api.NamedError ).ErrorName ())
353
334
wait .Done ()
354
335
}
355
336
0 commit comments