@@ -835,7 +835,7 @@ func TestCaptureFifoToFile(t *testing.T) {
835835 m := & Machine {
836836 exitCh : make (chan struct {}),
837837 }
838- if err := m .captureFifoToFile (fctesting .NewLogEntry (t ), fifoPath , testWriter ); err != nil {
838+ if err := m .captureFifoToFile (context . Background (), fctesting .NewLogEntry (t ), fifoPath , testWriter ); err != nil {
839839 t .Errorf ("Unexpected error: %v" , err )
840840 }
841841
@@ -877,12 +877,19 @@ func TestCaptureFifoToFile_nonblock(t *testing.T) {
877877 m := & Machine {
878878 exitCh : make (chan struct {}),
879879 }
880- if err := m .captureFifoToFile (fctesting .NewLogEntry (t ), fifoPath , testWriter ); err != nil {
880+ if err := m .captureFifoToFile (context . Background (), fctesting .NewLogEntry (t ), fifoPath , testWriter ); err != nil {
881881 t .Errorf ("Unexpected error: %v" , err )
882882 }
883883
884884 defer os .Remove (logPath )
885885
886+ // we sleep here to check to see the io.Copy is working properly in
887+ // captureFifoToFile. This is due to the fifo being opened with O_NONBLOCK,
888+ // which causes io.Copy to exit immediately with no error.
889+ //
890+ // https://github.com/firecracker-microvm/firecracker-go-sdk/issues/156
891+ time .Sleep (250 * time .Millisecond )
892+
886893 f , err := os .OpenFile (fifoPath , os .O_RDWR , 0600 )
887894 if err != nil {
888895 t .Fatalf ("Failed to open file, %q: %v" , fifoPath , err )
@@ -1066,14 +1073,20 @@ func TestCaptureFifoToFile_leak(t *testing.T) {
10661073 logger := fctesting .NewLogEntry (t )
10671074 logger .Logger .Level = logrus .WarnLevel
10681075 logger .Logger .Out = loggerBuffer
1069- err = m .captureFifoToFile (logger , fifoPath , buf )
1076+ err = m .captureFifoToFile (context . Background (), logger , fifoPath , buf )
10701077 assert .NoError (t , err , "failed to capture fifo to file" )
10711078 close (m .exitCh )
10721079
10731080 // wait sometime for the logs to populate
10741081 time .Sleep (250 * time .Millisecond )
1075- expected := `io.Copy failed to copy contents of fifo pipe: read testdata/TestCaptureFifoToFileLeak.fifo: file already closed`
1076- assert .Contains (t , loggerBuffer .String (), expected )
1082+ expectedClosedFifo := `reading from a closed fifo`
1083+ expectedClosedFile := `file already closed`
1084+ logs := loggerBuffer .String ()
1085+ if ! (strings .Contains (logs , expectedClosedFifo ) ||
1086+ strings .Contains (logs , expectedClosedFile )) {
1087+
1088+ t .Errorf ("logs did not container a closed fifo error or closed file" )
1089+ }
10771090}
10781091
10791092func TestWaitWithKill (t * testing.T ) {
0 commit comments