Skip to content

Commit 64cdd2c

Browse files
committed
Fix TestCreateVM_Isolated stability by creating fresh ctx/fcClient per subtest
Signed-off-by: Arjun Raja Yogidas <arjunry@amazon.com>
1 parent 587a1f8 commit 64cdd2c

File tree

1 file changed

+59
-25
lines changed

1 file changed

+59
-25
lines changed

runtime/service_integ_test.go

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ func iperf3Image(ctx context.Context, client *containerd.Client, snapshotterName
112112
func TestShimExitsUponContainerDelete_Isolated(t *testing.T) {
113113
integtest.Prepare(t)
114114

115-
ctx := namespaces.WithNamespace(context.Background(), defaultNamespace)
115+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
116+
defer cancel()
117+
ctx = namespaces.WithNamespace(ctx, defaultNamespace)
116118

117119
client, err := containerd.New(integtest.ContainerdSockPath)
118120
require.NoError(t, err, "unable to create client to containerd service at %s, is containerd running?", integtest.ContainerdSockPath)
@@ -732,7 +734,9 @@ func TestLongUnixSocketPath_Isolated(t *testing.T) {
732734
namespace := strings.Repeat("n", 20)
733735
vmID := strings.Repeat("v", 64)
734736

735-
ctx := namespaces.WithNamespace(context.Background(), namespace)
737+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
738+
defer cancel()
739+
ctx = namespaces.WithNamespace(ctx, namespace)
736740

737741
fcClient, err := integtest.NewFCControlClient(integtest.ContainerdSockPath)
738742
require.NoError(t, err, "failed to create fccontrol client")
@@ -827,7 +831,9 @@ func TestStubBlockDevices_Isolated(t *testing.T) {
827831

828832
const vmID = 0
829833

830-
ctx := namespaces.WithNamespace(context.Background(), "default")
834+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
835+
defer cancel()
836+
ctx = namespaces.WithNamespace(ctx, "default")
831837

832838
client, err := containerd.New(integtest.ContainerdSockPath, containerd.WithDefaultRuntime(firecrackerRuntime))
833839
require.NoError(t, err, "unable to create client to containerd service at %s, is containerd running?", integtest.ContainerdSockPath)
@@ -963,7 +969,9 @@ func startAndWaitTask(ctx context.Context, t testing.TB, c containerd.Container)
963969
}
964970

965971
func testCreateContainerWithSameName(t *testing.T, vmID string) {
966-
ctx := namespaces.WithNamespace(context.Background(), "default")
972+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
973+
defer cancel()
974+
ctx = namespaces.WithNamespace(ctx, "default")
967975

968976
// Explicitly specify Container Count = 2 to workaround #230
969977
if len(vmID) != 0 {
@@ -1051,7 +1059,9 @@ func TestCreateContainerWithSameName_Isolated(t *testing.T) {
10511059
func TestStubDriveReserveAndReleaseByContainers_Isolated(t *testing.T) {
10521060
integtest.Prepare(t)
10531061

1054-
ctx := namespaces.WithNamespace(context.Background(), "default")
1062+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
1063+
defer cancel()
1064+
ctx = namespaces.WithNamespace(ctx, "default")
10551065

10561066
client, err := containerd.New(integtest.ContainerdSockPath, containerd.WithDefaultRuntime(firecrackerRuntime))
10571067
require.NoError(t, err, "unable to create client to containerd service at %s, is containerd running?", integtest.ContainerdSockPath)
@@ -1439,7 +1449,9 @@ func TestUpdateVMMetadata_Isolated(t *testing.T) {
14391449

14401450
func TestMemoryBalloon_Isolated(t *testing.T) {
14411451
integtest.Prepare(t)
1442-
ctx := namespaces.WithNamespace(context.Background(), defaultNamespace)
1452+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
1453+
defer cancel()
1454+
ctx = namespaces.WithNamespace(ctx, defaultNamespace)
14431455

14441456
numberOfVms := defaultNumberOfVms
14451457
if str := os.Getenv(numberOfVmsEnvName); str != "" {
@@ -1680,7 +1692,9 @@ func TestStopVM_Isolated(t *testing.T) {
16801692
require.NoError(t, err, "unable to create client to containerd service at %s, is containerd running?", integtest.ContainerdSockPath)
16811693
defer client.Close()
16821694

1683-
ctx := namespaces.WithNamespace(context.Background(), "default")
1695+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
1696+
defer cancel()
1697+
ctx = namespaces.WithNamespace(ctx, "default")
16841698

16851699
image, err := alpineImage(ctx, client, defaultSnapshotterName)
16861700
require.NoError(t, err, "failed to get alpine image")
@@ -1905,7 +1919,9 @@ func TestStopVMFailFast_Isolated(t *testing.T) {
19051919

19061920
name := testNameToVMID(t.Name())
19071921

1908-
ctx := namespaces.WithNamespace(context.Background(), "default")
1922+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
1923+
defer cancel()
1924+
ctx = namespaces.WithNamespace(ctx, "default")
19091925

19101926
image, err := alpineImage(ctx, client, defaultSnapshotterName)
19111927
require.NoError(t, err, "failed to get alpine image")
@@ -1957,7 +1973,9 @@ func TestExec_Isolated(t *testing.T) {
19571973
require.NoError(t, err, "unable to create client to containerd service at %s, is containerd running?", integtest.ContainerdSockPath)
19581974
defer client.Close()
19591975

1960-
ctx := namespaces.WithNamespace(context.Background(), "default")
1976+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
1977+
defer cancel()
1978+
ctx = namespaces.WithNamespace(ctx, "default")
19611979

19621980
image, err := alpineImage(ctx, client, defaultSnapshotterName)
19631981
require.NoError(t, err, "failed to get alpine image")
@@ -2073,7 +2091,9 @@ func TestEvents_Isolated(t *testing.T) {
20732091
require.NoError(t, err, "unable to create client to containerd service at %s, is containerd running?", integtest.ContainerdSockPath)
20742092
defer client.Close()
20752093

2076-
ctx := namespaces.WithNamespace(context.Background(), "default")
2094+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
2095+
defer cancel()
2096+
ctx = namespaces.WithNamespace(ctx, "default")
20772097

20782098
// If we don't have enough events within 30 seconds, the context will be cancelled and the loop below will be interrupted
20792099
subscribeCtx, subscribeCancel := context.WithTimeout(ctx, 30*time.Second)
@@ -2159,7 +2179,9 @@ func TestOOM_Isolated(t *testing.T) {
21592179
require.NoError(t, err, "unable to create client to containerd service at %s, is containerd running?", integtest.ContainerdSockPath)
21602180
defer client.Close()
21612181

2162-
ctx := namespaces.WithNamespace(context.Background(), "default")
2182+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
2183+
defer cancel()
2184+
ctx = namespaces.WithNamespace(ctx, "default")
21632185

21642186
// If we don't have enough events within 30 seconds, the context will be cancelled and the loop below will be interrupted
21652187
subscribeCtx, subscribeCancel := context.WithTimeout(ctx, 30*time.Second)
@@ -2250,17 +2272,12 @@ func TestCreateVM_Isolated(t *testing.T) {
22502272
require.NoError(t, err, "unable to create client to containerd service at %s, is containerd running?", integtest.ContainerdSockPath)
22512273
defer client.Close()
22522274

2253-
ctx := namespaces.WithNamespace(context.Background(), "default")
2254-
2255-
fcClient, err := integtest.NewFCControlClient(integtest.ContainerdSockPath)
2256-
require.NoError(t, err, "failed to create ttrpc client")
2257-
22582275
kernelArgs := integtest.DefaultRuntimeConfig.KernelArgs
22592276

22602277
type subtest struct {
22612278
name string
22622279
request proto.CreateVMRequest
2263-
validate func(*testing.T, error)
2280+
validate func(*testing.T, context.Context, error)
22642281
validateUsesFindProcess bool
22652282
stopVM bool
22662283
}
@@ -2269,7 +2286,7 @@ func TestCreateVM_Isolated(t *testing.T) {
22692286
{
22702287
name: "Happy Case",
22712288
request: proto.CreateVMRequest{},
2272-
validate: func(t *testing.T, err error) {
2289+
validate: func(t *testing.T, ctx context.Context, err error) {
22732290
require.NoError(t, err)
22742291
},
22752292
stopVM: true,
@@ -2283,7 +2300,7 @@ func TestCreateVM_Isolated(t *testing.T) {
22832300
HostPath: "/var/lib/firecracker-containerd/runtime/rootfs-debug.img",
22842301
},
22852302
},
2286-
validate: func(t *testing.T, err error) {
2303+
validate: func(t *testing.T, ctx context.Context, err error) {
22872304
require.NotNil(t, err, "expected an error but did not receive any")
22882305
time.Sleep(5 * time.Second)
22892306
firecrackerProcesses, err := findProcess(ctx, findFirecracker)
@@ -2300,11 +2317,12 @@ func TestCreateVM_Isolated(t *testing.T) {
23002317
RootDrive: &proto.FirecrackerRootDrive{
23012318
HostPath: "/var/lib/firecracker-containerd/runtime/rootfs-debug.img",
23022319
},
2320+
TimeoutSeconds: 5,
23032321
},
2304-
validate: func(t *testing.T, err error) {
2322+
validate: func(t *testing.T, ctx context.Context, err error) {
23052323
require.Error(t, err)
23062324
assert.Equal(t, codes.DeadlineExceeded, status.Code(err))
2307-
assert.Contains(t, err.Error(), "didn't start within 20s")
2325+
assert.Contains(t, err.Error(), "didn't start within 5s")
23082326
},
23092327
stopVM: true,
23102328
},
@@ -2317,7 +2335,7 @@ func TestCreateVM_Isolated(t *testing.T) {
23172335
},
23182336
TimeoutSeconds: 60,
23192337
},
2320-
validate: func(t *testing.T, err error) {
2338+
validate: func(t *testing.T, ctx context.Context, err error) {
23212339
require.NoError(t, err)
23222340
},
23232341
stopVM: true,
@@ -2331,6 +2349,15 @@ func TestCreateVM_Isolated(t *testing.T) {
23312349
if !s.validateUsesFindProcess {
23322350
t.Parallel()
23332351
}
2352+
2353+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
2354+
defer cancel()
2355+
ctx = namespaces.WithNamespace(ctx, "default")
2356+
2357+
fcClient, err := integtest.NewFCControlClient(integtest.ContainerdSockPath)
2358+
require.NoError(t, err, "failed to create ttrpc client")
2359+
defer fcClient.Close()
2360+
23342361
vmID := testNameToVMID(t.Name())
23352362

23362363
tempDir := t.TempDir()
@@ -2349,7 +2376,7 @@ func TestCreateVM_Isolated(t *testing.T) {
23492376
requireNonEmptyFifo(t, metricsFile)
23502377

23512378
// Some test cases are expected to have an error, some are not.
2352-
s.validate(t, createVMErr)
2379+
s.validate(t, ctx, createVMErr)
23532380

23542381
if createVMErr == nil && s.stopVM {
23552382
// Ensure the response fields are populated correctly
@@ -2360,6 +2387,8 @@ func TestCreateVM_Isolated(t *testing.T) {
23602387
TimeoutSeconds: 30,
23612388
})
23622389
require.Equal(t, status.Code(err), codes.OK)
2390+
2391+
time.Sleep(500 * time.Millisecond)
23632392
}
23642393
}
23652394

@@ -2525,7 +2554,9 @@ func TestAttach_Isolated(t *testing.T) {
25252554
require.NoError(t, err, "unable to create client to containerd service at %s, is containerd running?", integtest.ContainerdSockPath)
25262555
defer client.Close()
25272556

2528-
ctx := namespaces.WithNamespace(context.Background(), "default")
2557+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
2558+
defer cancel()
2559+
ctx = namespaces.WithNamespace(ctx, "default")
25292560

25302561
image, err := alpineImage(ctx, client, defaultSnapshotterName)
25312562
require.NoError(t, err, "failed to get alpine image")
@@ -2627,7 +2658,9 @@ func TestBrokenPipe_Isolated(t *testing.T) {
26272658
require.NoError(t, err, "unable to create client to containerd service at %s, is containerd running?", integtest.ContainerdSockPath)
26282659
defer client.Close()
26292660

2630-
ctx := namespaces.WithNamespace(context.Background(), "default")
2661+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
2662+
defer cancel()
2663+
ctx = namespaces.WithNamespace(ctx, "default")
26312664

26322665
image, err := alpineImage(ctx, client, defaultSnapshotterName)
26332666
require.NoError(t, err, "failed to get alpine image")
@@ -2641,6 +2674,7 @@ func TestBrokenPipe_Isolated(t *testing.T) {
26412674
containerd.WithNewSpec(oci.WithProcessArgs("/usr/bin/yes")),
26422675
)
26432676
require.NoError(t, err)
2677+
defer c.Delete(ctx, containerd.WithSnapshotCleanup)
26442678

26452679
var stdout1 errorBuffer
26462680
var stderr1 errorBuffer

0 commit comments

Comments
 (0)