From e7c0317533e7c969bfaae0e2ff8f6ddb23eec904 Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Mon, 4 Apr 2022 13:33:18 -0400 Subject: [PATCH] clean up test directories --- command/operator_debug_test.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/command/operator_debug_test.go b/command/operator_debug_test.go index 439026c70128..31cf64dd2a3a 100644 --- a/command/operator_debug_test.go +++ b/command/operator_debug_test.go @@ -384,9 +384,11 @@ func TestDebug_Bad_CSIPlugin_Names(t *testing.T) { // Setup mock UI ui := cli.NewMockUi() cmd := &OperatorDebugCommand{Meta: Meta{Ui: ui}} + testDir := t.TempDir() + defer os.Remove(testDir) // Debug on the leader and all client nodes - code := cmd.Run([]string{"-address", url, "-duration", "250ms", "-interval", "250ms", "-server-id", "leader", "-node-id", "all", "-output", os.TempDir()}) + code := cmd.Run([]string{"-address", url, "-duration", "250ms", "-interval", "250ms", "-server-id", "leader", "-node-id", "all", "-output", testDir}) assert.Equal(t, 0, code) // Bad plugin name should be escaped before it reaches the sandbox test @@ -394,7 +396,6 @@ func TestDebug_Bad_CSIPlugin_Names(t *testing.T) { require.Contains(t, ui.OutputWriter.String(), "Starting debugger") path := cmd.collectDir - defer os.Remove(path) var pluginFiles []string for _, pluginName := range cases { @@ -475,6 +476,8 @@ func TestDebug_CapturedFiles(t *testing.T) { ui := cli.NewMockUi() cmd := &OperatorDebugCommand{Meta: Meta{Ui: ui}} + testDir := t.TempDir() + defer os.Remove(testDir) duration := 2 * time.Second interval := 750 * time.Millisecond @@ -482,7 +485,7 @@ func TestDebug_CapturedFiles(t *testing.T) { code := cmd.Run([]string{ "-address", url, - "-output", os.TempDir(), + "-output", testDir, "-server-id", serverName, "-node-id", clientID, "-duration", duration.String(), @@ -491,10 +494,6 @@ func TestDebug_CapturedFiles(t *testing.T) { "-pprof-interval", "250ms", }) - // Get capture directory - path := cmd.collectDir - defer os.Remove(path) - // There should be no errors require.Empty(t, ui.ErrorWriter.String()) require.Equal(t, 0, code) @@ -535,11 +534,13 @@ func TestDebug_ExistingOutput(t *testing.T) { // Fails existing output format := "2006-01-02-150405Z" stamped := "nomad-debug-" + time.Now().UTC().Format(format) - path := filepath.Join(os.TempDir(), stamped) + path := filepath.Join(t.TempDir(), stamped) os.MkdirAll(path, 0755) defer os.Remove(path) - code := cmd.Run([]string{"-output", os.TempDir(), "-duration", "50ms", "-interval", "50ms"}) + otherTestDir := t.TempDir() + defer os.Remove(otherTestDir) + code := cmd.Run([]string{"-output", otherTestDir, "-duration", "50ms", "-interval", "50ms"}) require.Equal(t, 2, code) } @@ -642,12 +643,12 @@ func TestDebug_WriteBytes_Nil(t *testing.T) { ui := cli.NewMockUi() cmd := &OperatorDebugCommand{Meta: Meta{Ui: ui}} - testDir = os.TempDir() + testDir = t.TempDir() + defer os.Remove(testDir) cmd.collectDir = testDir testFile = "test_nil.json" testPath = filepath.Join(testDir, testFile) - defer os.Remove(testPath) // Write nil file at top level of collect directory err := cmd.writeBytes("", testFile, testBytes) @@ -661,7 +662,7 @@ func TestDebug_WriteBytes_PathEscapesSandbox(t *testing.T) { var testDir, testFile string var testBytes []byte - testDir = os.TempDir() + testDir = t.TempDir() defer os.Remove(testDir) testFile = "testing.json" @@ -719,7 +720,7 @@ func TestDebug_CollectConsul(t *testing.T) { c.consul = ce // Setup capture directory - testDir := os.TempDir() + testDir := t.TempDir() defer os.Remove(testDir) c.collectDir = testDir @@ -762,7 +763,7 @@ func TestDebug_CollectVault(t *testing.T) { c.vault = ve // Set capture directory - testDir := os.TempDir() + testDir := t.TempDir() defer os.Remove(testDir) c.collectDir = testDir