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:
@@ -44,36 +44,44 @@ import (
|
||||
// TestGetUserFromImage tests the logic of getting image uid or user name of image user.
|
||||
func TestGetUserFromImage(t *testing.T) {
|
||||
newI64 := func(i int64) *int64 { return &i }
|
||||
for c, test := range map[string]struct {
|
||||
for _, test := range []struct {
|
||||
desc string
|
||||
user string
|
||||
uid *int64
|
||||
name string
|
||||
}{
|
||||
"no gid": {
|
||||
{
|
||||
desc: "no gid",
|
||||
user: "0",
|
||||
uid: newI64(0),
|
||||
},
|
||||
"uid/gid": {
|
||||
{
|
||||
desc: "uid/gid",
|
||||
user: "0:1",
|
||||
uid: newI64(0),
|
||||
},
|
||||
"empty user": {
|
||||
{
|
||||
desc: "empty user",
|
||||
user: "",
|
||||
},
|
||||
"multiple separators": {
|
||||
{
|
||||
desc: "multiple separators",
|
||||
user: "1:2:3",
|
||||
uid: newI64(1),
|
||||
},
|
||||
"root username": {
|
||||
{
|
||||
desc: "root username",
|
||||
user: "root:root",
|
||||
name: "root",
|
||||
},
|
||||
"username": {
|
||||
{
|
||||
desc: "username",
|
||||
user: "test:test",
|
||||
name: "test",
|
||||
},
|
||||
} {
|
||||
t.Run(c, func(t *testing.T) {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
actualUID, actualName := getUserFromImage(test.user)
|
||||
assert.Equal(t, test.uid, actualUID)
|
||||
assert.Equal(t, test.name, actualName)
|
||||
@@ -145,19 +153,22 @@ systemd_cgroup = true
|
||||
require.NoError(t, err)
|
||||
require.Len(t, nonNilOptsConfig.Runtimes, 3)
|
||||
|
||||
for desc, test := range map[string]struct {
|
||||
for _, test := range []struct {
|
||||
desc string
|
||||
r criconfig.Runtime
|
||||
c criconfig.Config
|
||||
expectedOptions interface{}
|
||||
}{
|
||||
"when options is nil, should return nil option for io.containerd.runc.v2": {
|
||||
{
|
||||
desc: "when options is nil, should return nil option for io.containerd.runc.v2",
|
||||
r: nilOptsConfig.Runtimes["runcv2"],
|
||||
c: nilOptsConfig,
|
||||
expectedOptions: nil,
|
||||
},
|
||||
"when options is not nil, should be able to decode for io.containerd.runc.v2": {
|
||||
r: nonNilOptsConfig.Runtimes["runcv2"],
|
||||
c: nonNilOptsConfig,
|
||||
{
|
||||
desc: "when options is not nil, should be able to decode for io.containerd.runc.v2",
|
||||
r: nonNilOptsConfig.Runtimes["runcv2"],
|
||||
c: nonNilOptsConfig,
|
||||
expectedOptions: &runcoptions.Options{
|
||||
BinaryName: "runc",
|
||||
Root: "/runcv2",
|
||||
@@ -165,7 +176,8 @@ systemd_cgroup = true
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
opts, err := generateRuntimeOptions(test.r, test.c)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, test.expectedOptions, opts)
|
||||
@@ -174,18 +186,21 @@ systemd_cgroup = true
|
||||
}
|
||||
|
||||
func TestEnvDeduplication(t *testing.T) {
|
||||
for desc, test := range map[string]struct {
|
||||
for _, test := range []struct {
|
||||
desc string
|
||||
existing []string
|
||||
kv [][2]string
|
||||
expected []string
|
||||
}{
|
||||
"single env": {
|
||||
{
|
||||
desc: "single env",
|
||||
kv: [][2]string{
|
||||
{"a", "b"},
|
||||
},
|
||||
expected: []string{"a=b"},
|
||||
},
|
||||
"multiple envs": {
|
||||
{
|
||||
desc: "multiple envs",
|
||||
kv: [][2]string{
|
||||
{"a", "b"},
|
||||
{"c", "d"},
|
||||
@@ -197,7 +212,8 @@ func TestEnvDeduplication(t *testing.T) {
|
||||
"e=f",
|
||||
},
|
||||
},
|
||||
"env override": {
|
||||
{
|
||||
desc: "env override",
|
||||
kv: [][2]string{
|
||||
{"k1", "v1"},
|
||||
{"k2", "v2"},
|
||||
@@ -213,7 +229,8 @@ func TestEnvDeduplication(t *testing.T) {
|
||||
"k4=v6",
|
||||
},
|
||||
},
|
||||
"existing env": {
|
||||
{
|
||||
desc: "existing env",
|
||||
existing: []string{
|
||||
"k1=v1",
|
||||
"k2=v2",
|
||||
@@ -232,7 +249,8 @@ func TestEnvDeduplication(t *testing.T) {
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
var spec runtimespec.Spec
|
||||
if len(test.existing) > 0 {
|
||||
spec.Process = &runtimespec.Process{
|
||||
@@ -248,17 +266,20 @@ func TestEnvDeduplication(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPassThroughAnnotationsFilter(t *testing.T) {
|
||||
for desc, test := range map[string]struct {
|
||||
for _, test := range []struct {
|
||||
desc string
|
||||
podAnnotations map[string]string
|
||||
runtimePodAnnotations []string
|
||||
passthroughAnnotations map[string]string
|
||||
}{
|
||||
"should support direct match": {
|
||||
{
|
||||
desc: "should support direct match",
|
||||
podAnnotations: map[string]string{"c": "d", "d": "e"},
|
||||
runtimePodAnnotations: []string{"c"},
|
||||
passthroughAnnotations: map[string]string{"c": "d"},
|
||||
},
|
||||
"should support wildcard match": {
|
||||
{
|
||||
desc: "should support wildcard match",
|
||||
podAnnotations: map[string]string{
|
||||
"t.f": "j",
|
||||
"z.g": "o",
|
||||
@@ -273,7 +294,8 @@ func TestPassThroughAnnotationsFilter(t *testing.T) {
|
||||
"y.ca": "b",
|
||||
},
|
||||
},
|
||||
"should support wildcard match all": {
|
||||
{
|
||||
desc: "should support wildcard match all",
|
||||
podAnnotations: map[string]string{
|
||||
"t.f": "j",
|
||||
"z.g": "o",
|
||||
@@ -290,7 +312,8 @@ func TestPassThroughAnnotationsFilter(t *testing.T) {
|
||||
"y": "b",
|
||||
},
|
||||
},
|
||||
"should support match including path separator": {
|
||||
{
|
||||
desc: "should support match including path separator",
|
||||
podAnnotations: map[string]string{
|
||||
"matchend.com/end": "1",
|
||||
"matchend.com/end1": "2",
|
||||
@@ -349,7 +372,8 @@ func TestPassThroughAnnotationsFilter(t *testing.T) {
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
passthroughAnnotations := getPassthroughAnnotations(test.podAnnotations, test.runtimePodAnnotations)
|
||||
assert.Equal(t, test.passthroughAnnotations, passthroughAnnotations)
|
||||
})
|
||||
@@ -437,28 +461,34 @@ func TestValidateTargetContainer(t *testing.T) {
|
||||
err = addContainer(c, testOtherContainerID, testOtherContainerSandboxID, testOtherContainerPID, createdAt, startedAt, 0)
|
||||
require.NoError(t, err, "error creating test container in other pod")
|
||||
|
||||
for desc, test := range map[string]struct {
|
||||
for _, test := range []struct {
|
||||
desc string
|
||||
targetContainerID string
|
||||
expectError bool
|
||||
}{
|
||||
"target container in pod": {
|
||||
{
|
||||
desc: "target container in pod",
|
||||
targetContainerID: testTargetContainerID,
|
||||
expectError: false,
|
||||
},
|
||||
"target stopped container in pod": {
|
||||
{
|
||||
desc: "target stopped container in pod",
|
||||
targetContainerID: testStoppedContainerID,
|
||||
expectError: true,
|
||||
},
|
||||
"target container does not exist": {
|
||||
{
|
||||
desc: "target container does not exist",
|
||||
targetContainerID: "no-container-with-this-id",
|
||||
expectError: true,
|
||||
},
|
||||
"target container in other pod": {
|
||||
{
|
||||
desc: "target container in other pod",
|
||||
targetContainerID: testOtherContainerID,
|
||||
expectError: true,
|
||||
},
|
||||
} {
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
targetContainer, err := c.validateTargetContainer(testSandboxID, test.targetContainerID)
|
||||
if test.expectError {
|
||||
require.Error(t, err, "target should have been invalid but no error")
|
||||
@@ -520,6 +550,8 @@ func TestHostNetwork(t *testing.T) {
|
||||
if goruntime.GOOS != "linux" {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if hostNetwork(tt.c) != tt.expected {
|
||||
t.Errorf("failed hostNetwork got %t expected %t", hostNetwork(tt.c), tt.expected)
|
||||
|
||||
Reference in New Issue
Block a user