2015-09-25 04:31:15 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-07-21 04:07:32 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent"
|
2015-09-25 04:31:15 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2015-09-25 18:49:31 +00:00
|
|
|
func TestClientConfigCommand_Implements(t *testing.T) {
|
2017-07-21 04:24:21 +00:00
|
|
|
t.Parallel()
|
2018-03-21 00:37:28 +00:00
|
|
|
var _ cli.Command = &NodeConfigCommand{}
|
2015-09-25 04:31:15 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 18:49:31 +00:00
|
|
|
func TestClientConfigCommand_UpdateServers(t *testing.T) {
|
2017-07-21 04:24:21 +00:00
|
|
|
t.Parallel()
|
2017-07-21 04:07:32 +00:00
|
|
|
srv, _, url := testServer(t, true, func(c *agent.Config) {
|
2015-09-25 04:31:15 +00:00
|
|
|
c.Server.BootstrapExpect = 0
|
|
|
|
})
|
2017-07-21 04:07:32 +00:00
|
|
|
defer srv.Shutdown()
|
2015-09-25 04:31:15 +00:00
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2018-03-21 00:37:28 +00:00
|
|
|
cmd := &NodeConfigCommand{Meta: Meta{Ui: ui}}
|
2015-09-25 18:49:31 +00:00
|
|
|
|
|
|
|
// Fails if trying to update with no servers
|
|
|
|
code := cmd.Run([]string{"-update-servers"})
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("expected exit 1, got: %d", code)
|
|
|
|
}
|
2018-04-18 17:51:17 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
|
2015-09-25 18:49:31 +00:00
|
|
|
t.Fatalf("expected help output, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
2015-09-25 04:31:15 +00:00
|
|
|
|
2018-01-26 01:56:47 +00:00
|
|
|
// Set the servers list with bad addresses
|
2016-06-01 09:14:59 +00:00
|
|
|
code = cmd.Run([]string{"-address=" + url, "-update-servers", "127.0.0.42", "198.18.5.5"})
|
2018-01-26 01:56:47 +00:00
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("expected exit 1, got: %d", code)
|
2015-09-25 04:31:15 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 01:56:47 +00:00
|
|
|
// Set the servers list with good addresses
|
|
|
|
code = cmd.Run([]string{"-address=" + url, "-update-servers", srv.Config.AdvertiseAddrs.RPC})
|
2015-09-25 04:31:15 +00:00
|
|
|
if code != 0 {
|
2018-01-26 01:56:47 +00:00
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
2015-09-25 04:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 18:49:31 +00:00
|
|
|
func TestClientConfigCommand_Fails(t *testing.T) {
|
2017-07-21 04:24:21 +00:00
|
|
|
t.Parallel()
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2018-03-21 00:37:28 +00:00
|
|
|
cmd := &NodeConfigCommand{Meta: Meta{Ui: ui}}
|
2015-09-25 04:31:15 +00:00
|
|
|
|
|
|
|
// Fails on misuse
|
|
|
|
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
|
|
|
|
t.Fatalf("expected exit code 1, got: %d", code)
|
|
|
|
}
|
2018-04-18 17:51:17 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
|
2015-09-25 04:31:15 +00:00
|
|
|
t.Fatalf("expected help output, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
2015-09-25 18:49:31 +00:00
|
|
|
// Fails if no valid flags given
|
|
|
|
if code := cmd.Run([]string{}); code != 1 {
|
|
|
|
t.Fatalf("expected exit code 1, got: %d", code)
|
2015-09-25 04:31:15 +00:00
|
|
|
}
|
2018-04-18 17:51:17 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
|
2015-09-25 04:31:15 +00:00
|
|
|
t.Fatalf("expected help output, got: %s", out)
|
|
|
|
}
|
2015-09-25 18:49:31 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
2015-09-25 04:31:15 +00:00
|
|
|
|
|
|
|
// Fails on connection failure
|
2015-09-25 18:49:31 +00:00
|
|
|
if code := cmd.Run([]string{"-address=nope", "-servers"}); code != 1 {
|
2015-09-25 04:31:15 +00:00
|
|
|
t.Fatalf("expected exit code 1, got: %d", code)
|
|
|
|
}
|
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying server list") {
|
|
|
|
t.Fatalf("expected failed query error, got: %s", out)
|
|
|
|
}
|
|
|
|
}
|