@@ -25,6 +25,7 @@ import (
2525 "path/filepath"
2626 "strconv"
2727 "strings"
28+ "sync"
2829 "syscall"
2930 "testing"
3031 "time"
@@ -146,7 +147,12 @@ func TestJailerMicroVMExecution(t *testing.T) {
146147 socketPath := filepath .Join (jailerTestPath , "firecracker" , "TestJailerMicroVMExecution.socket" )
147148 logFifo := filepath .Join (tmpDir , "firecracker.log" )
148149 metricsFifo := filepath .Join (tmpDir , "firecracker-metrics" )
150+ capturedLog := filepath .Join (tmpDir , "writer.fifo" )
151+ fw , err := os .OpenFile (capturedLog , os .O_CREATE | os .O_RDWR , 0600 )
152+ require .NoError (t , err , "failed to open fifo writer file" )
149153 defer func () {
154+ fw .Close ()
155+ os .Remove (capturedLog )
150156 os .Remove (socketPath )
151157 os .Remove (logFifo )
152158 os .Remove (metricsFifo )
@@ -183,6 +189,7 @@ func TestJailerMicroVMExecution(t *testing.T) {
183189 ExecFile : getFirecrackerBinaryPath (),
184190 ChrootStrategy : NewNaiveChrootStrategy (jailerFullRootPath , vmlinuxPath ),
185191 },
192+ FifoLogWriter : fw ,
186193 }
187194
188195 if _ , err := os .Stat (vmlinuxPath ); err != nil {
@@ -250,6 +257,10 @@ func TestJailerMicroVMExecution(t *testing.T) {
250257 }
251258
252259 m .StopVMM ()
260+
261+ info , err := os .Stat (capturedLog )
262+ assert .NoError (t , err , "failed to stat captured log file" )
263+ assert .NotEqual (t , 0 , info .Size ())
253264}
254265
255266func TestMicroVMExecution (t * testing.T ) {
@@ -263,7 +274,12 @@ func TestMicroVMExecution(t *testing.T) {
263274 socketPath := filepath .Join (testDataPath , "TestMicroVMExecution.sock" )
264275 logFifo := filepath .Join (testDataPath , "firecracker.log" )
265276 metricsFifo := filepath .Join (testDataPath , "firecracker-metrics" )
277+ capturedLog := filepath .Join (testDataPath , "writer.fifo" )
278+ fw , err := os .OpenFile (capturedLog , os .O_CREATE | os .O_RDWR , 0600 )
279+ require .NoError (t , err , "failed to open fifo writer file" )
266280 defer func () {
281+ fw .Close ()
282+ os .Remove (capturedLog )
267283 os .Remove (socketPath )
268284 os .Remove (logFifo )
269285 os .Remove (metricsFifo )
@@ -292,6 +308,7 @@ func TestMicroVMExecution(t *testing.T) {
292308 Debug : true ,
293309 DisableValidation : true ,
294310 NetworkInterfaces : networkIfaces ,
311+ FifoLogWriter : fw ,
295312 }
296313
297314 ctx := context .Background ()
@@ -353,6 +370,10 @@ func TestMicroVMExecution(t *testing.T) {
353370 // didn't for some reason, we still need to terminate it:
354371 m .StopVMM ()
355372 m .Wait (vmmCtx )
373+
374+ info , err := os .Stat (capturedLog )
375+ assert .NoError (t , err , "failed to stat captured log file" )
376+ assert .NotEqual (t , 0 , info .Size ())
356377}
357378
358379func TestStartVMM (t * testing.T ) {
@@ -774,7 +795,7 @@ func TestLogFiles(t *testing.T) {
774795}
775796
776797func TestCaptureFifoToFile (t * testing.T ) {
777- fifoPath := filepath .Join (testDataPath , "fifo " )
798+ fifoPath := filepath .Join (testDataPath , "TestCaptureFifoToFile " )
778799
779800 if err := syscall .Mkfifo (fifoPath , 0700 ); err != nil {
780801 t .Fatalf ("Unexpected error during syscall.Mkfifo call: %v" , err )
@@ -786,32 +807,90 @@ func TestCaptureFifoToFile(t *testing.T) {
786807 t .Fatalf ("Failed to open file, %q: %v" , fifoPath , err )
787808 }
788809
789- f .Write ([]byte ("Hello world!" ))
810+ expectedBytes := []byte ("Hello world!" )
811+ f .Write (expectedBytes )
790812 defer f .Close ()
791813
792- go func () {
793- t := time .NewTicker (250 * time .Millisecond )
794- select {
795- case <- t .C :
796- f .Close ()
797- }
798- }()
814+ time .AfterFunc (250 * time .Millisecond , func () { f .Close () })
815+
816+ logPath := fifoPath + ".log"
817+ logFile , err := os .OpenFile (logPath , os .O_CREATE | os .O_APPEND | os .O_WRONLY , 0644 )
818+ if err != nil {
819+ t .Fatalf ("Failed to create log file: %v" , err )
820+ }
821+
822+ var wg sync.WaitGroup
823+ wg .Add (1 )
824+
825+ testWriter := & fctesting.TestWriter {
826+ WriteFn : func (b []byte ) (int , error ) {
827+ defer wg .Done ()
828+
829+ return logFile .Write (b )
830+ },
831+ }
832+
833+ if err := captureFifoToFile (fctesting .NewLogEntry (t ), fifoPath , testWriter ); err != nil {
834+ t .Errorf ("Unexpected error: %v" , err )
835+ }
836+
837+ defer os .Remove (logPath )
838+
839+ wg .Wait ()
840+ _ , err = os .Stat (logPath )
841+ assert .NoError (t , err , "failed to stat file" )
842+ b , err := ioutil .ReadFile (logPath )
843+ assert .NoError (t , err , "failed to read logPath" )
844+ assert .Equal (t , expectedBytes , b )
845+ }
846+
847+ func TestCaptureFifoToFile_nonblock (t * testing.T ) {
848+ fifoPath := filepath .Join (testDataPath , "TestCaptureFifoToFile_nonblock" )
799849
800- fifoLogPath := fifoPath + ".log"
801- fifo , err := os .OpenFile (fifoLogPath , os .O_CREATE | os .O_APPEND | os .O_WRONLY , 0644 )
850+ if err := syscall .Mkfifo (fifoPath , 0700 ); err != nil {
851+ t .Fatalf ("Unexpected error during syscall.Mkfifo call: %v" , err )
852+ }
853+ defer os .Remove (fifoPath )
854+
855+ logPath := fifoPath + ".log"
856+ logFile , err := os .OpenFile (logPath , os .O_CREATE | os .O_APPEND | os .O_WRONLY , 0644 )
802857 if err != nil {
803- t .Fatalf ("Failed to create fifo file: %v" , err )
858+ t .Fatalf ("Failed to create log file: %v" , err )
859+ }
860+
861+ var wg sync.WaitGroup
862+ wg .Add (1 )
863+
864+ testWriter := & fctesting.TestWriter {
865+ WriteFn : func (b []byte ) (int , error ) {
866+ defer wg .Done ()
867+
868+ return logFile .Write (b )
869+ },
804870 }
805871
806- if err := captureFifoToFile (fctesting .NewLogEntry (t ), fifoPath , fifo ); err != nil {
872+ if err := captureFifoToFile (fctesting .NewLogEntry (t ), fifoPath , testWriter ); err != nil {
807873 t .Errorf ("Unexpected error: %v" , err )
808874 }
809875
810- defer os .Remove (fifoLogPath )
876+ defer os .Remove (logPath )
811877
812- if _ , err := os .Stat (fifoLogPath ); err != nil {
813- t .Errorf ("Failed to stat file: %v" , err )
878+ f , err := os .OpenFile (fifoPath , os .O_RDWR , 0600 )
879+ if err != nil {
880+ t .Fatalf ("Failed to open file, %q: %v" , fifoPath , err )
814881 }
882+ expectedBytes := []byte ("Hello world!" )
883+ f .Write (expectedBytes )
884+ defer f .Close ()
885+
886+ time .AfterFunc (250 * time .Millisecond , func () { f .Close () })
887+
888+ wg .Wait ()
889+ _ , err = os .Stat (logPath )
890+ assert .NoError (t , err , "failed to stat file" )
891+ b , err := ioutil .ReadFile (logPath )
892+ assert .NoError (t , err , "failed to read logPath" )
893+ assert .Equal (t , expectedBytes , b )
815894}
816895
817896func TestSocketPathSet (t * testing.T ) {
0 commit comments