2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-06-01 13:25:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2021-09-18 00:37:05 +00:00
|
|
|
"os"
|
2015-06-01 13:25:51 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
2021-09-18 00:37:05 +00:00
|
|
|
|
2022-01-13 18:22:49 +00:00
|
|
|
"github.com/creack/pty"
|
2023-03-03 19:43:20 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2023-03-03 19:43:20 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
2021-09-18 00:37:05 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2023-03-03 19:43:20 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2021-10-13 21:26:56 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-06-01 13:25:51 +00:00
|
|
|
)
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
func TestMeta_FlagSet(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-06-01 13:25:51 +00:00
|
|
|
cases := []struct {
|
|
|
|
Flags FlagSetFlags
|
|
|
|
Expected []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
FlagSetNone,
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
{
|
2015-09-14 20:13:52 +00:00
|
|
|
FlagSetClient,
|
2016-10-26 21:08:46 +00:00
|
|
|
[]string{
|
|
|
|
"address",
|
|
|
|
"no-color",
|
2021-10-06 14:02:42 +00:00
|
|
|
"force-color",
|
2016-10-26 21:08:46 +00:00
|
|
|
"region",
|
2017-09-07 23:56:15 +00:00
|
|
|
"namespace",
|
2016-10-26 21:08:46 +00:00
|
|
|
"ca-cert",
|
|
|
|
"ca-path",
|
|
|
|
"client-cert",
|
|
|
|
"client-key",
|
|
|
|
"insecure",
|
2019-09-24 14:05:40 +00:00
|
|
|
"tls-server-name",
|
2016-10-26 21:08:46 +00:00
|
|
|
"tls-skip-verify",
|
2017-09-12 15:56:55 +00:00
|
|
|
"token",
|
2016-10-26 21:08:46 +00:00
|
|
|
},
|
2015-06-01 13:25:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
var m Meta
|
|
|
|
fs := m.FlagSet("foo", tc.Flags)
|
|
|
|
|
|
|
|
actual := make([]string, 0, 0)
|
|
|
|
fs.VisitAll(func(f *flag.Flag) {
|
|
|
|
actual = append(actual, f.Name)
|
|
|
|
})
|
|
|
|
sort.Strings(actual)
|
|
|
|
sort.Strings(tc.Expected)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, tc.Expected) {
|
|
|
|
t.Fatalf("%d: flags: %#v\n\nExpected: %#v\nGot: %#v",
|
|
|
|
i, tc.Flags, tc.Expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-18 00:37:05 +00:00
|
|
|
|
|
|
|
func TestMeta_Colorize(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
|
2021-09-18 00:37:05 +00:00
|
|
|
type testCaseSetupFn func(*testing.T, *Meta)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
SetupFn testCaseSetupFn
|
|
|
|
ExpectColor bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "disable colors if UI is not colored",
|
|
|
|
ExpectColor: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "colors if UI is colored",
|
|
|
|
SetupFn: func(t *testing.T, m *Meta) {
|
|
|
|
m.Ui = &cli.ColoredUi{}
|
|
|
|
},
|
|
|
|
ExpectColor: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "disable colors via CLI flag",
|
|
|
|
SetupFn: func(t *testing.T, m *Meta) {
|
2021-10-06 14:02:42 +00:00
|
|
|
m.SetupUi([]string{"-no-color"})
|
|
|
|
},
|
|
|
|
ExpectColor: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "disable colors via env var",
|
|
|
|
SetupFn: func(t *testing.T, m *Meta) {
|
2022-08-17 20:22:26 +00:00
|
|
|
t.Setenv(EnvNomadCLINoColor, "1")
|
2021-10-06 14:02:42 +00:00
|
|
|
m.SetupUi([]string{})
|
|
|
|
},
|
|
|
|
ExpectColor: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "force colors via CLI flag",
|
|
|
|
SetupFn: func(t *testing.T, m *Meta) {
|
|
|
|
m.SetupUi([]string{"-force-color"})
|
|
|
|
},
|
|
|
|
ExpectColor: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "force colors via env var",
|
|
|
|
SetupFn: func(t *testing.T, m *Meta) {
|
2022-08-17 20:22:26 +00:00
|
|
|
t.Setenv(EnvNomadCLIForceColor, "1")
|
2021-10-06 14:02:42 +00:00
|
|
|
m.SetupUi([]string{})
|
|
|
|
},
|
|
|
|
ExpectColor: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "no color take predecence over force color via CLI flag",
|
|
|
|
SetupFn: func(t *testing.T, m *Meta) {
|
|
|
|
m.SetupUi([]string{"-no-color", "-force-color"})
|
|
|
|
},
|
|
|
|
ExpectColor: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "no color take predecence over force color via env var",
|
|
|
|
SetupFn: func(t *testing.T, m *Meta) {
|
2022-08-17 20:22:26 +00:00
|
|
|
t.Setenv(EnvNomadCLINoColor, "1")
|
2021-10-06 14:02:42 +00:00
|
|
|
m.SetupUi([]string{"-force-color"})
|
2021-09-18 00:37:05 +00:00
|
|
|
},
|
|
|
|
ExpectColor: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
// Create fake test terminal.
|
|
|
|
_, tty, err := pty.Open()
|
2021-10-13 21:26:56 +00:00
|
|
|
require.NoError(t, err)
|
2021-09-18 00:37:05 +00:00
|
|
|
defer tty.Close()
|
|
|
|
|
|
|
|
oldStdout := os.Stdout
|
|
|
|
defer func() { os.Stdout = oldStdout }()
|
|
|
|
os.Stdout = tty
|
|
|
|
|
2021-10-13 21:26:56 +00:00
|
|
|
// Make sure color related environment variables are clean.
|
2022-08-17 20:22:26 +00:00
|
|
|
t.Setenv(EnvNomadCLIForceColor, "")
|
|
|
|
t.Setenv(EnvNomadCLINoColor, "")
|
2021-10-06 14:02:42 +00:00
|
|
|
|
2021-09-18 00:37:05 +00:00
|
|
|
// Run test case.
|
|
|
|
m := &Meta{}
|
|
|
|
if tc.SetupFn != nil {
|
|
|
|
tc.SetupFn(t, m)
|
|
|
|
}
|
|
|
|
|
2021-10-13 21:26:56 +00:00
|
|
|
require.Equal(t, !tc.ExpectColor, m.Colorize().Disable)
|
2021-09-18 00:37:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-03-03 19:43:20 +00:00
|
|
|
|
|
|
|
func TestMeta_JobByPrefix(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
srv, client, _ := testServer(t, true, nil)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
|
|
|
|
// Wait for a node to be ready
|
|
|
|
waitForNodes(t, client)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
meta := &Meta{Ui: ui, namespace: api.AllNamespacesNamespace}
|
|
|
|
client.SetNamespace(api.AllNamespacesNamespace)
|
|
|
|
|
|
|
|
jobs := []struct {
|
|
|
|
namespace string
|
|
|
|
id string
|
|
|
|
}{
|
|
|
|
{namespace: "default", id: "example"},
|
|
|
|
{namespace: "default", id: "job"},
|
|
|
|
{namespace: "default", id: "job-1"},
|
|
|
|
{namespace: "default", id: "job-2"},
|
|
|
|
{namespace: "prod", id: "job-1"},
|
|
|
|
}
|
|
|
|
for _, j := range jobs {
|
|
|
|
job := testJob(j.id)
|
|
|
|
job.Namespace = pointer.Of(j.namespace)
|
|
|
|
|
|
|
|
_, err := client.Namespaces().Register(&api.Namespace{Name: j.namespace}, nil)
|
|
|
|
must.NoError(t, err)
|
|
|
|
|
|
|
|
w := &api.WriteOptions{Namespace: j.namespace}
|
|
|
|
resp, _, err := client.Jobs().Register(job, w)
|
|
|
|
must.NoError(t, err)
|
|
|
|
|
|
|
|
code := waitForSuccess(ui, client, fullId, t, resp.EvalID)
|
|
|
|
must.Zero(t, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
prefix string
|
|
|
|
filterFunc JobByPrefixFilterFunc
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "exact match",
|
|
|
|
prefix: "job",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "partial match",
|
|
|
|
prefix: "exam",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "match with filter",
|
|
|
|
prefix: "job-",
|
|
|
|
filterFunc: func(j *api.JobListStub) bool {
|
|
|
|
// Filter out jobs with "job-" so that only "job-2" matches.
|
|
|
|
return j.ID == "job-2"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple matches",
|
|
|
|
prefix: "job-",
|
|
|
|
expectedError: "matched multiple jobs",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no match",
|
|
|
|
prefix: "not-found",
|
|
|
|
expectedError: "No job(s) with prefix or ID",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple matches across namespaces",
|
|
|
|
prefix: "job-1",
|
|
|
|
expectedError: "matched multiple jobs",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
job, err := meta.JobByPrefix(client, tc.prefix, tc.filterFunc)
|
|
|
|
if tc.expectedError != "" {
|
|
|
|
must.Nil(t, job)
|
|
|
|
must.ErrorContains(t, err, tc.expectedError)
|
|
|
|
} else {
|
|
|
|
must.NoError(t, err)
|
|
|
|
must.NotNil(t, job)
|
|
|
|
must.StrContains(t, *job.ID, tc.prefix)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|