cli/command/container: TestCreateContainerImagePullPolicy: use sub-tests

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-06-08 16:34:08 +02:00
parent 0c5adb2e98
commit c2c6fbe23c
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 48 additions and 44 deletions

View File

@ -79,8 +79,10 @@ func TestCIDFileCloseWithWrite(t *testing.T) {
} }
func TestCreateContainerImagePullPolicy(t *testing.T) { func TestCreateContainerImagePullPolicy(t *testing.T) {
imageName := "does-not-exist-locally" const (
containerID := "abcdef" imageName = "does-not-exist-locally"
containerID = "abcdef"
)
config := &containerConfig{ config := &containerConfig{
Config: &container.Config{ Config: &container.Config{
Image: imageName, Image: imageName,
@ -110,50 +112,52 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
ExpectedErrMsg: "error fake not found", ExpectedErrMsg: "error fake not found",
}, },
} }
for _, c := range cases { for _, tc := range cases {
c := c tc := tc
pullCounter := 0 t.Run(tc.PullPolicy, func(t *testing.T) {
pullCounter := 0
client := &fakeClient{ client := &fakeClient{
createContainerFunc: func( createContainerFunc: func(
config *container.Config, config *container.Config,
hostConfig *container.HostConfig, hostConfig *container.HostConfig,
networkingConfig *network.NetworkingConfig, networkingConfig *network.NetworkingConfig,
platform *specs.Platform, platform *specs.Platform,
containerName string, containerName string,
) (container.CreateResponse, error) { ) (container.CreateResponse, error) {
defer func() { c.ResponseCounter++ }() defer func() { tc.ResponseCounter++ }()
switch c.ResponseCounter { switch tc.ResponseCounter {
case 0: case 0:
return container.CreateResponse{}, fakeNotFound{} return container.CreateResponse{}, fakeNotFound{}
default: default:
return container.CreateResponse{ID: containerID}, nil return container.CreateResponse{ID: containerID}, nil
} }
}, },
imageCreateFunc: func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) { imageCreateFunc: func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
defer func() { pullCounter++ }() defer func() { pullCounter++ }()
return io.NopCloser(strings.NewReader("")), nil return io.NopCloser(strings.NewReader("")), nil
}, },
infoFunc: func() (types.Info, error) { infoFunc: func() (types.Info, error) {
return types.Info{IndexServerAddress: "https://indexserver.example.com"}, nil return types.Info{IndexServerAddress: "https://indexserver.example.com"}, nil
}, },
} }
cli := test.NewFakeCli(client) fakeCLI := test.NewFakeCli(client)
body, err := createContainer(context.Background(), cli, config, &createOptions{ body, err := createContainer(context.Background(), fakeCLI, config, &createOptions{
name: "name", name: "name",
platform: runtime.GOOS, platform: runtime.GOOS,
untrusted: true, untrusted: true,
pull: c.PullPolicy, pull: tc.PullPolicy,
})
if tc.ExpectedErrMsg != "" {
assert.Check(t, is.ErrorContains(err, tc.ExpectedErrMsg))
} else {
assert.Check(t, err)
assert.Check(t, is.DeepEqual(tc.ExpectedBody, *body))
}
assert.Check(t, is.Equal(tc.ExpectedPulls, pullCounter))
}) })
if c.ExpectedErrMsg != "" {
assert.ErrorContains(t, err, c.ExpectedErrMsg)
} else {
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(c.ExpectedBody, *body))
}
assert.Check(t, is.Equal(c.ExpectedPulls, pullCounter))
} }
} }