2020-06-25 16:51:23 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDebugUtils(t *testing.T) {
|
|
|
|
xs := argNodes("foo, bar")
|
|
|
|
require.Equal(t, []string{"foo", "bar"}, xs)
|
|
|
|
|
|
|
|
xs = argNodes("")
|
|
|
|
require.Len(t, xs, 0)
|
|
|
|
require.Empty(t, xs)
|
2020-08-11 17:14:28 +00:00
|
|
|
|
|
|
|
// address calculation honors CONSUL_HTTP_SSL
|
|
|
|
e := &external{addrVal: "http://127.0.0.1:8500", ssl: true}
|
|
|
|
require.Equal(t, "https://127.0.0.1:8500", e.addr("foo"))
|
|
|
|
|
|
|
|
e = &external{addrVal: "http://127.0.0.1:8500", ssl: false}
|
|
|
|
require.Equal(t, "http://127.0.0.1:8500", e.addr("foo"))
|
|
|
|
|
|
|
|
e = &external{addrVal: "127.0.0.1:8500", ssl: false}
|
|
|
|
require.Equal(t, "http://127.0.0.1:8500", e.addr("foo"))
|
|
|
|
|
|
|
|
e = &external{addrVal: "127.0.0.1:8500", ssl: true}
|
|
|
|
require.Equal(t, "https://127.0.0.1:8500", e.addr("foo"))
|
2020-06-25 16:51:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 02:30:01 +00:00
|
|
|
func TestDebugSuccesses(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
srv, _, _ := testServer(t, false, nil)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
cmd := &OperatorDebugCommand{Meta: Meta{Ui: ui}}
|
|
|
|
|
|
|
|
// NOTE -- duration must be shorter than default 2m to prevent testify from timing out
|
|
|
|
|
|
|
|
// Debug on the leader
|
|
|
|
code := cmd.Run([]string{"-duration", "250ms", "-server-id", "leader"})
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
require.Contains(t, ui.OutputWriter.String(), "Starting debugger")
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
|
|
|
|
// Debug on all servers
|
|
|
|
code = cmd.Run([]string{"-duration", "250ms", "-server-id", "all"})
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
require.Contains(t, ui.OutputWriter.String(), "Starting debugger")
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
}
|
|
|
|
|
2020-06-25 16:51:23 +00:00
|
|
|
func TestDebugFails(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
srv, _, _ := testServer(t, false, nil)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2020-08-11 19:39:44 +00:00
|
|
|
cmd := &OperatorDebugCommand{Meta: Meta{Ui: ui}}
|
2020-06-25 16:51:23 +00:00
|
|
|
|
|
|
|
// Fails incorrect args
|
|
|
|
code := cmd.Run([]string{"some", "bad", "args"})
|
|
|
|
require.Equal(t, 1, code)
|
|
|
|
|
|
|
|
// Fails illegal node ids
|
|
|
|
code = cmd.Run([]string{"-node-id", "foo:bar"})
|
|
|
|
require.Equal(t, 1, code)
|
|
|
|
|
|
|
|
// Fails missing node ids
|
|
|
|
code = cmd.Run([]string{"-node-id", "abc,def"})
|
|
|
|
require.Equal(t, 1, code)
|
|
|
|
|
|
|
|
// Fails bad durations
|
|
|
|
code = cmd.Run([]string{"-duration", "foo"})
|
|
|
|
require.Equal(t, 1, code)
|
|
|
|
|
|
|
|
// Fails bad durations
|
|
|
|
code = cmd.Run([]string{"-interval", "bar"})
|
|
|
|
require.Equal(t, 1, code)
|
|
|
|
|
|
|
|
// Fails existing output
|
|
|
|
format := "2006-01-02-150405Z"
|
|
|
|
stamped := "nomad-debug-" + time.Now().UTC().Format(format)
|
|
|
|
path := filepath.Join(os.TempDir(), stamped)
|
|
|
|
os.MkdirAll(path, 0755)
|
|
|
|
defer os.Remove(path)
|
|
|
|
code = cmd.Run([]string{"-output", os.TempDir()})
|
|
|
|
require.Equal(t, 2, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDebugCapturedFiles(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
srv, _, url := testServer(t, false, nil)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2020-08-11 19:39:44 +00:00
|
|
|
cmd := &OperatorDebugCommand{Meta: Meta{Ui: ui}}
|
2020-06-25 16:51:23 +00:00
|
|
|
|
|
|
|
code := cmd.Run([]string{
|
|
|
|
"-address", url,
|
|
|
|
"-output", os.TempDir(),
|
|
|
|
"-server-id", "leader",
|
2020-08-11 17:14:28 +00:00
|
|
|
"-duration", "1300ms",
|
|
|
|
"-interval", "600ms",
|
2020-06-25 16:51:23 +00:00
|
|
|
})
|
|
|
|
|
2020-08-11 17:14:28 +00:00
|
|
|
path := cmd.collectDir
|
|
|
|
defer os.Remove(path)
|
|
|
|
|
2020-06-25 16:51:23 +00:00
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// Version is always captured
|
|
|
|
require.FileExists(t, filepath.Join(path, "version", "agent-self.json"))
|
|
|
|
|
2020-08-11 17:14:28 +00:00
|
|
|
// Consul and Vault contain results or errors
|
2020-06-25 16:51:23 +00:00
|
|
|
_, err := os.Stat(filepath.Join(path, "version", "consul-agent-self.json"))
|
2020-08-11 17:14:28 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-25 16:51:23 +00:00
|
|
|
_, err = os.Stat(filepath.Join(path, "version", "vault-sys-health.json"))
|
2020-08-11 17:14:28 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-25 16:51:23 +00:00
|
|
|
|
|
|
|
// Monitor files are only created when selected
|
|
|
|
require.FileExists(t, filepath.Join(path, "server", "leader", "monitor.log"))
|
|
|
|
require.FileExists(t, filepath.Join(path, "server", "leader", "profile.prof"))
|
|
|
|
require.FileExists(t, filepath.Join(path, "server", "leader", "trace.prof"))
|
|
|
|
require.FileExists(t, filepath.Join(path, "server", "leader", "goroutine.prof"))
|
|
|
|
|
|
|
|
// Multiple snapshots are collected, 00 is always created
|
2020-08-11 17:14:28 +00:00
|
|
|
require.FileExists(t, filepath.Join(path, "nomad", "0000", "jobs.json"))
|
|
|
|
require.FileExists(t, filepath.Join(path, "nomad", "0000", "nodes.json"))
|
2020-10-06 02:30:01 +00:00
|
|
|
require.FileExists(t, filepath.Join(path, "nomad", "0000", "metrics.json"))
|
2020-06-25 16:51:23 +00:00
|
|
|
|
|
|
|
// Multiple snapshots are collected, 01 requires two intervals
|
2020-08-11 17:14:28 +00:00
|
|
|
require.FileExists(t, filepath.Join(path, "nomad", "0001", "jobs.json"))
|
|
|
|
require.FileExists(t, filepath.Join(path, "nomad", "0001", "nodes.json"))
|
2020-10-06 02:30:01 +00:00
|
|
|
require.FileExists(t, filepath.Join(path, "nomad", "0001", "metrics.json"))
|
2020-06-25 16:51:23 +00:00
|
|
|
}
|