open-nomad/command/metrics_test.go
Seth Hoenig 911700ffea
build: update to go1.20 (#16029)
* build: update to go1.20

* build: use stringy go1.20 in circle yaml

* tests: handle new x509 certificate error structure in go1.20

* cl: add cl entry
2023-02-03 08:14:53 -06:00

95 lines
1.9 KiB
Go

package command
import (
"testing"
"github.com/hashicorp/nomad/ci"
"github.com/mitchellh/cli"
"github.com/shoenig/test/must"
)
var _ cli.Command = &OperatorMetricsCommand{}
func TestCommand_Metrics_Cases(t *testing.T) {
ci.Parallel(t)
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,
"",
"dial tcp: lookup foo", // dns resolution error messages changes with Go, OS version
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
code := cmd.Run(c.args)
stdOut := ui.OutputWriter.String()
stdErr := ui.ErrorWriter.String()
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)
ui.OutputWriter.Reset()
ui.ErrorWriter.Reset()
})
}
}