Parcourir la source

fix(etcd): Fix forced config reset

When a server name or a data directory were not provided, the
reset functionality would fail to clear out config files from
the appropriate place. This calcualtes the default server name
and data directory before reset is called.
Brian Waldon il y a 12 ans
Parent
commit
e7839e8c57
3 fichiers modifiés avec 33 ajouts et 15 suppressions
  1. 0 10
      etcd.go
  2. 15 5
      server/config.go
  3. 18 0
      server/config_test.go

+ 0 - 10
etcd.go

@@ -52,16 +52,6 @@ func main() {
 		profile(config.CPUProfileFile)
 		profile(config.CPUProfileFile)
 	}
 	}
 
 
-	// Only guess the machine name if there is no data dir specified
-	// because the info file will should have our name
-	if config.Name == "" && config.DataDir == "" {
-		config.NameFromHostname()
-	}
-
-	if config.DataDir == "" && config.Name != "" {
-		config.DataDirFromName()
-	}
-
 	if config.DataDir == "" {
 	if config.DataDir == "" {
 		log.Fatal("The data dir was not set and could not be guessed from machine name")
 		log.Fatal("The data dir was not set and could not be guessed from machine name")
 	}
 	}

+ 15 - 5
server/config.go

@@ -131,6 +131,11 @@ func (c *Config) Load(arguments []string) error {
 		return fmt.Errorf("sanitize: %v", err)
 		return fmt.Errorf("sanitize: %v", err)
 	}
 	}
 
 
+	// Force remove server configuration if specified.
+	if c.Force {
+		c.Reset()
+	}
+
 	return nil
 	return nil
 }
 }
 
 
@@ -278,11 +283,6 @@ func (c *Config) LoadFlags(arguments []string) error {
 		c.CorsOrigins = trimsplit(cors, ",")
 		c.CorsOrigins = trimsplit(cors, ",")
 	}
 	}
 
 
-	// Force remove server configuration if specified.
-	if c.Force {
-		c.Reset()
-	}
-
 	return nil
 	return nil
 }
 }
 
 
@@ -404,6 +404,16 @@ func (c *Config) Sanitize() error {
 		return fmt.Errorf("Peer Listen Host: %s", err)
 		return fmt.Errorf("Peer Listen Host: %s", err)
 	}
 	}
 
 
+	// Only guess the machine name if there is no data dir specified
+	// because the info file should have our name
+	if c.Name == "" && c.DataDir == "" {
+		c.NameFromHostname()
+	}
+
+	if c.DataDir == "" && c.Name != "" {
+		c.DataDirFromName()
+	}
+
 	return nil
 	return nil
 }
 }
 
 

+ 18 - 0
server/config_test.go

@@ -313,6 +313,24 @@ func TestConfigNameFlag(t *testing.T) {
 	assert.Equal(t, c.Name, "test-name", "")
 	assert.Equal(t, c.Name, "test-name", "")
 }
 }
 
 
+// Ensures that a Name gets guessed if not specified
+func TestConfigNameGuess(t *testing.T) {
+	c := NewConfig()
+	assert.Nil(t, c.LoadFlags([]string{}), "")
+	assert.Nil(t, c.Sanitize())
+	name, _ := os.Hostname()
+	assert.Equal(t, c.Name, name, "")
+}
+
+// Ensures that a DataDir gets guessed if not specified
+func TestConfigDataDirGuess(t *testing.T) {
+	c := NewConfig()
+	assert.Nil(t, c.LoadFlags([]string{}), "")
+	assert.Nil(t, c.Sanitize())
+	name, _ := os.Hostname()
+	assert.Equal(t, c.DataDir, name+".etcd", "")
+}
+
 // Ensures that Snapshot can be parsed from the environment.
 // Ensures that Snapshot can be parsed from the environment.
 func TestConfigSnapshotEnv(t *testing.T) {
 func TestConfigSnapshotEnv(t *testing.T) {
 	withEnv("ETCD_SNAPSHOT", "1", func(c *Config) {
 	withEnv("ETCD_SNAPSHOT", "1", func(c *Config) {