mirror of
https://github.com/kemko/nomad.git
synced 2026-01-07 02:45:42 +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:
@@ -45,11 +45,7 @@ func newHarness(t *testing.T, plugins []string) *harness {
|
||||
}
|
||||
|
||||
// Build a temp directory
|
||||
path, err := ioutil.TempDir("", t.Name())
|
||||
if err != nil {
|
||||
t.Fatalf("failed to build tmp directory")
|
||||
}
|
||||
h.tmpDir = path
|
||||
h.tmpDir = t.TempDir()
|
||||
|
||||
// Get our own executable path
|
||||
selfExe, err := os.Executable()
|
||||
@@ -100,13 +96,6 @@ func (h *harness) pluginDir() string {
|
||||
return h.tmpDir
|
||||
}
|
||||
|
||||
// cleanup removes the temp directory
|
||||
func (h *harness) cleanup() {
|
||||
if err := os.RemoveAll(h.tmpDir); err != nil {
|
||||
h.t.Fatalf("failed to remove tmp directory %q: %v", h.tmpDir, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginLoader_External(t *testing.T) {
|
||||
ci.Parallel(t)
|
||||
require := require.New(t)
|
||||
@@ -115,7 +104,6 @@ func TestPluginLoader_External(t *testing.T) {
|
||||
plugins := []string{"mock-device", "mock-device-2"}
|
||||
pluginVersions := []string{"v0.0.1", "v0.0.2"}
|
||||
h := newHarness(t, plugins)
|
||||
defer h.cleanup()
|
||||
|
||||
logger := testlog.HCLogger(t)
|
||||
logger.SetLevel(log.Trace)
|
||||
@@ -175,7 +163,6 @@ func TestPluginLoader_External_ApiVersions(t *testing.T) {
|
||||
plugins := []string{"mock-device", "mock-device-2", "mock-device-3"}
|
||||
pluginVersions := []string{"v0.0.1", "v0.0.2"}
|
||||
h := newHarness(t, plugins)
|
||||
defer h.cleanup()
|
||||
|
||||
logger := testlog.HCLogger(t)
|
||||
logger.SetLevel(log.Trace)
|
||||
@@ -279,7 +266,6 @@ func TestPluginLoader_External_NoApiVersion(t *testing.T) {
|
||||
plugins := []string{"mock-device"}
|
||||
pluginVersions := []string{"v0.0.1", "v0.0.2"}
|
||||
h := newHarness(t, plugins)
|
||||
defer h.cleanup()
|
||||
|
||||
logger := testlog.HCLogger(t)
|
||||
logger.SetLevel(log.Trace)
|
||||
@@ -309,7 +295,6 @@ func TestPluginLoader_External_Config(t *testing.T) {
|
||||
plugins := []string{"mock-device", "mock-device-2"}
|
||||
pluginVersions := []string{"v0.0.1", "v0.0.2"}
|
||||
h := newHarness(t, plugins)
|
||||
defer h.cleanup()
|
||||
|
||||
logger := testlog.HCLogger(t)
|
||||
logger.SetLevel(log.Trace)
|
||||
@@ -376,7 +361,6 @@ func TestPluginLoader_External_Config_Bad(t *testing.T) {
|
||||
plugins := []string{"mock-device"}
|
||||
pluginVersions := []string{"v0.0.1"}
|
||||
h := newHarness(t, plugins)
|
||||
defer h.cleanup()
|
||||
|
||||
logger := testlog.HCLogger(t)
|
||||
logger.SetLevel(log.Trace)
|
||||
@@ -411,7 +395,6 @@ func TestPluginLoader_External_VersionOverlap(t *testing.T) {
|
||||
plugins := []string{"mock-device", "mock-device-2"}
|
||||
pluginVersions := []string{"v0.0.1", "v0.0.2"}
|
||||
h := newHarness(t, plugins)
|
||||
defer h.cleanup()
|
||||
|
||||
logger := testlog.HCLogger(t)
|
||||
logger.SetLevel(log.Trace)
|
||||
@@ -461,7 +444,6 @@ func TestPluginLoader_Internal(t *testing.T) {
|
||||
|
||||
// Create the harness
|
||||
h := newHarness(t, nil)
|
||||
defer h.cleanup()
|
||||
|
||||
plugins := []string{"mock-device", "mock-device-2"}
|
||||
pluginVersions := []string{"v0.0.1", "v0.0.2"}
|
||||
@@ -525,7 +507,6 @@ func TestPluginLoader_Internal_ApiVersions(t *testing.T) {
|
||||
plugins := []string{"mock-device", "mock-device-2", "mock-device-3"}
|
||||
pluginVersions := []string{"v0.0.1", "v0.0.2"}
|
||||
h := newHarness(t, nil)
|
||||
defer h.cleanup()
|
||||
|
||||
logger := testlog.HCLogger(t)
|
||||
logger.SetLevel(log.Trace)
|
||||
@@ -607,7 +588,6 @@ func TestPluginLoader_Internal_NoApiVersion(t *testing.T) {
|
||||
plugins := []string{"mock-device"}
|
||||
pluginVersions := []string{"v0.0.1", "v0.0.2"}
|
||||
h := newHarness(t, nil)
|
||||
defer h.cleanup()
|
||||
|
||||
logger := testlog.HCLogger(t)
|
||||
logger.SetLevel(log.Trace)
|
||||
@@ -636,7 +616,6 @@ func TestPluginLoader_Internal_Config(t *testing.T) {
|
||||
|
||||
// Create the harness
|
||||
h := newHarness(t, nil)
|
||||
defer h.cleanup()
|
||||
|
||||
plugins := []string{"mock-device", "mock-device-2"}
|
||||
pluginVersions := []string{"v0.0.1", "v0.0.2"}
|
||||
@@ -707,7 +686,6 @@ func TestPluginLoader_Internal_ExternalConfig(t *testing.T) {
|
||||
|
||||
// Create the harness
|
||||
h := newHarness(t, nil)
|
||||
defer h.cleanup()
|
||||
|
||||
plugin := "mock-device"
|
||||
pluginVersion := "v0.0.1"
|
||||
@@ -778,7 +756,6 @@ func TestPluginLoader_Internal_Config_Bad(t *testing.T) {
|
||||
|
||||
// Create the harness
|
||||
h := newHarness(t, nil)
|
||||
defer h.cleanup()
|
||||
|
||||
plugins := []string{"mock-device"}
|
||||
pluginVersions := []string{"v0.0.1"}
|
||||
@@ -820,7 +797,6 @@ func TestPluginLoader_InternalOverrideExternal(t *testing.T) {
|
||||
pluginApiVersions := []string{device.ApiVersion010}
|
||||
|
||||
h := newHarness(t, plugins)
|
||||
defer h.cleanup()
|
||||
|
||||
logger := testlog.HCLogger(t)
|
||||
logger.SetLevel(log.Trace)
|
||||
@@ -877,7 +853,6 @@ func TestPluginLoader_ExternalOverrideInternal(t *testing.T) {
|
||||
pluginApiVersions := []string{device.ApiVersion010}
|
||||
|
||||
h := newHarness(t, plugins)
|
||||
defer h.cleanup()
|
||||
|
||||
logger := testlog.HCLogger(t)
|
||||
logger.SetLevel(log.Trace)
|
||||
@@ -932,7 +907,6 @@ func TestPluginLoader_Dispense_External(t *testing.T) {
|
||||
plugin := "mock-device"
|
||||
pluginVersion := "v0.0.1"
|
||||
h := newHarness(t, []string{plugin})
|
||||
defer h.cleanup()
|
||||
|
||||
expKey := "set_config_worked"
|
||||
|
||||
@@ -980,7 +954,6 @@ func TestPluginLoader_Dispense_Internal(t *testing.T) {
|
||||
pluginVersion := "v0.0.1"
|
||||
pluginApiVersions := []string{device.ApiVersion010}
|
||||
h := newHarness(t, nil)
|
||||
defer h.cleanup()
|
||||
|
||||
expKey := "set_config_worked"
|
||||
expNomadConfig := &base.AgentConfig{
|
||||
@@ -1038,7 +1011,6 @@ func TestPluginLoader_Dispense_NoConfigSchema_External(t *testing.T) {
|
||||
plugin := "mock-device"
|
||||
pluginVersion := "v0.0.1"
|
||||
h := newHarness(t, []string{plugin})
|
||||
defer h.cleanup()
|
||||
|
||||
expKey := "set_config_worked"
|
||||
|
||||
@@ -1087,7 +1059,6 @@ func TestPluginLoader_Dispense_NoConfigSchema_Internal(t *testing.T) {
|
||||
pluginVersion := "v0.0.1"
|
||||
pluginApiVersions := []string{device.ApiVersion010}
|
||||
h := newHarness(t, nil)
|
||||
defer h.cleanup()
|
||||
|
||||
expKey := "set_config_worked"
|
||||
|
||||
@@ -1137,7 +1108,6 @@ func TestPluginLoader_Reattach_External(t *testing.T) {
|
||||
plugin := "mock-device"
|
||||
pluginVersion := "v0.0.1"
|
||||
h := newHarness(t, []string{plugin})
|
||||
defer h.cleanup()
|
||||
|
||||
expKey := "set_config_worked"
|
||||
|
||||
@@ -1200,7 +1170,6 @@ func TestPluginLoader_Bad_Executable(t *testing.T) {
|
||||
// Create a plugin
|
||||
plugin := "mock-device"
|
||||
h := newHarness(t, []string{plugin})
|
||||
defer h.cleanup()
|
||||
|
||||
logger := testlog.HCLogger(t)
|
||||
logger.SetLevel(log.Trace)
|
||||
@@ -1233,7 +1202,6 @@ func TestPluginLoader_External_SkipBadFiles(t *testing.T) {
|
||||
plugins := []string{"mock-device"}
|
||||
pluginVersions := []string{"v0.0.1"}
|
||||
h := newHarness(t, nil)
|
||||
defer h.cleanup()
|
||||
|
||||
// Create a folder inside our plugin dir
|
||||
require.NoError(os.Mkdir(filepath.Join(h.pluginDir(), "folder"), 0666))
|
||||
|
||||
Reference in New Issue
Block a user