github.com/ssdev-go/moby@v17.12.1-ce-rc2+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/docker/docker/internal/testutil"
     8  	"github.com/gotestyourself/gotestyourself/fs"
     9  	"github.com/sirupsen/logrus"
    10  	"github.com/spf13/pflag"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func defaultOptions(configFile string) *daemonOptions {
    16  	opts := newDaemonOptions(&config.Config{})
    17  	opts.flags = &pflag.FlagSet{}
    18  	opts.InstallFlags(opts.flags)
    19  	installConfigFlags(opts.daemonConfig, opts.flags)
    20  	opts.flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "")
    21  	opts.configFile = configFile
    22  	return opts
    23  }
    24  
    25  func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) {
    26  	opts := defaultOptions("")
    27  	opts.Debug = true
    28  
    29  	loadedConfig, err := loadDaemonCliConfig(opts)
    30  	require.NoError(t, err)
    31  	require.NotNil(t, loadedConfig)
    32  	if !loadedConfig.Debug {
    33  		t.Fatalf("expected debug to be copied from the common flags, got false")
    34  	}
    35  }
    36  
    37  func TestLoadDaemonCliConfigWithTLS(t *testing.T) {
    38  	opts := defaultOptions("")
    39  	opts.TLSOptions.CAFile = "/tmp/ca.pem"
    40  	opts.TLS = true
    41  
    42  	loadedConfig, err := loadDaemonCliConfig(opts)
    43  	require.NoError(t, err)
    44  	require.NotNil(t, loadedConfig)
    45  	assert.Equal(t, "/tmp/ca.pem", loadedConfig.CommonTLSOptions.CAFile)
    46  }
    47  
    48  func TestLoadDaemonCliConfigWithConflicts(t *testing.T) {
    49  	tempFile := fs.NewFile(t, "config", fs.WithContent(`{"labels": ["l3=foo"]}`))
    50  	defer tempFile.Remove()
    51  	configFile := tempFile.Path()
    52  
    53  	opts := defaultOptions(configFile)
    54  	flags := opts.flags
    55  
    56  	assert.NoError(t, flags.Set("config-file", configFile))
    57  	assert.NoError(t, flags.Set("label", "l1=bar"))
    58  	assert.NoError(t, flags.Set("label", "l2=baz"))
    59  
    60  	_, err := loadDaemonCliConfig(opts)
    61  	testutil.ErrorContains(t, err, "as a flag and in the configuration file: labels")
    62  }
    63  
    64  func TestLoadDaemonCliWithConflictingNodeGenericResources(t *testing.T) {
    65  	tempFile := fs.NewFile(t, "config", fs.WithContent(`{"node-generic-resources": ["foo=bar", "bar=baz"]}`))
    66  	defer tempFile.Remove()
    67  	configFile := tempFile.Path()
    68  
    69  	opts := defaultOptions(configFile)
    70  	flags := opts.flags
    71  
    72  	assert.NoError(t, flags.Set("config-file", configFile))
    73  	assert.NoError(t, flags.Set("node-generic-resource", "r1=bar"))
    74  	assert.NoError(t, flags.Set("node-generic-resource", "r2=baz"))
    75  
    76  	_, err := loadDaemonCliConfig(opts)
    77  	testutil.ErrorContains(t, err, "as a flag and in the configuration file: node-generic-resources")
    78  }
    79  
    80  func TestLoadDaemonCliWithConflictingLabels(t *testing.T) {
    81  	opts := defaultOptions("")
    82  	flags := opts.flags
    83  
    84  	assert.NoError(t, flags.Set("label", "foo=bar"))
    85  	assert.NoError(t, flags.Set("label", "foo=baz"))
    86  
    87  	_, err := loadDaemonCliConfig(opts)
    88  	assert.EqualError(t, err, "conflict labels for foo=baz and foo=bar")
    89  }
    90  
    91  func TestLoadDaemonCliWithDuplicateLabels(t *testing.T) {
    92  	opts := defaultOptions("")
    93  	flags := opts.flags
    94  
    95  	assert.NoError(t, flags.Set("label", "foo=the-same"))
    96  	assert.NoError(t, flags.Set("label", "foo=the-same"))
    97  
    98  	_, err := loadDaemonCliConfig(opts)
    99  	assert.NoError(t, err)
   100  }
   101  
   102  func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) {
   103  	tempFile := fs.NewFile(t, "config", fs.WithContent(`{"tlsverify": true}`))
   104  	defer tempFile.Remove()
   105  
   106  	opts := defaultOptions(tempFile.Path())
   107  	opts.TLSOptions.CAFile = "/tmp/ca.pem"
   108  
   109  	loadedConfig, err := loadDaemonCliConfig(opts)
   110  	require.NoError(t, err)
   111  	require.NotNil(t, loadedConfig)
   112  	assert.Equal(t, loadedConfig.TLS, true)
   113  }
   114  
   115  func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) {
   116  	tempFile := fs.NewFile(t, "config", fs.WithContent(`{"tlsverify": false}`))
   117  	defer tempFile.Remove()
   118  
   119  	opts := defaultOptions(tempFile.Path())
   120  	opts.TLSOptions.CAFile = "/tmp/ca.pem"
   121  
   122  	loadedConfig, err := loadDaemonCliConfig(opts)
   123  	require.NoError(t, err)
   124  	require.NotNil(t, loadedConfig)
   125  	assert.True(t, loadedConfig.TLS)
   126  }
   127  
   128  func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) {
   129  	tempFile := fs.NewFile(t, "config", fs.WithContent(`{}`))
   130  	defer tempFile.Remove()
   131  
   132  	opts := defaultOptions(tempFile.Path())
   133  	opts.TLSOptions.CAFile = "/tmp/ca.pem"
   134  
   135  	loadedConfig, err := loadDaemonCliConfig(opts)
   136  	require.NoError(t, err)
   137  	require.NotNil(t, loadedConfig)
   138  	assert.False(t, loadedConfig.TLS)
   139  }
   140  
   141  func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) {
   142  	tempFile := fs.NewFile(t, "config", fs.WithContent(`{"log-level": "warn"}`))
   143  	defer tempFile.Remove()
   144  
   145  	opts := defaultOptions(tempFile.Path())
   146  	loadedConfig, err := loadDaemonCliConfig(opts)
   147  	require.NoError(t, err)
   148  	require.NotNil(t, loadedConfig)
   149  	assert.Equal(t, "warn", loadedConfig.LogLevel)
   150  	assert.Equal(t, logrus.WarnLevel, logrus.GetLevel())
   151  }
   152  
   153  func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) {
   154  	content := `{"tlscacert": "/etc/certs/ca.pem", "log-driver": "syslog"}`
   155  	tempFile := fs.NewFile(t, "config", fs.WithContent(content))
   156  	defer tempFile.Remove()
   157  
   158  	opts := defaultOptions(tempFile.Path())
   159  	loadedConfig, err := loadDaemonCliConfig(opts)
   160  	require.NoError(t, err)
   161  	require.NotNil(t, loadedConfig)
   162  	assert.Equal(t, "/etc/certs/ca.pem", loadedConfig.CommonTLSOptions.CAFile)
   163  	assert.Equal(t, "syslog", loadedConfig.LogConfig.Type)
   164  }
   165  
   166  func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) {
   167  	content := `{
   168  		"allow-nondistributable-artifacts": ["allow-nondistributable-artifacts.com"],
   169  		"registry-mirrors": ["https://mirrors.docker.com"],
   170  		"insecure-registries": ["https://insecure.docker.com"]
   171  	}`
   172  	tempFile := fs.NewFile(t, "config", fs.WithContent(content))
   173  	defer tempFile.Remove()
   174  
   175  	opts := defaultOptions(tempFile.Path())
   176  	loadedConfig, err := loadDaemonCliConfig(opts)
   177  	require.NoError(t, err)
   178  	require.NotNil(t, loadedConfig)
   179  
   180  	assert.Len(t, loadedConfig.AllowNondistributableArtifacts, 1)
   181  	assert.Len(t, loadedConfig.Mirrors, 1)
   182  	assert.Len(t, loadedConfig.InsecureRegistries, 1)
   183  }