diff --git a/command/agent/command.go b/command/agent/command.go index 73acf307e..16025c9d5 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -164,7 +164,13 @@ func (c *Command) readConfig() *Config { if config.NodeName == "" { hostname, err := os.Hostname() if err != nil { - c.Ui.Error(fmt.Sprintf("Error determining hostname: %s", err)) + c.Ui.Error(fmt.Sprintf("Error determining node name: %s", err)) + return nil + } + + hostname = strings.TrimSpace(hostname) + if hostname == "" { + c.Ui.Error("Node name can not be empty") return nil } config.NodeName = hostname diff --git a/command/agent/command_test.go b/command/agent/command_test.go index bfa68ffad..cc0ec21e3 100644 --- a/command/agent/command_test.go +++ b/command/agent/command_test.go @@ -114,24 +114,43 @@ func TestReadCliConfig(t *testing.T) { shutdownCh := make(chan struct{}) defer close(shutdownCh) - tmpDir, err := ioutil.TempDir("", "consul") - if err != nil { - t.Fatalf("err: %s", err) + // Test config parse + { + tmpDir, err := ioutil.TempDir("", "consul") + if err != nil { + t.Fatalf("err: %s", err) + } + + cmd := &Command{ + args: []string{ + "-data-dir", tmpDir, + "-node", `"a"`, + "-advertise-wan", "1.2.3.4", + }, + ShutdownCh: shutdownCh, + Ui: new(cli.MockUi), + } + + config := cmd.readConfig() + if config.AdvertiseAddrWan != "1.2.3.4" { + t.Fatalf("expected -advertise-addr-wan 1.2.3.4 got %s", config.AdvertiseAddrWan) + } } - cmd := &Command{ - args: []string{ - "-data-dir", tmpDir, - "-node", `"a"`, - "-advertise-wan", "1.2.3.4", - }, - ShutdownCh: shutdownCh, - Ui: new(cli.MockUi), - } + // Test empty node name + { + cmd := &Command{ + args: []string{ + "-node", `""`, + }, + ShutdownCh: shutdownCh, + Ui: new(cli.MockUi), + } - config := cmd.readConfig() - if config.AdvertiseAddrWan != "1.2.3.4" { - t.Fatalf("expected -advertise-addr-wan 1.2.3.4 got %s", config.AdvertiseAddrWan) + config := cmd.readConfig() + if config != nil { + t.Errorf(`Expected -node="" to fail`) + } } }