Node names are not allowed to be empty

This commit is contained in:
Sean Chittenden 2016-03-31 14:47:55 -07:00
parent 1924c64e3b
commit c03d025903
2 changed files with 41 additions and 16 deletions

View File

@ -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

View File

@ -114,6 +114,8 @@ func TestReadCliConfig(t *testing.T) {
shutdownCh := make(chan struct{})
defer close(shutdownCh)
// Test config parse
{
tmpDir, err := ioutil.TempDir("", "consul")
if err != nil {
t.Fatalf("err: %s", err)
@ -133,6 +135,23 @@ func TestReadCliConfig(t *testing.T) {
if config.AdvertiseAddrWan != "1.2.3.4" {
t.Fatalf("expected -advertise-addr-wan 1.2.3.4 got %s", config.AdvertiseAddrWan)
}
}
// Test empty node name
{
cmd := &Command{
args: []string{
"-node", `""`,
},
ShutdownCh: shutdownCh,
Ui: new(cli.MockUi),
}
config := cmd.readConfig()
if config != nil {
t.Errorf(`Expected -node="" to fail`)
}
}
}
func TestRetryJoinFail(t *testing.T) {