github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/cmd/dockerd/daemon_unix_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/docker/docker/daemon/config"
     9  	"github.com/gotestyourself/gotestyourself/fs"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestLoadDaemonCliConfigWithDaemonFlags(t *testing.T) {
    15  	content := `{"log-opts": {"max-size": "1k"}}`
    16  	tempFile := fs.NewFile(t, "config", fs.WithContent(content))
    17  	defer tempFile.Remove()
    18  
    19  	opts := defaultOptions(tempFile.Path())
    20  	opts.Debug = true
    21  	opts.LogLevel = "info"
    22  	assert.NoError(t, opts.flags.Set("selinux-enabled", "true"))
    23  
    24  	loadedConfig, err := loadDaemonCliConfig(opts)
    25  	require.NoError(t, err)
    26  	require.NotNil(t, loadedConfig)
    27  
    28  	assert.True(t, loadedConfig.Debug)
    29  	assert.Equal(t, "info", loadedConfig.LogLevel)
    30  	assert.True(t, loadedConfig.EnableSelinuxSupport)
    31  	assert.Equal(t, "json-file", loadedConfig.LogConfig.Type)
    32  	assert.Equal(t, "1k", loadedConfig.LogConfig.Config["max-size"])
    33  }
    34  
    35  func TestLoadDaemonConfigWithNetwork(t *testing.T) {
    36  	content := `{"bip": "127.0.0.2", "ip": "127.0.0.1"}`
    37  	tempFile := fs.NewFile(t, "config", fs.WithContent(content))
    38  	defer tempFile.Remove()
    39  
    40  	opts := defaultOptions(tempFile.Path())
    41  	loadedConfig, err := loadDaemonCliConfig(opts)
    42  	require.NoError(t, err)
    43  	require.NotNil(t, loadedConfig)
    44  
    45  	assert.Equal(t, "127.0.0.2", loadedConfig.IP)
    46  	assert.Equal(t, "127.0.0.1", loadedConfig.DefaultIP.String())
    47  }
    48  
    49  func TestLoadDaemonConfigWithMapOptions(t *testing.T) {
    50  	content := `{
    51  		"cluster-store-opts": {"kv.cacertfile": "/var/lib/docker/discovery_certs/ca.pem"},
    52  		"log-opts": {"tag": "test"}
    53  }`
    54  	tempFile := fs.NewFile(t, "config", fs.WithContent(content))
    55  	defer tempFile.Remove()
    56  
    57  	opts := defaultOptions(tempFile.Path())
    58  	loadedConfig, err := loadDaemonCliConfig(opts)
    59  	require.NoError(t, err)
    60  	require.NotNil(t, loadedConfig)
    61  	assert.NotNil(t, loadedConfig.ClusterOpts)
    62  
    63  	expectedPath := "/var/lib/docker/discovery_certs/ca.pem"
    64  	assert.Equal(t, expectedPath, loadedConfig.ClusterOpts["kv.cacertfile"])
    65  	assert.NotNil(t, loadedConfig.LogConfig.Config)
    66  	assert.Equal(t, "test", loadedConfig.LogConfig.Config["tag"])
    67  }
    68  
    69  func TestLoadDaemonConfigWithTrueDefaultValues(t *testing.T) {
    70  	content := `{ "userland-proxy": false }`
    71  	tempFile := fs.NewFile(t, "config", fs.WithContent(content))
    72  	defer tempFile.Remove()
    73  
    74  	opts := defaultOptions(tempFile.Path())
    75  	loadedConfig, err := loadDaemonCliConfig(opts)
    76  	require.NoError(t, err)
    77  	require.NotNil(t, loadedConfig)
    78  
    79  	assert.False(t, loadedConfig.EnableUserlandProxy)
    80  
    81  	// make sure reloading doesn't generate configuration
    82  	// conflicts after normalizing boolean values.
    83  	reload := func(reloadedConfig *config.Config) {
    84  		assert.False(t, reloadedConfig.EnableUserlandProxy)
    85  	}
    86  	assert.NoError(t, config.Reload(opts.configFile, opts.flags, reload))
    87  }
    88  
    89  func TestLoadDaemonConfigWithTrueDefaultValuesLeaveDefaults(t *testing.T) {
    90  	tempFile := fs.NewFile(t, "config", fs.WithContent(`{}`))
    91  	defer tempFile.Remove()
    92  
    93  	opts := defaultOptions(tempFile.Path())
    94  	loadedConfig, err := loadDaemonCliConfig(opts)
    95  	require.NoError(t, err)
    96  	require.NotNil(t, loadedConfig)
    97  
    98  	assert.True(t, loadedConfig.EnableUserlandProxy)
    99  }