Improve testing of never pull and always pull scenarios

Signed-off-by: Zander Mackie <zmackie@gmail.com>
This commit is contained in:
Zander Mackie 2019-03-01 07:00:25 -05:00 committed by Brian Goff
parent ec56136d61
commit ffba7659cc
1 changed files with 25 additions and 13 deletions

View File

@ -128,6 +128,7 @@ func TestCreateContainerPullsImageIfMissing(t *testing.T) {
func TestCreateContainerNeverPullsImage(t *testing.T) {
imageName := "does-not-exist-locally"
responseCounter := 0
pullCounter := 0
client := &fakeClient{
createContainerFunc: func(
@ -145,7 +146,13 @@ func TestCreateContainerNeverPullsImage(t *testing.T) {
}
},
imageCreateFunc: func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
defer func() { pullCounter++ }()
switch pullCounter {
case 0:
return ioutil.NopCloser(strings.NewReader("")), nil
default:
return nil, errors.New("unexpected pull")
}
},
infoFunc: func() (types.Info, error) {
return types.Info{IndexServerAddress: "http://indexserver"}, nil
@ -169,7 +176,9 @@ func TestCreateContainerNeverPullsImage(t *testing.T) {
func TestCreateContainerAlwaysPullsImage(t *testing.T) {
imageName := "does-not-exist-locally"
pullTries := 7
responseCounter := 0
pullCounter := 0
containerID := "abcdef"
client := &fakeClient{
@ -181,13 +190,12 @@ func TestCreateContainerAlwaysPullsImage(t *testing.T) {
) (container.ContainerCreateCreatedBody, error) {
defer func() { responseCounter++ }()
switch responseCounter {
case 0:
return container.ContainerCreateCreatedBody{ID: containerID}, nil
default:
return container.ContainerCreateCreatedBody{}, errors.New("unexpected")
return container.ContainerCreateCreatedBody{ID: containerID}, nil
}
},
imageCreateFunc: func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
defer func() { pullCounter++ }()
return ioutil.NopCloser(strings.NewReader("")), nil
},
infoFunc: func() (types.Info, error) {
@ -201,6 +209,7 @@ func TestCreateContainerAlwaysPullsImage(t *testing.T) {
},
HostConfig: &container.HostConfig{},
}
for i := 0; i < pullTries; i++ {
body, err := createContainer(context.Background(), cli, config, &createOptions{
name: "name",
platform: runtime.GOOS,
@ -210,6 +219,9 @@ func TestCreateContainerAlwaysPullsImage(t *testing.T) {
assert.NilError(t, err)
expected := container.ContainerCreateCreatedBody{ID: containerID}
assert.Check(t, is.DeepEqual(expected, *body))
}
assert.Check(t, is.Equal(responseCounter, pullCounter))
}
func TestNewCreateCommandWithContentTrustErrors(t *testing.T) {