gopkg.in/docker/docker.v20@v20.10.27/daemon/config/config_unix_test.go (about)

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