@@ -25,6 +25,7 @@ import (
25
25
"path/filepath"
26
26
"strconv"
27
27
"strings"
28
+ "sync"
28
29
"syscall"
29
30
"testing"
30
31
"time"
@@ -146,7 +147,12 @@ func TestJailerMicroVMExecution(t *testing.T) {
146
147
socketPath := filepath .Join (jailerTestPath , "firecracker" , "TestJailerMicroVMExecution.socket" )
147
148
logFifo := filepath .Join (tmpDir , "firecracker.log" )
148
149
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" )
149
153
defer func () {
154
+ fw .Close ()
155
+ os .Remove (capturedLog )
150
156
os .Remove (socketPath )
151
157
os .Remove (logFifo )
152
158
os .Remove (metricsFifo )
@@ -183,6 +189,7 @@ func TestJailerMicroVMExecution(t *testing.T) {
183
189
ExecFile : getFirecrackerBinaryPath (),
184
190
ChrootStrategy : NewNaiveChrootStrategy (jailerFullRootPath , vmlinuxPath ),
185
191
},
192
+ FifoLogWriter : fw ,
186
193
}
187
194
188
195
if _ , err := os .Stat (vmlinuxPath ); err != nil {
@@ -250,6 +257,10 @@ func TestJailerMicroVMExecution(t *testing.T) {
250
257
}
251
258
252
259
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 ())
253
264
}
254
265
255
266
func TestMicroVMExecution (t * testing.T ) {
@@ -263,7 +274,12 @@ func TestMicroVMExecution(t *testing.T) {
263
274
socketPath := filepath .Join (testDataPath , "TestMicroVMExecution.sock" )
264
275
logFifo := filepath .Join (testDataPath , "firecracker.log" )
265
276
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" )
266
280
defer func () {
281
+ fw .Close ()
282
+ os .Remove (capturedLog )
267
283
os .Remove (socketPath )
268
284
os .Remove (logFifo )
269
285
os .Remove (metricsFifo )
@@ -292,6 +308,7 @@ func TestMicroVMExecution(t *testing.T) {
292
308
Debug : true ,
293
309
DisableValidation : true ,
294
310
NetworkInterfaces : networkIfaces ,
311
+ FifoLogWriter : fw ,
295
312
}
296
313
297
314
ctx := context .Background ()
@@ -353,6 +370,10 @@ func TestMicroVMExecution(t *testing.T) {
353
370
// didn't for some reason, we still need to terminate it:
354
371
m .StopVMM ()
355
372
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 ())
356
377
}
357
378
358
379
func TestStartVMM (t * testing.T ) {
@@ -774,7 +795,7 @@ func TestLogFiles(t *testing.T) {
774
795
}
775
796
776
797
func TestCaptureFifoToFile (t * testing.T ) {
777
- fifoPath := filepath .Join (testDataPath , "fifo " )
798
+ fifoPath := filepath .Join (testDataPath , "TestCaptureFifoToFile " )
778
799
779
800
if err := syscall .Mkfifo (fifoPath , 0700 ); err != nil {
780
801
t .Fatalf ("Unexpected error during syscall.Mkfifo call: %v" , err )
@@ -786,32 +807,90 @@ func TestCaptureFifoToFile(t *testing.T) {
786
807
t .Fatalf ("Failed to open file, %q: %v" , fifoPath , err )
787
808
}
788
809
789
- f .Write ([]byte ("Hello world!" ))
810
+ expectedBytes := []byte ("Hello world!" )
811
+ f .Write (expectedBytes )
790
812
defer f .Close ()
791
813
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" )
799
849
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 )
802
857
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
+ },
804
870
}
805
871
806
- if err := captureFifoToFile (fctesting .NewLogEntry (t ), fifoPath , fifo ); err != nil {
872
+ if err := captureFifoToFile (fctesting .NewLogEntry (t ), fifoPath , testWriter ); err != nil {
807
873
t .Errorf ("Unexpected error: %v" , err )
808
874
}
809
875
810
- defer os .Remove (fifoLogPath )
876
+ defer os .Remove (logPath )
811
877
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 )
814
881
}
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 )
815
894
}
816
895
817
896
func TestSocketPathSet (t * testing.T ) {
0 commit comments