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>
This commit is contained in:
Sebastiaan van Stijn 2022-06-06 18:55:05 +02:00
parent 9b39cab510
commit 62f71c4505
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 22 additions and 37 deletions

View file

@ -63,33 +63,25 @@ func TestDaemonConfigurationMerge(t *testing.T) {
"Hard": 2048,
"Soft": 1024
}
},
"log-opts": {
"tag": "test_tag"
}
}`
file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
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.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"))
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())
cc, err := MergeDaemonConfigurations(conf, flags, file.Path())
assert.NilError(t, err)
assert.Check(t, cc.Debug)
@ -97,7 +89,7 @@ func TestDaemonConfigurationMerge(t *testing.T) {
expectedLogConfig := LogConfig{
Type: "syslog",
Config: map[string]string{"tag": "test_tag"},
Config: map[string]string{"tag": "from_flag"},
}
assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))

View file

@ -20,30 +20,23 @@ func TestDaemonConfigurationMerge(t *testing.T) {
f.Write([]byte(`
{
"debug": true,
"log-opts": {
"tag": "test_tag"
}
"debug": true
}`))
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)
var debug bool
flags.BoolVarP(&debug, "debug", "D", false, "")
flags.Var(opts.NewNamedMapOpts("log-opts", nil, nil), "log-opt", "")
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(c, flags, configFile)
cc, err := MergeDaemonConfigurations(conf, flags, configFile)
assert.NilError(t, err)
assert.Check(t, cc.Debug)
@ -51,7 +44,7 @@ func TestDaemonConfigurationMerge(t *testing.T) {
expectedLogConfig := LogConfig{
Type: "syslog",
Config: map[string]string{"tag": "test_tag"},
Config: map[string]string{"tag": "from_flag"},
}
assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))