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-08-23 17:35:09 -04:00
|
|
|
"github.com/docker/docker/internal/testutil"
|
2018-03-13 15:28:34 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-08-23 17:25:00 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/fs"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-06-22 18:36:51 -04:00
|
|
|
"github.com/spf13/pflag"
|
2015-12-10 18:35:10 -05:00
|
|
|
)
|
|
|
|
|
2017-06-01 16:34:31 -04:00
|
|
|
func defaultOptions(configFile string) *daemonOptions {
|
|
|
|
opts := newDaemonOptions(&config.Config{})
|
|
|
|
opts.flags = &pflag.FlagSet{}
|
|
|
|
opts.InstallFlags(opts.flags)
|
2017-01-23 06:23:07 -05:00
|
|
|
installConfigFlags(opts.daemonConfig, opts.flags)
|
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) {
|
2016-06-22 18:36:51 -04:00
|
|
|
opts := defaultOptions("")
|
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) {
|
2016-06-22 18:36:51 -04:00
|
|
|
opts := defaultOptions("")
|
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
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
opts := defaultOptions(configFile)
|
|
|
|
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)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
testutil.ErrorContains(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
|
|
|
|
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()
|
|
|
|
|
|
|
|
opts := defaultOptions(configFile)
|
|
|
|
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)
|
|
|
|
testutil.ErrorContains(t, err, "as a flag and in the configuration file: node-generic-resources")
|
|
|
|
}
|
|
|
|
|
2017-11-11 21:09:28 -05:00
|
|
|
func TestLoadDaemonCliWithConflictingLabels(t *testing.T) {
|
|
|
|
opts := defaultOptions("")
|
|
|
|
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) {
|
|
|
|
opts := defaultOptions("")
|
|
|
|
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
|
|
|
|
2017-08-23 17:25:00 -04:00
|
|
|
opts := defaultOptions(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)
|
|
|
|
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
|
|
|
|
2017-08-23 17:25:00 -04:00
|
|
|
opts := defaultOptions(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)
|
|
|
|
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
|
|
|
|
2017-08-23 17:25:00 -04:00
|
|
|
opts := defaultOptions(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)
|
|
|
|
assert.Check(t, !loadedConfig.TLS)
|
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()
|
|
|
|
|
2017-08-23 17:25:00 -04:00
|
|
|
opts := defaultOptions(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))
|
|
|
|
assert.Check(t, is.Equal(logrus.WarnLevel, logrus.GetLevel()))
|
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()
|
|
|
|
|
2017-08-23 17:25:00 -04:00
|
|
|
opts := defaultOptions(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 := `{
|
2017-05-09 17:00:31 -04:00
|
|
|
"allow-nondistributable-artifacts": ["allow-nondistributable-artifacts.com"],
|
2016-06-22 18:36:51 -04:00
|
|
|
"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
|
|
|
}`
|
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()
|
|
|
|
|
2017-08-23 17:25:00 -04:00
|
|
|
opts := defaultOptions(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
|
|
|
}
|