14
14
package firecracker
15
15
16
16
import (
17
+ "bytes"
17
18
"context"
18
19
"errors"
19
20
"flag"
@@ -30,6 +31,7 @@ import (
30
31
"testing"
31
32
"time"
32
33
34
+ "github.com/sirupsen/logrus"
33
35
"github.com/stretchr/testify/assert"
34
36
"github.com/stretchr/testify/require"
35
37
@@ -830,7 +832,10 @@ func TestCaptureFifoToFile(t *testing.T) {
830
832
},
831
833
}
832
834
833
- if err := captureFifoToFile (fctesting .NewLogEntry (t ), fifoPath , testWriter ); err != nil {
835
+ m := & Machine {
836
+ exitCh : make (chan struct {}),
837
+ }
838
+ if err := m .captureFifoToFile (fctesting .NewLogEntry (t ), fifoPath , testWriter ); err != nil {
834
839
t .Errorf ("Unexpected error: %v" , err )
835
840
}
836
841
@@ -869,7 +874,10 @@ func TestCaptureFifoToFile_nonblock(t *testing.T) {
869
874
},
870
875
}
871
876
872
- if err := captureFifoToFile (fctesting .NewLogEntry (t ), fifoPath , testWriter ); err != nil {
877
+ m := & Machine {
878
+ exitCh : make (chan struct {}),
879
+ }
880
+ if err := m .captureFifoToFile (fctesting .NewLogEntry (t ), fifoPath , testWriter ); err != nil {
873
881
t .Errorf ("Unexpected error: %v" , err )
874
882
}
875
883
@@ -1025,3 +1033,45 @@ func TestPID(t *testing.T) {
1025
1033
}
1026
1034
1027
1035
}
1036
+
1037
+ func TestCaptureFifoToFile_leak (t * testing.T ) {
1038
+ m := & Machine {
1039
+ exitCh : make (chan struct {}),
1040
+ }
1041
+
1042
+ fifoPath := filepath .Join (testDataPath , "TestCaptureFifoToFileLeak.fifo" )
1043
+ err := syscall .Mkfifo (fifoPath , 0700 )
1044
+ require .NoError (t , err , "failed to make fifo" )
1045
+ defer os .Remove (fifoPath )
1046
+
1047
+ fd , err := syscall .Open (fifoPath , syscall .O_RDWR | syscall .O_NONBLOCK , 0600 )
1048
+ require .NoError (t , err , "failed to open fifo path" )
1049
+ f := os .NewFile (uintptr (fd ), fifoPath )
1050
+ assert .NotNil (t , f , "failed to create new file" )
1051
+ go func () {
1052
+ for {
1053
+ select {
1054
+ case <- m .exitCh :
1055
+ break
1056
+ default :
1057
+ _ , err := f .Write ([]byte ("A" ))
1058
+ assert .NoError (t , err , "failed to write bytes to fifo" )
1059
+ }
1060
+ }
1061
+ }()
1062
+
1063
+ buf := bytes .NewBuffer (nil )
1064
+
1065
+ loggerBuffer := bytes .NewBuffer (nil )
1066
+ logger := fctesting .NewLogEntry (t )
1067
+ logger .Logger .Level = logrus .WarnLevel
1068
+ logger .Logger .Out = loggerBuffer
1069
+ err = m .captureFifoToFile (logger , fifoPath , buf )
1070
+ assert .NoError (t , err , "failed to capture fifo to file" )
1071
+ close (m .exitCh )
1072
+
1073
+ // wait sometime for the logs to populate
1074
+ 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 )
1077
+ }
0 commit comments