open-nomad/command/metrics_test.go
Seth Hoenig 2f0cfb5740 build: upgrade and speedup circleci configuration
This PR upgrades our CI images and fixes some affected tests.

- upgrade go-machine-image to premade latest ubuntu LTS (ubuntu-2004:202111-02)

- eliminate go-machine-recent-image (no longer necessary)

- manage GOPATH in GNUMakefile (see https://discuss.circleci.com/t/gopath-is-set-to-multiple-directories/7174)

- fix tcp dial error check (message seems to be OS specific)

- spot check values measured instead of specifically 'RSS' (rss no longer reported in cgroups v2)

- use safe MkdirTemp for generating tmpfiles

NOT applied: (too flakey)

- eliminate setting GOMAXPROCS=1 (build tools were also affected by this setting)

- upgrade resource type for all imanges to large (2C -> 4C)
2022-01-24 08:28:14 -06: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,
"",
"dial tcp: lookup foo: Temporary failure in name resolution",
},
}
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()
})
}
}