2015-12-10 23:35:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-01-23 11:23:07 +00:00
|
|
|
"github.com/docker/docker/daemon/config"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-06-22 22:36:51 +00:00
|
|
|
"github.com/spf13/pflag"
|
2018-06-11 13:32:11 +00:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/fs"
|
2015-12-10 23:35:10 +00:00
|
|
|
)
|
|
|
|
|
2018-10-15 07:52:53 +00:00
|
|
|
func defaultOptions(t *testing.T, configFile string) *daemonOptions {
|
2017-06-01 20:34:31 +00:00
|
|
|
opts := newDaemonOptions(&config.Config{})
|
|
|
|
opts.flags = &pflag.FlagSet{}
|
|
|
|
opts.InstallFlags(opts.flags)
|
2018-10-15 07:52:53 +00:00
|
|
|
if err := installConfigFlags(opts.daemonConfig, opts.flags); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defaultDaemonConfigFile, err := getDefaultDaemonConfigFile()
|
|
|
|
assert.NilError(t, err)
|
2017-03-30 10:01:00 +00:00
|
|
|
opts.flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "")
|
2016-06-22 22:36:51 +00:00
|
|
|
opts.configFile = configFile
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:35:10 +00:00
|
|
|
func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) {
|
2018-10-15 07:52:53 +00:00
|
|
|
opts := defaultOptions(t, "")
|
2017-06-01 20:34:31 +00:00
|
|
|
opts.Debug = true
|
2015-12-10 23:35:10 +00:00
|
|
|
|
2016-06-22 22:36:51 +00:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
2015-12-10 23:35:10 +00: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 07:52:53 +00:00
|
|
|
opts := defaultOptions(t, "")
|
2017-06-01 20:34:31 +00:00
|
|
|
opts.TLSOptions.CAFile = "/tmp/ca.pem"
|
|
|
|
opts.TLS = true
|
2016-06-22 22:36:51 +00:00
|
|
|
|
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
|
|
|
assert.Check(t, is.Equal("/tmp/ca.pem", loadedConfig.CommonTLSOptions.CAFile))
|
2015-12-10 23:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithConflicts(t *testing.T) {
|
2017-08-23 21:25:00 +00:00
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{"labels": ["l3=foo"]}`))
|
2016-06-22 22:36:51 +00:00
|
|
|
defer tempFile.Remove()
|
2017-08-23 21:25:00 +00:00
|
|
|
configFile := tempFile.Path()
|
2015-12-10 23:35:10 +00:00
|
|
|
|
2018-10-15 07:52:53 +00:00
|
|
|
opts := defaultOptions(t, configFile)
|
2016-06-22 22:36:51 +00:00
|
|
|
flags := opts.flags
|
2015-12-10 23:35:10 +00:00
|
|
|
|
2018-03-13 19:28:34 +00: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 23:35:10 +00:00
|
|
|
|
2016-06-22 22:36:51 +00:00
|
|
|
_, err := loadDaemonCliConfig(opts)
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "as a flag and in the configuration file: labels"))
|
2015-12-10 23:35:10 +00:00
|
|
|
}
|
2016-01-19 19:16:07 +00:00
|
|
|
|
2018-01-26 21:15:36 +00: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 07:52:53 +00:00
|
|
|
opts := defaultOptions(t, configFile)
|
2018-01-26 21:15:36 +00:00
|
|
|
flags := opts.flags
|
|
|
|
|
2018-03-13 19:28:34 +00: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 21:15:36 +00:00
|
|
|
|
|
|
|
_, err := loadDaemonCliConfig(opts)
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "as a flag and in the configuration file: node-generic-resources"))
|
2018-01-26 21:15:36 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 02:09:28 +00:00
|
|
|
func TestLoadDaemonCliWithConflictingLabels(t *testing.T) {
|
2018-10-15 07:52:53 +00:00
|
|
|
opts := defaultOptions(t, "")
|
2017-11-12 02:09:28 +00:00
|
|
|
flags := opts.flags
|
|
|
|
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, flags.Set("label", "foo=bar"))
|
|
|
|
assert.Check(t, flags.Set("label", "foo=baz"))
|
2017-11-12 02:09:28 +00:00
|
|
|
|
|
|
|
_, err := loadDaemonCliConfig(opts)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.Error(err, "conflict labels for foo=baz and foo=bar"))
|
2017-11-12 02:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliWithDuplicateLabels(t *testing.T) {
|
2018-10-15 07:52:53 +00:00
|
|
|
opts := defaultOptions(t, "")
|
2017-11-12 02:09:28 +00:00
|
|
|
flags := opts.flags
|
|
|
|
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, flags.Set("label", "foo=the-same"))
|
|
|
|
assert.Check(t, flags.Set("label", "foo=the-same"))
|
2017-11-12 02:09:28 +00:00
|
|
|
|
|
|
|
_, err := loadDaemonCliConfig(opts)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2017-11-12 02:09:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-19 19:16:07 +00:00
|
|
|
func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) {
|
2017-08-23 21:25:00 +00:00
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{"tlsverify": true}`))
|
2016-06-22 22:36:51 +00:00
|
|
|
defer tempFile.Remove()
|
2016-01-19 19:16:07 +00:00
|
|
|
|
2018-10-15 07:52:53 +00:00
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
2017-06-01 20:34:31 +00:00
|
|
|
opts.TLSOptions.CAFile = "/tmp/ca.pem"
|
2016-01-19 19:16:07 +00:00
|
|
|
|
2016-06-22 22:36:51 +00:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
|
|
|
assert.Check(t, is.Equal(loadedConfig.TLS, true))
|
2016-01-19 19:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) {
|
2017-08-23 21:25:00 +00:00
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{"tlsverify": false}`))
|
2016-06-22 22:36:51 +00:00
|
|
|
defer tempFile.Remove()
|
2016-01-19 19:16:07 +00:00
|
|
|
|
2018-10-15 07:52:53 +00:00
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
2017-06-01 20:34:31 +00:00
|
|
|
opts.TLSOptions.CAFile = "/tmp/ca.pem"
|
2016-01-19 19:16:07 +00:00
|
|
|
|
2016-06-22 22:36:51 +00:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
|
|
|
assert.Check(t, loadedConfig.TLS)
|
2016-01-19 19:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) {
|
2017-08-23 21:25:00 +00:00
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{}`))
|
2016-06-22 22:36:51 +00:00
|
|
|
defer tempFile.Remove()
|
2016-03-08 21:03:37 +00:00
|
|
|
|
2018-10-15 07:52:53 +00:00
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
2017-06-01 20:34:31 +00:00
|
|
|
opts.TLSOptions.CAFile = "/tmp/ca.pem"
|
2016-01-19 19:16:07 +00:00
|
|
|
|
2016-06-22 22:36:51 +00:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
|
|
|
assert.Check(t, !loadedConfig.TLS)
|
2016-01-19 19:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) {
|
2017-08-23 21:25:00 +00:00
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{"log-level": "warn"}`))
|
2016-06-22 22:36:51 +00:00
|
|
|
defer tempFile.Remove()
|
|
|
|
|
2018-10-15 07:52:53 +00:00
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
2016-06-22 22:36:51 +00:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
|
|
|
assert.Check(t, is.Equal("warn", loadedConfig.LogLevel))
|
2016-01-19 19:16:07 +00:00
|
|
|
}
|
2016-01-20 22:16:49 +00:00
|
|
|
|
2016-01-22 18:14:48 +00:00
|
|
|
func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) {
|
2016-06-22 22:36:51 +00:00
|
|
|
content := `{"tlscacert": "/etc/certs/ca.pem", "log-driver": "syslog"}`
|
2017-08-23 21:25:00 +00:00
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(content))
|
2016-06-22 22:36:51 +00:00
|
|
|
defer tempFile.Remove()
|
|
|
|
|
2018-10-15 07:52:53 +00:00
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
2016-06-22 22:36:51 +00:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 19:28:34 +00: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 22:16:49 +00:00
|
|
|
}
|
2016-03-08 21:03:37 +00:00
|
|
|
|
|
|
|
func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) {
|
2016-06-22 22:36:51 +00:00
|
|
|
content := `{
|
2017-05-09 21:00:31 +00:00
|
|
|
"allow-nondistributable-artifacts": ["allow-nondistributable-artifacts.com"],
|
2016-06-22 22:36:51 +00:00
|
|
|
"registry-mirrors": ["https://mirrors.docker.com"],
|
2016-06-23 15:25:51 +00:00
|
|
|
"insecure-registries": ["https://insecure.docker.com"]
|
2016-06-22 22:36:51 +00:00
|
|
|
}`
|
2017-08-23 21:25:00 +00:00
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(content))
|
2016-06-22 22:36:51 +00:00
|
|
|
defer tempFile.Remove()
|
|
|
|
|
2018-10-15 07:52:53 +00:00
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
2016-06-22 22:36:51 +00:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
2016-06-22 22:36:51 +00:00
|
|
|
|
2018-03-13 19:28:34 +00: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 21:03:37 +00:00
|
|
|
}
|
2018-11-24 13:39:08 +00: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()))
|
|
|
|
}
|