Skip to content

Commit

Permalink
Backport of test: use T.TempDir to create temporary test directory …
Browse files Browse the repository at this point in the history
…into release/1.3.x (#12966)

This pull request was automerged via backport-assistant
  • Loading branch information
hc-github-team-nomad-core authored May 12, 2022
1 parent 394eafe commit b47797d
Show file tree
Hide file tree
Showing 53 changed files with 221 additions and 769 deletions.
61 changes: 10 additions & 51 deletions client/allocdir/alloc_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -51,11 +50,7 @@ var (
func TestAllocDir_BuildAlloc(t *testing.T) {
ci.Parallel(t)

tmp, err := ioutil.TempDir("", "AllocDir")
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}
defer os.RemoveAll(tmp)
tmp := t.TempDir()

d := NewAllocDir(testlog.HCLogger(t), tmp, "test")
defer d.Destroy()
Expand Down Expand Up @@ -103,11 +98,7 @@ func TestAllocDir_MountSharedAlloc(t *testing.T) {
ci.Parallel(t)
MountCompatible(t)

tmp, err := ioutil.TempDir("", "AllocDir")
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}
defer os.RemoveAll(tmp)
tmp := t.TempDir()

d := NewAllocDir(testlog.HCLogger(t), tmp, "test")
defer d.Destroy()
Expand Down Expand Up @@ -150,11 +141,7 @@ func TestAllocDir_MountSharedAlloc(t *testing.T) {
func TestAllocDir_Snapshot(t *testing.T) {
ci.Parallel(t)

tmp, err := ioutil.TempDir("", "AllocDir")
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}
defer os.RemoveAll(tmp)
tmp := t.TempDir()

d := NewAllocDir(testlog.HCLogger(t), tmp, "test")
defer d.Destroy()
Expand Down Expand Up @@ -232,17 +219,8 @@ func TestAllocDir_Snapshot(t *testing.T) {
func TestAllocDir_Move(t *testing.T) {
ci.Parallel(t)

tmp1, err := ioutil.TempDir("", "AllocDir")
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}
defer os.RemoveAll(tmp1)

tmp2, err := ioutil.TempDir("", "AllocDir")
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}
defer os.RemoveAll(tmp2)
tmp1 := t.TempDir()
tmp2 := t.TempDir()

// Create two alloc dirs
d1 := NewAllocDir(testlog.HCLogger(t), tmp1, "test")
Expand Down Expand Up @@ -302,11 +280,7 @@ func TestAllocDir_Move(t *testing.T) {
func TestAllocDir_EscapeChecking(t *testing.T) {
ci.Parallel(t)

tmp, err := ioutil.TempDir("", "AllocDir")
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}
defer os.RemoveAll(tmp)
tmp := t.TempDir()

d := NewAllocDir(testlog.HCLogger(t), tmp, "test")
if err := d.Build(); err != nil {
Expand Down Expand Up @@ -373,11 +347,7 @@ func TestAllocDir_ReadAt_SecretDir(t *testing.T) {
func TestAllocDir_SplitPath(t *testing.T) {
ci.Parallel(t)

dir, err := ioutil.TempDir("", "tmpdirtest")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

dest := filepath.Join(dir, "/foo/bar/baz")
if err := os.MkdirAll(dest, os.ModePerm); err != nil {
Expand All @@ -401,11 +371,7 @@ func TestAllocDir_CreateDir(t *testing.T) {
t.Skip("Must be root to run test")
}

dir, err := ioutil.TempDir("", "tmpdirtest")
if err != nil {
t.Fatalf("err: %v", err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

// create a subdir and a file
subdir := filepath.Join(dir, "subdir")
Expand All @@ -418,10 +384,7 @@ func TestAllocDir_CreateDir(t *testing.T) {
}

// Create the above hierarchy under another destination
dir1, err := ioutil.TempDir("/tmp", "tempdirdest")
if err != nil {
t.Fatalf("err: %v", err)
}
dir1 := t.TempDir()

if err := createDir(dir1, subdir); err != nil {
t.Fatalf("err: %v", err)
Expand All @@ -440,11 +403,7 @@ func TestAllocDir_CreateDir(t *testing.T) {
func TestPathFuncs(t *testing.T) {
ci.Parallel(t)

dir, err := ioutil.TempDir("", "nomadtest-pathfuncs")
if err != nil {
t.Fatalf("error creating temp dir: %v", err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

missingDir := filepath.Join(dir, "does-not-exist")

Expand Down
17 changes: 2 additions & 15 deletions client/allocdir/fs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -55,13 +54,7 @@ func TestLinuxRootSecretDir(t *testing.T) {
t.Skip("Must be run as root")
}

tmpdir, err := ioutil.TempDir("", "nomadtest-rootsecretdir")
if err != nil {
t.Fatalf("unable to create tempdir for test: %v", err)
}
defer os.RemoveAll(tmpdir)

secretsDir := filepath.Join(tmpdir, TaskSecrets)
secretsDir := filepath.Join(t.TempDir(), TaskSecrets)

// removing a nonexistent secrets dir should NOT error
if err := removeSecretDir(secretsDir); err != nil {
Expand Down Expand Up @@ -117,13 +110,7 @@ func TestLinuxUnprivilegedSecretDir(t *testing.T) {
t.Skip("Must not be run as root")
}

tmpdir, err := ioutil.TempDir("", "nomadtest-secretdir")
if err != nil {
t.Fatalf("unable to create tempdir for test: %s", err)
}
defer os.RemoveAll(tmpdir)

secretsDir := filepath.Join(tmpdir, TaskSecrets)
secretsDir := filepath.Join(t.TempDir(), TaskSecrets)

// removing a nonexistent secrets dir should NOT error
if err := removeSecretDir(secretsDir); err != nil {
Expand Down
32 changes: 6 additions & 26 deletions client/allocdir/task_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import (
func TestTaskDir_EmbedNonexistent(t *testing.T) {
ci.Parallel(t)

tmp, err := ioutil.TempDir("", "AllocDir")
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}
defer os.RemoveAll(tmp)
tmp := t.TempDir()

d := NewAllocDir(testlog.HCLogger(t), tmp, "test")
defer d.Destroy()
Expand All @@ -38,11 +34,7 @@ func TestTaskDir_EmbedNonexistent(t *testing.T) {
func TestTaskDir_EmbedDirs(t *testing.T) {
ci.Parallel(t)

tmp, err := ioutil.TempDir("", "AllocDir")
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}
defer os.RemoveAll(tmp)
tmp := t.TempDir()

d := NewAllocDir(testlog.HCLogger(t), tmp, "test")
defer d.Destroy()
Expand All @@ -53,11 +45,7 @@ func TestTaskDir_EmbedDirs(t *testing.T) {

// Create a fake host directory, with a file, and a subfolder that contains
// a file.
host, err := ioutil.TempDir("", "AllocDirHost")
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}
defer os.RemoveAll(host)
host := t.TempDir()

subDirName := "subdir"
subDir := filepath.Join(host, subDirName)
Expand Down Expand Up @@ -96,11 +84,7 @@ func TestTaskDir_NonRoot_Image(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("test should be run as non-root user")
}
tmp, err := ioutil.TempDir("", "AllocDir")
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}
defer os.RemoveAll(tmp)
tmp := t.TempDir()

d := NewAllocDir(testlog.HCLogger(t), tmp, "test")
defer d.Destroy()
Expand All @@ -121,11 +105,7 @@ func TestTaskDir_NonRoot(t *testing.T) {
t.Skip("test should be run as non-root user")
}

tmp, err := ioutil.TempDir("", "AllocDir")
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}
defer os.RemoveAll(tmp)
tmp := t.TempDir()

d := NewAllocDir(testlog.HCLogger(t), tmp, "test")
defer d.Destroy()
Expand All @@ -139,7 +119,7 @@ func TestTaskDir_NonRoot(t *testing.T) {
}

// ${TASK_DIR}/alloc should not exist!
if _, err = os.Stat(td.SharedTaskDir); !os.IsNotExist(err) {
if _, err := os.Stat(td.SharedTaskDir); !os.IsNotExist(err) {
t.Fatalf("Expected a NotExist error for shared alloc dir in task dir: %q", td.SharedTaskDir)
}
}
8 changes: 1 addition & 7 deletions client/allocrunner/consul_grpc_sock_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"sync"
"testing"
Expand Down Expand Up @@ -156,11 +154,7 @@ func TestConsulGRPCSocketHook_Prerun_Error(t *testing.T) {
func TestConsulGRPCSocketHook_proxy_Unix(t *testing.T) {
ci.Parallel(t)

dir, err := ioutil.TempDir("", "nomadtest_proxy_Unix")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()

// Setup fake listener that would be inside the netns (normally a unix
// socket, but it doesn't matter for this test).
Expand Down
24 changes: 6 additions & 18 deletions client/allocrunner/taskrunner/artifact_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ func TestTaskRunner_ArtifactHook_PartialDone(t *testing.T) {
defer ts.Close()

// Create the target directory.
destdir, err := ioutil.TempDir("", "nomadtest-dest")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(destdir))
}()
destdir := t.TempDir()

req := &interfaces.TaskPrestartRequest{
TaskEnv: taskenv.NewTaskEnv(nil, nil, nil, nil, destdir, ""),
Expand All @@ -112,7 +108,7 @@ func TestTaskRunner_ArtifactHook_PartialDone(t *testing.T) {

// On first run file1 (foo) should download but file2 (bar) should
// fail.
err = artifactHook.Prestart(context.Background(), req, &resp)
err := artifactHook.Prestart(context.Background(), req, &resp)

require.NotNil(t, err)
require.True(t, structs.IsRecoverable(err))
Expand Down Expand Up @@ -179,11 +175,7 @@ func TestTaskRunner_ArtifactHook_ConcurrentDownloadSuccess(t *testing.T) {
defer ts.Close()

// Create the target directory.
destdir, err := ioutil.TempDir("", "nomadtest-dest")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(destdir))
}()
destdir := t.TempDir()

req := &interfaces.TaskPrestartRequest{
TaskEnv: taskenv.NewTaskEnv(nil, nil, nil, nil, destdir, ""),
Expand Down Expand Up @@ -225,7 +217,7 @@ func TestTaskRunner_ArtifactHook_ConcurrentDownloadSuccess(t *testing.T) {
resp := interfaces.TaskPrestartResponse{}

// start the hook
err = artifactHook.Prestart(context.Background(), req, &resp)
err := artifactHook.Prestart(context.Background(), req, &resp)

require.NoError(t, err)
require.True(t, resp.Done)
Expand Down Expand Up @@ -273,11 +265,7 @@ func TestTaskRunner_ArtifactHook_ConcurrentDownloadFailure(t *testing.T) {
defer ts.Close()

// Create the target directory.
destdir, err := ioutil.TempDir("", "nomadtest-dest")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(destdir))
}()
destdir := t.TempDir()

req := &interfaces.TaskPrestartRequest{
TaskEnv: taskenv.NewTaskEnv(nil, nil, nil, nil, destdir, ""),
Expand Down Expand Up @@ -307,7 +295,7 @@ func TestTaskRunner_ArtifactHook_ConcurrentDownloadFailure(t *testing.T) {
resp := interfaces.TaskPrestartResponse{}

// On first run all files will be downloaded except file0.txt
err = artifactHook.Prestart(context.Background(), req, &resp)
err := artifactHook.Prestart(context.Background(), req, &resp)

require.Error(t, err)
require.True(t, structs.IsRecoverable(err))
Expand Down
Loading

0 comments on commit b47797d

Please sign in to comment.