github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/cmd/dockerd/daemon_test.go (about)

     1  package main
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/docker/daemon/config"
     7  	"github.com/sirupsen/logrus"
     8  	"github.com/spf13/pflag"
     9  	"gotest.tools/v3/assert"
    10  	is "gotest.tools/v3/assert/cmp"
    11  	"gotest.tools/v3/fs"
    12  )
    13  
    14  func defaultOptions(t *testing.T, configFile string) *daemonOptions {
    15  	opts := newDaemonOptions(&config.Config{})
    16  	opts.flags = &pflag.FlagSet{}
    17  	opts.InstallFlags(opts.flags)
    18  	if err := installConfigFlags(opts.daemonConfig, opts.flags); err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	defaultDaemonConfigFile, err := getDefaultDaemonConfigFile()
    22  	assert.NilError(t, err)
    23  	opts.flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "")
    24  	opts.configFile = configFile
    25  	return opts
    26  }
    27  
    28  func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) {
    29  	opts := defaultOptions(t, "")
    30  	opts.Debug = true
    31  
    32  	loadedConfig, err := loadDaemonCliConfig(opts)
    33  	assert.NilError(t, err)
    34  	assert.Assert(t, loadedConfig != nil)
    35  	if !loadedConfig.Debug {
    36  		t.Fatalf("expected debug to be copied from the common flags, got false")
    37  	}
    38  }
    39  
    40  func TestLoadDaemonCliConfigWithTLS(t *testing.T) {
    41  	opts := defaultOptions(t, "")
    42  	opts.TLSOptions.CAFile = "/tmp/ca.pem"
    43  	opts.TLS = true
    44  
    45  	loadedConfig, err := loadDaemonCliConfig(opts)
    46  	assert.NilError(t, err)
    47  	assert.Assert(t, loadedConfig != nil)
    48  	assert.Check(t, is.Equal("/tmp/ca.pem", loadedConfig.CommonTLSOptions.CAFile))
    49  }
    50  
    51  func TestLoadDaemonCliConfigWithConflicts(t *testing.T) {
    52  	tempFile := fs.NewFile(t, "config", fs.WithContent(`{"labels": ["l3=foo"]}`))
    53  	defer tempFile.Remove()
    54  	configFile := tempFile.Path()
    55  
    56  	opts := defaultOptions(t, configFile)
    57  	flags := opts.flags
    58  
    59  	assert.Check(t, flags.Set("config-file", configFile))
    60  	assert.Check(t, flags.Set("label", "l1=bar"))
    61  	assert.Check(t, flags.Set("label", "l2=baz"))
    62  
    63  	_, err := loadDaemonCliConfig(opts)
    64  	assert.Check(t, is.ErrorContains(err, "as a flag and in the configuration file: labels"))
    65  }
    66  
    67  func TestLoadDaemonCliWithConflictingNodeGenericResources(t *testing.T) {
    68  	tempFile := fs.NewFile(t, "config", fs.WithContent(`{"node-generic-resources": ["foo=bar", "bar=baz"]}`))
    69  	defer tempFile.Remove()
    70  	configFile := tempFile.Path()
    71  
    72  	opts := defaultOptions(t, configFile)
    73  	flags := opts.flags
    74  
    75  	assert.Check(t, flags.Set("config-file", configFile))
    76  	assert.Check(t, flags.Set("node-generic-resource", "r1=bar"))
    77  	assert.Check(t, flags.Set("node-generic-resource", "r2=baz"))
    78  
    79  	_, err := loadDaemonCliConfig(opts)
    80  	assert.Check(t, is.ErrorContains(err, "as a flag and in the configuration file: node-generic-resources"))
    81  }
    82  
    83  func TestLoadDaemonCliWithConflictingLabels(t *testing.T) {
    84  	opts := defaultOptions(t, "")
    85  	flags := opts.flags
    86  
    87  	assert.Check(t, flags.Set("label", "foo=bar"))
    88  	assert.Check(t, flags.Set("label", "foo=baz"))
    89  
    90  	_, err := loadDaemonCliConfig(opts)
    91  	assert.Check(t, is.Error(err, "conflict labels for foo=baz and foo=bar"))
    92  }
    93  
    94  func TestLoadDaemonCliWithDuplicateLabels(t *testing.T) {
    95  	opts := defaultOptions(t, "")
    96  	flags := opts.flags
    97  
    98  	assert.Check(t, flags.Set("label", "foo=the-same"))
    99  	assert.Check(t, flags.Set("label", "foo=the-same"))
   100  
   101  	_, err := loadDaemonCliConfig(opts)
   102  	assert.Check(t, err)
   103  }
   104  
   105  func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) {
   106  	tempFile := fs.NewFile(t, "config", fs.WithContent(`{"tlsverify": true}`))
   107  	defer tempFile.Remove()
   108  
   109  	opts := defaultOptions(t, tempFile.Path())
   110  	opts.TLSOptions.CAFile = "/tmp/ca.pem"
   111  
   112  	loadedConfig, err := loadDaemonCliConfig(opts)
   113  	assert.NilError(t, err)
   114  	assert.Assert(t, loadedConfig != nil)
   115  	assert.Check(t, is.Equal(loadedConfig.TLS, true))
   116  }
   117  
   118  func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) {
   119  	tempFile := fs.NewFile(t, "config", fs.WithContent(`{"tlsverify": false}`))
   120  	defer tempFile.Remove()
   121  
   122  	opts := defaultOptions(t, tempFile.Path())
   123  	opts.TLSOptions.CAFile = "/tmp/ca.pem"
   124  
   125  	loadedConfig, err := loadDaemonCliConfig(opts)
   126  	assert.NilError(t, err)
   127  	assert.Assert(t, loadedConfig != nil)
   128  	assert.Check(t, loadedConfig.TLS)
   129  }
   130  
   131  func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) {
   132  	tempFile := fs.NewFile(t, "config", fs.WithContent(`{}`))
   133  	defer tempFile.Remove()
   134  
   135  	opts := defaultOptions(t, tempFile.Path())
   136  	opts.TLSOptions.CAFile = "/tmp/ca.pem"
   137  
   138  	loadedConfig, err := loadDaemonCliConfig(opts)
   139  	assert.NilError(t, err)
   140  	assert.Assert(t, loadedConfig != nil)
   141  	assert.Check(t, !loadedConfig.TLS)
   142  }
   143  
   144  func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) {
   145  	tempFile := fs.NewFile(t, "config", fs.WithContent(`{"log-level": "warn"}`))
   146  	defer tempFile.Remove()
   147  
   148  	opts := defaultOptions(t, tempFile.Path())
   149  	loadedConfig, err := loadDaemonCliConfig(opts)
   150  	assert.NilError(t, err)
   151  	assert.Assert(t, loadedConfig != nil)
   152  	assert.Check(t, is.Equal("warn", loadedConfig.LogLevel))
   153  }
   154  
   155  func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) {
   156  	content := `{"tlscacert": "/etc/certs/ca.pem", "log-driver": "syslog"}`
   157  	tempFile := fs.NewFile(t, "config", fs.WithContent(content))
   158  	defer tempFile.Remove()
   159  
   160  	opts := defaultOptions(t, tempFile.Path())
   161  	loadedConfig, err := loadDaemonCliConfig(opts)
   162  	assert.NilError(t, err)
   163  	assert.Assert(t, loadedConfig != nil)
   164  	assert.Check(t, is.Equal("/etc/certs/ca.pem", loadedConfig.CommonTLSOptions.CAFile))
   165  	assert.Check(t, is.Equal("syslog", loadedConfig.LogConfig.Type))
   166  }
   167  
   168  func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) {
   169  	content := `{
   170  		"allow-nondistributable-artifacts": ["allow-nondistributable-artifacts.com"],
   171  		"registry-mirrors": ["https://mirrors.docker.com"],
   172  		"insecure-registries": ["https://insecure.docker.com"]
   173  	}`
   174  	tempFile := fs.NewFile(t, "config", fs.WithContent(content))
   175  	defer tempFile.Remove()
   176  
   177  	opts := defaultOptions(t, tempFile.Path())
   178  	loadedConfig, err := loadDaemonCliConfig(opts)
   179  	assert.NilError(t, err)
   180  	assert.Assert(t, loadedConfig != nil)
   181  
   182  	assert.Check(t, is.Len(loadedConfig.AllowNondistributableArtifacts, 1))
   183  	assert.Check(t, is.Len(loadedConfig.Mirrors, 1))
   184  	assert.Check(t, is.Len(loadedConfig.InsecureRegistries, 1))
   185  }
   186  
   187  func TestConfigureDaemonLogs(t *testing.T) {
   188  	conf := &config.Config{}
   189  	err := configureDaemonLogs(conf)
   190  	assert.NilError(t, err)
   191  	assert.Check(t, is.Equal(logrus.InfoLevel, logrus.GetLevel()))
   192  
   193  	conf.LogLevel = "warn"
   194  	err = configureDaemonLogs(conf)
   195  	assert.NilError(t, err)
   196  	assert.Check(t, is.Equal(logrus.WarnLevel, logrus.GetLevel()))
   197  
   198  	conf.LogLevel = "foobar"
   199  	err = configureDaemonLogs(conf)
   200  	assert.Error(t, err, "unable to parse logging level: foobar")
   201  
   202  	// log level should not be changed after a failure
   203  	assert.Check(t, is.Equal(logrus.WarnLevel, logrus.GetLevel()))
   204  }