@@ -17,6 +17,7 @@ import (
17
17
18
18
type FakeDocker struct {
19
19
pushImageFunc func (opts docker.PushImageOptions , auth docker.AuthConfiguration ) error
20
+ pullImageFunc func (opts docker.PullImageOptions , auth docker.AuthConfiguration ) error
20
21
buildImageFunc func (opts docker.BuildImageOptions ) error
21
22
removeImageFunc func (name string ) error
22
23
@@ -51,6 +52,20 @@ func fakePushImageFunc(opts docker.PushImageOptions, auth docker.AuthConfigurati
51
52
}
52
53
return nil
53
54
}
55
+
56
+ func fakePullImageFunc (opts docker.PullImageOptions , auth docker.AuthConfiguration ) error {
57
+ switch opts .Repository {
58
+ case "repo_test_succ_foo_bar" :
59
+ return nil
60
+ case "repo_test_err_exist_foo_bar" :
61
+ fooBarRunTimes ++
62
+ return errors .New (RetriableErrors [0 ])
63
+ case "repo_test_err_no_exist_foo_bar" :
64
+ return errors .New ("no_exist_err_foo_bar" )
65
+ }
66
+ return nil
67
+ }
68
+
54
69
func (d * FakeDocker ) BuildImage (opts docker.BuildImageOptions ) error {
55
70
if d .buildImageFunc != nil {
56
71
return d .buildImageFunc (opts )
@@ -77,6 +92,9 @@ func (d *FakeDocker) DownloadFromContainer(id string, opts docker.DownloadFromCo
77
92
return nil
78
93
}
79
94
func (d * FakeDocker ) PullImage (opts docker.PullImageOptions , auth docker.AuthConfiguration ) error {
95
+ if d .pullImageFunc != nil {
96
+ return d .pullImageFunc (opts , auth )
97
+ }
80
98
return nil
81
99
}
82
100
func (d * FakeDocker ) RemoveContainer (opts docker.RemoveContainerOptions ) error {
@@ -141,23 +159,23 @@ func TestTagImage(t *testing.T) {
141
159
func TestPushImage (t * testing.T ) {
142
160
var testImageName string
143
161
144
- bakRetryCount := DefaultPushRetryCount
145
- bakRetryDelay := DefaultPushRetryDelay
162
+ bakRetryCount := DefaultPushOrPullRetryCount
163
+ bakRetryDelay := DefaultPushOrPullRetryDelay
146
164
147
165
fakeDocker := NewFakeDockerClient ()
148
166
fakeDocker .pushImageFunc = fakePushImageFunc
149
167
testAuth := docker.AuthConfiguration {
150
- Username : "usernname_foo_bar " ,
168
+ Username : "username_foo_bar " ,
151
169
Password : "password_foo_bar" ,
152
170
Email : "email_foo_bar" ,
153
171
ServerAddress : "serveraddress_foo_bar" ,
154
172
}
155
173
156
174
//make test quickly, and recover the value after testing
157
- DefaultPushRetryCount = 2
158
- defer func () { DefaultPushRetryCount = bakRetryCount }()
159
- DefaultPushRetryDelay = 1
160
- defer func () { DefaultPushRetryDelay = bakRetryDelay }()
175
+ DefaultPushOrPullRetryCount = 2
176
+ defer func () { DefaultPushOrPullRetryCount = bakRetryCount }()
177
+ DefaultPushOrPullRetryDelay = 1
178
+ defer func () { DefaultPushOrPullRetryDelay = bakRetryDelay }()
161
179
162
180
//expect succ
163
181
testImageName = "repo_foo_bar:tag_test_succ_foo_bar"
@@ -172,7 +190,7 @@ func TestPushImage(t *testing.T) {
172
190
t .Errorf ("Unexpect push image : %v, want error" , err )
173
191
}
174
192
//expect run 3 times
175
- if fooBarRunTimes != (DefaultPushRetryCount + 1 ) {
193
+ if fooBarRunTimes != (DefaultPushOrPullRetryCount + 1 ) {
176
194
t .Errorf ("Unexpect run times : %d, we expect run three times" , fooBarRunTimes )
177
195
}
178
196
@@ -184,6 +202,52 @@ func TestPushImage(t *testing.T) {
184
202
defer func () { fooBarRunTimes = 0 }()
185
203
}
186
204
205
+ func TestPullImage (t * testing.T ) {
206
+ var testImageName string
207
+
208
+ bakRetryCount := DefaultPushOrPullRetryCount
209
+ bakRetryDelay := DefaultPushOrPullRetryDelay
210
+
211
+ fakeDocker := NewFakeDockerClient ()
212
+ fakeDocker .pullImageFunc = fakePullImageFunc
213
+ testAuth := docker.AuthConfiguration {
214
+ Username : "username_foo_bar" ,
215
+ Password : "password_foo_bar" ,
216
+ Email : "email_foo_bar" ,
217
+ ServerAddress : "serveraddress_foo_bar" ,
218
+ }
219
+
220
+ //make test quickly, and recover the value after testing
221
+ DefaultPushOrPullRetryCount = 2
222
+ defer func () { DefaultPushOrPullRetryCount = bakRetryCount }()
223
+ DefaultPushOrPullRetryDelay = 1
224
+ defer func () { DefaultPushOrPullRetryDelay = bakRetryDelay }()
225
+
226
+ //expect succ
227
+ testImageName = "repo_test_succ_foo_bar"
228
+ if err := pullImage (fakeDocker , testImageName , testAuth ); err != nil {
229
+ t .Errorf ("Unexpect pull image : %v, want succ" , err )
230
+ }
231
+
232
+ //expect fail
233
+ testImageName = "repo_test_err_exist_foo_bar"
234
+ err := pullImage (fakeDocker , testImageName , testAuth )
235
+ if err == nil {
236
+ t .Errorf ("Unexpect pull image : %v, want error" , err )
237
+ }
238
+ //expect run 3 times
239
+ if fooBarRunTimes != (DefaultPushOrPullRetryCount + 1 ) {
240
+ t .Errorf ("Unexpect run times : %d, we expect run three times" , fooBarRunTimes )
241
+ }
242
+
243
+ //expect fail
244
+ testImageName = "repo_test_err_no_exist_foo_bar"
245
+ if err := pullImage (fakeDocker , testImageName , testAuth ); err == nil {
246
+ t .Errorf ("Unexpect pull image : %v, want error" , err )
247
+ }
248
+ defer func () { fooBarRunTimes = 0 }()
249
+ }
250
+
187
251
func TestGetContainerNameOrID (t * testing.T ) {
188
252
c := & docker.Container {}
189
253
c .ID = "ID"
0 commit comments