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:
Wei Fu
2023-04-15 21:53:01 +08:00
parent ffc70c45c4
commit 8bcfdda39b
27 changed files with 973 additions and 493 deletions

View File

@@ -28,30 +28,35 @@ import (
func TestWaitSandboxStop(t *testing.T) {
id := "test-id"
for desc, test := range map[string]struct {
for _, test := range []struct {
desc string
state sandboxstore.State
cancel bool
timeout time.Duration
expectErr bool
}{
"should return error if timeout exceeds": {
{
desc: "should return error if timeout exceeds",
state: sandboxstore.StateReady,
timeout: 200 * time.Millisecond,
expectErr: true,
},
"should return error if context is cancelled": {
{
desc: "should return error if context is cancelled",
state: sandboxstore.StateReady,
timeout: time.Hour,
cancel: true,
expectErr: true,
},
"should not return error if sandbox is stopped before timeout": {
{
desc: "should not return error if sandbox is stopped before timeout",
state: sandboxstore.StateNotReady,
timeout: time.Hour,
expectErr: false,
},
} {
t.Run(desc, func(t *testing.T) {
test := test
t.Run(test.desc, func(t *testing.T) {
c := newTestCRIService()
sandbox := sandboxstore.NewSandbox(
sandboxstore.Metadata{ID: id},
@@ -69,7 +74,7 @@ func TestWaitSandboxStop(t *testing.T) {
ctx = timeoutCtx
}
err := c.waitSandboxStop(ctx, sandbox)
assert.Equal(t, test.expectErr, err != nil, desc)
assert.Equal(t, test.expectErr, err != nil, test.desc)
})
}
}