123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- package main
- import (
- "testing"
- "github.com/containerd/log"
- "github.com/docker/docker/daemon/config"
- "github.com/google/go-cmp/cmp/cmpopts"
- "github.com/spf13/pflag"
- "gotest.tools/v3/assert"
- is "gotest.tools/v3/assert/cmp"
- "gotest.tools/v3/fs"
- )
- func defaultOptions(t *testing.T, configFile string) *daemonOptions {
- cfg, err := config.New()
- assert.NilError(t, err)
- opts := newDaemonOptions(cfg)
- opts.flags = &pflag.FlagSet{}
- opts.installFlags(opts.flags)
- err = installConfigFlags(opts.daemonConfig, opts.flags)
- assert.NilError(t, err)
- defaultDaemonConfigFile, err := getDefaultDaemonConfigFile()
- assert.NilError(t, err)
- opts.flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "")
- opts.configFile = configFile
- err = opts.flags.Parse([]string{})
- assert.NilError(t, err)
- return opts
- }
- func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) {
- opts := defaultOptions(t, "")
- opts.Debug = true
- loadedConfig, err := loadDaemonCliConfig(opts)
- assert.NilError(t, err)
- assert.Assert(t, loadedConfig != nil)
- if !loadedConfig.Debug {
- t.Fatalf("expected debug to be copied from the common flags, got false")
- }
- }
- func TestLoadDaemonCliConfigWithTLS(t *testing.T) {
- opts := defaultOptions(t, "")
- opts.TLSOptions.CAFile = "/tmp/ca.pem"
- opts.TLS = true
- loadedConfig, err := loadDaemonCliConfig(opts)
- assert.NilError(t, err)
- assert.Assert(t, loadedConfig != nil)
- assert.Check(t, is.Equal("/tmp/ca.pem", loadedConfig.TLSOptions.CAFile))
- }
- func TestLoadDaemonCliConfigWithConflicts(t *testing.T) {
- tempFile := fs.NewFile(t, "config", fs.WithContent(`{"labels": ["l3=foo"]}`))
- defer tempFile.Remove()
- configFile := tempFile.Path()
- opts := defaultOptions(t, configFile)
- flags := opts.flags
- assert.Check(t, flags.Set("config-file", configFile))
- assert.Check(t, flags.Set("label", "l1=bar"))
- assert.Check(t, flags.Set("label", "l2=baz"))
- _, err := loadDaemonCliConfig(opts)
- assert.Check(t, is.ErrorContains(err, "as a flag and in the configuration file: labels"))
- }
- 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(t, configFile)
- flags := opts.flags
- 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"))
- _, err := loadDaemonCliConfig(opts)
- assert.Check(t, is.ErrorContains(err, "as a flag and in the configuration file: node-generic-resources"))
- }
- func TestLoadDaemonCliWithConflictingLabels(t *testing.T) {
- opts := defaultOptions(t, "")
- flags := opts.flags
- assert.Check(t, flags.Set("label", "foo=bar"))
- assert.Check(t, flags.Set("label", "foo=baz"))
- _, err := loadDaemonCliConfig(opts)
- assert.Check(t, is.Error(err, "conflict labels for foo=baz and foo=bar"))
- }
- func TestLoadDaemonCliWithDuplicateLabels(t *testing.T) {
- opts := defaultOptions(t, "")
- flags := opts.flags
- assert.Check(t, flags.Set("label", "foo=the-same"))
- assert.Check(t, flags.Set("label", "foo=the-same"))
- _, err := loadDaemonCliConfig(opts)
- assert.Check(t, err)
- }
- func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) {
- tempFile := fs.NewFile(t, "config", fs.WithContent(`{"tlsverify": true}`))
- defer tempFile.Remove()
- opts := defaultOptions(t, tempFile.Path())
- opts.TLSOptions.CAFile = "/tmp/ca.pem"
- loadedConfig, err := loadDaemonCliConfig(opts)
- assert.NilError(t, err)
- assert.Assert(t, loadedConfig != nil)
- assert.Check(t, is.Equal(*loadedConfig.TLS, true))
- }
- func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) {
- tempFile := fs.NewFile(t, "config", fs.WithContent(`{"tlsverify": false}`))
- defer tempFile.Remove()
- opts := defaultOptions(t, tempFile.Path())
- opts.TLSOptions.CAFile = "/tmp/ca.pem"
- loadedConfig, err := loadDaemonCliConfig(opts)
- assert.NilError(t, err)
- assert.Assert(t, loadedConfig != nil)
- assert.Check(t, *loadedConfig.TLS)
- }
- func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) {
- tempFile := fs.NewFile(t, "config", fs.WithContent(`{}`))
- defer tempFile.Remove()
- opts := defaultOptions(t, tempFile.Path())
- opts.TLSOptions.CAFile = "/tmp/ca.pem"
- loadedConfig, err := loadDaemonCliConfig(opts)
- assert.NilError(t, err)
- assert.Assert(t, loadedConfig != nil)
- assert.Check(t, loadedConfig.TLS == nil)
- }
- func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) {
- tempFile := fs.NewFile(t, "config", fs.WithContent(`{"log-level": "warn"}`))
- defer tempFile.Remove()
- opts := defaultOptions(t, tempFile.Path())
- loadedConfig, err := loadDaemonCliConfig(opts)
- assert.NilError(t, err)
- assert.Assert(t, loadedConfig != nil)
- assert.Check(t, is.Equal("warn", loadedConfig.LogLevel))
- }
- 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)
- assert.Check(t, is.Equal(log.JSONFormat, loadedConfig.LogFormat))
- }
- 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"))
- }
- func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) {
- content := `{"tlscacert": "/etc/certs/ca.pem", "log-driver": "syslog"}`
- tempFile := fs.NewFile(t, "config", fs.WithContent(content))
- defer tempFile.Remove()
- opts := defaultOptions(t, tempFile.Path())
- loadedConfig, err := loadDaemonCliConfig(opts)
- assert.NilError(t, err)
- assert.Assert(t, loadedConfig != nil)
- assert.Check(t, is.Equal("/etc/certs/ca.pem", loadedConfig.TLSOptions.CAFile))
- assert.Check(t, is.Equal("syslog", loadedConfig.LogConfig.Type))
- }
- func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) {
- content := `{
- "allow-nondistributable-artifacts": ["allow-nondistributable-artifacts.example.com"],
- "registry-mirrors": ["https://mirrors.example.com"],
- "insecure-registries": ["https://insecure-registry.example.com"]
- }`
- tempFile := fs.NewFile(t, "config", fs.WithContent(content))
- defer tempFile.Remove()
- opts := defaultOptions(t, tempFile.Path())
- loadedConfig, err := loadDaemonCliConfig(opts)
- assert.NilError(t, err)
- assert.Assert(t, loadedConfig != nil)
- assert.Check(t, is.Len(loadedConfig.AllowNondistributableArtifacts, 1))
- assert.Check(t, is.Len(loadedConfig.Mirrors, 1))
- assert.Check(t, is.Len(loadedConfig.InsecureRegistries, 1))
- }
- func TestConfigureDaemonLogs(t *testing.T) {
- conf := &config.Config{}
- configureDaemonLogs(conf)
- assert.Check(t, is.Equal(log.InfoLevel, log.GetLevel()))
- // log level should not be changed when passing an invalid value
- conf.LogLevel = "foobar"
- configureDaemonLogs(conf)
- assert.Check(t, is.Equal(log.InfoLevel, log.GetLevel()))
- conf.LogLevel = "warn"
- configureDaemonLogs(conf)
- assert.Check(t, is.Equal(log.WarnLevel, log.GetLevel()))
- }
- func TestCDISpecDirs(t *testing.T) {
- testCases := []struct {
- description string
- configContent string
- experimental bool
- specDirs []string
- expectedCDISpecDirs []string
- }{
- {
- description: "experimental and no spec dirs specified returns default",
- specDirs: nil,
- experimental: true,
- expectedCDISpecDirs: []string{"/etc/cdi", "/var/run/cdi"},
- },
- {
- description: "experimental and specified spec dirs are returned",
- specDirs: []string{"/foo/bar", "/baz/qux"},
- experimental: true,
- expectedCDISpecDirs: []string{"/foo/bar", "/baz/qux"},
- },
- {
- description: "experimental and empty string as spec dir returns empty slice",
- specDirs: []string{""},
- experimental: true,
- expectedCDISpecDirs: []string{},
- },
- {
- description: "experimental and empty config option returns empty slice",
- configContent: `{"cdi-spec-dirs": []}`,
- experimental: true,
- expectedCDISpecDirs: []string{},
- },
- {
- description: "non-experimental and no spec dirs specified returns no cdi spec dirs",
- specDirs: nil,
- experimental: false,
- expectedCDISpecDirs: nil,
- },
- {
- description: "non-experimental and specified spec dirs returns no cdi spec dirs",
- specDirs: []string{"/foo/bar", "/baz/qux"},
- experimental: false,
- 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))
- }
- if tc.experimental {
- assert.Check(t, flags.Set("experimental", "true"))
- }
- loadedConfig, err := loadDaemonCliConfig(opts)
- assert.NilError(t, err)
- assert.Check(t, is.DeepEqual(tc.expectedCDISpecDirs, loadedConfig.CDISpecDirs, cmpopts.EquateEmpty()))
- })
- }
- }
|