1// +build ent
2
3package command
4
5import (
6	"strings"
7	"testing"
8
9	"github.com/hashicorp/nomad/api"
10	"github.com/mitchellh/cli"
11	"github.com/posener/complete"
12	"github.com/stretchr/testify/assert"
13)
14
15func TestNamespaceInspectCommand_Implements(t *testing.T) {
16	t.Parallel()
17	var _ cli.Command = &NamespaceInspectCommand{}
18}
19
20func TestNamespaceInspectCommand_Fails(t *testing.T) {
21	t.Parallel()
22	ui := new(cli.MockUi)
23	cmd := &NamespaceInspectCommand{Meta: Meta{Ui: ui}}
24
25	// Fails on misuse
26	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
27		t.Fatalf("expected exit code 1, got: %d", code)
28	}
29	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
30		t.Fatalf("expected help output, got: %s", out)
31	}
32	ui.ErrorWriter.Reset()
33
34	if code := cmd.Run([]string{"-address=nope", "foo"}); code != 1 {
35		t.Fatalf("expected exit code 1, got: %d", code)
36	}
37	if out := ui.ErrorWriter.String(); !strings.Contains(out, "retrieving namespace") {
38		t.Fatalf("connection error, got: %s", out)
39	}
40	ui.ErrorWriter.Reset()
41}
42
43func TestNamespaceInspectCommand_Good(t *testing.T) {
44	t.Parallel()
45
46	// Create a server
47	srv, client, url := testServer(t, true, nil)
48	defer srv.Shutdown()
49
50	ui := new(cli.MockUi)
51	cmd := &NamespaceInspectCommand{Meta: Meta{Ui: ui}}
52
53	// Create a namespace
54	ns := &api.Namespace{
55		Name: "foo",
56	}
57	_, err := client.Namespaces().Register(ns, nil)
58	assert.Nil(t, err)
59
60	// Inspect
61	if code := cmd.Run([]string{"-address=" + url, ns.Name}); code != 0 {
62		t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
63	}
64
65	out := ui.OutputWriter.String()
66	if !strings.Contains(out, ns.Name) {
67		t.Fatalf("expected namespace, got: %s", out)
68	}
69}
70
71func TestNamespaceInspectCommand_AutocompleteArgs(t *testing.T) {
72	assert := assert.New(t)
73	t.Parallel()
74
75	srv, client, url := testServer(t, true, nil)
76	defer srv.Shutdown()
77
78	ui := new(cli.MockUi)
79	cmd := &NamespaceInspectCommand{Meta: Meta{Ui: ui, flagAddress: url}}
80
81	// Create a namespace
82	ns := &api.Namespace{
83		Name: "foo",
84	}
85	_, err := client.Namespaces().Register(ns, nil)
86	assert.Nil(err)
87
88	args := complete.Args{Last: "f"}
89	predictor := cmd.AutocompleteArgs()
90
91	res := predictor.Predict(args)
92	assert.Equal(1, len(res))
93	assert.Equal(ns.Name, res[0])
94}
95
96// This test should demonstrate the behavior of a namespace
97// and prefix collision.  In that case, the Namespace status
98// command should pull the matching namespace rather than
99// displaying the multiple match error
100func TestNamespaceInspectCommand_NamespaceMatchesPrefix(t *testing.T) {
101	t.Parallel()
102
103	// Create a server
104	srv, client, url := testServer(t, true, nil)
105	defer srv.Shutdown()
106
107	ui := new(cli.MockUi)
108	cmd := &NamespaceInspectCommand{Meta: Meta{Ui: ui}}
109
110	// Create a namespace that uses foo as a prefix
111	ns := &api.Namespace{Name: "fooBar"}
112	_, err := client.Namespaces().Register(ns, nil)
113	assert.Nil(t, err)
114
115	// Create a foo namespace
116	ns2 := &api.Namespace{Name: "foo"}
117	_, err = client.Namespaces().Register(ns2, nil)
118	assert.Nil(t, err)
119
120	// Adding a NS after to prevent sort from creating
121	// false successes
122	ns = &api.Namespace{Name: "fooBaz"}
123	_, err = client.Namespaces().Register(ns, nil)
124	assert.Nil(t, err)
125
126	// Check status on namespace
127	code := cmd.Run([]string{"-address=" + url, ns2.Name})
128	if code != 0 {
129		t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
130	}
131	// Check to ensure we got the proper foo
132	out := ui.OutputWriter.String()
133	if !strings.Contains(out, "\"foo\",\n") {
134		t.Fatalf("expected namespace foo, got: %s", out)
135	}
136}
137