Skip to content

Commit 2fd80c0

Browse files
committed
Test that Machine#Wait() with context cancellation
The method should guarantee that the underlying process is dead, but it doesn't. Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent 7dce86a commit 2fd80c0

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

machine_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,54 @@ func TestWaitWithKill(t *testing.T) {
11141114
require.Error(t, err, "Firecracker was killed and it must be reported")
11151115
}
11161116

1117+
func isProcessAlive(pid int) (bool, error) {
1118+
// Using kill(2) with signal=0 to check the existence of the process,
1119+
// because os.FindProcess always returns a process, regardless of whether the process is
1120+
// alive or not.
1121+
// https://golang.org/pkg/os/#FindProcess
1122+
err := syscall.Kill(pid, syscall.Signal(0))
1123+
if err != nil {
1124+
if errno, ok := err.(syscall.Errno); ok {
1125+
if errno == syscall.ESRCH {
1126+
return false, nil
1127+
}
1128+
}
1129+
}
1130+
return true, nil
1131+
}
1132+
1133+
func TestWaitWithCancel(t *testing.T) {
1134+
fctesting.RequiresRoot(t)
1135+
ctx, cancel := context.WithCancel(context.Background())
1136+
1137+
socketPath := filepath.Join(testDataPath, t.Name())
1138+
defer os.Remove(socketPath)
1139+
1140+
cfg := createValidConfig(t, socketPath)
1141+
cmd := VMCommandBuilder{}.
1142+
WithSocketPath(cfg.SocketPath).
1143+
WithBin(getFirecrackerBinaryPath()).
1144+
Build(ctx)
1145+
m, err := NewMachine(ctx, cfg, WithProcessRunner(cmd))
1146+
require.NoError(t, err)
1147+
1148+
err = m.Start(ctx)
1149+
require.NoError(t, err)
1150+
1151+
pid, err := m.PID()
1152+
require.NoError(t, err)
1153+
1154+
go func() {
1155+
cancel()
1156+
}()
1157+
1158+
err = m.Wait(context.Background())
1159+
require.Error(t, err, "Firecracker was killed and it must be reported")
1160+
1161+
alive, err := isProcessAlive(pid)
1162+
require.False(t, alive, "The process must not be there")
1163+
}
1164+
11171165
func TestWaitWithInvalidBinary(t *testing.T) {
11181166
ctx := context.Background()
11191167

0 commit comments

Comments
 (0)