@@ -109,7 +109,7 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
109
109
}, {
110
110
PullPolicy : PullImageNever ,
111
111
ExpectedPulls : 0 ,
112
- ExpectedErrMsg : "error fake not found " ,
112
+ ExpectedErrMsg : "No such image: does-not-exist-locally:latest " ,
113
113
},
114
114
}
115
115
for _ , tc := range cases {
@@ -127,7 +127,7 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
127
127
defer func () { tc .ResponseCounter ++ }()
128
128
switch tc .ResponseCounter {
129
129
case 0 :
130
- return container.CreateResponse {}, fakeNotFound {}
130
+ return container.CreateResponse {}, fakeNotFound {image : imageName + ":latest" }
131
131
default :
132
132
return container.CreateResponse {ID : containerID }, nil
133
133
}
@@ -160,6 +160,87 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
160
160
}
161
161
}
162
162
163
+ func TestCreateContainerImagePullMissingValidate (t * testing.T ) {
164
+ const (
165
+ containerID = "abcdef"
166
+ )
167
+
168
+ cases := []struct {
169
+ ContainerImage string
170
+ MissingImage string
171
+ ExpectedPulls int
172
+ ExpectedErrMsg string
173
+ }{
174
+ {
175
+ ContainerImage : "does-not-exist-locally" ,
176
+ MissingImage : "does-not-exist-locally:latest" ,
177
+ ExpectedPulls : 1 ,
178
+ ExpectedErrMsg : "No such image: does-not-exist-locally:latest" ,
179
+ }, {
180
+ ContainerImage : "registry:5000/does-not-exist-locally" ,
181
+ MissingImage : "registry:5000/does-not-exist-locally:latest" ,
182
+ ExpectedPulls : 1 ,
183
+ ExpectedErrMsg : "No such image: registry:5000/does-not-exist-locally" ,
184
+ }, {
185
+ ContainerImage : "registry:5000/does-not-exist-locally:tag" ,
186
+ MissingImage : "registry:5000/does-not-exist-locally:tag" ,
187
+ ExpectedPulls : 1 ,
188
+ ExpectedErrMsg : "No such image: registry:5000/does-not-exist-locally:tag" ,
189
+ }, {
190
+ ContainerImage : "registry:5000/does-not-exist-locally@sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" ,
191
+ MissingImage : "registry:5000/does-not-exist-locally@sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" ,
192
+ ExpectedPulls : 1 ,
193
+ ExpectedErrMsg : "No such image: registry:5000/does-not-exist-locally@sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" ,
194
+ }, {
195
+ ContainerImage : "does-not-exist-locally" ,
196
+ MissingImage : "some-other-image" ,
197
+ ExpectedPulls : 0 ,
198
+ ExpectedErrMsg : "No such image: some-other-image" ,
199
+ },
200
+ }
201
+ for _ , tc := range cases {
202
+ t .Run (tc .MissingImage , func (t * testing.T ) {
203
+ pullCounter := 0
204
+
205
+ config := & containerConfig {
206
+ Config : & container.Config {
207
+ Image : tc .ContainerImage ,
208
+ },
209
+ HostConfig : & container.HostConfig {},
210
+ }
211
+
212
+ client := & fakeClient {
213
+ createContainerFunc : func (
214
+ config * container.Config ,
215
+ hostConfig * container.HostConfig ,
216
+ networkingConfig * network.NetworkingConfig ,
217
+ platform * specs.Platform ,
218
+ containerName string ,
219
+ ) (container.CreateResponse , error ) {
220
+ return container.CreateResponse {}, fakeNotFound {image : tc .MissingImage }
221
+ },
222
+ imageCreateFunc : func (ctx context.Context , parentReference string , options image.CreateOptions ) (io.ReadCloser , error ) {
223
+ defer func () { pullCounter ++ }()
224
+ return io .NopCloser (strings .NewReader ("" )), nil
225
+ },
226
+ infoFunc : func () (system.Info , error ) {
227
+ return system.Info {IndexServerAddress : "https://indexserver.example.com" }, nil
228
+ },
229
+ }
230
+ fakeCLI := test .NewFakeCli (client )
231
+ _ , err := createContainer (context .Background (), fakeCLI , config , & createOptions {
232
+ name : "name" ,
233
+ platform : runtime .GOOS ,
234
+ untrusted : true ,
235
+ pull : PullImageMissing ,
236
+ })
237
+
238
+ assert .Check (t , is .ErrorContains (err , tc .ExpectedErrMsg ))
239
+ assert .Check (t , is .Equal (tc .ExpectedPulls , pullCounter ))
240
+ })
241
+ }
242
+ }
243
+
163
244
func TestCreateContainerImagePullPolicyInvalid (t * testing.T ) {
164
245
cases := []struct {
165
246
PullPolicy string
@@ -378,7 +459,17 @@ func TestCreateContainerWithProxyConfig(t *testing.T) {
378
459
assert .NilError (t , err )
379
460
}
380
461
381
- type fakeNotFound struct {}
462
+ type fakeNotFound struct {
463
+ image string
464
+ }
465
+
466
+ func (f fakeNotFound ) NotFound () {}
467
+ func (f fakeNotFound ) Error () string {
468
+ img := "fake"
382
469
383
- func (f fakeNotFound ) NotFound () {}
384
- func (f fakeNotFound ) Error () string { return "error fake not found" }
470
+ if f .image != "" {
471
+ img = f .image
472
+ }
473
+
474
+ return "No such image: " + img
475
+ }
0 commit comments