2015-12-10 18:35:10 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-01-19 14:16:07 -05:00
|
|
|
"github.com/Sirupsen/logrus"
|
2017-01-23 06:23:07 -05:00
|
|
|
"github.com/docker/docker/daemon/config"
|
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
|
|
|
"github.com/docker/docker/pkg/testutil"
|
2016-06-22 18:36:51 -04:00
|
|
|
"github.com/docker/docker/pkg/testutil/tempfile"
|
|
|
|
"github.com/spf13/pflag"
|
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
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
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)
|
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
|
|
|
require.NoError(t, err)
|
|
|
|
require.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("")
|
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)
|
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
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, loadedConfig)
|
|
|
|
assert.Equal(t, "/tmp/ca.pem", loadedConfig.CommonTLSOptions.CAFile)
|
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
|
|
|
|
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
|
|
|
assert.NoError(t, flags.Set("config-file", configFile))
|
|
|
|
assert.NoError(t, flags.Set("label", "l1=bar"))
|
|
|
|
assert.NoError(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
|
|
|
|
|
|
|
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())
|
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)
|
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
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, loadedConfig)
|
2016-06-22 18:36:51 -04:00
|
|
|
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())
|
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)
|
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
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, loadedConfig)
|
|
|
|
assert.True(t, loadedConfig.TLS)
|
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())
|
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)
|
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
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, loadedConfig)
|
|
|
|
assert.False(t, loadedConfig.TLS)
|
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)
|
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
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, loadedConfig)
|
|
|
|
assert.Equal(t, "warn", loadedConfig.LogLevel)
|
|
|
|
assert.Equal(t, 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"}`
|
|
|
|
tempFile := tempfile.NewTempFile(t, "config", content)
|
|
|
|
defer tempFile.Remove()
|
|
|
|
|
|
|
|
opts := defaultOptions(tempFile.Name())
|
|
|
|
loadedConfig, 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
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, loadedConfig)
|
|
|
|
assert.Equal(t, "/etc/certs/ca.pem", loadedConfig.CommonTLSOptions.CAFile)
|
|
|
|
assert.Equal(t, "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
|
|
|
}`
|
|
|
|
tempFile := tempfile.NewTempFile(t, "config", content)
|
|
|
|
defer tempFile.Remove()
|
|
|
|
|
|
|
|
opts := defaultOptions(tempFile.Name())
|
|
|
|
loadedConfig, 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
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, loadedConfig)
|
2016-06-22 18:36:51 -04:00
|
|
|
|
2017-05-09 17:00:31 -04:00
|
|
|
assert.Len(t, loadedConfig.AllowNondistributableArtifacts, 1)
|
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
|
|
|
assert.Len(t, loadedConfig.Mirrors, 1)
|
|
|
|
assert.Len(t, loadedConfig.InsecureRegistries, 1)
|
2016-03-08 16:03:37 -05:00
|
|
|
}
|