mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 18:35:44 +03:00
test: use T.TempDir to create temporary test directory (#12853)
* test: use `T.TempDir` to create temporary test directory
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.
Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
is also tedious, but `t.TempDir` handles this for us nicely.
Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix TestLogmon_Start_restart on Windows
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestConsul_Integration
t.TempDir fails to perform the cleanup properly because the folder is
still in use
testing.go:967: TempDir RemoveAll cleanup: unlinkat /tmp/TestConsul_Integration2837567823/002/191a6f1a-5371-cf7c-da38-220fe85d10e5/web/secrets: device or resource busy
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
@@ -417,9 +417,7 @@ func TestUniversalExecutor_LookupTaskBin(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
// Create a temp dir
|
||||
tmpDir, err := ioutil.TempDir("", "")
|
||||
require.Nil(err)
|
||||
defer os.Remove(tmpDir)
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Create the command
|
||||
cmd := &ExecCommand{Env: []string{"PATH=/bin"}, TaskDir: tmpDir}
|
||||
@@ -429,7 +427,7 @@ func TestUniversalExecutor_LookupTaskBin(t *testing.T) {
|
||||
|
||||
// Write a file under foo
|
||||
filePath := filepath.Join(tmpDir, "foo", "tmp.txt")
|
||||
err = ioutil.WriteFile(filePath, []byte{1, 2}, os.ModeAppend)
|
||||
err := ioutil.WriteFile(filePath, []byte{1, 2}, os.ModeAppend)
|
||||
require.NoError(err)
|
||||
|
||||
// Lookout with an absolute path to the binary
|
||||
|
||||
@@ -415,16 +415,14 @@ func TestUniversalExecutor_LookupPath(t *testing.T) {
|
||||
ci.Parallel(t)
|
||||
require := require.New(t)
|
||||
// Create a temp dir
|
||||
tmpDir, err := ioutil.TempDir("", "")
|
||||
require.Nil(err)
|
||||
defer os.Remove(tmpDir)
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Make a foo subdir
|
||||
os.MkdirAll(filepath.Join(tmpDir, "foo"), 0700)
|
||||
|
||||
// Write a file under foo
|
||||
filePath := filepath.Join(tmpDir, "foo", "tmp.txt")
|
||||
err = ioutil.WriteFile(filePath, []byte{1, 2}, os.ModeAppend)
|
||||
err := ioutil.WriteFile(filePath, []byte{1, 2}, os.ModeAppend)
|
||||
require.Nil(err)
|
||||
|
||||
// Lookup with full path on host to binary
|
||||
@@ -606,9 +604,7 @@ func TestExecutor_Start_NonExecutableBinaries(t *testing.T) {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
tmpDir, err := ioutil.TempDir("", "nomad-executor-tests")
|
||||
require.NoError(err)
|
||||
defer os.RemoveAll(tmpDir)
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
nonExecutablePath := filepath.Join(tmpDir, "nonexecutablefile")
|
||||
ioutil.WriteFile(nonExecutablePath,
|
||||
|
||||
@@ -4,9 +4,7 @@
|
||||
package hostnames
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@@ -92,10 +90,7 @@ func TestGenerateEtcHostsMount(t *testing.T) {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
taskDir, err := ioutil.TempDir("",
|
||||
fmt.Sprintf("generateEtcHosts_Test-%s", tc.name))
|
||||
defer os.RemoveAll(taskDir)
|
||||
require.NoError(err)
|
||||
taskDir := t.TempDir()
|
||||
dest := filepath.Join(taskDir, "hosts")
|
||||
|
||||
got, err := GenerateEtcHostsMount(taskDir, tc.spec, tc.extraHosts)
|
||||
|
||||
@@ -5,7 +5,6 @@ package resolvconf
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@@ -18,10 +17,7 @@ func Test_copySystemDNS(t *testing.T) {
|
||||
data, err := ioutil.ReadFile(dresolvconf.Path())
|
||||
require.NoError(err)
|
||||
|
||||
tmp, err := ioutil.TempDir("", "copySystemDNS_Test")
|
||||
require.NoError(err)
|
||||
defer os.RemoveAll(tmp)
|
||||
dest := filepath.Join(tmp, "resolv.conf")
|
||||
dest := filepath.Join(t.TempDir(), "resolv.conf")
|
||||
|
||||
require.NoError(copySystemDNS(dest))
|
||||
require.FileExists(dest)
|
||||
|
||||
Reference in New Issue
Block a user