Skip to content

Commit 47c3192

Browse files
committed
Don't use WaitForProcessToExist()
The function is waiting till the process exists, which unintentionally masks timing issues. Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent fb85753 commit 47c3192

File tree

1 file changed

+60
-11
lines changed

1 file changed

+60
-11
lines changed

runtime/service_integ_test.go

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,29 @@ func TestRandomness_Isolated(t *testing.T) {
13231323
}
13241324
}
13251325

1326+
func findProcess(
1327+
ctx context.Context,
1328+
matcher func(context.Context, *process.Process) (bool, error),
1329+
) ([]*process.Process, error) {
1330+
processes, err := process.ProcessesWithContext(ctx)
1331+
if err != nil {
1332+
return nil, err
1333+
}
1334+
1335+
var matches []*process.Process
1336+
for _, p := range processes {
1337+
isMatch, err := matcher(ctx, p)
1338+
if err != nil {
1339+
return nil, err
1340+
}
1341+
if isMatch {
1342+
matches = append(matches, p)
1343+
}
1344+
}
1345+
1346+
return matches, nil
1347+
}
1348+
13261349
func TestStopVM_Isolated(t *testing.T) {
13271350
prepareIntegTest(t)
13281351
require := require.New(t)
@@ -1344,9 +1367,12 @@ func TestStopVM_Isolated(t *testing.T) {
13441367
name string
13451368
createVMRequest proto.CreateVMRequest
13461369
stopFunc func(ctx context.Context, fcClient fccontrol.FirecrackerService, vmID string)
1370+
withStopVM bool
13471371
}{
13481372
{
1349-
name: "Successful",
1373+
name: "Successful",
1374+
withStopVM: true,
1375+
13501376
createVMRequest: proto.CreateVMRequest{},
13511377
stopFunc: func(ctx context.Context, fcClient fccontrol.FirecrackerService, vmID string) {
13521378
_, err = fcClient.StopVM(ctx, &proto.StopVMRequest{VMID: vmID})
@@ -1355,7 +1381,9 @@ func TestStopVM_Isolated(t *testing.T) {
13551381
},
13561382

13571383
{
1358-
name: "Jailer",
1384+
name: "Jailer",
1385+
withStopVM: true,
1386+
13591387
createVMRequest: proto.CreateVMRequest{
13601388
JailerConfig: &proto.JailerConfig{
13611389
UID: 300000,
@@ -1398,7 +1426,9 @@ func TestStopVM_Isolated(t *testing.T) {
13981426
// Firecracker is too fast to test a case where we hit the timeout on a StopVMRequest.
13991427
// The rootfs below explicitly sleeps 60 seconds after shutting down the agent to simulate the case.
14001428
{
1401-
name: "Timeout",
1429+
name: "Timeout",
1430+
withStopVM: true,
1431+
14021432
createVMRequest: proto.CreateVMRequest{
14031433
RootDrive: &proto.FirecrackerRootDrive{
14041434
HostPath: "/var/lib/firecracker-containerd/runtime/rootfs-slow-reboot.img",
@@ -1414,10 +1444,12 @@ func TestStopVM_Isolated(t *testing.T) {
14141444

14151445
// Test that the shim shuts down if the VM stops unexpectedly
14161446
{
1417-
name: "SIGKILLFirecracker",
1447+
name: "SIGKILLFirecracker",
1448+
withStopVM: false,
1449+
14181450
createVMRequest: proto.CreateVMRequest{},
14191451
stopFunc: func(ctx context.Context, _ fccontrol.FirecrackerService, _ string) {
1420-
firecrackerProcesses, err := internal.WaitForProcessToExist(ctx, time.Second, findFirecracker)
1452+
firecrackerProcesses, err := findProcess(ctx, findFirecracker)
14211453
require.NoError(err, "failed waiting for expected firecracker process %q to come up", firecrackerProcessName)
14221454
require.Len(firecrackerProcesses, 1, "expected only one firecracker process to exist")
14231455
firecrackerProcess := firecrackerProcesses[0]
@@ -1430,14 +1462,16 @@ func TestStopVM_Isolated(t *testing.T) {
14301462
// Test that StopVM returns the expected error when the VMM exits with an error (simulated by sending
14311463
// SIGKILL to the VMM in the middle of a StopVM call).
14321464
{
1433-
name: "ErrorExit",
1465+
name: "ErrorExit",
1466+
withStopVM: true,
1467+
14341468
createVMRequest: proto.CreateVMRequest{
14351469
RootDrive: &proto.FirecrackerRootDrive{
14361470
HostPath: "/var/lib/firecracker-containerd/runtime/rootfs-slow-reboot.img",
14371471
},
14381472
},
14391473
stopFunc: func(ctx context.Context, fcClient fccontrol.FirecrackerService, vmID string) {
1440-
firecrackerProcesses, err := internal.WaitForProcessToExist(ctx, time.Second, findFirecracker)
1474+
firecrackerProcesses, err := findProcess(ctx, findFirecracker)
14411475
require.NoError(err, "failed waiting for expected firecracker process %q to come up", firecrackerProcessName)
14421476
require.Len(firecrackerProcesses, 1, "expected only one firecracker process to exist")
14431477
firecrackerProcess := firecrackerProcesses[0]
@@ -1482,15 +1516,30 @@ func TestStopVM_Isolated(t *testing.T) {
14821516
stdout := startAndWaitTask(ctx, t, c)
14831517
require.Equal("hello", stdout)
14841518

1485-
shimProcesses, err := internal.WaitForProcessToExist(ctx, time.Second, findShim)
1486-
require.NoError(err, "failed waiting for expected shim process %q to come up", shimProcessName)
1519+
shimProcesses, err := findProcess(ctx, findShim)
1520+
require.NoError(err, "failed to find shim process %q", shimProcessName)
14871521
require.Len(shimProcesses, 1, "expected only one shim process to exist")
14881522
shimProcess := shimProcesses[0]
14891523

1490-
if test.stopFunc != nil {
1491-
test.stopFunc(ctx, fcClient, vmID)
1524+
fcProcesses, err := findProcess(ctx, findFirecracker)
1525+
require.NoError(err, "failed to find firecracker")
1526+
require.Len(fcProcesses, 1, "expected only one firecracker process to exist")
1527+
fcProcess := fcProcesses[0]
1528+
1529+
test.stopFunc(ctx, fcClient, vmID)
1530+
1531+
// If the function above uses StopVMRequest,
1532+
// shim gurantees that the underlying Firecracker process is dead
1533+
// (either gracefully or forcibly) at the end of the request.
1534+
if test.withStopVM {
1535+
fcExists, err := process.PidExists(fcProcess.Pid)
1536+
require.NoError(err, "failed to find firecracker")
1537+
require.False(fcExists, "firecracker %s is still there", vmID)
14921538
}
14931539

1540+
// Since the shim is the one which is writing the response,
1541+
// it cannot gurantee that the itself is dead at the end of the request.
1542+
// But it should be dead eventually.
14941543
err = internal.WaitForPidToExit(ctx, time.Second, shimProcess.Pid)
14951544
require.NoError(err, "shim hasn't been terminated")
14961545
})

0 commit comments

Comments
 (0)