Skip to content

Commit c2c6fbe

Browse files
committed
cli/command/container: TestCreateContainerImagePullPolicy: use sub-tests
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 0c5adb2 commit c2c6fbe

1 file changed

Lines changed: 47 additions & 43 deletions

File tree

cli/command/container/create_test.go

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ func TestCIDFileCloseWithWrite(t *testing.T) {
7979
}
8080

8181
func TestCreateContainerImagePullPolicy(t *testing.T) {
82-
imageName := "does-not-exist-locally"
83-
containerID := "abcdef"
82+
const (
83+
imageName = "does-not-exist-locally"
84+
containerID = "abcdef"
85+
)
8486
config := &containerConfig{
8587
Config: &container.Config{
8688
Image: imageName,
@@ -110,50 +112,52 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
110112
ExpectedErrMsg: "error fake not found",
111113
},
112114
}
113-
for _, c := range cases {
114-
c := c
115-
pullCounter := 0
115+
for _, tc := range cases {
116+
tc := tc
117+
t.Run(tc.PullPolicy, func(t *testing.T) {
118+
pullCounter := 0
116119

117-
client := &fakeClient{
118-
createContainerFunc: func(
119-
config *container.Config,
120-
hostConfig *container.HostConfig,
121-
networkingConfig *network.NetworkingConfig,
122-
platform *specs.Platform,
123-
containerName string,
124-
) (container.CreateResponse, error) {
125-
defer func() { c.ResponseCounter++ }()
126-
switch c.ResponseCounter {
127-
case 0:
128-
return container.CreateResponse{}, fakeNotFound{}
129-
default:
130-
return container.CreateResponse{ID: containerID}, nil
131-
}
132-
},
133-
imageCreateFunc: func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
134-
defer func() { pullCounter++ }()
135-
return io.NopCloser(strings.NewReader("")), nil
136-
},
137-
infoFunc: func() (types.Info, error) {
138-
return types.Info{IndexServerAddress: "https://indexserver.example.com"}, nil
139-
},
140-
}
141-
cli := test.NewFakeCli(client)
142-
body, err := createContainer(context.Background(), cli, config, &createOptions{
143-
name: "name",
144-
platform: runtime.GOOS,
145-
untrusted: true,
146-
pull: c.PullPolicy,
147-
})
120+
client := &fakeClient{
121+
createContainerFunc: func(
122+
config *container.Config,
123+
hostConfig *container.HostConfig,
124+
networkingConfig *network.NetworkingConfig,
125+
platform *specs.Platform,
126+
containerName string,
127+
) (container.CreateResponse, error) {
128+
defer func() { tc.ResponseCounter++ }()
129+
switch tc.ResponseCounter {
130+
case 0:
131+
return container.CreateResponse{}, fakeNotFound{}
132+
default:
133+
return container.CreateResponse{ID: containerID}, nil
134+
}
135+
},
136+
imageCreateFunc: func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
137+
defer func() { pullCounter++ }()
138+
return io.NopCloser(strings.NewReader("")), nil
139+
},
140+
infoFunc: func() (types.Info, error) {
141+
return types.Info{IndexServerAddress: "https://indexserver.example.com"}, nil
142+
},
143+
}
144+
fakeCLI := test.NewFakeCli(client)
145+
body, err := createContainer(context.Background(), fakeCLI, config, &createOptions{
146+
name: "name",
147+
platform: runtime.GOOS,
148+
untrusted: true,
149+
pull: tc.PullPolicy,
150+
})
148151

149-
if c.ExpectedErrMsg != "" {
150-
assert.ErrorContains(t, err, c.ExpectedErrMsg)
151-
} else {
152-
assert.NilError(t, err)
153-
assert.Check(t, is.DeepEqual(c.ExpectedBody, *body))
154-
}
152+
if tc.ExpectedErrMsg != "" {
153+
assert.Check(t, is.ErrorContains(err, tc.ExpectedErrMsg))
154+
} else {
155+
assert.Check(t, err)
156+
assert.Check(t, is.DeepEqual(tc.ExpectedBody, *body))
157+
}
155158

156-
assert.Check(t, is.Equal(c.ExpectedPulls, pullCounter))
159+
assert.Check(t, is.Equal(tc.ExpectedPulls, pullCounter))
160+
})
157161
}
158162
}
159163

0 commit comments

Comments
 (0)