Skip to content

Commit 4b4aee5

Browse files
committed
Don't create temporary files under testdata/
BuildKite runs multiple tests in parallel. Using testdata/ from the tests would cause a race condition. Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent a46dd08 commit 4b4aee5

File tree

3 files changed

+73
-36
lines changed

3 files changed

+73
-36
lines changed

firecracker_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package firecracker
22

33
import (
44
"context"
5-
"os"
65
"path/filepath"
76
"testing"
87
"time"
@@ -17,7 +16,8 @@ func TestClient(t *testing.T) {
1716
}
1817

1918
ctx := context.Background()
20-
socketpath := filepath.Join(testDataPath, "test.socket")
19+
socketpath, cleanup := makeSocketPath(t)
20+
defer cleanup()
2121

2222
cmd := VMCommandBuilder{}.
2323
WithBin(getFirecrackerBinaryPath()).
@@ -32,7 +32,6 @@ func TestClient(t *testing.T) {
3232
if err := cmd.Process.Kill(); err != nil {
3333
t.Errorf("failed to kill process: %v", err)
3434
}
35-
os.Remove(socketpath)
3635
}()
3736

3837
drive := &models.Drive{

machine_test.go

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ func envOrDefault(k, empty string) string {
7676
return value
7777
}
7878

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+
7991
func init() {
8092
flag.BoolVar(&skipTuntap, "test.skip-tuntap", false, "Disables tests that require a tuntap device")
8193

@@ -279,19 +291,18 @@ func TestMicroVMExecution(t *testing.T) {
279291
var nCpus int64 = 2
280292
cpuTemplate := models.CPUTemplate(models.CPUTemplateT2)
281293
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")
286303
fw, err := os.OpenFile(capturedLog, os.O_CREATE|os.O_RDWR, 0600)
287304
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()
295306

296307
vmlinuxPath := getVmlinuxPath(t)
297308

@@ -384,8 +395,8 @@ func TestMicroVMExecution(t *testing.T) {
384395
}
385396

386397
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()
389400
cfg := Config{
390401
SocketPath: socketPath,
391402
}
@@ -509,8 +520,8 @@ func testLogAndMetrics(t *testing.T, logLevel string) string {
509520
}
510521

511522
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()
514525

515526
cfg := Config{
516527
SocketPath: socketPath,
@@ -744,7 +755,8 @@ func TestWaitForSocket(t *testing.T) {
744755
// 2. The expected file is not created within the deadline
745756
// 3. The process responsible for creating the file exits
746757
// (indicated by an error published to exitchan)
747-
filename := "./test-create-file"
758+
filename, cleanup := makeSocketPath(t)
759+
defer cleanup()
748760
errchan := make(chan error)
749761

750762
m := Machine{
@@ -772,7 +784,7 @@ func TestWaitForSocket(t *testing.T) {
772784
t.Error("waitforSocket did not return an expected timeout error")
773785
}
774786

775-
os.Remove(filename)
787+
cleanup()
776788

777789
// No socket file
778790
if err := m.waitForSocket(100*time.Millisecond, errchan); err != context.DeadlineExceeded {
@@ -900,7 +912,11 @@ func TestLogFiles(t *testing.T) {
900912
}
901913

902914
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")
904920

905921
if err := syscall.Mkfifo(fifoPath, 0700); err != nil {
906922
t.Fatalf("Unexpected error during syscall.Mkfifo call: %v", err)
@@ -953,7 +969,11 @@ func TestCaptureFifoToFile(t *testing.T) {
953969
}
954970

955971
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")
957977

958978
if err := syscall.Mkfifo(fifoPath, 0700); err != nil {
959979
t.Fatalf("Unexpected error during syscall.Mkfifo call: %v", err)
@@ -1071,17 +1091,21 @@ func TestPID(t *testing.T) {
10711091
t.Errorf("expected an error, but received none")
10721092
}
10731093

1094+
dir, err := ioutil.TempDir("", t.Name())
1095+
require.NoError(t, err)
1096+
defer os.RemoveAll(dir)
1097+
10741098
var nCpus int64 = 2
10751099
cpuTemplate := models.CPUTemplate(models.CPUTemplateT2)
10761100
var memSz int64 = 256
1077-
socketPath := filepath.Join(testDataPath, "TestPID.sock")
1101+
socketPath := filepath.Join(dir, "TestPID.sock")
10781102
defer os.Remove(socketPath)
10791103

10801104
vmlinuxPath := getVmlinuxPath(t)
10811105

10821106
rootfsBytes, err := ioutil.ReadFile(testRootfs)
10831107
require.NoError(t, err, "failed to read rootfs file")
1084-
rootfsPath := filepath.Join(testDataPath, "TestPID.img")
1108+
rootfsPath := filepath.Join(dir, "TestPID.img")
10851109
err = ioutil.WriteFile(rootfsPath, rootfsBytes, 0666)
10861110
require.NoError(t, err, "failed to copy vm rootfs to %s", rootfsPath)
10871111

@@ -1148,8 +1172,12 @@ func TestCaptureFifoToFile_leak(t *testing.T) {
11481172
exitCh: make(chan struct{}),
11491173
}
11501174

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)
11531181
require.NoError(t, err, "failed to make fifo")
11541182
defer os.Remove(fifoPath)
11551183

@@ -1196,9 +1224,6 @@ func TestCaptureFifoToFile_leak(t *testing.T) {
11961224
}
11971225
}
11981226

1199-
// Replace filesystem-unsafe characters (such as /) which are often seen in Go's test names
1200-
var fsSafeTestName = strings.NewReplacer("/", "_")
1201-
12021227
func TestWait(t *testing.T) {
12031228
fctesting.RequiresRoot(t)
12041229

@@ -1247,8 +1272,8 @@ func TestWait(t *testing.T) {
12471272
ctx := context.Background()
12481273
vmContext, vmCancel := context.WithCancel(context.Background())
12491274

1250-
socketPath := filepath.Join(testDataPath, fsSafeTestName.Replace(t.Name()))
1251-
defer os.Remove(socketPath)
1275+
socketPath, cleanup := makeSocketPath(t)
1276+
defer cleanup()
12521277

12531278
// Tee logs for validation:
12541279
var logBuffer bytes.Buffer
@@ -1378,7 +1403,8 @@ func createValidConfig(t *testing.T, socketPath string) Config {
13781403
}
13791404

13801405
func TestSignalForwarding(t *testing.T) {
1381-
socketPath := filepath.Join(testDataPath, "TestSignalForwarding.sock")
1406+
socketPath, cleanup := makeSocketPath(t)
1407+
defer cleanup()
13821408

13831409
forwardedSignals := []os.Signal{
13841410
syscall.SIGUSR1,
@@ -1484,6 +1510,10 @@ func TestSignalForwarding(t *testing.T) {
14841510
func TestPauseResume(t *testing.T) {
14851511
fctesting.RequiresRoot(t)
14861512

1513+
dir, err := ioutil.TempDir("", t.Name())
1514+
require.NoError(t, err)
1515+
defer os.RemoveAll(dir)
1516+
14871517
cases := []struct {
14881518
name string
14891519
state func(m *Machine, ctx context.Context)
@@ -1548,8 +1578,8 @@ func TestPauseResume(t *testing.T) {
15481578
t.Run(c.name, func(t *testing.T) {
15491579
ctx := context.Background()
15501580

1551-
socketPath := filepath.Join(testDataPath, fsSafeTestName.Replace(t.Name()))
1552-
defer os.Remove(socketPath)
1581+
socketPath, cleanup := makeSocketPath(t)
1582+
defer cleanup()
15531583

15541584
// Tee logs for validation:
15551585
var logBuffer bytes.Buffer
@@ -1591,6 +1621,10 @@ func TestPauseResume(t *testing.T) {
15911621
func TestCreateSnapshot(t *testing.T) {
15921622
fctesting.RequiresRoot(t)
15931623

1624+
dir, err := ioutil.TempDir("", t.Name())
1625+
require.NoError(t, err)
1626+
defer os.RemoveAll(dir)
1627+
15941628
cases := []struct {
15951629
name string
15961630
createSnapshot func(m *Machine, ctx context.Context, memPath, snapPath string)
@@ -1618,7 +1652,7 @@ func TestCreateSnapshot(t *testing.T) {
16181652
t.Run(c.name, func(t *testing.T) {
16191653
ctx := context.Background()
16201654

1621-
socketPath := filepath.Join(testDataPath, fsSafeTestName.Replace(t.Name()))
1655+
socketPath := filepath.Join(dir, fsSafeTestName.Replace(t.Name()))
16221656
snapPath := socketPath + "SnapFile"
16231657
memPath := socketPath + "MemFile"
16241658
defer os.Remove(socketPath)

network_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@ func testNetworkMachineCNI(t *testing.T, useConfFile bool) {
255255

256256
cniBinPath := []string{"/opt/cni/bin", testDataBin}
257257

258-
testCNIDir := filepath.Join(testDataPath, "TestCNI")
258+
dir, err := ioutil.TempDir("", fsSafeTestName.Replace(t.Name()))
259+
require.NoError(t, err)
260+
defer os.RemoveAll(dir)
261+
262+
testCNIDir := filepath.Join(dir, "TestCNI")
259263
os.RemoveAll(testCNIDir)
260264
defer os.RemoveAll(testCNIDir)
261265

0 commit comments

Comments
 (0)