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 <fuweid89@gmail.com>
This commit is contained in:
Wei Fu
2023-04-16 16:47:02 +08:00
parent ffc70c45c4
commit 4192ca8f8c
23 changed files with 948 additions and 479 deletions

View File

@@ -85,7 +85,8 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta
}
func TestToCRIContainerStatus(t *testing.T) {
for desc, test := range map[string]struct {
for _, test := range []struct {
desc string
startedAt int64
finishedAt int64
exitCode int32
@@ -94,14 +95,17 @@ func TestToCRIContainerStatus(t *testing.T) {
expectedState runtime.ContainerState
expectedReason string
}{
"container created": {
{
desc: "container created",
expectedState: runtime.ContainerState_CONTAINER_CREATED,
},
"container running": {
{
desc: "container running",
startedAt: time.Now().UnixNano(),
expectedState: runtime.ContainerState_CONTAINER_RUNNING,
},
"container exited with reason": {
{
desc: "container exited with reason",
startedAt: time.Now().UnixNano(),
finishedAt: time.Now().UnixNano(),
exitCode: 1,
@@ -110,7 +114,8 @@ func TestToCRIContainerStatus(t *testing.T) {
expectedState: runtime.ContainerState_CONTAINER_EXITED,
expectedReason: "test-reason",
},
"container exited with exit code 0 without reason": {
{
desc: "container exited with exit code 0 without reason",
startedAt: time.Now().UnixNano(),
finishedAt: time.Now().UnixNano(),
exitCode: 0,
@@ -118,7 +123,8 @@ func TestToCRIContainerStatus(t *testing.T) {
expectedState: runtime.ContainerState_CONTAINER_EXITED,
expectedReason: completeExitReason,
},
"container exited with non-zero exit code without reason": {
{
desc: "container exited with non-zero exit code without reason",
startedAt: time.Now().UnixNano(),
finishedAt: time.Now().UnixNano(),
exitCode: 1,
@@ -127,7 +133,8 @@ func TestToCRIContainerStatus(t *testing.T) {
expectedReason: errorExitReason,
},
} {
t.Run(desc, func(t *testing.T) {
test := test
t.Run(test.desc, func(t *testing.T) {
metadata, status, _, expected := getContainerStatusTestData()
// Update status with test case.
@@ -151,7 +158,7 @@ func TestToCRIContainerStatus(t *testing.T) {
containerStatus := toCRIContainerStatus(container,
expected.Image,
expected.ImageRef)
assert.Equal(t, expected, containerStatus, desc)
assert.Equal(t, expected, containerStatus, test.desc)
})
}
}
@@ -173,7 +180,8 @@ func TestToCRIContainerInfo(t *testing.T) {
}
func TestContainerStatus(t *testing.T) {
for desc, test := range map[string]struct {
for _, test := range []struct {
desc string
exist bool
imageExist bool
startedAt int64
@@ -182,18 +190,21 @@ func TestContainerStatus(t *testing.T) {
expectedState runtime.ContainerState
expectErr bool
}{
"container created": {
{
desc: "container created",
exist: true,
imageExist: true,
expectedState: runtime.ContainerState_CONTAINER_CREATED,
},
"container running": {
{
desc: "container running",
exist: true,
imageExist: true,
startedAt: time.Now().UnixNano(),
expectedState: runtime.ContainerState_CONTAINER_RUNNING,
},
"container exited": {
{
desc: "container exited",
exist: true,
imageExist: true,
startedAt: time.Now().UnixNano(),
@@ -201,18 +212,21 @@ func TestContainerStatus(t *testing.T) {
reason: "test-reason",
expectedState: runtime.ContainerState_CONTAINER_EXITED,
},
"container not exist": {
{
desc: "container not exist",
exist: false,
imageExist: true,
expectErr: true,
},
"image not exist": {
{
desc: "image not exist",
exist: false,
imageExist: false,
expectErr: true,
},
} {
t.Run(desc, func(t *testing.T) {
test := test
t.Run(test.desc, func(t *testing.T) {
c := newTestCRIService()
metadata, status, image, expected := getContainerStatusTestData()
// Update status with test case.