浏览代码

daemon/config: fix TestDaemonConfigurationMerge

This test was validating that the config file would not overwrite the
log-opt, but the test did not set up the flags correctly; as the flags
were not marked as "changed", it would not detect a conflict between
the config-file and daemon-flags.

This patch:

- removes the incorrect fields from the JSON file
- initializes the Config using config.New(), so that any defaults are also set
- sets flag values by actually setting them through the flags

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 3 年之前
父节点
当前提交
62f71c4505
共有 2 个文件被更改,包括 24 次插入39 次删除
  1. 12 20
      daemon/config/config_linux_test.go
  2. 12 19
      daemon/config/config_windows_test.go

+ 12 - 20
daemon/config/config_linux_test.go

@@ -63,33 +63,25 @@ func TestDaemonConfigurationMerge(t *testing.T) {
 					"Hard": 2048,
 					"Hard": 2048,
 					"Soft": 1024
 					"Soft": 1024
 				}
 				}
-			},
-			"log-opts": {
-				"tag": "test_tag"
 			}
 			}
 		}`
 		}`
 
 
 	file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
 	file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
 	defer file.Remove()
 	defer file.Remove()
 
 
-	c := &Config{
-		CommonConfig: CommonConfig{
-			AutoRestart: true,
-			LogConfig: LogConfig{
-				Type:   "syslog",
-				Config: map[string]string{"tag": "test"},
-			},
-		},
-	}
+	conf := New()
 
 
 	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
 	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
-
-	var debug bool
-	flags.BoolVarP(&debug, "debug", "D", false, "")
-	flags.Var(opts.NewNamedUlimitOpt("default-ulimits", nil), "default-ulimit", "")
-	flags.Var(opts.NewNamedMapOpts("log-opts", nil, nil), "log-opt", "")
-
-	cc, err := MergeDaemonConfigurations(c, flags, file.Path())
+	flags.BoolVarP(&conf.Debug, "debug", "D", false, "")
+	flags.BoolVarP(&conf.AutoRestart, "restart", "r", true, "")
+	flags.Var(opts.NewNamedUlimitOpt("default-ulimits", &conf.Ulimits), "default-ulimit", "")
+	flags.StringVar(&conf.LogConfig.Type, "log-driver", "json-file", "")
+	flags.Var(opts.NewNamedMapOpts("log-opts", conf.LogConfig.Config, nil), "log-opt", "")
+	assert.Check(t, flags.Set("restart", "true"))
+	assert.Check(t, flags.Set("log-driver", "syslog"))
+	assert.Check(t, flags.Set("log-opt", "tag=from_flag"))
+
+	cc, err := MergeDaemonConfigurations(conf, flags, file.Path())
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 
 
 	assert.Check(t, cc.Debug)
 	assert.Check(t, cc.Debug)
@@ -97,7 +89,7 @@ func TestDaemonConfigurationMerge(t *testing.T) {
 
 
 	expectedLogConfig := LogConfig{
 	expectedLogConfig := LogConfig{
 		Type:   "syslog",
 		Type:   "syslog",
-		Config: map[string]string{"tag": "test_tag"},
+		Config: map[string]string{"tag": "from_flag"},
 	}
 	}
 
 
 	assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))
 	assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))

+ 12 - 19
daemon/config/config_windows_test.go

@@ -20,30 +20,23 @@ func TestDaemonConfigurationMerge(t *testing.T) {
 
 
 	f.Write([]byte(`
 	f.Write([]byte(`
 		{
 		{
-			"debug": true,
-			"log-opts": {
-				"tag": "test_tag"
-			}
+			"debug": true
 		}`))
 		}`))
 
 
 	f.Close()
 	f.Close()
 
 
-	c := &Config{
-		CommonConfig: CommonConfig{
-			AutoRestart: true,
-			LogConfig: LogConfig{
-				Type:   "syslog",
-				Config: map[string]string{"tag": "test"},
-			},
-		},
-	}
+	conf := New()
 
 
 	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
 	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
-	var debug bool
-	flags.BoolVarP(&debug, "debug", "D", false, "")
-	flags.Var(opts.NewNamedMapOpts("log-opts", nil, nil), "log-opt", "")
-
-	cc, err := MergeDaemonConfigurations(c, flags, configFile)
+	flags.BoolVarP(&conf.Debug, "debug", "D", false, "")
+	flags.BoolVarP(&conf.AutoRestart, "restart", "r", true, "")
+	flags.StringVar(&conf.LogConfig.Type, "log-driver", "json-file", "")
+	flags.Var(opts.NewNamedMapOpts("log-opts", conf.LogConfig.Config, nil), "log-opt", "")
+	assert.Check(t, flags.Set("restart", "true"))
+	assert.Check(t, flags.Set("log-driver", "syslog"))
+	assert.Check(t, flags.Set("log-opt", "tag=from_flag"))
+
+	cc, err := MergeDaemonConfigurations(conf, flags, configFile)
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 
 
 	assert.Check(t, cc.Debug)
 	assert.Check(t, cc.Debug)
@@ -51,7 +44,7 @@ func TestDaemonConfigurationMerge(t *testing.T) {
 
 
 	expectedLogConfig := LogConfig{
 	expectedLogConfig := LogConfig{
 		Type:   "syslog",
 		Type:   "syslog",
-		Config: map[string]string{"tag": "test_tag"},
+		Config: map[string]string{"tag": "from_flag"},
 	}
 	}
 
 
 	assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))
 	assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))