diff --git a/cli/command/container/create_test.go b/cli/command/container/create_test.go index 725b6c9326..be7c039eb1 100644 --- a/cli/command/container/create_test.go +++ b/cli/command/container/create_test.go @@ -79,8 +79,10 @@ func TestCIDFileCloseWithWrite(t *testing.T) { } func TestCreateContainerImagePullPolicy(t *testing.T) { - imageName := "does-not-exist-locally" - containerID := "abcdef" + const ( + imageName = "does-not-exist-locally" + containerID = "abcdef" + ) config := &containerConfig{ Config: &container.Config{ Image: imageName, @@ -110,50 +112,52 @@ func TestCreateContainerImagePullPolicy(t *testing.T) { ExpectedErrMsg: "error fake not found", }, } - for _, c := range cases { - c := c - pullCounter := 0 + for _, tc := range cases { + tc := tc + t.Run(tc.PullPolicy, func(t *testing.T) { + pullCounter := 0 - client := &fakeClient{ - createContainerFunc: func( - config *container.Config, - hostConfig *container.HostConfig, - networkingConfig *network.NetworkingConfig, - platform *specs.Platform, - containerName string, - ) (container.CreateResponse, error) { - defer func() { c.ResponseCounter++ }() - switch c.ResponseCounter { - case 0: - return container.CreateResponse{}, fakeNotFound{} - default: - return container.CreateResponse{ID: containerID}, nil - } - }, - imageCreateFunc: func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) { - defer func() { pullCounter++ }() - return io.NopCloser(strings.NewReader("")), nil - }, - infoFunc: func() (types.Info, error) { - return types.Info{IndexServerAddress: "https://indexserver.example.com"}, nil - }, - } - cli := test.NewFakeCli(client) - body, err := createContainer(context.Background(), cli, config, &createOptions{ - name: "name", - platform: runtime.GOOS, - untrusted: true, - pull: c.PullPolicy, + client := &fakeClient{ + createContainerFunc: func( + config *container.Config, + hostConfig *container.HostConfig, + networkingConfig *network.NetworkingConfig, + platform *specs.Platform, + containerName string, + ) (container.CreateResponse, error) { + defer func() { tc.ResponseCounter++ }() + switch tc.ResponseCounter { + case 0: + return container.CreateResponse{}, fakeNotFound{} + default: + return container.CreateResponse{ID: containerID}, nil + } + }, + imageCreateFunc: func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) { + defer func() { pullCounter++ }() + return io.NopCloser(strings.NewReader("")), nil + }, + infoFunc: func() (types.Info, error) { + return types.Info{IndexServerAddress: "https://indexserver.example.com"}, nil + }, + } + fakeCLI := test.NewFakeCli(client) + body, err := createContainer(context.Background(), fakeCLI, config, &createOptions{ + name: "name", + platform: runtime.GOOS, + untrusted: true, + 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)) } }