Skip to content

Commit

Permalink
clean up test directories
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross committed Apr 4, 2022
1 parent 0a250b1 commit e7c0317
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions command/operator_debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,17 +384,18 @@ 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
require.NotContains(t, ui.ErrorWriter.String(), "file path escapes capture directory")
require.Contains(t, ui.OutputWriter.String(), "Starting debugger")

path := cmd.collectDir
defer os.Remove(path)

var pluginFiles []string
for _, pluginName := range cases {
Expand Down Expand Up @@ -475,14 +476,16 @@ 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
waitTime := 2 * duration

code := cmd.Run([]string{
"-address", url,
"-output", os.TempDir(),
"-output", testDir,
"-server-id", serverName,
"-node-id", clientID,
"-duration", duration.String(),
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand All @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit e7c0317

Please sign in to comment.