mirror of https://github.com/docker/cli.git
Use single table for all ContainerImagePullPolicy tests
- Cleans up assertions - Centralizes and simplifies handler functions Signed-off-by: Zander Mackie <zmackie@gmail.com>
This commit is contained in:
parent
ffba7659cc
commit
483c53ad9d
|
@ -18,7 +18,6 @@ import (
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/docker/api/types/network"
|
"github.com/docker/docker/api/types/network"
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"gotest.tools/assert"
|
"gotest.tools/assert"
|
||||||
is "gotest.tools/assert/cmp"
|
is "gotest.tools/assert/cmp"
|
||||||
"gotest.tools/fs"
|
"gotest.tools/fs"
|
||||||
|
@ -76,58 +75,39 @@ func TestCIDFileCloseWithWrite(t *testing.T) {
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateContainerPullsImageIfMissing(t *testing.T) {
|
func TestCreateContainerImagePullPolicy(t *testing.T) {
|
||||||
imageName := "does-not-exist-locally"
|
imageName := "does-not-exist-locally"
|
||||||
responseCounter := 0
|
|
||||||
containerID := "abcdef"
|
containerID := "abcdef"
|
||||||
|
|
||||||
client := &fakeClient{
|
|
||||||
createContainerFunc: func(
|
|
||||||
config *container.Config,
|
|
||||||
hostConfig *container.HostConfig,
|
|
||||||
networkingConfig *network.NetworkingConfig,
|
|
||||||
containerName string,
|
|
||||||
) (container.ContainerCreateCreatedBody, error) {
|
|
||||||
defer func() { responseCounter++ }()
|
|
||||||
switch responseCounter {
|
|
||||||
case 0:
|
|
||||||
return container.ContainerCreateCreatedBody{}, fakeNotFound{}
|
|
||||||
case 1:
|
|
||||||
return container.ContainerCreateCreatedBody{ID: containerID}, nil
|
|
||||||
default:
|
|
||||||
return container.ContainerCreateCreatedBody{}, errors.New("unexpected")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
imageCreateFunc: func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
|
|
||||||
return ioutil.NopCloser(strings.NewReader("")), nil
|
|
||||||
},
|
|
||||||
infoFunc: func() (types.Info, error) {
|
|
||||||
return types.Info{IndexServerAddress: "http://indexserver"}, nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cli := test.NewFakeCli(client)
|
|
||||||
config := &containerConfig{
|
config := &containerConfig{
|
||||||
Config: &container.Config{
|
Config: &container.Config{
|
||||||
Image: imageName,
|
Image: imageName,
|
||||||
},
|
},
|
||||||
HostConfig: &container.HostConfig{},
|
HostConfig: &container.HostConfig{},
|
||||||
}
|
}
|
||||||
body, err := createContainer(context.Background(), cli, config, &createOptions{
|
|
||||||
name: "name",
|
|
||||||
platform: runtime.GOOS,
|
|
||||||
untrusted: true,
|
|
||||||
pull: PullImageMissing,
|
|
||||||
})
|
|
||||||
assert.NilError(t, err)
|
|
||||||
expected := container.ContainerCreateCreatedBody{ID: containerID}
|
|
||||||
assert.Check(t, is.DeepEqual(expected, *body))
|
|
||||||
stderr := cli.ErrBuffer().String()
|
|
||||||
assert.Check(t, is.Contains(stderr, "Unable to find image 'does-not-exist-locally:latest' locally"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreateContainerNeverPullsImage(t *testing.T) {
|
cases := []struct {
|
||||||
imageName := "does-not-exist-locally"
|
PullPolicy string
|
||||||
responseCounter := 0
|
ExpectedPulls int
|
||||||
|
ExpectedBody container.ContainerCreateCreatedBody
|
||||||
|
ExpectedErrMsg string
|
||||||
|
ResponseCounter int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
PullPolicy: PullImageMissing,
|
||||||
|
ExpectedPulls: 1,
|
||||||
|
ExpectedBody: container.ContainerCreateCreatedBody{ID: containerID},
|
||||||
|
}, {
|
||||||
|
PullPolicy: PullImageAlways,
|
||||||
|
ExpectedPulls: 1,
|
||||||
|
ExpectedBody: container.ContainerCreateCreatedBody{ID: containerID},
|
||||||
|
ResponseCounter: 1, // This lets us return a container on the first pull
|
||||||
|
}, {
|
||||||
|
PullPolicy: PullImageNever,
|
||||||
|
ExpectedPulls: 0,
|
||||||
|
ExpectedErrMsg: "error fake not found",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
pullCounter := 0
|
pullCounter := 0
|
||||||
|
|
||||||
client := &fakeClient{
|
client := &fakeClient{
|
||||||
|
@ -137,59 +117,10 @@ func TestCreateContainerNeverPullsImage(t *testing.T) {
|
||||||
networkingConfig *network.NetworkingConfig,
|
networkingConfig *network.NetworkingConfig,
|
||||||
containerName string,
|
containerName string,
|
||||||
) (container.ContainerCreateCreatedBody, error) {
|
) (container.ContainerCreateCreatedBody, error) {
|
||||||
defer func() { responseCounter++ }()
|
defer func() { c.ResponseCounter++ }()
|
||||||
switch responseCounter {
|
switch c.ResponseCounter {
|
||||||
case 0:
|
case 0:
|
||||||
return container.ContainerCreateCreatedBody{}, fakeNotFound{}
|
return container.ContainerCreateCreatedBody{}, fakeNotFound{}
|
||||||
default:
|
|
||||||
return container.ContainerCreateCreatedBody{}, errors.New("unexpected")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
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
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cli := test.NewFakeCli(client)
|
|
||||||
config := &containerConfig{
|
|
||||||
Config: &container.Config{
|
|
||||||
Image: imageName,
|
|
||||||
},
|
|
||||||
HostConfig: &container.HostConfig{},
|
|
||||||
}
|
|
||||||
_, err := createContainer(context.Background(), cli, config, &createOptions{
|
|
||||||
name: "name",
|
|
||||||
platform: runtime.GOOS,
|
|
||||||
untrusted: true,
|
|
||||||
pull: PullImageNever,
|
|
||||||
})
|
|
||||||
assert.ErrorContains(t, err, "fake not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreateContainerAlwaysPullsImage(t *testing.T) {
|
|
||||||
imageName := "does-not-exist-locally"
|
|
||||||
pullTries := 7
|
|
||||||
responseCounter := 0
|
|
||||||
pullCounter := 0
|
|
||||||
containerID := "abcdef"
|
|
||||||
|
|
||||||
client := &fakeClient{
|
|
||||||
createContainerFunc: func(
|
|
||||||
config *container.Config,
|
|
||||||
hostConfig *container.HostConfig,
|
|
||||||
networkingConfig *network.NetworkingConfig,
|
|
||||||
containerName string,
|
|
||||||
) (container.ContainerCreateCreatedBody, error) {
|
|
||||||
defer func() { responseCounter++ }()
|
|
||||||
switch responseCounter {
|
|
||||||
default:
|
default:
|
||||||
return container.ContainerCreateCreatedBody{ID: containerID}, nil
|
return container.ContainerCreateCreatedBody{ID: containerID}, nil
|
||||||
}
|
}
|
||||||
|
@ -203,27 +134,23 @@ func TestCreateContainerAlwaysPullsImage(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cli := test.NewFakeCli(client)
|
cli := test.NewFakeCli(client)
|
||||||
config := &containerConfig{
|
|
||||||
Config: &container.Config{
|
|
||||||
Image: imageName,
|
|
||||||
},
|
|
||||||
HostConfig: &container.HostConfig{},
|
|
||||||
}
|
|
||||||
for i := 0; i < pullTries; i++ {
|
|
||||||
body, err := createContainer(context.Background(), cli, config, &createOptions{
|
body, err := createContainer(context.Background(), cli, config, &createOptions{
|
||||||
name: "name",
|
name: "name",
|
||||||
platform: runtime.GOOS,
|
platform: runtime.GOOS,
|
||||||
untrusted: true,
|
untrusted: true,
|
||||||
pull: PullImageAlways,
|
pull: c.PullPolicy,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if c.ExpectedErrMsg != "" {
|
||||||
|
assert.ErrorContains(t, err, c.ExpectedErrMsg)
|
||||||
|
} else {
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
expected := container.ContainerCreateCreatedBody{ID: containerID}
|
assert.Check(t, is.DeepEqual(c.ExpectedBody, *body))
|
||||||
assert.Check(t, is.DeepEqual(expected, *body))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Check(t, is.Equal(responseCounter, pullCounter))
|
assert.Check(t, is.Equal(c.ExpectedPulls, pullCounter))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewCreateCommandWithContentTrustErrors(t *testing.T) {
|
func TestNewCreateCommandWithContentTrustErrors(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
Loading…
Reference in New Issue