1package command
2
3import (
4	"strings"
5	"testing"
6
7	"github.com/hashicorp/nomad/command/agent"
8	"github.com/mitchellh/cli"
9)
10
11func TestClientConfigCommand_Implements(t *testing.T) {
12	t.Parallel()
13	var _ cli.Command = &NodeConfigCommand{}
14}
15
16func TestClientConfigCommand_UpdateServers(t *testing.T) {
17	t.Parallel()
18	srv, _, url := testServer(t, true, func(c *agent.Config) {
19		c.Server.BootstrapExpect = 0
20	})
21	defer srv.Shutdown()
22
23	ui := new(cli.MockUi)
24	cmd := &NodeConfigCommand{Meta: Meta{Ui: ui}}
25
26	// Fails if trying to update with no servers
27	code := cmd.Run([]string{"-update-servers"})
28	if code != 1 {
29		t.Fatalf("expected exit 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	// Set the servers list with bad addresses
37	code = cmd.Run([]string{"-address=" + url, "-update-servers", "127.0.0.42", "198.18.5.5"})
38	if code != 1 {
39		t.Fatalf("expected exit 1, got: %d", code)
40	}
41
42	// Set the servers list with good addresses
43	code = cmd.Run([]string{"-address=" + url, "-update-servers", srv.Config.AdvertiseAddrs.RPC})
44	if code != 0 {
45		t.Fatalf("expected exit 0, got: %d", code)
46	}
47}
48
49func TestClientConfigCommand_Fails(t *testing.T) {
50	t.Parallel()
51	ui := new(cli.MockUi)
52	cmd := &NodeConfigCommand{Meta: Meta{Ui: ui}}
53
54	// Fails on misuse
55	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
56		t.Fatalf("expected exit code 1, got: %d", code)
57	}
58	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
59		t.Fatalf("expected help output, got: %s", out)
60	}
61	ui.ErrorWriter.Reset()
62
63	// Fails if no valid flags given
64	if code := cmd.Run([]string{}); code != 1 {
65		t.Fatalf("expected exit code 1, got: %d", code)
66	}
67	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
68		t.Fatalf("expected help output, got: %s", out)
69	}
70	ui.ErrorWriter.Reset()
71
72	// Fails on connection failure
73	if code := cmd.Run([]string{"-address=nope", "-servers"}); code != 1 {
74		t.Fatalf("expected exit code 1, got: %d", code)
75	}
76	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying server list") {
77		t.Fatalf("expected failed query error, got: %s", out)
78	}
79}
80