2015-12-10 23:35:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2017-01-23 11:23:07 +00:00
|
|
|
"github.com/docker/docker/daemon/config"
|
2023-07-25 11:49:42 +00:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2016-06-22 22:36:51 +00:00
|
|
|
"github.com/spf13/pflag"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/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 {
|
2022-08-17 12:07:03 +00:00
|
|
|
cfg, err := config.New()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
opts := newDaemonOptions(cfg)
|
2017-06-01 20:34:31 +00:00
|
|
|
opts.flags = &pflag.FlagSet{}
|
2022-04-22 14:49:26 +00:00
|
|
|
opts.installFlags(opts.flags)
|
2022-08-17 12:07:03 +00:00
|
|
|
err = installConfigFlags(opts.daemonConfig, opts.flags)
|
|
|
|
assert.NilError(t, err)
|
2018-10-15 07:52:53 +00:00
|
|
|
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
|
2022-06-06 10:17:47 +00:00
|
|
|
err = opts.flags.Parse([]string{})
|
|
|
|
assert.NilError(t, err)
|
2016-06-22 22:36:51 +00:00
|
|
|
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)
|
2022-12-15 09:26:50 +00:00
|
|
|
assert.Check(t, is.Equal("/tmp/ca.pem", loadedConfig.TLSOptions.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)
|
2020-07-28 23:01:08 +00:00
|
|
|
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)
|
2020-07-28 23:01:08 +00:00
|
|
|
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)
|
2020-07-28 23:01:08 +00:00
|
|
|
assert.Check(t, loadedConfig.TLS == nil)
|
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
|
|
|
|
2023-06-13 21:54:14 +00:00
|
|
|
func TestLoadDaemonCliConfigWithLogFormat(t *testing.T) {
|
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{"log-format": "json"}`))
|
|
|
|
defer tempFile.Remove()
|
|
|
|
|
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, loadedConfig != nil)
|
2023-08-30 20:11:24 +00:00
|
|
|
assert.Check(t, is.Equal(log.JSONFormat, loadedConfig.LogFormat))
|
2023-06-13 21:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadDaemonCliConfigWithInvalidLogFormat(t *testing.T) {
|
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(`{"log-format": "foo"}`))
|
|
|
|
defer tempFile.Remove()
|
|
|
|
|
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
|
|
|
_, err := loadDaemonCliConfig(opts)
|
|
|
|
assert.Check(t, is.ErrorContains(err, "invalid log format: foo"))
|
|
|
|
}
|
|
|
|
|
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)
|
2022-12-15 09:26:50 +00:00
|
|
|
assert.Check(t, is.Equal("/etc/certs/ca.pem", loadedConfig.TLSOptions.CAFile))
|
2018-03-13 19:28:34 +00:00
|
|
|
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 := `{
|
2021-04-02 12:06:27 +00: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 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{}
|
2022-04-22 13:59:23 +00:00
|
|
|
configureDaemonLogs(conf)
|
2023-07-30 15:18:56 +00:00
|
|
|
assert.Check(t, is.Equal(log.InfoLevel, log.GetLevel()))
|
2018-11-24 13:39:08 +00:00
|
|
|
|
2022-04-22 13:59:23 +00:00
|
|
|
// log level should not be changed when passing an invalid value
|
2018-11-24 13:39:08 +00:00
|
|
|
conf.LogLevel = "foobar"
|
2022-04-22 13:59:23 +00:00
|
|
|
configureDaemonLogs(conf)
|
2023-07-30 15:18:56 +00:00
|
|
|
assert.Check(t, is.Equal(log.InfoLevel, log.GetLevel()))
|
|
|
|
|
|
|
|
conf.LogLevel = "warn"
|
|
|
|
configureDaemonLogs(conf)
|
2023-09-15 11:51:51 +00:00
|
|
|
assert.Check(t, is.Equal(log.WarnLevel, log.GetLevel()))
|
2018-11-24 13:39:08 +00:00
|
|
|
}
|
2023-07-25 11:49:42 +00:00
|
|
|
|
|
|
|
func TestCDISpecDirs(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
description string
|
|
|
|
configContent string
|
|
|
|
specDirs []string
|
|
|
|
expectedCDISpecDirs []string
|
|
|
|
}{
|
|
|
|
{
|
2024-01-16 20:12:09 +00:00
|
|
|
description: "CDI enabled and no spec dirs specified returns default",
|
2023-07-25 11:49:42 +00:00
|
|
|
specDirs: nil,
|
2024-01-16 20:12:09 +00:00
|
|
|
configContent: `{"features": {"cdi": true}}`,
|
2023-07-25 11:49:42 +00:00
|
|
|
expectedCDISpecDirs: []string{"/etc/cdi", "/var/run/cdi"},
|
|
|
|
},
|
|
|
|
{
|
2024-01-16 20:12:09 +00:00
|
|
|
description: "CDI enabled and specified spec dirs are returned",
|
2023-07-25 11:49:42 +00:00
|
|
|
specDirs: []string{"/foo/bar", "/baz/qux"},
|
2024-01-16 20:12:09 +00:00
|
|
|
configContent: `{"features": {"cdi": true}}`,
|
2023-07-25 11:49:42 +00:00
|
|
|
expectedCDISpecDirs: []string{"/foo/bar", "/baz/qux"},
|
|
|
|
},
|
|
|
|
{
|
2024-01-16 20:12:09 +00:00
|
|
|
description: "CDI enabled and empty string as spec dir returns empty slice",
|
2023-07-25 11:49:42 +00:00
|
|
|
specDirs: []string{""},
|
2024-01-16 20:12:09 +00:00
|
|
|
configContent: `{"features": {"cdi": true}}`,
|
2023-07-25 11:49:42 +00:00
|
|
|
expectedCDISpecDirs: []string{},
|
|
|
|
},
|
|
|
|
{
|
2024-01-16 20:12:09 +00:00
|
|
|
description: "CDI enabled and empty config option returns empty slice",
|
|
|
|
configContent: `{"cdi-spec-dirs": [], "features": {"cdi": true}}`,
|
2023-07-25 11:49:42 +00:00
|
|
|
expectedCDISpecDirs: []string{},
|
|
|
|
},
|
|
|
|
{
|
2024-01-16 20:12:09 +00:00
|
|
|
description: "CDI disabled and no spec dirs specified returns no cdi spec dirs",
|
2023-07-25 11:49:42 +00:00
|
|
|
specDirs: nil,
|
|
|
|
expectedCDISpecDirs: nil,
|
|
|
|
},
|
|
|
|
{
|
2024-01-16 20:12:09 +00:00
|
|
|
description: "CDI disabled and specified spec dirs returns no cdi spec dirs",
|
2023-07-25 11:49:42 +00:00
|
|
|
specDirs: []string{"/foo/bar", "/baz/qux"},
|
|
|
|
expectedCDISpecDirs: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
|
|
tempFile := fs.NewFile(t, "config", fs.WithContent(tc.configContent))
|
|
|
|
defer tempFile.Remove()
|
|
|
|
|
|
|
|
opts := defaultOptions(t, tempFile.Path())
|
|
|
|
|
|
|
|
flags := opts.flags
|
|
|
|
for _, specDir := range tc.specDirs {
|
|
|
|
assert.Check(t, flags.Set("cdi-spec-dir", specDir))
|
|
|
|
}
|
|
|
|
|
|
|
|
loadedConfig, err := loadDaemonCliConfig(opts)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
assert.Check(t, is.DeepEqual(tc.expectedCDISpecDirs, loadedConfig.CDISpecDirs, cmpopts.EquateEmpty()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|