1package command
2
3import (
4	"fmt"
5	"strings"
6	"testing"
7
8	"github.com/hashicorp/nomad/testutil"
9	"github.com/mitchellh/cli"
10	"github.com/posener/complete"
11	"github.com/stretchr/testify/assert"
12)
13
14func TestNodeEligibilityCommand_Implements(t *testing.T) {
15	t.Parallel()
16	var _ cli.Command = &NodeEligibilityCommand{}
17}
18
19func TestNodeEligibilityCommand_Fails(t *testing.T) {
20	t.Parallel()
21	srv, _, url := testServer(t, false, nil)
22	defer srv.Shutdown()
23
24	ui := new(cli.MockUi)
25	cmd := &NodeEligibilityCommand{Meta: Meta{Ui: ui}}
26
27	// Fails on misuse
28	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
29		t.Fatalf("expected exit code 1, got: %d", code)
30	}
31	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
32		t.Fatalf("expected help output, got: %s", out)
33	}
34	ui.ErrorWriter.Reset()
35
36	// Fails on connection failure
37	if code := cmd.Run([]string{"-address=nope", "-enable", "12345678-abcd-efab-cdef-123456789abc"}); code != 1 {
38		t.Fatalf("expected exit code 1, got: %d", code)
39	}
40	expected := "Error updating scheduling eligibility"
41	if out := ui.ErrorWriter.String(); !strings.Contains(out, expected) {
42		t.Fatalf("expected %q, got: %s", expected, out)
43	}
44	ui.ErrorWriter.Reset()
45
46	// Fails on non-existent node
47	if code := cmd.Run([]string{"-address=" + url, "-enable", "12345678-abcd-efab-cdef-123456789abc"}); code != 1 {
48		t.Fatalf("expected exit 1, got: %d", code)
49	}
50	if out := ui.ErrorWriter.String(); !strings.Contains(out, "No node(s) with prefix or id") {
51		t.Fatalf("expected not exist error, got: %s", out)
52	}
53	ui.ErrorWriter.Reset()
54
55	// Fails if both enable and disable specified
56	if code := cmd.Run([]string{"-enable", "-disable", "12345678-abcd-efab-cdef-123456789abc"}); code != 1 {
57		t.Fatalf("expected exit 1, got: %d", code)
58	}
59	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
60		t.Fatalf("expected help output, got: %s", out)
61	}
62	ui.ErrorWriter.Reset()
63
64	// Fails if neither enable or disable specified
65	if code := cmd.Run([]string{"12345678-abcd-efab-cdef-123456789abc"}); code != 1 {
66		t.Fatalf("expected exit 1, got: %d", code)
67	}
68	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
69		t.Fatalf("expected help output, got: %s", out)
70	}
71	ui.ErrorWriter.Reset()
72
73	// Fail on identifier with too few characters
74	if code := cmd.Run([]string{"-address=" + url, "-enable", "1"}); code != 1 {
75		t.Fatalf("expected exit 1, got: %d", code)
76	}
77	if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") {
78		t.Fatalf("expected too few characters error, got: %s", out)
79	}
80	ui.ErrorWriter.Reset()
81
82	// Identifiers with uneven length should produce a query result
83	if code := cmd.Run([]string{"-address=" + url, "-enable", "123"}); code != 1 {
84		t.Fatalf("expected exit 1, got: %d", code)
85	}
86	if out := ui.ErrorWriter.String(); !strings.Contains(out, "No node(s) with prefix or id") {
87		t.Fatalf("expected not exist error, got: %s", out)
88	}
89	ui.ErrorWriter.Reset()
90}
91
92func TestNodeEligibilityCommand_AutocompleteArgs(t *testing.T) {
93	assert := assert.New(t)
94	t.Parallel()
95
96	srv, client, url := testServer(t, true, nil)
97	defer srv.Shutdown()
98
99	// Wait for a node to appear
100	var nodeID string
101	testutil.WaitForResult(func() (bool, error) {
102		nodes, _, err := client.Nodes().List(nil)
103		if err != nil {
104			return false, err
105		}
106		if len(nodes) == 0 {
107			return false, fmt.Errorf("missing node")
108		}
109		nodeID = nodes[0].ID
110		return true, nil
111	}, func(err error) {
112		t.Fatalf("err: %s", err)
113	})
114
115	ui := new(cli.MockUi)
116	cmd := &NodeEligibilityCommand{Meta: Meta{Ui: ui, flagAddress: url}}
117
118	prefix := nodeID[:len(nodeID)-5]
119	args := complete.Args{Last: prefix}
120	predictor := cmd.AutocompleteArgs()
121
122	res := predictor.Predict(args)
123	assert.Equal(1, len(res))
124	assert.Equal(nodeID, res[0])
125}
126