2015-12-10 18:35:10 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-01-19 14:16:07 -05:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-04-21 17:51:28 -04:00
|
|
|
cliflags "github.com/docker/docker/cli/flags"
|
2017-01-23 06:23:07 -05:00
|
|
|
"github.com/docker/docker/daemon/config"
|
2016-06-22 18:36:51 -04:00
|
|
|
"github.com/docker/docker/pkg/testutil/assert"
|
|
|
|
"github.com/docker/docker/pkg/testutil/tempfile"
|
|
|
|
"github.com/spf13/pflag"
|
2015-12-10 18:35:10 -05:00
|
|
|
)
|
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
func defaultOptions(configFile string) daemonOptions {
|
|
|
|
opts := daemonOptions{
|
2017-01-23 06:23:07 -05:00
|
|
|
daemonConfig: &config.Config{},
|
2016-06-22 18:36:51 -04:00
|
|
|
flags: &pflag.FlagSet{},
|
|
|
|
common: cliflags.NewCommonOptions(),
|
|
|
|
}
|
|
|
|
opts.common.InstallFlags(opts.flags)
|
2017-01-23 06:23:07 -05:00
|
|
|
installConfigFlags(opts.daemonConfig, opts.flags)
|
2016-06-22 18:36:51 -04:00
|
|
|
opts.flags.StringVar(&opts.configFile, flagDaemonConfigFile, defaultDaemonConfigFile, "")
|
|
|
|
opts.configFile = configFile
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) {
|
2016-06-22 18:36:51 -04:00
|
|
|
opts := defaultOptions("")
|
|
|
|
opts.common.Debug = true
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NotNil(t, loadedConfig)
|
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) {
|
2016-06-22 18:36:51 -04:00
|
|
|
opts := defaultOptions("")
|
|
|
|
opts.common.TLSOptions.CAFile = "/tmp/ca.pem"
|
|
|
|
opts.common.TLS = true
|
|
|
|
|
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NotNil(t, loadedConfig)
|
|
|
|
assert.Equal(t, loadedConfig.CommonTLSOptions.CAFile, "/tmp/ca.pem")
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithConflicts(t *testing.T) {
|
2016-06-22 18:36:51 -04:00
|
|
|
tempFile := tempfile.NewTempFile(t, "config", `{"labels": ["l3=foo"]}`)
|
|
|
|
defer tempFile.Remove()
|
|
|
|
configFile := tempFile.Name()
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
opts := defaultOptions(configFile)
|
|
|
|
flags := opts.flags
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
assert.NilError(t, flags.Set(flagDaemonConfigFile, configFile))
|
|
|
|
assert.NilError(t, flags.Set("label", "l1=bar"))
|
|
|
|
assert.NilError(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)
|
|
|
|
assert.Error(t, 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
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) {
|
2016-06-22 18:36:51 -04:00
|
|
|
tempFile := tempfile.NewTempFile(t, "config", `{"tlsverify": true}`)
|
|
|
|
defer tempFile.Remove()
|
2016-01-19 14:16:07 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
opts := defaultOptions(tempFile.Name())
|
|
|
|
opts.common.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)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NotNil(t, loadedConfig)
|
|
|
|
assert.Equal(t, loadedConfig.TLS, true)
|
2016-01-19 14:16:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) {
|
2016-06-22 18:36:51 -04:00
|
|
|
tempFile := tempfile.NewTempFile(t, "config", `{"tlsverify": false}`)
|
|
|
|
defer tempFile.Remove()
|
2016-01-19 14:16:07 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
opts := defaultOptions(tempFile.Name())
|
|
|
|
opts.common.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)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NotNil(t, loadedConfig)
|
|
|
|
assert.Equal(t, loadedConfig.TLS, true)
|
2016-01-19 14:16:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) {
|
2016-06-22 18:36:51 -04:00
|
|
|
tempFile := tempfile.NewTempFile(t, "config", `{}`)
|
|
|
|
defer tempFile.Remove()
|
2016-03-08 16:03:37 -05:00
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
opts := defaultOptions(tempFile.Name())
|
|
|
|
opts.common.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)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NotNil(t, loadedConfig)
|
|
|
|
assert.Equal(t, loadedConfig.TLS, false)
|
2016-01-19 14:16:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) {
|
2016-06-22 18:36:51 -04:00
|
|
|
tempFile := tempfile.NewTempFile(t, "config", `{"log-level": "warn"}`)
|
|
|
|
defer tempFile.Remove()
|
|
|
|
|
|
|
|
opts := defaultOptions(tempFile.Name())
|
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NotNil(t, loadedConfig)
|
|
|
|
assert.Equal(t, loadedConfig.LogLevel, "warn")
|
|
|
|
assert.Equal(t, logrus.GetLevel(), logrus.WarnLevel)
|
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"}`
|
|
|
|
tempFile := tempfile.NewTempFile(t, "config", content)
|
|
|
|
defer tempFile.Remove()
|
|
|
|
|
|
|
|
opts := defaultOptions(tempFile.Name())
|
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NotNil(t, loadedConfig)
|
|
|
|
assert.Equal(t, loadedConfig.CommonTLSOptions.CAFile, "/etc/certs/ca.pem")
|
|
|
|
assert.Equal(t, loadedConfig.LogConfig.Type, "syslog")
|
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 := `{
|
|
|
|
"registry-mirrors": ["https://mirrors.docker.com"],
|
2016-06-23 11:25:51 -04:00
|
|
|
"insecure-registries": ["https://insecure.docker.com"]
|
2016-06-22 18:36:51 -04:00
|
|
|
}`
|
|
|
|
tempFile := tempfile.NewTempFile(t, "config", content)
|
|
|
|
defer tempFile.Remove()
|
|
|
|
|
|
|
|
opts := defaultOptions(tempFile.Name())
|
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NotNil(t, loadedConfig)
|
|
|
|
|
|
|
|
assert.Equal(t, len(loadedConfig.Mirrors), 1)
|
|
|
|
assert.Equal(t, len(loadedConfig.InsecureRegistries), 1)
|
2016-03-08 16:03:37 -05:00
|
|
|
}
|