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) {
imageName := "does-not-exist-locally"
containerID := "abcdef"
const (
imageName = "does-not-exist-locally"
containerID = "abcdef"
)
config := &containerConfig{
Config: &container.Config{
Image: imageName,
@ -110,8 +112,9 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
ExpectedErrMsg: "error fake not found",
},
}
for _, c := range cases {
c := c
for _, tc := range cases {
tc := tc
t.Run(tc.PullPolicy, func(t *testing.T) {
pullCounter := 0
client := &fakeClient{
@ -122,8 +125,8 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
platform *specs.Platform,
containerName string,
) (container.CreateResponse, error) {
defer func() { c.ResponseCounter++ }()
switch c.ResponseCounter {
defer func() { tc.ResponseCounter++ }()
switch tc.ResponseCounter {
case 0:
return container.CreateResponse{}, fakeNotFound{}
default:
@ -138,22 +141,23 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
return types.Info{IndexServerAddress: "https://indexserver.example.com"}, nil
},
}
cli := test.NewFakeCli(client)
body, err := createContainer(context.Background(), cli, config, &createOptions{
fakeCLI := test.NewFakeCli(client)
body, err := createContainer(context.Background(), fakeCLI, config, &createOptions{
name: "name",
platform: runtime.GOOS,
untrusted: true,
pull: c.PullPolicy,
pull: tc.PullPolicy,
})
if c.ExpectedErrMsg != "" {
assert.ErrorContains(t, err, c.ExpectedErrMsg)
if tc.ExpectedErrMsg != "" {
assert.Check(t, is.ErrorContains(err, tc.ExpectedErrMsg))
} else {
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(c.ExpectedBody, *body))
assert.Check(t, err)
assert.Check(t, is.DeepEqual(tc.ExpectedBody, *body))
}
assert.Check(t, is.Equal(c.ExpectedPulls, pullCounter))
assert.Check(t, is.Equal(tc.ExpectedPulls, pullCounter))
})
}
}