@@ -1114,6 +1114,54 @@ func TestWaitWithKill(t *testing.T) {
1114
1114
require .Error (t , err , "Firecracker was killed and it must be reported" )
1115
1115
}
1116
1116
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
+
1117
1165
func TestWaitWithInvalidBinary (t * testing.T ) {
1118
1166
ctx := context .Background ()
1119
1167
0 commit comments