diff --git a/client/fs_endpoint_test.go b/client/fs_endpoint_test.go index a8c71265c352..317a2c9686d1 100644 --- a/client/fs_endpoint_test.go +++ b/client/fs_endpoint_test.go @@ -31,19 +31,14 @@ import ( "github.com/stretchr/testify/require" ) -// tempAllocDir returns a new alloc dir that is rooted in a temp dir. The caller -// should destroy the temp dir. +// tempAllocDir returns a new alloc dir that is rooted in a temp dir. Caller +// should cleanup with AllocDir.Destroy() func tempAllocDir(t testing.TB) *allocdir.AllocDir { - dir, err := ioutil.TempDir("", "nomadtest") - if err != nil { - t.Fatalf("TempDir() failed: %v", err) - } + dir := t.TempDir() - if err := os.Chmod(dir, 0777); err != nil { - t.Fatalf("failed to chmod dir: %v", err) - } + require.NoError(t, os.Chmod(dir, 0o777)) - return allocdir.NewAllocDir(testlog.HCLogger(t), dir, "test") + return allocdir.NewAllocDir(testlog.HCLogger(t), dir, "test_allocid") } type nopWriteCloser struct { @@ -1561,12 +1556,11 @@ func TestFS_findClosest(t *testing.T) { func TestFS_streamFile_NoFile(t *testing.T) { t.Parallel() - require := require.New(t) c, cleanup := TestClient(t, nil) defer cleanup() ad := tempAllocDir(t) - defer os.RemoveAll(ad.AllocDir) + defer ad.Destroy() frames := make(chan *sframer.StreamFrame, 32) framer := sframer.NewStreamFramer(frames, streamHeartbeatRate, streamBatchWindow, streamFrameSize) @@ -1575,11 +1569,11 @@ func TestFS_streamFile_NoFile(t *testing.T) { err := c.endpoints.FileSystem.streamFile( context.Background(), 0, "foo", 0, ad, framer, nil) - require.NotNil(err) + require.NoError(t, err) if runtime.GOOS == "windows" { - require.Contains(err.Error(), "cannot find the file") + require.Contains(t, err.Error(), "cannot find the file") } else { - require.Contains(err.Error(), "no such file") + require.Contains(t, err.Error(), "no such file") } } @@ -1660,16 +1654,15 @@ func TestFS_streamFile_Truncate(t *testing.T) { // Get a temp alloc dir ad := tempAllocDir(t) - defer os.RemoveAll(ad.AllocDir) + require.NoError(t, ad.Build()) + defer ad.Destroy() // Create a file in the temp dir data := []byte("helloworld") streamFile := "stream_file" streamFilePath := filepath.Join(ad.AllocDir, streamFile) f, err := os.Create(streamFilePath) - if err != nil { - t.Fatalf("Failed to create file: %v", err) - } + require.NoError(t, err) defer f.Close() // Start the reader @@ -1768,7 +1761,8 @@ func TestFS_streamImpl_Delete(t *testing.T) { // Get a temp alloc dir ad := tempAllocDir(t) - defer os.RemoveAll(ad.AllocDir) + require.NoError(t, ad.Build()) + defer ad.Destroy() // Create a file in the temp dir data := []byte("helloworld") @@ -1840,7 +1834,8 @@ func TestFS_logsImpl_NoFollow(t *testing.T) { // Get a temp alloc dir and create the log dir ad := tempAllocDir(t) - defer os.RemoveAll(ad.AllocDir) + require.NoError(t, ad.Build()) + defer ad.Destroy() logDir := filepath.Join(ad.SharedDir, allocdir.LogDirName) if err := os.MkdirAll(logDir, 0777); err != nil { @@ -1908,7 +1903,8 @@ func TestFS_logsImpl_Follow(t *testing.T) { // Get a temp alloc dir and create the log dir ad := tempAllocDir(t) - defer os.RemoveAll(ad.AllocDir) + require.NoError(t, ad.Build()) + defer ad.Destroy() logDir := filepath.Join(ad.SharedDir, allocdir.LogDirName) if err := os.MkdirAll(logDir, 0777); err != nil { diff --git a/drivers/docker/driver_unix_test.go b/drivers/docker/driver_unix_test.go index 019b42764b15..701ed8985713 100644 --- a/drivers/docker/driver_unix_test.go +++ b/drivers/docker/driver_unix_test.go @@ -765,15 +765,15 @@ func copyImage(t *testing.T, taskDir *allocdir.TaskDir, image string) { // copyFile moves an existing file to the destination func copyFile(src, dst string, t *testing.T) { + t.Helper() in, err := os.Open(src) if err != nil { t.Fatalf("copying %v -> %v failed: %v", src, dst, err) } defer in.Close() out, err := os.Create(dst) - if err != nil { - t.Fatalf("copying %v -> %v failed: %v", src, dst, err) - } + require.NoError(t, err, "copying %v -> %v failed: %v", src, dst, err) + defer func() { if err := out.Close(); err != nil { t.Fatalf("copying %v -> %v failed: %v", src, dst, err) diff --git a/drivers/shared/executor/executor_linux_test.go b/drivers/shared/executor/executor_linux_test.go index 16d5fdc3f71e..687c646350ee 100644 --- a/drivers/shared/executor/executor_linux_test.go +++ b/drivers/shared/executor/executor_linux_test.go @@ -64,7 +64,7 @@ func testExecutorCommandWithChroot(t *testing.T) *testExecCmd { task := alloc.Job.TaskGroups[0].Tasks[0] taskEnv := taskenv.NewBuilder(mock.Node(), alloc, task, "global").Build() - allocDir := allocdir.NewAllocDir(testlog.HCLogger(t), t.TempDir(), alloc.ID) + allocDir := allocdir.NewAllocDir(testlog.HCLogger(t), os.TempDir(), alloc.ID) if err := allocDir.Build(); err != nil { t.Fatalf("AllocDir.Build() failed: %v", err) } diff --git a/plugins/drivers/testutils/testing.go b/plugins/drivers/testutils/testing.go index b26b7e1f32bc..32cc24477315 100644 --- a/plugins/drivers/testutils/testing.go +++ b/plugins/drivers/testutils/testing.go @@ -83,10 +83,12 @@ func (h *DriverHarness) Kill() { func (h *DriverHarness) MkAllocDir(t *drivers.TaskConfig, enableLogs bool) func() { dir, err := ioutil.TempDir("", "nomad_driver_harness-") require.NoError(h.t, err) - t.AllocDir = dir allocDir := allocdir.NewAllocDir(h.logger, dir, t.AllocID) require.NoError(h.t, allocDir.Build()) + + t.AllocDir = allocDir.AllocDir + taskDir := allocDir.NewTaskDir(t.Name) caps, err := h.Capabilities()