@@ -1081,37 +1081,76 @@ func TestCaptureFifoToFile_leak(t *testing.T) {
1081
1081
assert .Contains (t , loggerBuffer .String (), `file already closed` , "log" )
1082
1082
}
1083
1083
1084
- func TestWaitWithKill (t * testing.T ) {
1084
+ // Replace filesystem-unsafe characters (such as /) which are often seen in Go's test names
1085
+ var fsSafeTestName = strings .NewReplacer ("/" , "_" )
1086
+
1087
+ func TestWait (t * testing.T ) {
1085
1088
fctesting .RequiresRoot (t )
1086
- ctx := context .Background ()
1087
1089
1088
- socketPath := filepath .Join (testDataPath , t .Name ())
1089
- defer os .Remove (socketPath )
1090
+ cases := []struct {
1091
+ name string
1092
+ stop func (m * Machine )
1093
+ }{
1094
+ {
1095
+ name : "StopVMM" ,
1096
+ stop : func (m * Machine ) {
1097
+ err := m .StopVMM ()
1098
+ require .NoError (t , err )
1099
+ },
1100
+ },
1101
+ {
1102
+ name : "Kill" ,
1103
+ stop : func (m * Machine ) {
1104
+ pid , err := m .PID ()
1105
+ require .NoError (t , err )
1106
+
1107
+ process , err := os .FindProcess (pid )
1108
+ err = process .Kill ()
1109
+ require .NoError (t , err )
1110
+ },
1111
+ },
1112
+ {
1113
+ name : "Context Cancel" ,
1114
+ },
1115
+ }
1090
1116
1091
- cfg := createValidConfig (t , socketPath )
1092
- cmd := VMCommandBuilder {}.
1093
- WithSocketPath (cfg .SocketPath ).
1094
- WithBin (getFirecrackerBinaryPath ()).
1095
- Build (ctx )
1096
- m , err := NewMachine (ctx , cfg , WithProcessRunner (cmd ))
1097
- require .NoError (t , err )
1117
+ for _ , c := range cases {
1118
+ t .Run (c .name , func (t * testing.T ) {
1119
+ ctx := context .Background ()
1120
+ vmContext , vmCancel := context .WithCancel (context .Background ())
1098
1121
1099
- err = m . Start ( ctx )
1100
- require . NoError ( t , err )
1122
+ socketPath := filepath . Join ( testDataPath , fsSafeTestName . Replace ( t . Name ()) )
1123
+ defer os . Remove ( socketPath )
1101
1124
1102
- go func () {
1103
- pid , err := m .PID ()
1104
- require .NoError (t , err )
1125
+ cfg := createValidConfig (t , socketPath )
1126
+ cmd := VMCommandBuilder {}.
1127
+ WithSocketPath (cfg .SocketPath ).
1128
+ WithBin (getFirecrackerBinaryPath ()).
1129
+ Build (vmContext )
1130
+ m , err := NewMachine (ctx , cfg , WithProcessRunner (cmd ))
1131
+ require .NoError (t , err )
1105
1132
1106
- process , err := os . FindProcess ( pid )
1107
- require .NoError (t , err )
1133
+ err = m . Start ( ctx )
1134
+ require .NoError (t , err )
1108
1135
1109
- err = process .Kill ()
1110
- require .NoError (t , err )
1111
- }()
1136
+ pid , err := m .PID ()
1137
+ require .NoError (t , err )
1112
1138
1113
- err = m .Wait (ctx )
1114
- require .Error (t , err , "Firecracker was killed and it must be reported" )
1139
+ go func () {
1140
+ if c .stop != nil {
1141
+ c .stop (m )
1142
+ } else {
1143
+ vmCancel ()
1144
+ }
1145
+ }()
1146
+
1147
+ err = m .Wait (ctx )
1148
+ require .Error (t , err , "Firecracker was killed and it must be reported" )
1149
+
1150
+ alive , err := isProcessAlive (pid )
1151
+ require .False (t , alive , "pid=%d is still there" , pid )
1152
+ })
1153
+ }
1115
1154
}
1116
1155
1117
1156
func isProcessAlive (pid int ) (bool , error ) {
@@ -1130,38 +1169,6 @@ func isProcessAlive(pid int) (bool, error) {
1130
1169
return true , nil
1131
1170
}
1132
1171
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
-
1165
1172
func TestWaitWithInvalidBinary (t * testing.T ) {
1166
1173
ctx := context .Background ()
1167
1174
0 commit comments