pkg/cri/sbserver: 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 <fuweid89@gmail.com>
This commit is contained in:
@@ -88,18 +88,21 @@ func TestPodAnnotationPassthroughContainerSpec(t *testing.T) {
|
||||
testContainerName := "container-name"
|
||||
testPid := uint32(1234)
|
||||
|
||||
for desc, test := range map[string]struct {
|
||||
for _, test := range []struct {
|
||||
desc string
|
||||
podAnnotations []string
|
||||
configChange func(*runtime.PodSandboxConfig)
|
||||
specCheck func(*testing.T, *runtimespec.Spec)
|
||||
}{
|
||||
"a passthrough annotation should be passed as an OCI annotation": {
|
||||
{
|
||||
desc: "a passthrough annotation should be passed as an OCI annotation",
|
||||
podAnnotations: []string{"c"},
|
||||
specCheck: func(t *testing.T, spec *runtimespec.Spec) {
|
||||
assert.Equal(t, spec.Annotations["c"], "d")
|
||||
},
|
||||
},
|
||||
"a non-passthrough annotation should not be passed as an OCI annotation": {
|
||||
{
|
||||
desc: "a non-passthrough annotation should not be passed as an OCI annotation",
|
||||
configChange: func(c *runtime.PodSandboxConfig) {
|
||||
c.Annotations["d"] = "e"
|
||||
},
|
||||
@@ -110,7 +113,8 @@ func TestPodAnnotationPassthroughContainerSpec(t *testing.T) {
|
||||
assert.False(t, ok)
|
||||
},
|
||||
},
|
||||
"passthrough annotations should support wildcard match": {
|
||||
{
|
||||
desc: "passthrough annotations should support wildcard match",
|
||||
configChange: func(c *runtime.PodSandboxConfig) {
|
||||
c.Annotations["t.f"] = "j"
|
||||
c.Annotations["z.g"] = "o"
|
||||
@@ -131,7 +135,8 @@ func TestPodAnnotationPassthroughContainerSpec(t *testing.T) {
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
c := newTestCRIService()
|
||||
containerConfig, sandboxConfig, imageConfig, specCheck := getCreateContainerTestData()
|
||||
if test.configChange != nil {
|
||||
@@ -154,7 +159,8 @@ func TestPodAnnotationPassthroughContainerSpec(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContainerSpecCommand(t *testing.T) {
|
||||
for desc, test := range map[string]struct {
|
||||
for _, test := range []struct {
|
||||
desc string
|
||||
criEntrypoint []string
|
||||
criArgs []string
|
||||
imageEntrypoint []string
|
||||
@@ -162,42 +168,49 @@ func TestContainerSpecCommand(t *testing.T) {
|
||||
expected []string
|
||||
expectErr bool
|
||||
}{
|
||||
"should use cri entrypoint if it's specified": {
|
||||
{
|
||||
desc: "should use cri entrypoint if it's specified",
|
||||
criEntrypoint: []string{"a", "b"},
|
||||
imageEntrypoint: []string{"c", "d"},
|
||||
imageArgs: []string{"e", "f"},
|
||||
expected: []string{"a", "b"},
|
||||
},
|
||||
"should use cri entrypoint if it's specified even if it's empty": {
|
||||
{
|
||||
desc: "should use cri entrypoint if it's specified even if it's empty",
|
||||
criEntrypoint: []string{},
|
||||
criArgs: []string{"a", "b"},
|
||||
imageEntrypoint: []string{"c", "d"},
|
||||
imageArgs: []string{"e", "f"},
|
||||
expected: []string{"a", "b"},
|
||||
},
|
||||
"should use cri entrypoint and args if they are specified": {
|
||||
{
|
||||
desc: "should use cri entrypoint and args if they are specified",
|
||||
criEntrypoint: []string{"a", "b"},
|
||||
criArgs: []string{"c", "d"},
|
||||
imageEntrypoint: []string{"e", "f"},
|
||||
imageArgs: []string{"g", "h"},
|
||||
expected: []string{"a", "b", "c", "d"},
|
||||
},
|
||||
"should use image entrypoint if cri entrypoint is not specified": {
|
||||
{
|
||||
desc: "should use image entrypoint if cri entrypoint is not specified",
|
||||
criArgs: []string{"a", "b"},
|
||||
imageEntrypoint: []string{"c", "d"},
|
||||
imageArgs: []string{"e", "f"},
|
||||
expected: []string{"c", "d", "a", "b"},
|
||||
},
|
||||
"should use image args if both cri entrypoint and args are not specified": {
|
||||
{
|
||||
desc: "should use image args if both cri entrypoint and args are not specified",
|
||||
imageEntrypoint: []string{"c", "d"},
|
||||
imageArgs: []string{"e", "f"},
|
||||
expected: []string{"c", "d", "e", "f"},
|
||||
},
|
||||
"should return error if both entrypoint and args are empty": {
|
||||
{
|
||||
desc: "should return error if both entrypoint and args are empty",
|
||||
expectErr: true,
|
||||
},
|
||||
} {
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
config, _, imageConfig, _ := getCreateContainerTestData()
|
||||
config.Command = test.criEntrypoint
|
||||
config.Args = test.criArgs
|
||||
@@ -211,19 +224,21 @@ func TestContainerSpecCommand(t *testing.T) {
|
||||
return
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, test.expected, spec.Process.Args, desc)
|
||||
assert.Equal(t, test.expected, spec.Process.Args, test.desc)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestVolumeMounts(t *testing.T) {
|
||||
testContainerRootDir := "test-container-root"
|
||||
for desc, test := range map[string]struct {
|
||||
for _, test := range []struct {
|
||||
desc string
|
||||
criMounts []*runtime.Mount
|
||||
imageVolumes map[string]struct{}
|
||||
expectedMountDest []string
|
||||
}{
|
||||
"should setup rw mount for image volumes": {
|
||||
{
|
||||
desc: "should setup rw mount for image volumes",
|
||||
imageVolumes: map[string]struct{}{
|
||||
"/test-volume-1": {},
|
||||
"/test-volume-2": {},
|
||||
@@ -233,7 +248,8 @@ func TestVolumeMounts(t *testing.T) {
|
||||
"/test-volume-2",
|
||||
},
|
||||
},
|
||||
"should skip image volumes if already mounted by CRI": {
|
||||
{
|
||||
desc: "should skip image volumes if already mounted by CRI",
|
||||
criMounts: []*runtime.Mount{
|
||||
{
|
||||
ContainerPath: "/test-volume-1",
|
||||
@@ -248,7 +264,8 @@ func TestVolumeMounts(t *testing.T) {
|
||||
"/test-volume-2",
|
||||
},
|
||||
},
|
||||
"should compare and return cleanpath": {
|
||||
{
|
||||
desc: "should compare and return cleanpath",
|
||||
criMounts: []*runtime.Mount{
|
||||
{
|
||||
ContainerPath: "/test-volume-1",
|
||||
@@ -264,7 +281,8 @@ func TestVolumeMounts(t *testing.T) {
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
config := &imagespec.ImageConfig{
|
||||
Volumes: test.imageVolumes,
|
||||
}
|
||||
@@ -301,14 +319,16 @@ func TestContainerAnnotationPassthroughContainerSpec(t *testing.T) {
|
||||
testContainerName := "container-name"
|
||||
testPid := uint32(1234)
|
||||
|
||||
for desc, test := range map[string]struct {
|
||||
for _, test := range []struct {
|
||||
desc string
|
||||
podAnnotations []string
|
||||
containerAnnotations []string
|
||||
podConfigChange func(*runtime.PodSandboxConfig)
|
||||
configChange func(*runtime.ContainerConfig)
|
||||
specCheck func(*testing.T, *runtimespec.Spec)
|
||||
}{
|
||||
"passthrough annotations from pod and container should be passed as an OCI annotation": {
|
||||
{
|
||||
desc: "passthrough annotations from pod and container should be passed as an OCI annotation",
|
||||
podConfigChange: func(p *runtime.PodSandboxConfig) {
|
||||
p.Annotations["pod.annotation.1"] = "1"
|
||||
p.Annotations["pod.annotation.2"] = "2"
|
||||
@@ -334,7 +354,8 @@ func TestContainerAnnotationPassthroughContainerSpec(t *testing.T) {
|
||||
assert.False(t, ok)
|
||||
},
|
||||
},
|
||||
"passthrough annotations from pod and container should support wildcard": {
|
||||
{
|
||||
desc: "passthrough annotations from pod and container should support wildcard",
|
||||
podConfigChange: func(p *runtime.PodSandboxConfig) {
|
||||
p.Annotations["pod.annotation.1"] = "1"
|
||||
p.Annotations["pod.annotation.2"] = "2"
|
||||
@@ -356,7 +377,8 @@ func TestContainerAnnotationPassthroughContainerSpec(t *testing.T) {
|
||||
assert.Equal(t, "3", spec.Annotations["pod.annotation.3"])
|
||||
},
|
||||
},
|
||||
"annotations should not pass through if no passthrough annotations are configured": {
|
||||
{
|
||||
desc: "annotations should not pass through if no passthrough annotations are configured",
|
||||
podConfigChange: func(p *runtime.PodSandboxConfig) {
|
||||
p.Annotations["pod.annotation.1"] = "1"
|
||||
p.Annotations["pod.annotation.2"] = "2"
|
||||
@@ -385,7 +407,8 @@ func TestContainerAnnotationPassthroughContainerSpec(t *testing.T) {
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
c := newTestCRIService()
|
||||
containerConfig, sandboxConfig, imageConfig, specCheck := getCreateContainerTestData()
|
||||
if test.configChange != nil {
|
||||
@@ -441,13 +464,15 @@ func TestBaseRuntimeSpec(t *testing.T) {
|
||||
|
||||
func TestLinuxContainerMounts(t *testing.T) {
|
||||
const testSandboxID = "test-id"
|
||||
for desc, test := range map[string]struct {
|
||||
for _, test := range []struct {
|
||||
desc string
|
||||
statFn func(string) (os.FileInfo, error)
|
||||
criMounts []*runtime.Mount
|
||||
securityContext *runtime.LinuxContainerSecurityContext
|
||||
expectedMounts []*runtime.Mount
|
||||
}{
|
||||
"should setup ro mount when rootfs is read-only": {
|
||||
{
|
||||
desc: "should setup ro mount when rootfs is read-only",
|
||||
securityContext: &runtime.LinuxContainerSecurityContext{
|
||||
ReadonlyRootfs: true,
|
||||
},
|
||||
@@ -478,7 +503,8 @@ func TestLinuxContainerMounts(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
"should setup rw mount when rootfs is read-write": {
|
||||
{
|
||||
desc: "should setup rw mount when rootfs is read-write",
|
||||
securityContext: &runtime.LinuxContainerSecurityContext{},
|
||||
expectedMounts: []*runtime.Mount{
|
||||
{
|
||||
@@ -507,7 +533,8 @@ func TestLinuxContainerMounts(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
"should use host /dev/shm when host ipc is set": {
|
||||
{
|
||||
desc: "should use host /dev/shm when host ipc is set",
|
||||
securityContext: &runtime.LinuxContainerSecurityContext{
|
||||
NamespaceOptions: &runtime.NamespaceOption{Ipc: runtime.NamespaceMode_NODE},
|
||||
},
|
||||
@@ -537,7 +564,8 @@ func TestLinuxContainerMounts(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
"should skip container mounts if already mounted by CRI": {
|
||||
{
|
||||
desc: "should skip container mounts if already mounted by CRI",
|
||||
criMounts: []*runtime.Mount{
|
||||
{
|
||||
ContainerPath: "/etc/hostname",
|
||||
@@ -559,7 +587,8 @@ func TestLinuxContainerMounts(t *testing.T) {
|
||||
securityContext: &runtime.LinuxContainerSecurityContext{},
|
||||
expectedMounts: nil,
|
||||
},
|
||||
"should skip hostname mount if the old sandbox doesn't have hostname file": {
|
||||
{
|
||||
desc: "should skip hostname mount if the old sandbox doesn't have hostname file",
|
||||
statFn: func(path string) (os.FileInfo, error) {
|
||||
assert.Equal(t, filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hostname"), path)
|
||||
return nil, errors.New("random error")
|
||||
@@ -587,7 +616,8 @@ func TestLinuxContainerMounts(t *testing.T) {
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
config := &runtime.ContainerConfig{
|
||||
Metadata: &runtime.ContainerMetadata{
|
||||
Name: "test-name",
|
||||
@@ -601,7 +631,7 @@ func TestLinuxContainerMounts(t *testing.T) {
|
||||
c := newTestCRIService()
|
||||
c.os.(*ostesting.FakeOS).StatFn = test.statFn
|
||||
mounts := c.linuxContainerMounts(testSandboxID, config)
|
||||
assert.Equal(t, test.expectedMounts, mounts, desc)
|
||||
assert.Equal(t, test.expectedMounts, mounts, test.desc)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user