2020-10-06 02:30:01 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2020-10-06 02:30:01 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2023-02-03 14:14:53 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2020-10-06 02:30:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ cli.Command = &OperatorMetricsCommand{}
|
|
|
|
|
|
|
|
func TestCommand_Metrics_Cases(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-10-06 02:30:01 +00:00
|
|
|
|
|
|
|
srv, _, url := testServer(t, false, nil)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
|
2020-10-06 13:47:16 +00:00
|
|
|
ui := cli.NewMockUi()
|
2020-10-06 02:30:01 +00:00
|
|
|
cmd := &OperatorMetricsCommand{Meta: Meta{Ui: ui}}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedCode int
|
|
|
|
expectedOutput string
|
|
|
|
expectedError string
|
|
|
|
}{
|
2020-10-14 19:16:10 +00:00
|
|
|
{
|
|
|
|
"gotemplate MetricsSummary",
|
|
|
|
[]string{"-address=" + url, "-t", "'{{ .Timestamp }}'"},
|
|
|
|
0,
|
|
|
|
"UTC",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"json formatted MetricsSummary",
|
|
|
|
[]string{"-address=" + url, "-json"},
|
|
|
|
0,
|
|
|
|
"{",
|
|
|
|
"",
|
|
|
|
},
|
2020-10-06 02:30:01 +00:00
|
|
|
{
|
|
|
|
"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,
|
|
|
|
"",
|
2023-02-03 14:14:53 +00:00
|
|
|
"dial tcp: lookup foo", // dns resolution error messages changes with Go, OS version
|
2020-10-06 02:30:01 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
code := cmd.Run(c.args)
|
2023-02-03 14:14:53 +00:00
|
|
|
stdOut := ui.OutputWriter.String()
|
|
|
|
stdErr := ui.ErrorWriter.String()
|
2020-10-06 02:30:01 +00:00
|
|
|
|
2023-02-03 14:14:53 +00:00
|
|
|
must.Eq(t, code, c.expectedCode, must.Sprintf("expected exit code %d, got: %d: %s", c.expectedCode, code, stdErr))
|
|
|
|
must.StrContains(t, stdOut, c.expectedOutput)
|
|
|
|
must.StrContains(t, stdErr, c.expectedError)
|
2020-10-06 02:30:01 +00:00
|
|
|
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|