open-nomad/command/metrics_test.go
Dave May f37e90be18
Metrics gotemplate support, debug bundle features (#9067)
* add goroutine text profiles to nomad operator debug

* add server-id=all to nomad operator debug

* fix bug from changing metrics from string to []byte

* Add function to return MetricsSummary struct, metrics gotemplate support

* fix bug resolving 'server-id=all' when no servers are available

* add url to operator_debug tests

* removed test section which is used for future operator_debug.go changes

* separate metrics from operator, use only structs from go-metrics

* ensure parent directories are created as needed

* add suggested comments for text debug pprof

* move check down to where it is used

* add WaitForFiles helper function to wait for multiple files to exist

* compact metrics check

Co-authored-by: Drew Bailey <2614075+drewbailey@users.noreply.github.com>

* fix github's silly apply suggestion

Co-authored-by: Drew Bailey <2614075+drewbailey@users.noreply.github.com>
2020-10-14 15:16:10 -04:00

94 lines
1.9 KiB
Go

package command
import (
"testing"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/require"
)
var _ cli.Command = &OperatorMetricsCommand{}
func TestCommand_Metrics_Cases(t *testing.T) {
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := cli.NewMockUi()
cmd := &OperatorMetricsCommand{Meta: Meta{Ui: ui}}
cases := []struct {
name string
args []string
expectedCode int
expectedOutput string
expectedError string
}{
{
"gotemplate MetricsSummary",
[]string{"-address=" + url, "-t", "'{{ .Timestamp }}'"},
0,
"UTC",
"",
},
{
"json formatted MetricsSummary",
[]string{"-address=" + url, "-json"},
0,
"{",
"",
},
{
"pretty print json",
[]string{"-address=" + url, "-pretty"},
0,
"{",
"",
},
{
"prometheus format",
[]string{"-address=" + url, "-format", "prometheus"},
0,
"# HELP",
"",
},
{
"bad argument",
[]string{"-address=" + url, "-foo", "bar"},
1,
"Usage: nomad operator metrics",
"flag provided but not defined: -foo",
},
{
"bad address - no protocol",
[]string{"-address=foo"},
1,
"",
"Error getting metrics: Get \"/v1/metrics\": unsupported protocol scheme",
},
{
"bad address - fake host",
[]string{"-address=http://foo"},
1,
"",
"no such host",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
code := cmd.Run(c.args)
out := ui.OutputWriter.String()
outerr := ui.ErrorWriter.String()
require.Equalf(t, code, c.expectedCode, "expected exit code %d, got: %d: %s", c.expectedCode, code, outerr)
require.Contains(t, out, c.expectedOutput, "expected output \"%s\", got \"%s\"", c.expectedOutput, out)
require.Containsf(t, outerr, c.expectedError, "expected error \"%s\", got \"%s\"", c.expectedError, outerr)
ui.OutputWriter.Reset()
ui.ErrorWriter.Reset()
})
}
}