github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/daemon/config/config_unix_test.go (about)

     1  // +build !windows
     2  
     3  package config // import "github.com/docker/docker/daemon/config"
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/docker/docker/opts"
     9  	units "github.com/docker/go-units"
    10  	"github.com/spf13/pflag"
    11  	"gotest.tools/v3/assert"
    12  	is "gotest.tools/v3/assert/cmp"
    13  	"gotest.tools/v3/fs"
    14  )
    15  
    16  func TestGetConflictFreeConfiguration(t *testing.T) {
    17  	configFileData := `
    18  		{
    19  			"debug": true,
    20  			"default-ulimits": {
    21  				"nofile": {
    22  					"Name": "nofile",
    23  					"Hard": 2048,
    24  					"Soft": 1024
    25  				}
    26  			},
    27  			"log-opts": {
    28  				"tag": "test_tag"
    29  			}
    30  		}`
    31  
    32  	file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
    33  	defer file.Remove()
    34  
    35  	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
    36  	var debug bool
    37  	flags.BoolVarP(&debug, "debug", "D", false, "")
    38  	flags.Var(opts.NewNamedUlimitOpt("default-ulimits", nil), "default-ulimit", "")
    39  	flags.Var(opts.NewNamedMapOpts("log-opts", nil, nil), "log-opt", "")
    40  
    41  	cc, err := getConflictFreeConfiguration(file.Path(), flags)
    42  	assert.NilError(t, err)
    43  
    44  	assert.Check(t, cc.Debug)
    45  
    46  	expectedUlimits := map[string]*units.Ulimit{
    47  		"nofile": {
    48  			Name: "nofile",
    49  			Hard: 2048,
    50  			Soft: 1024,
    51  		},
    52  	}
    53  
    54  	assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
    55  }
    56  
    57  func TestDaemonConfigurationMerge(t *testing.T) {
    58  	configFileData := `
    59  		{
    60  			"debug": true,
    61  			"default-ulimits": {
    62  				"nofile": {
    63  					"Name": "nofile",
    64  					"Hard": 2048,
    65  					"Soft": 1024
    66  				}
    67  			},
    68  			"log-opts": {
    69  				"tag": "test_tag"
    70  			}
    71  		}`
    72  
    73  	file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
    74  	defer file.Remove()
    75  
    76  	c := &Config{
    77  		CommonConfig: CommonConfig{
    78  			AutoRestart: true,
    79  			LogConfig: LogConfig{
    80  				Type:   "syslog",
    81  				Config: map[string]string{"tag": "test"},
    82  			},
    83  		},
    84  	}
    85  
    86  	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
    87  
    88  	var debug bool
    89  	flags.BoolVarP(&debug, "debug", "D", false, "")
    90  	flags.Var(opts.NewNamedUlimitOpt("default-ulimits", nil), "default-ulimit", "")
    91  	flags.Var(opts.NewNamedMapOpts("log-opts", nil, nil), "log-opt", "")
    92  
    93  	cc, err := MergeDaemonConfigurations(c, flags, file.Path())
    94  	assert.NilError(t, err)
    95  
    96  	assert.Check(t, cc.Debug)
    97  	assert.Check(t, cc.AutoRestart)
    98  
    99  	expectedLogConfig := LogConfig{
   100  		Type:   "syslog",
   101  		Config: map[string]string{"tag": "test_tag"},
   102  	}
   103  
   104  	assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))
   105  
   106  	expectedUlimits := map[string]*units.Ulimit{
   107  		"nofile": {
   108  			Name: "nofile",
   109  			Hard: 2048,
   110  			Soft: 1024,
   111  		},
   112  	}
   113  
   114  	assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
   115  }
   116  
   117  func TestDaemonConfigurationMergeShmSize(t *testing.T) {
   118  	data := `{"default-shm-size": "1g"}`
   119  
   120  	file := fs.NewFile(t, "docker-config", fs.WithContent(data))
   121  	defer file.Remove()
   122  
   123  	c := &Config{}
   124  
   125  	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
   126  	shmSize := opts.MemBytes(DefaultShmSize)
   127  	flags.Var(&shmSize, "default-shm-size", "")
   128  
   129  	cc, err := MergeDaemonConfigurations(c, flags, file.Path())
   130  	assert.NilError(t, err)
   131  
   132  	expectedValue := 1 * 1024 * 1024 * 1024
   133  	assert.Check(t, is.Equal(int64(expectedValue), cc.ShmSize.Value()))
   134  }