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,8 +112,9 @@ 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
t.Run(tc.PullPolicy, func(t *testing.T) {
pullCounter := 0 pullCounter := 0
client := &fakeClient{ client := &fakeClient{
@ -122,8 +125,8 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
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:
@ -138,22 +141,23 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
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 c.ExpectedErrMsg != "" { if tc.ExpectedErrMsg != "" {
assert.ErrorContains(t, err, c.ExpectedErrMsg) assert.Check(t, is.ErrorContains(err, tc.ExpectedErrMsg))
} else { } else {
assert.NilError(t, err) assert.Check(t, err)
assert.Check(t, is.DeepEqual(c.ExpectedBody, *body)) assert.Check(t, is.DeepEqual(tc.ExpectedBody, *body))
} }
assert.Check(t, is.Equal(c.ExpectedPulls, pullCounter)) assert.Check(t, is.Equal(tc.ExpectedPulls, pullCounter))
})
} }
} }