Skip to content

Commit d659a45

Browse files
authored
Merge pull request #580 from Juneezee/test/t.TempDir
test: use `T.TempDir` to create temporary test directory
2 parents 2721a56 + 18ec2cf commit d659a45

File tree

7 files changed

+23
-59
lines changed

7 files changed

+23
-59
lines changed

internal/vm/dir_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,17 @@
1414
package vm
1515

1616
import (
17-
"io/ioutil"
18-
"os"
1917
"path"
2018
"strings"
2119
"testing"
2220

2321
"github.com/stretchr/testify/assert"
24-
"github.com/stretchr/testify/require"
2522
)
2623

2724
var invalidContainerIDs = []string{"", "id?", "*", "id/1", "id\\"}
2825

2926
func TestShimDir(t *testing.T) {
30-
runDir, err := ioutil.TempDir("", "run")
31-
require.NoError(t, err)
32-
defer os.RemoveAll(runDir)
27+
runDir := t.TempDir()
3328

3429
tests := []struct {
3530
name string

internal/vm/ioproxy_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,12 @@ func fileConnector(path string, flag int) IOConnector {
4141
}
4242

4343
func TestProxy(t *testing.T) {
44-
dir, err := ioutil.TempDir("", t.Name())
45-
require.NoError(t, err)
46-
defer os.RemoveAll(dir)
44+
dir := t.TempDir()
4745

4846
ctx := context.Background()
4947
content := "hello world"
5048

51-
err = ioutil.WriteFile(filepath.Join(dir, "input"), []byte(content), 0600)
49+
err := ioutil.WriteFile(filepath.Join(dir, "input"), []byte(content), 0600)
5250
require.NoError(t, err)
5351

5452
pair := &IOConnectorPair{

runtime/drive_handler_test.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ package main
1515

1616
import (
1717
"context"
18+
"io/ioutil"
1819
"os"
1920
"path/filepath"
2021
"strconv"
2122
"testing"
2223

23-
"io/ioutil"
24-
2524
"github.com/containerd/containerd/log"
2625
"github.com/firecracker-microvm/firecracker-containerd/internal/vm"
2726
"github.com/firecracker-microvm/firecracker-containerd/proto"
@@ -54,12 +53,10 @@ func TestContainerStubs(t *testing.T) {
5453
ctx := context.Background()
5554
logger := log.G(ctx)
5655

57-
tmpDir, err := ioutil.TempDir("", "TestCreateContainerStubs")
58-
require.NoError(t, err, "failed to create temporary directory")
59-
defer os.RemoveAll(tmpDir)
56+
tmpDir := t.TempDir()
6057

6158
stubDir := filepath.Join(tmpDir, "stubs")
62-
err = os.MkdirAll(stubDir, 0700)
59+
err := os.MkdirAll(stubDir, 0700)
6360
require.NoError(t, err, "failed to create stub dir")
6461

6562
patchedSrcDir := filepath.Join(tmpDir, "patchedSrcs")
@@ -127,12 +124,10 @@ func TestDriveMountStubs(t *testing.T) {
127124
ctx := context.Background()
128125
logger := log.G(ctx)
129126

130-
tmpDir, err := ioutil.TempDir("", "Test-CreateContainerStubs")
131-
require.NoError(t, err, "failed to create temporary directory")
132-
defer os.RemoveAll(tmpDir)
127+
tmpDir := t.TempDir()
133128

134129
stubDir := filepath.Join(tmpDir, "stubs")
135-
err = os.MkdirAll(stubDir, 0700)
130+
err := os.MkdirAll(stubDir, 0700)
136131
require.NoError(t, err, "failed to create stub dir")
137132

138133
patchedSrcDir := filepath.Join(tmpDir, "patchedSrcs")

runtime/jailer_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package main
1515

1616
import (
1717
"context"
18-
"io/ioutil"
1918
"os"
2019
"os/exec"
2120
"path/filepath"
@@ -62,14 +61,12 @@ func createSparseFile(path string, size int) error {
6261
}
6362

6463
func TestCopyFile_sparse(t *testing.T) {
65-
dir, err := ioutil.TempDir("", t.Name())
66-
require.NoError(t, err)
67-
defer os.RemoveAll(dir)
64+
dir := t.TempDir()
6865

6966
expectedSize := 1024
7067

7168
src := filepath.Join(dir, "original-sparse-file")
72-
err = createSparseFile(src, expectedSize)
69+
err := createSparseFile(src, expectedSize)
7370
require.NoError(t, err)
7471

7572
dst := filepath.Join(dir, "copied-as-sparse")
@@ -104,9 +101,7 @@ func TestJailer_invalidUIDGID(t *testing.T) {
104101
func TestNewJailer_Isolated(t *testing.T) {
105102
prepareIntegTest(t)
106103

107-
ociBundlePath, err := ioutil.TempDir("", t.Name())
108-
require.NoError(t, err)
109-
defer os.RemoveAll(ociBundlePath)
104+
ociBundlePath := t.TempDir()
110105
b, err := exec.Command(
111106
"cp",
112107
"firecracker-runc-config.json.example",

runtime/runc_jailer_test.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ import (
3535

3636
func TestBuildJailedRootHandler_Isolated(t *testing.T) {
3737
internal.RequiresIsolation(t)
38-
dir, err := ioutil.TempDir("", "TestBuildJailedRootHandler")
39-
require.NoError(t, err, "failed to create temporary directory")
38+
dir := t.TempDir()
4039

41-
defer os.RemoveAll(dir)
4240
kernelImagePath := filepath.Join(dir, "kernel-image")
4341
kernelImageFd, err := os.OpenFile(kernelImagePath, os.O_CREATE, 0600)
4442
require.NoError(t, err, "failed to create kernel image")
@@ -107,12 +105,11 @@ func TestMkdirAllWithPermissions_Isolated(t *testing.T) {
107105
// requires isolation so we can change uid/gid of files
108106
internal.RequiresIsolation(t)
109107

110-
tmpdir, err := ioutil.TempDir("", "TestMkdirAllWithPermissions")
111-
require.NoError(t, err, "failed to create temporary directory")
108+
tmpdir := t.TempDir()
112109

113110
existingPath := filepath.Join(tmpdir, "exists")
114111
existingMode := os.FileMode(0700)
115-
err = os.Mkdir(existingPath, existingMode)
112+
err := os.Mkdir(existingPath, existingMode)
116113
require.NoError(t, err, "failed to create existing part of test directory")
117114

118115
nonExistingPath := filepath.Join(existingPath, "nonexistent")
@@ -139,9 +136,7 @@ func TestBindMountToJail_Isolated(t *testing.T) {
139136
// The user must be root to call chown.
140137
internal.RequiresIsolation(t)
141138

142-
dir, err := ioutil.TempDir("", t.Name())
143-
require.NoError(t, err)
144-
defer os.RemoveAll(dir)
139+
dir := t.TempDir()
145140

146141
f, err := os.Create(filepath.Join(dir, "src1"))
147142
require.NoError(t, err)
@@ -196,13 +191,12 @@ func TestFifoHandler_Isolated(t *testing.T) {
196191
for _, tc := range testcases {
197192
tc := tc
198193
t.Run(tc.name, func(t *testing.T) {
199-
dir, err := ioutil.TempDir("", testNameToVMID(t.Name()))
200-
require.NoError(t, err)
194+
dir := t.TempDir()
201195

202196
logPath := filepath.Join(dir, tc.logPath)
203197
metricsPath := filepath.Join(dir, tc.metricsPath)
204198

205-
err = os.MkdirAll(filepath.Dir(logPath), 0750)
199+
err := os.MkdirAll(filepath.Dir(logPath), 0750)
206200
require.NoError(t, err)
207201
err = ioutil.WriteFile(logPath, []byte("log"), 0644)
208202
require.NoError(t, err)

runtime/service_integ_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,9 +2196,7 @@ func TestCreateVM_Isolated(t *testing.T) {
21962196
}
21972197
vmID := testNameToVMID(t.Name())
21982198

2199-
tempDir, err := ioutil.TempDir("", vmID)
2200-
require.NoError(t, err)
2201-
defer os.RemoveAll(tempDir)
2199+
tempDir := t.TempDir()
22022200

22032201
logFile := filepath.Join(tempDir, "log.fifo")
22042202
metricsFile := filepath.Join(tempDir, "metrics.fifo")
@@ -2220,7 +2218,7 @@ func TestCreateVM_Isolated(t *testing.T) {
22202218
// Ensure the response fields are populated correctly
22212219
assert.Equal(t, request.VMID, resp.VMID)
22222220

2223-
_, err = fcClient.StopVM(ctx, &proto.StopVMRequest{VMID: request.VMID})
2221+
_, err := fcClient.StopVM(ctx, &proto.StopVMRequest{VMID: request.VMID})
22242222
require.Equal(t, status.Code(err), codes.OK)
22252223
}
22262224
}
@@ -2318,9 +2316,7 @@ func TestPauseResume_Isolated(t *testing.T) {
23182316
runTest := func(t *testing.T, request *proto.CreateVMRequest, state func(ctx context.Context, resp *proto.CreateVMResponse)) {
23192317
vmID := testNameToVMID(t.Name())
23202318

2321-
tempDir, err := ioutil.TempDir("", vmID)
2322-
require.NoError(t, err)
2323-
defer os.RemoveAll(tempDir)
2319+
tempDir := t.TempDir()
23242320

23252321
logFile := filepath.Join(tempDir, "log.fifo")
23262322
metricsFile := filepath.Join(tempDir, "metrics.fifo")
@@ -2347,7 +2343,7 @@ func TestPauseResume_Isolated(t *testing.T) {
23472343
assert.Equal(t, request.VMID, resp.VMID)
23482344

23492345
// Resume the VM since state() may pause the VM.
2350-
_, err = fcClient.ResumeVM(ctx, &proto.ResumeVMRequest{VMID: vmID})
2346+
_, err := fcClient.ResumeVM(ctx, &proto.ResumeVMRequest{VMID: vmID})
23512347
require.NoError(t, err)
23522348

23532349
_, err = fcClient.StopVM(ctx, &proto.StopVMRequest{VMID: vmID})

runtime/service_test.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package main
1616
import (
1717
"context"
1818
"fmt"
19-
"io/ioutil"
2019
"os"
2120
"path/filepath"
2221
"testing"
@@ -234,9 +233,7 @@ func TestBuildVMConfiguration(t *testing.T) {
234233
config: tc.config,
235234
}
236235

237-
tempDir, err := ioutil.TempDir(os.TempDir(), namespace)
238-
assert.NoError(t, err)
239-
defer os.RemoveAll(tempDir)
236+
tempDir := t.TempDir()
240237

241238
svc.shimDir = vm.Dir(tempDir)
242239
svc.jailer = newNoopJailer(context.Background(), svc.logger, svc.shimDir)
@@ -304,13 +301,7 @@ func TestDebugConfig(t *testing.T) {
304301
},
305302
}
306303

307-
cwd, err := os.Getwd()
308-
require.NoError(t, err, "failed to get working dir")
309-
310-
path, err := ioutil.TempDir(cwd, "TestDebugConfig")
311-
assert.NoError(t, err, "failed to create temp directory")
312-
313-
defer os.RemoveAll(path)
304+
path := t.TempDir()
314305

315306
for i, c := range cases {
316307
c := c

0 commit comments

Comments
 (0)