Skip to content

Commit 4192ca8

Browse files
committed
pkg/cri/server: sub-test uses array and capture range var
Using array to build sub-tests is to avoid random pick. The shuffle thing should be handled by go-test framework. And we should capture range var before runing sub-test. Signed-off-by: Wei Fu <[email protected]>
1 parent ffc70c4 commit 4192ca8

23 files changed

Lines changed: 948 additions & 479 deletions

pkg/cri/server/container_create_linux_test.go

Lines changed: 260 additions & 132 deletions
Large diffs are not rendered by default.

pkg/cri/server/container_create_test.go

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,21 @@ func TestPodAnnotationPassthroughContainerSpec(t *testing.T) {
8181
testContainerName := "container-name"
8282
testPid := uint32(1234)
8383

84-
for desc, test := range map[string]struct {
84+
for _, test := range []struct {
85+
desc string
8586
podAnnotations []string
8687
configChange func(*runtime.PodSandboxConfig)
8788
specCheck func(*testing.T, *runtimespec.Spec)
8889
}{
89-
"a passthrough annotation should be passed as an OCI annotation": {
90+
{
91+
desc: "a passthrough annotation should be passed as an OCI annotation",
9092
podAnnotations: []string{"c"},
9193
specCheck: func(t *testing.T, spec *runtimespec.Spec) {
9294
assert.Equal(t, spec.Annotations["c"], "d")
9395
},
9496
},
95-
"a non-passthrough annotation should not be passed as an OCI annotation": {
97+
{
98+
desc: "a non-passthrough annotation should not be passed as an OCI annotation",
9699
configChange: func(c *runtime.PodSandboxConfig) {
97100
c.Annotations["d"] = "e"
98101
},
@@ -103,7 +106,8 @@ func TestPodAnnotationPassthroughContainerSpec(t *testing.T) {
103106
assert.False(t, ok)
104107
},
105108
},
106-
"passthrough annotations should support wildcard match": {
109+
{
110+
desc: "passthrough annotations should support wildcard match",
107111
configChange: func(c *runtime.PodSandboxConfig) {
108112
c.Annotations["t.f"] = "j"
109113
c.Annotations["z.g"] = "o"
@@ -124,7 +128,8 @@ func TestPodAnnotationPassthroughContainerSpec(t *testing.T) {
124128
},
125129
},
126130
} {
127-
t.Run(desc, func(t *testing.T) {
131+
test := test
132+
t.Run(test.desc, func(t *testing.T) {
128133
c := newTestCRIService()
129134
containerConfig, sandboxConfig, imageConfig, specCheck := getCreateContainerTestData()
130135
if test.configChange != nil {
@@ -147,50 +152,58 @@ func TestPodAnnotationPassthroughContainerSpec(t *testing.T) {
147152
}
148153

149154
func TestContainerSpecCommand(t *testing.T) {
150-
for desc, test := range map[string]struct {
155+
for _, test := range []struct {
156+
desc string
151157
criEntrypoint []string
152158
criArgs []string
153159
imageEntrypoint []string
154160
imageArgs []string
155161
expected []string
156162
expectErr bool
157163
}{
158-
"should use cri entrypoint if it's specified": {
164+
{
165+
desc: "should use cri entrypoint if it's specified",
159166
criEntrypoint: []string{"a", "b"},
160167
imageEntrypoint: []string{"c", "d"},
161168
imageArgs: []string{"e", "f"},
162169
expected: []string{"a", "b"},
163170
},
164-
"should use cri entrypoint if it's specified even if it's empty": {
171+
{
172+
desc: "should use cri entrypoint if it's specified even if it's empty",
165173
criEntrypoint: []string{},
166174
criArgs: []string{"a", "b"},
167175
imageEntrypoint: []string{"c", "d"},
168176
imageArgs: []string{"e", "f"},
169177
expected: []string{"a", "b"},
170178
},
171-
"should use cri entrypoint and args if they are specified": {
179+
{
180+
desc: "should use cri entrypoint and args if they are specified",
172181
criEntrypoint: []string{"a", "b"},
173182
criArgs: []string{"c", "d"},
174183
imageEntrypoint: []string{"e", "f"},
175184
imageArgs: []string{"g", "h"},
176185
expected: []string{"a", "b", "c", "d"},
177186
},
178-
"should use image entrypoint if cri entrypoint is not specified": {
187+
{
188+
desc: "should use image entrypoint if cri entrypoint is not specified",
179189
criArgs: []string{"a", "b"},
180190
imageEntrypoint: []string{"c", "d"},
181191
imageArgs: []string{"e", "f"},
182192
expected: []string{"c", "d", "a", "b"},
183193
},
184-
"should use image args if both cri entrypoint and args are not specified": {
194+
{
195+
desc: "should use image args if both cri entrypoint and args are not specified",
185196
imageEntrypoint: []string{"c", "d"},
186197
imageArgs: []string{"e", "f"},
187198
expected: []string{"c", "d", "e", "f"},
188199
},
189-
"should return error if both entrypoint and args are empty": {
200+
{
201+
desc: "should return error if both entrypoint and args are empty",
190202
expectErr: true,
191203
},
192204
} {
193-
t.Run(desc, func(t *testing.T) {
205+
test := test
206+
t.Run(test.desc, func(t *testing.T) {
194207
config, _, imageConfig, _ := getCreateContainerTestData()
195208
config.Command = test.criEntrypoint
196209
config.Args = test.criArgs
@@ -204,19 +217,21 @@ func TestContainerSpecCommand(t *testing.T) {
204217
return
205218
}
206219
assert.NoError(t, err)
207-
assert.Equal(t, test.expected, spec.Process.Args, desc)
220+
assert.Equal(t, test.expected, spec.Process.Args, test.desc)
208221
})
209222
}
210223
}
211224

212225
func TestVolumeMounts(t *testing.T) {
213226
testContainerRootDir := "test-container-root"
214-
for desc, test := range map[string]struct {
227+
for _, test := range []struct {
228+
desc string
215229
criMounts []*runtime.Mount
216230
imageVolumes map[string]struct{}
217231
expectedMountDest []string
218232
}{
219-
"should setup rw mount for image volumes": {
233+
{
234+
desc: "should setup rw mount for image volumes",
220235
imageVolumes: map[string]struct{}{
221236
"/test-volume-1": {},
222237
"/test-volume-2": {},
@@ -226,7 +241,8 @@ func TestVolumeMounts(t *testing.T) {
226241
"/test-volume-2",
227242
},
228243
},
229-
"should skip image volumes if already mounted by CRI": {
244+
{
245+
desc: "should skip image volumes if already mounted by CRI",
230246
criMounts: []*runtime.Mount{
231247
{
232248
ContainerPath: "/test-volume-1",
@@ -241,7 +257,8 @@ func TestVolumeMounts(t *testing.T) {
241257
"/test-volume-2",
242258
},
243259
},
244-
"should compare and return cleanpath": {
260+
{
261+
desc: "should compare and return cleanpath",
245262
criMounts: []*runtime.Mount{
246263
{
247264
ContainerPath: "/test-volume-1",
@@ -257,7 +274,8 @@ func TestVolumeMounts(t *testing.T) {
257274
},
258275
},
259276
} {
260-
t.Run(desc, func(t *testing.T) {
277+
test := test
278+
t.Run(test.desc, func(t *testing.T) {
261279
config := &imagespec.ImageConfig{
262280
Volumes: test.imageVolumes,
263281
}
@@ -294,14 +312,16 @@ func TestContainerAnnotationPassthroughContainerSpec(t *testing.T) {
294312
testContainerName := "container-name"
295313
testPid := uint32(1234)
296314

297-
for desc, test := range map[string]struct {
315+
for _, test := range []struct {
316+
desc string
298317
podAnnotations []string
299318
containerAnnotations []string
300319
podConfigChange func(*runtime.PodSandboxConfig)
301320
configChange func(*runtime.ContainerConfig)
302321
specCheck func(*testing.T, *runtimespec.Spec)
303322
}{
304-
"passthrough annotations from pod and container should be passed as an OCI annotation": {
323+
{
324+
desc: "passthrough annotations from pod and container should be passed as an OCI annotation",
305325
podConfigChange: func(p *runtime.PodSandboxConfig) {
306326
p.Annotations["pod.annotation.1"] = "1"
307327
p.Annotations["pod.annotation.2"] = "2"
@@ -327,7 +347,8 @@ func TestContainerAnnotationPassthroughContainerSpec(t *testing.T) {
327347
assert.False(t, ok)
328348
},
329349
},
330-
"passthrough annotations from pod and container should support wildcard": {
350+
{
351+
desc: "passthrough annotations from pod and container should support wildcard",
331352
podConfigChange: func(p *runtime.PodSandboxConfig) {
332353
p.Annotations["pod.annotation.1"] = "1"
333354
p.Annotations["pod.annotation.2"] = "2"
@@ -349,7 +370,8 @@ func TestContainerAnnotationPassthroughContainerSpec(t *testing.T) {
349370
assert.Equal(t, "3", spec.Annotations["pod.annotation.3"])
350371
},
351372
},
352-
"annotations should not pass through if no passthrough annotations are configured": {
373+
{
374+
desc: "annotations should not pass through if no passthrough annotations are configured",
353375
podConfigChange: func(p *runtime.PodSandboxConfig) {
354376
p.Annotations["pod.annotation.1"] = "1"
355377
p.Annotations["pod.annotation.2"] = "2"
@@ -378,7 +400,8 @@ func TestContainerAnnotationPassthroughContainerSpec(t *testing.T) {
378400
},
379401
},
380402
} {
381-
t.Run(desc, func(t *testing.T) {
403+
test := test
404+
t.Run(test.desc, func(t *testing.T) {
382405
c := newTestCRIService()
383406
containerConfig, sandboxConfig, imageConfig, specCheck := getCreateContainerTestData()
384407
if test.configChange != nil {
@@ -440,20 +463,24 @@ func TestRuntimeSnapshotter(t *testing.T) {
440463
Snapshotter: "devmapper",
441464
}
442465

443-
for desc, test := range map[string]struct {
466+
for _, test := range []struct {
467+
desc string
444468
runtime config.Runtime
445469
expectSnapshotter string
446470
}{
447-
"should return default snapshotter when runtime.Snapshotter is not set": {
471+
{
472+
desc: "should return default snapshotter when runtime.Snapshotter is not set",
448473
runtime: defaultRuntime,
449474
expectSnapshotter: config.DefaultConfig().Snapshotter,
450475
},
451-
"should return overridden snapshotter when runtime.Snapshotter is set": {
476+
{
477+
desc: "should return overridden snapshotter when runtime.Snapshotter is set",
452478
runtime: fooRuntime,
453479
expectSnapshotter: "devmapper",
454480
},
455481
} {
456-
t.Run(desc, func(t *testing.T) {
482+
test := test
483+
t.Run(test.desc, func(t *testing.T) {
457484
cri := newTestCRIService()
458485
cri.config = config.Config{
459486
PluginConfig: config.DefaultConfig(),

pkg/cri/server/container_create_windows_test.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,33 +213,39 @@ func TestHostProcessRequirements(t *testing.T) {
213213
containerConfig, sandboxConfig, imageConfig, _ := getCreateContainerTestData()
214214
ociRuntime := config.Runtime{}
215215
c := newTestCRIService()
216-
for desc, test := range map[string]struct {
216+
for _, test := range []struct {
217+
desc string
217218
containerHostProcess bool
218219
sandboxHostProcess bool
219220
expectError bool
220221
}{
221-
"hostprocess container in non-hostprocess sandbox should fail": {
222+
{
223+
desc: "hostprocess container in non-hostprocess sandbox should fail",
222224
containerHostProcess: true,
223225
sandboxHostProcess: false,
224226
expectError: true,
225227
},
226-
"hostprocess container in hostprocess sandbox should be fine": {
228+
{
229+
desc: "hostprocess container in hostprocess sandbox should be fine",
227230
containerHostProcess: true,
228231
sandboxHostProcess: true,
229232
expectError: false,
230233
},
231-
"non-hostprocess container in hostprocess sandbox should fail": {
234+
{
235+
desc: "non-hostprocess container in hostprocess sandbox should fail",
232236
containerHostProcess: false,
233237
sandboxHostProcess: true,
234238
expectError: true,
235239
},
236-
"non-hostprocess container in non-hostprocess sandbox should be fine": {
240+
{
241+
desc: "non-hostprocess container in non-hostprocess sandbox should be fine",
237242
containerHostProcess: false,
238243
sandboxHostProcess: false,
239244
expectError: false,
240245
},
241246
} {
242-
t.Run(desc, func(t *testing.T) {
247+
test := test
248+
t.Run(test.desc, func(t *testing.T) {
243249
containerConfig.Windows.SecurityContext.HostProcess = test.containerHostProcess
244250
sandboxConfig.Windows.SecurityContext = &runtime.WindowsSandboxSecurityContext{
245251
HostProcess: test.sandboxHostProcess,
@@ -262,7 +268,8 @@ func TestEntrypointAndCmdForArgsEscaped(t *testing.T) {
262268
nsPath := "test-ns"
263269
c := newTestCRIService()
264270

265-
for name, test := range map[string]struct {
271+
for _, test := range []struct {
272+
name string
266273
imgEntrypoint []string
267274
imgCmd []string
268275
command []string
@@ -272,7 +279,8 @@ func TestEntrypointAndCmdForArgsEscaped(t *testing.T) {
272279
ArgsEscaped bool
273280
}{
274281
// override image entrypoint and cmd in shell form with container args and verify expected runtime spec
275-
"TestShellFormImgEntrypointCmdWithCtrArgs": {
282+
{
283+
name: "TestShellFormImgEntrypointCmdWithCtrArgs",
276284
imgEntrypoint: []string{`"C:\My Folder\MyProcess.exe" -arg1 "test value"`},
277285
imgCmd: []string{`cmd -args "hello world"`},
278286
command: nil,
@@ -282,7 +290,8 @@ func TestEntrypointAndCmdForArgsEscaped(t *testing.T) {
282290
ArgsEscaped: true,
283291
},
284292
// check image entrypoint and cmd in shell form without overriding with container command and args and verify expected runtime spec
285-
"TestShellFormImgEntrypointCmdWithoutCtrArgs": {
293+
{
294+
name: "TestShellFormImgEntrypointCmdWithoutCtrArgs",
286295
imgEntrypoint: []string{`"C:\My Folder\MyProcess.exe" -arg1 "test value"`},
287296
imgCmd: []string{`cmd -args "hello world"`},
288297
command: nil,
@@ -292,7 +301,8 @@ func TestEntrypointAndCmdForArgsEscaped(t *testing.T) {
292301
ArgsEscaped: true,
293302
},
294303
// override image entrypoint and cmd by container command and args in shell form and verify expected runtime spec
295-
"TestShellFormImgEntrypointCmdWithCtrEntrypointAndArgs": {
304+
{
305+
name: "TestShellFormImgEntrypointCmdWithCtrEntrypointAndArgs",
296306
imgEntrypoint: []string{`"C:\My Folder\MyProcess.exe" -arg1 "test value"`},
297307
imgCmd: []string{`cmd -args "hello world"`},
298308
command: []string{`C:\My Folder\MyProcess.exe`, "-arg1", "additional test value"},
@@ -302,7 +312,8 @@ func TestEntrypointAndCmdForArgsEscaped(t *testing.T) {
302312
ArgsEscaped: true,
303313
},
304314
// override image cmd by container args in exec form and verify expected runtime spec
305-
"TestExecFormImgEntrypointCmdWithCtrArgs": {
315+
{
316+
name: "TestExecFormImgEntrypointCmdWithCtrArgs",
306317
imgEntrypoint: []string{`C:\My Folder\MyProcess.exe`, "-arg1", "test value"},
307318
imgCmd: []string{"cmd", "-args", "hello world"},
308319
command: nil,
@@ -312,7 +323,8 @@ func TestEntrypointAndCmdForArgsEscaped(t *testing.T) {
312323
ArgsEscaped: false,
313324
},
314325
// check image entrypoint and cmd in exec form without overriding with container command and args and verify expected runtime spec
315-
"TestExecFormImgEntrypointCmdWithoutCtrArgs": {
326+
{
327+
name: "TestExecFormImgEntrypointCmdWithoutCtrArgs",
316328
imgEntrypoint: []string{`C:\My Folder\MyProcess.exe`, "-arg1", "test value"},
317329
imgCmd: []string{"cmd", "-args", "hello world"},
318330
command: nil,
@@ -322,7 +334,8 @@ func TestEntrypointAndCmdForArgsEscaped(t *testing.T) {
322334
ArgsEscaped: false,
323335
},
324336
} {
325-
t.Run(name, func(t *testing.T) {
337+
test := test
338+
t.Run(test.name, func(t *testing.T) {
326339
imageConfig := &imagespec.ImageConfig{
327340
Entrypoint: test.imgEntrypoint,
328341
Cmd: test.imgCmd,

0 commit comments

Comments
 (0)