@@ -1075,3 +1075,115 @@ func TestCaptureFifoToFile_leak(t *testing.T) {
1075
1075
expected := `io.Copy failed to copy contents of fifo pipe: read testdata/TestCaptureFifoToFileLeak.fifo: file already closed`
1076
1076
assert .Contains (t , loggerBuffer .String (), expected )
1077
1077
}
1078
+
1079
+ func TestWaitWithKill (t * testing.T ) {
1080
+ fctesting .RequiresRoot (t )
1081
+ ctx := context .Background ()
1082
+
1083
+ socketPath := filepath .Join (testDataPath , t .Name ())
1084
+ defer os .Remove (socketPath )
1085
+
1086
+ cfg := createValidConfig (t , socketPath )
1087
+ cmd := VMCommandBuilder {}.
1088
+ WithSocketPath (cfg .SocketPath ).
1089
+ WithBin (getFirecrackerBinaryPath ()).
1090
+ Build (ctx )
1091
+ m , err := NewMachine (ctx , cfg , WithProcessRunner (cmd ))
1092
+ require .NoError (t , err )
1093
+
1094
+ err = m .Start (ctx )
1095
+ require .NoError (t , err )
1096
+
1097
+ go func () {
1098
+ pid , err := m .PID ()
1099
+ require .NoError (t , err )
1100
+
1101
+ process , err := os .FindProcess (pid )
1102
+ require .NoError (t , err )
1103
+
1104
+ err = process .Kill ()
1105
+ require .NoError (t , err )
1106
+ }()
1107
+
1108
+ err = m .Wait (ctx )
1109
+ require .Error (t , err , "Firecracker was killed and it must be reported" )
1110
+ }
1111
+
1112
+ func TestWaitWithInvalidBinary (t * testing.T ) {
1113
+ ctx := context .Background ()
1114
+
1115
+ socketPath := filepath .Join (testDataPath , t .Name ())
1116
+ defer os .Remove (socketPath )
1117
+
1118
+ cfg := createValidConfig (t , socketPath )
1119
+ cmd := VMCommandBuilder {}.
1120
+ WithSocketPath (socketPath ).
1121
+ WithBin ("invalid-bin" ).
1122
+ Build (ctx )
1123
+ m , err := NewMachine (ctx , cfg , WithProcessRunner (cmd ))
1124
+ require .NoError (t , err )
1125
+
1126
+ ch := make (chan error )
1127
+
1128
+ go func () {
1129
+ err := m .Wait (ctx )
1130
+ require .Error (t , err , "Wait() reports an error" )
1131
+ ch <- err
1132
+ }()
1133
+
1134
+ err = m .Start (ctx )
1135
+ require .Error (t , err , "Start() reports an error" )
1136
+
1137
+ select {
1138
+ case errFromWait := <- ch :
1139
+ require .Equal (t , errFromWait , err )
1140
+ }
1141
+ }
1142
+
1143
+ func TestWaitWithNoSocket (t * testing.T ) {
1144
+ ctx := context .Background ()
1145
+
1146
+ socketPath := filepath .Join (testDataPath , t .Name ())
1147
+ defer os .Remove (socketPath )
1148
+ cfg := createValidConfig (t , socketPath )
1149
+
1150
+ m , err := NewMachine (ctx , cfg , WithProcessRunner (exec .Command ("sleep" , "10" )))
1151
+ require .NoError (t , err )
1152
+
1153
+ ch := make (chan error )
1154
+
1155
+ go func () {
1156
+ err := m .Wait (ctx )
1157
+ require .Error (t , err , "Wait() reports an error" )
1158
+ ch <- err
1159
+ }()
1160
+
1161
+ err = m .Start (ctx )
1162
+ require .Error (t , err , "Start() reports an error" )
1163
+
1164
+ select {
1165
+ case errFromWait := <- ch :
1166
+ require .Equal (t , errFromWait , err )
1167
+ }
1168
+ }
1169
+
1170
+ func createValidConfig (t * testing.T , socketPath string ) Config {
1171
+ return Config {
1172
+ SocketPath : socketPath ,
1173
+ KernelImagePath : getVmlinuxPath (t ),
1174
+ MachineCfg : models.MachineConfiguration {
1175
+ VcpuCount : Int64 (2 ),
1176
+ CPUTemplate : models .CPUTemplate (models .CPUTemplateT2 ),
1177
+ MemSizeMib : Int64 (256 ),
1178
+ HtEnabled : Bool (false ),
1179
+ },
1180
+ Drives : []models.Drive {
1181
+ {
1182
+ DriveID : String ("root" ),
1183
+ IsRootDevice : Bool (true ),
1184
+ IsReadOnly : Bool (true ),
1185
+ PathOnHost : String (testRootfs ),
1186
+ },
1187
+ },
1188
+ }
1189
+ }
0 commit comments