2018-10-17 20:20:35 +00:00
|
|
|
package debug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
2021-06-07 17:00:51 +00:00
|
|
|
"bufio"
|
2021-08-06 21:17:25 +00:00
|
|
|
"bytes"
|
2019-01-03 15:39:58 +00:00
|
|
|
"compress/gzip"
|
2021-08-06 21:17:25 +00:00
|
|
|
"encoding/json"
|
2018-10-17 20:20:35 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-06-07 17:00:51 +00:00
|
|
|
"regexp"
|
2018-10-17 20:20:35 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2021-06-07 17:00:51 +00:00
|
|
|
"time"
|
2018-10-17 20:20:35 +00:00
|
|
|
|
2021-08-06 17:46:01 +00:00
|
|
|
"github.com/google/pprof/profile"
|
2021-05-27 13:41:53 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-08-06 17:46:01 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2021-08-06 21:17:25 +00:00
|
|
|
"gotest.tools/v3/assert/cmp"
|
2021-08-06 17:46:01 +00:00
|
|
|
"gotest.tools/v3/fs"
|
2021-06-14 22:37:05 +00:00
|
|
|
|
2018-10-17 20:20:35 +00:00
|
|
|
"github.com/hashicorp/consul/agent"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2019-04-25 16:26:33 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2018-10-17 20:20:35 +00:00
|
|
|
)
|
|
|
|
|
2021-08-06 17:46:01 +00:00
|
|
|
func TestDebugCommand_Help_TextContainsNoTabs(t *testing.T) {
|
2021-08-06 18:24:12 +00:00
|
|
|
if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
|
2018-10-17 20:20:35 +00:00
|
|
|
t.Fatal("help has tabs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDebugCommand(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:20:35 +00:00
|
|
|
testDir := testutil.TempDir(t, "debug")
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, `
|
2018-10-17 20:20:35 +00:00
|
|
|
enable_debug = true
|
|
|
|
`)
|
|
|
|
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
2021-08-06 18:24:12 +00:00
|
|
|
cmd := New(ui)
|
2018-10-17 20:20:35 +00:00
|
|
|
cmd.validateTiming = false
|
|
|
|
|
2021-08-06 21:17:25 +00:00
|
|
|
it := &incrementalTime{
|
|
|
|
base: time.Date(2021, 7, 8, 9, 10, 11, 0, time.UTC),
|
|
|
|
}
|
|
|
|
cmd.timeNow = it.Now
|
|
|
|
|
2018-10-17 20:20:35 +00:00
|
|
|
outputPath := fmt.Sprintf("%s/debug", testDir)
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-output=" + outputPath,
|
2021-08-06 21:17:25 +00:00
|
|
|
"-duration=2s",
|
|
|
|
"-interval=1s",
|
2021-06-14 22:37:05 +00:00
|
|
|
"-archive=false",
|
2018-10-17 20:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
2021-06-14 22:37:05 +00:00
|
|
|
require.Equal(t, 0, code)
|
|
|
|
require.Equal(t, "", ui.ErrorWriter.String())
|
2018-10-17 20:20:35 +00:00
|
|
|
|
2021-08-06 17:46:01 +00:00
|
|
|
expected := fs.Expected(t,
|
|
|
|
fs.WithDir("debug",
|
2021-08-06 21:17:25 +00:00
|
|
|
fs.WithFile("index.json", "", fs.MatchFileContent(validIndexJSON)),
|
|
|
|
fs.WithFile("agent.json", "", fs.MatchFileContent(validJSON)),
|
|
|
|
fs.WithFile("host.json", "", fs.MatchFileContent(validJSON)),
|
|
|
|
fs.WithFile("members.json", "", fs.MatchFileContent(validJSON)),
|
|
|
|
fs.WithFile("metrics.json", "", fs.MatchAnyFileContent),
|
|
|
|
fs.WithFile("consul.log", "", fs.MatchFileContent(validLogFile)),
|
|
|
|
fs.WithFile("profile.prof", "", fs.MatchFileContent(validProfileData)),
|
|
|
|
fs.WithFile("trace.out", "", fs.MatchAnyFileContent),
|
|
|
|
fs.WithDir("2021-07-08T09-10-12Z",
|
|
|
|
fs.WithFile("goroutine.prof", "", fs.MatchFileContent(validProfileData)),
|
|
|
|
fs.WithFile("heap.prof", "", fs.MatchFileContent(validProfileData))),
|
|
|
|
fs.WithDir("2021-07-08T09-10-13Z",
|
|
|
|
fs.WithFile("goroutine.prof", "", fs.MatchFileContent(validProfileData)),
|
|
|
|
fs.WithFile("heap.prof", "", fs.MatchFileContent(validProfileData))),
|
|
|
|
// Ignore the extra directories, they should be the same as the first two
|
2021-08-06 17:46:01 +00:00
|
|
|
fs.MatchExtraFiles))
|
|
|
|
assert.Assert(t, fs.Equal(testDir, expected))
|
|
|
|
|
2021-08-06 21:17:25 +00:00
|
|
|
require.Equal(t, "", ui.ErrorWriter.String(), "expected no error output")
|
|
|
|
}
|
|
|
|
|
|
|
|
func validLogFile(raw []byte) fs.CompareResult {
|
|
|
|
scanner := bufio.NewScanner(bytes.NewReader(raw))
|
|
|
|
for scanner.Scan() {
|
|
|
|
logLine := scanner.Text()
|
|
|
|
if !validateLogLine([]byte(logLine)) {
|
|
|
|
return cmp.ResultFailure(fmt.Sprintf("log line is not valid %s", logLine))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if scanner.Err() != nil {
|
|
|
|
return cmp.ResultFailure(scanner.Err().Error())
|
|
|
|
}
|
|
|
|
return cmp.ResultSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
func validIndexJSON(raw []byte) fs.CompareResult {
|
|
|
|
var target debugIndex
|
|
|
|
decoder := json.NewDecoder(bytes.NewReader(raw))
|
|
|
|
decoder.DisallowUnknownFields()
|
|
|
|
if err := decoder.Decode(&target); err != nil {
|
|
|
|
return cmp.ResultFailure(err.Error())
|
|
|
|
}
|
|
|
|
return cmp.ResultSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
func validJSON(raw []byte) fs.CompareResult {
|
|
|
|
var target interface{}
|
|
|
|
decoder := json.NewDecoder(bytes.NewReader(raw))
|
|
|
|
if err := decoder.Decode(&target); err != nil {
|
|
|
|
return cmp.ResultFailure(err.Error())
|
|
|
|
}
|
|
|
|
return cmp.ResultSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
func validProfileData(raw []byte) fs.CompareResult {
|
|
|
|
if _, err := profile.ParseData(raw); err != nil {
|
|
|
|
return cmp.ResultFailure(err.Error())
|
|
|
|
}
|
|
|
|
return cmp.ResultSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
type incrementalTime struct {
|
|
|
|
base time.Time
|
|
|
|
next uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *incrementalTime) Now() time.Time {
|
|
|
|
t.next++
|
|
|
|
return t.base.Add(time.Duration(t.next) * time.Second)
|
2018-10-17 20:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDebugCommand_Archive(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:20:35 +00:00
|
|
|
testDir := testutil.TempDir(t, "debug")
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, `
|
2018-10-17 20:20:35 +00:00
|
|
|
enable_debug = true
|
|
|
|
`)
|
2021-06-14 22:37:05 +00:00
|
|
|
|
2018-10-17 20:20:35 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
2021-08-06 18:24:12 +00:00
|
|
|
cmd := New(ui)
|
2018-10-17 20:20:35 +00:00
|
|
|
cmd.validateTiming = false
|
|
|
|
|
|
|
|
outputPath := fmt.Sprintf("%s/debug", testDir)
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-output=" + outputPath,
|
|
|
|
"-capture=agent",
|
|
|
|
}
|
|
|
|
|
2021-06-14 22:37:05 +00:00
|
|
|
code := cmd.Run(args)
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
require.Equal(t, "", ui.ErrorWriter.String())
|
2018-10-17 20:20:35 +00:00
|
|
|
|
2021-06-14 22:37:05 +00:00
|
|
|
archivePath := outputPath + debugArchiveExtension
|
2018-10-17 20:20:35 +00:00
|
|
|
file, err := os.Open(archivePath)
|
2021-06-14 22:37:05 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-01-03 15:39:58 +00:00
|
|
|
gz, err := gzip.NewReader(file)
|
2021-06-14 22:37:05 +00:00
|
|
|
require.NoError(t, err)
|
2019-01-03 15:39:58 +00:00
|
|
|
tr := tar.NewReader(gz)
|
2018-10-17 20:20:35 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
h, err := tr.Next()
|
2021-06-14 22:37:05 +00:00
|
|
|
switch {
|
|
|
|
case err == io.EOF:
|
|
|
|
return
|
|
|
|
case err != nil:
|
2018-10-17 20:20:35 +00:00
|
|
|
t.Fatalf("failed to read file in archive: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore the outer directory
|
|
|
|
if h.Name == "debug" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// should only contain this one capture target
|
|
|
|
if h.Name != "debug/agent.json" && h.Name != "debug/index.json" {
|
|
|
|
t.Fatalf("archive contents do not match: %s", h.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDebugCommand_ArgsBad(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
2021-08-06 18:24:12 +00:00
|
|
|
cmd := New(ui)
|
2018-10-17 20:20:35 +00:00
|
|
|
|
2021-08-06 17:46:01 +00:00
|
|
|
args := []string{"foo", "bad"}
|
2018-10-17 20:20:35 +00:00
|
|
|
|
|
|
|
if code := cmd.Run(args); code == 0 {
|
|
|
|
t.Fatalf("should exit non-zero, got code: %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
errOutput := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(errOutput, "Too many arguments") {
|
|
|
|
t.Errorf("expected error output, got %q", errOutput)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 17:00:51 +00:00
|
|
|
func TestDebugCommand_InvalidFlags(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
2021-08-06 18:24:12 +00:00
|
|
|
cmd := New(ui)
|
2021-06-07 17:00:51 +00:00
|
|
|
cmd.validateTiming = false
|
|
|
|
|
|
|
|
outputPath := ""
|
|
|
|
args := []string{
|
|
|
|
"-invalid=value",
|
|
|
|
"-output=" + outputPath,
|
|
|
|
"-duration=100ms",
|
|
|
|
"-interval=50ms",
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code == 0 {
|
|
|
|
t.Fatalf("should exit non-zero, got code: %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
errOutput := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(errOutput, "==> Error parsing flags: flag provided but not defined:") {
|
|
|
|
t.Errorf("expected error output, got %q", errOutput)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:20:35 +00:00
|
|
|
func TestDebugCommand_OutputPathBad(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:20:35 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, "")
|
2018-10-17 20:20:35 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
2021-08-06 18:24:12 +00:00
|
|
|
cmd := New(ui)
|
2018-10-17 20:20:35 +00:00
|
|
|
cmd.validateTiming = false
|
|
|
|
|
|
|
|
outputPath := ""
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-output=" + outputPath,
|
|
|
|
"-duration=100ms",
|
|
|
|
"-interval=50ms",
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code == 0 {
|
|
|
|
t.Fatalf("should exit non-zero, got code: %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
errOutput := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(errOutput, "no such file or directory") {
|
|
|
|
t.Errorf("expected error output, got %q", errOutput)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDebugCommand_OutputPathExists(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:20:35 +00:00
|
|
|
testDir := testutil.TempDir(t, "debug")
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, "")
|
2018-10-17 20:20:35 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
2021-08-06 18:24:12 +00:00
|
|
|
cmd := New(ui)
|
2018-10-17 20:20:35 +00:00
|
|
|
cmd.validateTiming = false
|
|
|
|
|
|
|
|
outputPath := fmt.Sprintf("%s/debug", testDir)
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-output=" + outputPath,
|
|
|
|
"-duration=100ms",
|
|
|
|
"-interval=50ms",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a directory that conflicts with the output path
|
|
|
|
err := os.Mkdir(outputPath, 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("duplicate test directory creation failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code == 0 {
|
|
|
|
t.Fatalf("should exit non-zero, got code: %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
errOutput := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(errOutput, "directory already exists") {
|
|
|
|
t.Errorf("expected error output, got %q", errOutput)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDebugCommand_CaptureTargets(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:20:35 +00:00
|
|
|
cases := map[string]struct {
|
|
|
|
// used in -target param
|
|
|
|
targets []string
|
|
|
|
// existence verified after execution
|
|
|
|
files []string
|
|
|
|
// non-existence verified after execution
|
|
|
|
excludedFiles []string
|
|
|
|
}{
|
|
|
|
"single": {
|
|
|
|
[]string{"agent"},
|
|
|
|
[]string{"agent.json"},
|
2021-08-06 17:30:32 +00:00
|
|
|
[]string{"host.json", "members.json"},
|
2018-10-17 20:20:35 +00:00
|
|
|
},
|
|
|
|
"static": {
|
|
|
|
[]string{"agent", "host", "cluster"},
|
2021-08-06 17:30:32 +00:00
|
|
|
[]string{"agent.json", "host.json", "members.json"},
|
2021-08-06 21:17:25 +00:00
|
|
|
[]string{"metrics.json"},
|
2018-10-17 20:20:35 +00:00
|
|
|
},
|
|
|
|
"metrics-only": {
|
|
|
|
[]string{"metrics"},
|
2021-08-06 21:17:25 +00:00
|
|
|
[]string{"metrics.json"},
|
2021-08-06 17:30:32 +00:00
|
|
|
[]string{"agent.json", "host.json", "members.json"},
|
2018-10-17 20:20:35 +00:00
|
|
|
},
|
|
|
|
"all-but-pprof": {
|
|
|
|
[]string{
|
|
|
|
"metrics",
|
|
|
|
"logs",
|
|
|
|
"host",
|
|
|
|
"agent",
|
|
|
|
"cluster",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
"host.json",
|
|
|
|
"agent.json",
|
2021-08-06 17:30:32 +00:00
|
|
|
"members.json",
|
2021-08-06 21:17:25 +00:00
|
|
|
"metrics.json",
|
|
|
|
"consul.log",
|
2018-10-17 20:20:35 +00:00
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
testDir := testutil.TempDir(t, "debug")
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, `
|
2018-10-17 20:20:35 +00:00
|
|
|
enable_debug = true
|
|
|
|
`)
|
|
|
|
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
2021-08-06 18:24:12 +00:00
|
|
|
cmd := New(ui)
|
2018-10-17 20:20:35 +00:00
|
|
|
cmd.validateTiming = false
|
|
|
|
|
|
|
|
outputPath := fmt.Sprintf("%s/debug-%s", testDir, name)
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-output=" + outputPath,
|
|
|
|
"-archive=false",
|
|
|
|
"-duration=100ms",
|
|
|
|
"-interval=50ms",
|
|
|
|
}
|
|
|
|
for _, t := range tc.targets {
|
|
|
|
args = append(args, "-capture="+t)
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("should exit 0, got code: %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
errOutput := ui.ErrorWriter.String()
|
|
|
|
if errOutput != "" {
|
|
|
|
t.Errorf("expected no error output, got %q", errOutput)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the debug data was written
|
|
|
|
_, err := os.Stat(outputPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("output path should exist: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the captured static files exist
|
|
|
|
for _, f := range tc.files {
|
|
|
|
path := fmt.Sprintf("%s/%s", outputPath, f)
|
|
|
|
// Glob ignores file system errors
|
|
|
|
fs, _ := filepath.Glob(path)
|
|
|
|
if len(fs) <= 0 {
|
|
|
|
t.Fatalf("%s: output data should exist for %s", name, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure any excluded files do not exist
|
|
|
|
for _, f := range tc.excludedFiles {
|
|
|
|
path := fmt.Sprintf("%s/%s", outputPath, f)
|
|
|
|
// Glob ignores file system errors
|
|
|
|
fs, _ := filepath.Glob(path)
|
|
|
|
if len(fs) > 0 {
|
|
|
|
t.Fatalf("%s: output data should not exist for %s", name, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 17:00:51 +00:00
|
|
|
func validateLogLine(content []byte) bool {
|
|
|
|
fields := strings.SplitN(string(content), " ", 2)
|
|
|
|
if len(fields) != 2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const logTimeFormat = "2006-01-02T15:04:05.000"
|
|
|
|
t := content[:len(logTimeFormat)]
|
|
|
|
_, err := time.Parse(logTimeFormat, string(t))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
re := regexp.MustCompile(`(\[(ERROR|WARN|INFO|DEBUG|TRACE)]) (.*?): (.*)`)
|
|
|
|
valid := re.Match([]byte(fields[1]))
|
|
|
|
return valid
|
|
|
|
}
|
|
|
|
|
2021-08-06 17:46:01 +00:00
|
|
|
func TestDebugCommand_Prepare_ValidateTiming(t *testing.T) {
|
2018-10-17 20:20:35 +00:00
|
|
|
cases := map[string]struct {
|
|
|
|
duration string
|
|
|
|
interval string
|
2021-08-06 17:46:01 +00:00
|
|
|
expected string
|
2018-10-17 20:20:35 +00:00
|
|
|
}{
|
|
|
|
"both": {
|
2021-08-06 17:46:01 +00:00
|
|
|
duration: "20ms",
|
|
|
|
interval: "10ms",
|
|
|
|
expected: "duration must be longer",
|
2018-10-17 20:20:35 +00:00
|
|
|
},
|
|
|
|
"short interval": {
|
2021-08-06 17:46:01 +00:00
|
|
|
duration: "10s",
|
|
|
|
interval: "10ms",
|
|
|
|
expected: "interval must be longer",
|
2018-10-17 20:20:35 +00:00
|
|
|
},
|
|
|
|
"lower duration": {
|
2021-08-06 17:46:01 +00:00
|
|
|
duration: "20s",
|
|
|
|
interval: "30s",
|
|
|
|
expected: "must be longer than interval",
|
2018-10-17 20:20:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
2021-08-06 17:46:01 +00:00
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
2021-08-06 18:24:12 +00:00
|
|
|
cmd := New(ui)
|
2018-10-17 20:20:35 +00:00
|
|
|
|
2021-08-06 17:46:01 +00:00
|
|
|
args := []string{
|
|
|
|
"-duration=" + tc.duration,
|
|
|
|
"-interval=" + tc.interval,
|
|
|
|
}
|
|
|
|
err := cmd.flags.Parse(args)
|
|
|
|
require.NoError(t, err)
|
2018-10-17 20:20:35 +00:00
|
|
|
|
2021-08-06 17:46:01 +00:00
|
|
|
_, err = cmd.prepare()
|
|
|
|
testutil.RequireErrorContains(t, err, tc.expected)
|
|
|
|
})
|
2018-10-17 20:20:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDebugCommand_DebugDisabled(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:20:35 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testDir := testutil.TempDir(t, "debug")
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, `
|
2018-10-17 20:20:35 +00:00
|
|
|
enable_debug = false
|
|
|
|
`)
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
2021-08-06 18:24:12 +00:00
|
|
|
cmd := New(ui)
|
2018-10-17 20:20:35 +00:00
|
|
|
cmd.validateTiming = false
|
|
|
|
|
|
|
|
outputPath := fmt.Sprintf("%s/debug", testDir)
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-output=" + outputPath,
|
|
|
|
"-archive=false",
|
|
|
|
// CPU profile has a minimum of 1s
|
|
|
|
"-duration=1s",
|
|
|
|
"-interval=1s",
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("should exit 0, got code: %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
profiles := []string{"heap.prof", "profile.prof", "goroutine.prof", "trace.out"}
|
|
|
|
// Glob ignores file system errors
|
|
|
|
for _, v := range profiles {
|
2021-06-07 17:00:51 +00:00
|
|
|
fs, _ := filepath.Glob(fmt.Sprintf("%s/*r/%s", outputPath, v))
|
|
|
|
require.True(t, len(fs) == 0)
|
2018-10-17 20:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
errOutput := ui.ErrorWriter.String()
|
2022-10-27 13:25:18 +00:00
|
|
|
require.Contains(t, errOutput, "Unable to capture pprof")
|
2018-10-17 20:20:35 +00:00
|
|
|
}
|