2018-02-05 16:05:59 -05:00
|
|
|
package config // import "github.com/docker/docker/daemon/config"
|
2016-08-10 06:52:06 -04:00
|
|
|
|
|
|
|
import (
|
2021-08-24 06:10:50 -04:00
|
|
|
"os"
|
2016-08-10 06:52:06 -04:00
|
|
|
"testing"
|
2017-04-12 03:26:42 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
"github.com/spf13/pflag"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-08-10 06:52:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDaemonConfigurationMerge(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
f, err := os.CreateTemp("", "docker-config-")
|
2022-06-22 09:03:06 -04:00
|
|
|
assert.NilError(t, err)
|
2016-08-10 06:52:06 -04:00
|
|
|
|
|
|
|
configFile := f.Name()
|
|
|
|
|
|
|
|
f.Write([]byte(`
|
|
|
|
{
|
2022-06-06 12:55:05 -04:00
|
|
|
"debug": true
|
2016-08-10 06:52:06 -04:00
|
|
|
}`))
|
|
|
|
|
|
|
|
f.Close()
|
|
|
|
|
2022-06-06 15:11:08 -04:00
|
|
|
conf, err := New()
|
|
|
|
assert.NilError(t, err)
|
2016-08-10 06:52:06 -04:00
|
|
|
|
2017-04-12 03:26:42 -04:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
2022-06-06 12:55:05 -04:00
|
|
|
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)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2016-08-10 06:52:06 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, cc.Debug)
|
|
|
|
assert.Check(t, cc.AutoRestart)
|
2017-04-12 03:26:42 -04:00
|
|
|
|
|
|
|
expectedLogConfig := LogConfig{
|
|
|
|
Type: "syslog",
|
2022-06-06 12:55:05 -04:00
|
|
|
Config: map[string]string{"tag": "from_flag"},
|
2016-08-10 06:52:06 -04:00
|
|
|
}
|
2017-04-12 03:26:42 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))
|
2016-08-10 06:52:06 -04:00
|
|
|
}
|