2015-12-10 18:35:10 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-01-23 06:23:07 -05:00
|
|
|
"github.com/docker/docker/daemon/config"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-06-22 18:36:51 -04:00
|
|
|
"github.com/spf13/pflag"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/fs"
|
2015-12-10 18:35:10 -05:00
|
|
|
)
|
|
|
|
|
2018-10-15 03:52:53 -04:00
|
|
|
func defaultOptions(t *testing.T, configFile string) *daemonOptions {
|
2017-06-01 16:34:31 -04:00
|
|
|
opts := newDaemonOptions(&config.Config{})
|
|
|
|
opts.flags = &pflag.FlagSet{}
|
|
|
|
opts.InstallFlags(opts.flags)
|
2018-10-15 03:52:53 -04:00
|
|
|
if err := installConfigFlags(opts.daemonConfig, opts.flags); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defaultDaemonConfigFile, err := getDefaultDaemonConfigFile()
|
|
|
|
assert.NilError(t, err)
|
2017-03-30 06:01:00 -04:00
|
|
|
opts.flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "")
|
2016-06-22 18:36:51 -04:00
|
|
|
opts.configFile = configFile
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) {
|
2018-10-15 03:52:53 -04:00
|
|
|
opts := defaultOptions(t, "")
|
2017-06-01 16:34:31 -04:00
|
|
|
opts.Debug = true
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
2015-12-10 18:35:10 -05:00
|
|
|
if !loadedConfig.Debug {
|
|
|
|
t.Fatalf("expected debug to be copied from the common flags, got false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithTLS(t *testing.T) {
|
2018-10-15 03:52:53 -04:00
|
|
|
opts := defaultOptions(t, "")
|
2017-06-01 16:34:31 -04:00
|
|
|
opts.TLSOptions.CAFile = "/tmp/ca.pem"
|
|
|
|
opts.TLS = true
|
2016-06-22 18:36:51 -04:00
|
|
|
|
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
|
|
|
assert.Check(t, is.Equal("/tmp/ca.pem", loadedConfig.CommonTLSOptions.CAFile))
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithConflicts(t *testing.T) {
|
2017-08-23 17:25:00 -04:00
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{"labels": ["l3=foo"]}`))
|
2016-06-22 18:36:51 -04:00
|
|
|
defer tempFile.Remove()
|
2017-08-23 17:25:00 -04:00
|
|
|
configFile := tempFile.Path()
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2018-10-15 03:52:53 -04:00
|
|
|
opts := defaultOptions(t, configFile)
|
2016-06-22 18:36:51 -04:00
|
|
|
flags := opts.flags
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, flags.Set("config-file", configFile))
|
|
|
|
assert.Check(t, flags.Set("label", "l1=bar"))
|
|
|
|
assert.Check(t, flags.Set("label", "l2=baz"))
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
_, err := loadDaemonCliConfig(opts)
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "as a flag and in the configuration file: labels"))
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
2016-01-19 14:16:07 -05:00
|
|
|
|
2018-01-26 16:15:36 -05:00
|
|
|
func TestLoadDaemonCliWithConflictingNodeGenericResources(t *testing.T) {
|
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{"node-generic-resources": ["foo=bar", "bar=baz"]}`))
|
|
|
|
defer tempFile.Remove()
|
|
|
|
configFile := tempFile.Path()
|
|
|
|
|
2018-10-15 03:52:53 -04:00
|
|
|
opts := defaultOptions(t, configFile)
|
2018-01-26 16:15:36 -05:00
|
|
|
flags := opts.flags
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, flags.Set("config-file", configFile))
|
|
|
|
assert.Check(t, flags.Set("node-generic-resource", "r1=bar"))
|
|
|
|
assert.Check(t, flags.Set("node-generic-resource", "r2=baz"))
|
2018-01-26 16:15:36 -05:00
|
|
|
|
|
|
|
_, err := loadDaemonCliConfig(opts)
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "as a flag and in the configuration file: node-generic-resources"))
|
2018-01-26 16:15:36 -05:00
|
|
|
}
|
|
|
|
|
2017-11-11 21:09:28 -05:00
|
|
|
func TestLoadDaemonCliWithConflictingLabels(t *testing.T) {
|
2018-10-15 03:52:53 -04:00
|
|
|
opts := defaultOptions(t, "")
|
2017-11-11 21:09:28 -05:00
|
|
|
flags := opts.flags
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, flags.Set("label", "foo=bar"))
|
|
|
|
assert.Check(t, flags.Set("label", "foo=baz"))
|
2017-11-11 21:09:28 -05:00
|
|
|
|
|
|
|
_, err := loadDaemonCliConfig(opts)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Error(err, "conflict labels for foo=baz and foo=bar"))
|
2017-11-11 21:09:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliWithDuplicateLabels(t *testing.T) {
|
2018-10-15 03:52:53 -04:00
|
|
|
opts := defaultOptions(t, "")
|
2017-11-11 21:09:28 -05:00
|
|
|
flags := opts.flags
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, flags.Set("label", "foo=the-same"))
|
|
|
|
assert.Check(t, flags.Set("label", "foo=the-same"))
|
2017-11-11 21:09:28 -05:00
|
|
|
|
|
|
|
_, err := loadDaemonCliConfig(opts)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err)
|
2017-11-11 21:09:28 -05:00
|
|
|
}
|
|
|
|
|
2016-01-19 14:16:07 -05:00
|
|
|
func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) {
|
2017-08-23 17:25:00 -04:00
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{"tlsverify": true}`))
|
2016-06-22 18:36:51 -04:00
|
|
|
defer tempFile.Remove()
|
2016-01-19 14:16:07 -05:00
|
|
|
|
2018-10-15 03:52:53 -04:00
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
2017-06-01 16:34:31 -04:00
|
|
|
opts.TLSOptions.CAFile = "/tmp/ca.pem"
|
2016-01-19 14:16:07 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
2020-07-28 19:01:08 -04:00
|
|
|
assert.Check(t, is.Equal(*loadedConfig.TLS, true))
|
2016-01-19 14:16:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) {
|
2017-08-23 17:25:00 -04:00
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{"tlsverify": false}`))
|
2016-06-22 18:36:51 -04:00
|
|
|
defer tempFile.Remove()
|
2016-01-19 14:16:07 -05:00
|
|
|
|
2018-10-15 03:52:53 -04:00
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
2017-06-01 16:34:31 -04:00
|
|
|
opts.TLSOptions.CAFile = "/tmp/ca.pem"
|
2016-01-19 14:16:07 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
2020-07-28 19:01:08 -04:00
|
|
|
assert.Check(t, *loadedConfig.TLS)
|
2016-01-19 14:16:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) {
|
2017-08-23 17:25:00 -04:00
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{}`))
|
2016-06-22 18:36:51 -04:00
|
|
|
defer tempFile.Remove()
|
2016-03-08 16:03:37 -05:00
|
|
|
|
2018-10-15 03:52:53 -04:00
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
2017-06-01 16:34:31 -04:00
|
|
|
opts.TLSOptions.CAFile = "/tmp/ca.pem"
|
2016-01-19 14:16:07 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
2020-07-28 19:01:08 -04:00
|
|
|
assert.Check(t, loadedConfig.TLS == nil)
|
2016-01-19 14:16:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) {
|
2017-08-23 17:25:00 -04:00
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{"log-level": "warn"}`))
|
2016-06-22 18:36:51 -04:00
|
|
|
defer tempFile.Remove()
|
|
|
|
|
2018-10-15 03:52:53 -04:00
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
2016-06-22 18:36:51 -04:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
|
|
|
assert.Check(t, is.Equal("warn", loadedConfig.LogLevel))
|
2016-01-19 14:16:07 -05:00
|
|
|
}
|
2016-01-20 17:16:49 -05:00
|
|
|
|
2016-01-22 13:14:48 -05:00
|
|
|
func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) {
|
2016-06-22 18:36:51 -04:00
|
|
|
content := `{"tlscacert": "/etc/certs/ca.pem", "log-driver": "syslog"}`
|
2017-08-23 17:25:00 -04:00
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(content))
|
2016-06-22 18:36:51 -04:00
|
|
|
defer tempFile.Remove()
|
|
|
|
|
2018-10-15 03:52:53 -04:00
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
2016-06-22 18:36:51 -04:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
|
|
|
assert.Check(t, is.Equal("/etc/certs/ca.pem", loadedConfig.CommonTLSOptions.CAFile))
|
|
|
|
assert.Check(t, is.Equal("syslog", loadedConfig.LogConfig.Type))
|
2016-01-20 17:16:49 -05:00
|
|
|
}
|
2016-03-08 16:03:37 -05:00
|
|
|
|
|
|
|
func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) {
|
2016-06-22 18:36:51 -04:00
|
|
|
content := `{
|
2021-04-02 08:06:27 -04:00
|
|
|
"allow-nondistributable-artifacts": ["allow-nondistributable-artifacts.example.com"],
|
|
|
|
"registry-mirrors": ["https://mirrors.example.com"],
|
|
|
|
"insecure-registries": ["https://insecure-registry.example.com"]
|
2016-06-22 18:36:51 -04:00
|
|
|
}`
|
2017-08-23 17:25:00 -04:00
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(content))
|
2016-06-22 18:36:51 -04:00
|
|
|
defer tempFile.Remove()
|
|
|
|
|
2018-10-15 03:52:53 -04:00
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
2016-06-22 18:36:51 -04:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
2016-06-22 18:36:51 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Len(loadedConfig.AllowNondistributableArtifacts, 1))
|
|
|
|
assert.Check(t, is.Len(loadedConfig.Mirrors, 1))
|
|
|
|
assert.Check(t, is.Len(loadedConfig.InsecureRegistries, 1))
|
2016-03-08 16:03:37 -05:00
|
|
|
}
|
2018-11-24 08:39:08 -05:00
|
|
|
|
|
|
|
func TestConfigureDaemonLogs(t *testing.T) {
|
|
|
|
conf := &config.Config{}
|
|
|
|
err := configureDaemonLogs(conf)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(logrus.InfoLevel, logrus.GetLevel()))
|
|
|
|
|
|
|
|
conf.LogLevel = "warn"
|
|
|
|
err = configureDaemonLogs(conf)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(logrus.WarnLevel, logrus.GetLevel()))
|
|
|
|
|
|
|
|
conf.LogLevel = "foobar"
|
|
|
|
err = configureDaemonLogs(conf)
|
|
|
|
assert.Error(t, err, "unable to parse logging level: foobar")
|
|
|
|
|
|
|
|
// log level should not be changed after a failure
|
|
|
|
assert.Check(t, is.Equal(logrus.WarnLevel, logrus.GetLevel()))
|
|
|
|
}
|