@@ -76,6 +76,18 @@ func envOrDefault(k, empty string) string {
76
76
return value
77
77
}
78
78
79
+ // Replace filesystem-unsafe characters (such as /) which are often seen in Go's test names
80
+ var fsSafeTestName = strings .NewReplacer ("/" , "_" )
81
+
82
+ func makeSocketPath (tb testing.TB ) (string , func ()) {
83
+ tb .Helper ()
84
+
85
+ dir , err := ioutil .TempDir ("" , fsSafeTestName .Replace (tb .Name ()))
86
+ require .NoError (tb , err )
87
+
88
+ return filepath .Join (dir , "fc.sock" ), func () { os .RemoveAll (dir ) }
89
+ }
90
+
79
91
func init () {
80
92
flag .BoolVar (& skipTuntap , "test.skip-tuntap" , false , "Disables tests that require a tuntap device" )
81
93
@@ -279,19 +291,18 @@ func TestMicroVMExecution(t *testing.T) {
279
291
var nCpus int64 = 2
280
292
cpuTemplate := models .CPUTemplate (models .CPUTemplateT2 )
281
293
var memSz int64 = 256
282
- socketPath := filepath .Join (testDataPath , "TestMicroVMExecution.sock" )
283
- logFifo := filepath .Join (testDataPath , "firecracker.log" )
284
- metricsFifo := filepath .Join (testDataPath , "firecracker-metrics" )
285
- capturedLog := filepath .Join (testDataPath , "writer.fifo" )
294
+
295
+ dir , err := ioutil .TempDir ("" , t .Name ())
296
+ require .NoError (t , err )
297
+ defer os .RemoveAll (dir )
298
+
299
+ socketPath := filepath .Join (dir , "TestMicroVMExecution.sock" )
300
+ logFifo := filepath .Join (dir , "firecracker.log" )
301
+ metricsFifo := filepath .Join (dir , "firecracker-metrics" )
302
+ capturedLog := filepath .Join (dir , "writer.fifo" )
286
303
fw , err := os .OpenFile (capturedLog , os .O_CREATE | os .O_RDWR , 0600 )
287
304
require .NoError (t , err , "failed to open fifo writer file" )
288
- defer func () {
289
- fw .Close ()
290
- os .Remove (capturedLog )
291
- os .Remove (socketPath )
292
- os .Remove (logFifo )
293
- os .Remove (metricsFifo )
294
- }()
305
+ defer fw .Close ()
295
306
296
307
vmlinuxPath := getVmlinuxPath (t )
297
308
@@ -384,8 +395,8 @@ func TestMicroVMExecution(t *testing.T) {
384
395
}
385
396
386
397
func TestStartVMM (t * testing.T ) {
387
- socketPath := filepath . Join ( "testdata" , "TestStartVMM.sock" )
388
- defer os . Remove ( socketPath )
398
+ socketPath , cleanup := makeSocketPath ( t )
399
+ defer cleanup ( )
389
400
cfg := Config {
390
401
SocketPath : socketPath ,
391
402
}
@@ -509,8 +520,8 @@ func testLogAndMetrics(t *testing.T, logLevel string) string {
509
520
}
510
521
511
522
func TestStartVMMOnce (t * testing.T ) {
512
- socketPath := filepath . Join ( "testdata" , "TestStartVMMOnce.sock" )
513
- defer os . Remove ( socketPath )
523
+ socketPath , cleanup := makeSocketPath ( t )
524
+ defer cleanup ( )
514
525
515
526
cfg := Config {
516
527
SocketPath : socketPath ,
@@ -744,7 +755,8 @@ func TestWaitForSocket(t *testing.T) {
744
755
// 2. The expected file is not created within the deadline
745
756
// 3. The process responsible for creating the file exits
746
757
// (indicated by an error published to exitchan)
747
- filename := "./test-create-file"
758
+ filename , cleanup := makeSocketPath (t )
759
+ defer cleanup ()
748
760
errchan := make (chan error )
749
761
750
762
m := Machine {
@@ -772,7 +784,7 @@ func TestWaitForSocket(t *testing.T) {
772
784
t .Error ("waitforSocket did not return an expected timeout error" )
773
785
}
774
786
775
- os . Remove ( filename )
787
+ cleanup ( )
776
788
777
789
// No socket file
778
790
if err := m .waitForSocket (100 * time .Millisecond , errchan ); err != context .DeadlineExceeded {
@@ -900,7 +912,11 @@ func TestLogFiles(t *testing.T) {
900
912
}
901
913
902
914
func TestCaptureFifoToFile (t * testing.T ) {
903
- fifoPath := filepath .Join (testDataPath , "TestCaptureFifoToFile" )
915
+ dir , err := ioutil .TempDir ("" , t .Name ())
916
+ require .NoError (t , err )
917
+ defer os .RemoveAll (dir )
918
+
919
+ fifoPath := filepath .Join (dir , "TestCaptureFifoToFile" )
904
920
905
921
if err := syscall .Mkfifo (fifoPath , 0700 ); err != nil {
906
922
t .Fatalf ("Unexpected error during syscall.Mkfifo call: %v" , err )
@@ -953,7 +969,11 @@ func TestCaptureFifoToFile(t *testing.T) {
953
969
}
954
970
955
971
func TestCaptureFifoToFile_nonblock (t * testing.T ) {
956
- fifoPath := filepath .Join (testDataPath , "TestCaptureFifoToFile_nonblock" )
972
+ dir , err := ioutil .TempDir ("" , t .Name ())
973
+ require .NoError (t , err )
974
+ defer os .RemoveAll (dir )
975
+
976
+ fifoPath := filepath .Join (dir , "TestCaptureFifoToFile_nonblock" )
957
977
958
978
if err := syscall .Mkfifo (fifoPath , 0700 ); err != nil {
959
979
t .Fatalf ("Unexpected error during syscall.Mkfifo call: %v" , err )
@@ -1071,17 +1091,21 @@ func TestPID(t *testing.T) {
1071
1091
t .Errorf ("expected an error, but received none" )
1072
1092
}
1073
1093
1094
+ dir , err := ioutil .TempDir ("" , t .Name ())
1095
+ require .NoError (t , err )
1096
+ defer os .RemoveAll (dir )
1097
+
1074
1098
var nCpus int64 = 2
1075
1099
cpuTemplate := models .CPUTemplate (models .CPUTemplateT2 )
1076
1100
var memSz int64 = 256
1077
- socketPath := filepath .Join (testDataPath , "TestPID.sock" )
1101
+ socketPath := filepath .Join (dir , "TestPID.sock" )
1078
1102
defer os .Remove (socketPath )
1079
1103
1080
1104
vmlinuxPath := getVmlinuxPath (t )
1081
1105
1082
1106
rootfsBytes , err := ioutil .ReadFile (testRootfs )
1083
1107
require .NoError (t , err , "failed to read rootfs file" )
1084
- rootfsPath := filepath .Join (testDataPath , "TestPID.img" )
1108
+ rootfsPath := filepath .Join (dir , "TestPID.img" )
1085
1109
err = ioutil .WriteFile (rootfsPath , rootfsBytes , 0666 )
1086
1110
require .NoError (t , err , "failed to copy vm rootfs to %s" , rootfsPath )
1087
1111
@@ -1148,8 +1172,12 @@ func TestCaptureFifoToFile_leak(t *testing.T) {
1148
1172
exitCh : make (chan struct {}),
1149
1173
}
1150
1174
1151
- fifoPath := filepath .Join (testDataPath , "TestCaptureFifoToFileLeak.fifo" )
1152
- err := syscall .Mkfifo (fifoPath , 0700 )
1175
+ dir , err := ioutil .TempDir ("" , t .Name ())
1176
+ require .NoError (t , err )
1177
+ defer os .RemoveAll (dir )
1178
+
1179
+ fifoPath := filepath .Join (dir , "TestCaptureFifoToFileLeak.fifo" )
1180
+ err = syscall .Mkfifo (fifoPath , 0700 )
1153
1181
require .NoError (t , err , "failed to make fifo" )
1154
1182
defer os .Remove (fifoPath )
1155
1183
@@ -1196,9 +1224,6 @@ func TestCaptureFifoToFile_leak(t *testing.T) {
1196
1224
}
1197
1225
}
1198
1226
1199
- // Replace filesystem-unsafe characters (such as /) which are often seen in Go's test names
1200
- var fsSafeTestName = strings .NewReplacer ("/" , "_" )
1201
-
1202
1227
func TestWait (t * testing.T ) {
1203
1228
fctesting .RequiresRoot (t )
1204
1229
@@ -1247,8 +1272,8 @@ func TestWait(t *testing.T) {
1247
1272
ctx := context .Background ()
1248
1273
vmContext , vmCancel := context .WithCancel (context .Background ())
1249
1274
1250
- socketPath := filepath . Join ( testDataPath , fsSafeTestName . Replace ( t . Name ()) )
1251
- defer os . Remove ( socketPath )
1275
+ socketPath , cleanup := makeSocketPath ( t )
1276
+ defer cleanup ( )
1252
1277
1253
1278
// Tee logs for validation:
1254
1279
var logBuffer bytes.Buffer
@@ -1378,7 +1403,8 @@ func createValidConfig(t *testing.T, socketPath string) Config {
1378
1403
}
1379
1404
1380
1405
func TestSignalForwarding (t * testing.T ) {
1381
- socketPath := filepath .Join (testDataPath , "TestSignalForwarding.sock" )
1406
+ socketPath , cleanup := makeSocketPath (t )
1407
+ defer cleanup ()
1382
1408
1383
1409
forwardedSignals := []os.Signal {
1384
1410
syscall .SIGUSR1 ,
@@ -1484,6 +1510,10 @@ func TestSignalForwarding(t *testing.T) {
1484
1510
func TestPauseResume (t * testing.T ) {
1485
1511
fctesting .RequiresRoot (t )
1486
1512
1513
+ dir , err := ioutil .TempDir ("" , t .Name ())
1514
+ require .NoError (t , err )
1515
+ defer os .RemoveAll (dir )
1516
+
1487
1517
cases := []struct {
1488
1518
name string
1489
1519
state func (m * Machine , ctx context.Context )
@@ -1548,8 +1578,8 @@ func TestPauseResume(t *testing.T) {
1548
1578
t .Run (c .name , func (t * testing.T ) {
1549
1579
ctx := context .Background ()
1550
1580
1551
- socketPath := filepath . Join ( testDataPath , fsSafeTestName . Replace ( t . Name ()) )
1552
- defer os . Remove ( socketPath )
1581
+ socketPath , cleanup := makeSocketPath ( t )
1582
+ defer cleanup ( )
1553
1583
1554
1584
// Tee logs for validation:
1555
1585
var logBuffer bytes.Buffer
@@ -1591,6 +1621,10 @@ func TestPauseResume(t *testing.T) {
1591
1621
func TestCreateSnapshot (t * testing.T ) {
1592
1622
fctesting .RequiresRoot (t )
1593
1623
1624
+ dir , err := ioutil .TempDir ("" , t .Name ())
1625
+ require .NoError (t , err )
1626
+ defer os .RemoveAll (dir )
1627
+
1594
1628
cases := []struct {
1595
1629
name string
1596
1630
createSnapshot func (m * Machine , ctx context.Context , memPath , snapPath string )
@@ -1618,7 +1652,7 @@ func TestCreateSnapshot(t *testing.T) {
1618
1652
t .Run (c .name , func (t * testing.T ) {
1619
1653
ctx := context .Background ()
1620
1654
1621
- socketPath := filepath .Join (testDataPath , fsSafeTestName .Replace (t .Name ()))
1655
+ socketPath := filepath .Join (dir , fsSafeTestName .Replace (t .Name ()))
1622
1656
snapPath := socketPath + "SnapFile"
1623
1657
memPath := socketPath + "MemFile"
1624
1658
defer os .Remove (socketPath )
0 commit comments