github.com/docker/engine@v22.0.0-20211208180946-d456264580cf+incompatible/daemon/config/config_linux_test.go (about)

     1  package config // import "github.com/docker/docker/daemon/config"
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/docker/api/types"
     7  	"github.com/docker/docker/opts"
     8  	units "github.com/docker/go-units"
     9  	"github.com/spf13/pflag"
    10  	"gotest.tools/v3/assert"
    11  	is "gotest.tools/v3/assert/cmp"
    12  	"gotest.tools/v3/fs"
    13  )
    14  
    15  func TestGetConflictFreeConfiguration(t *testing.T) {
    16  	configFileData := `
    17  		{
    18  			"debug": true,
    19  			"default-ulimits": {
    20  				"nofile": {
    21  					"Name": "nofile",
    22  					"Hard": 2048,
    23  					"Soft": 1024
    24  				}
    25  			},
    26  			"log-opts": {
    27  				"tag": "test_tag"
    28  			}
    29  		}`
    30  
    31  	file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
    32  	defer file.Remove()
    33  
    34  	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
    35  	var debug bool
    36  	flags.BoolVarP(&debug, "debug", "D", false, "")
    37  	flags.Var(opts.NewNamedUlimitOpt("default-ulimits", nil), "default-ulimit", "")
    38  	flags.Var(opts.NewNamedMapOpts("log-opts", nil, nil), "log-opt", "")
    39  
    40  	cc, err := getConflictFreeConfiguration(file.Path(), flags)
    41  	assert.NilError(t, err)
    42  
    43  	assert.Check(t, cc.Debug)
    44  
    45  	expectedUlimits := map[string]*units.Ulimit{
    46  		"nofile": {
    47  			Name: "nofile",
    48  			Hard: 2048,
    49  			Soft: 1024,
    50  		},
    51  	}
    52  
    53  	assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
    54  }
    55  
    56  func TestDaemonConfigurationMerge(t *testing.T) {
    57  	configFileData := `
    58  		{
    59  			"debug": true,
    60  			"default-ulimits": {
    61  				"nofile": {
    62  					"Name": "nofile",
    63  					"Hard": 2048,
    64  					"Soft": 1024
    65  				}
    66  			},
    67  			"log-opts": {
    68  				"tag": "test_tag"
    69  			}
    70  		}`
    71  
    72  	file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
    73  	defer file.Remove()
    74  
    75  	c := &Config{
    76  		CommonConfig: CommonConfig{
    77  			AutoRestart: true,
    78  			LogConfig: LogConfig{
    79  				Type:   "syslog",
    80  				Config: map[string]string{"tag": "test"},
    81  			},
    82  		},
    83  	}
    84  
    85  	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
    86  
    87  	var debug bool
    88  	flags.BoolVarP(&debug, "debug", "D", false, "")
    89  	flags.Var(opts.NewNamedUlimitOpt("default-ulimits", nil), "default-ulimit", "")
    90  	flags.Var(opts.NewNamedMapOpts("log-opts", nil, nil), "log-opt", "")
    91  
    92  	cc, err := MergeDaemonConfigurations(c, flags, file.Path())
    93  	assert.NilError(t, err)
    94  
    95  	assert.Check(t, cc.Debug)
    96  	assert.Check(t, cc.AutoRestart)
    97  
    98  	expectedLogConfig := LogConfig{
    99  		Type:   "syslog",
   100  		Config: map[string]string{"tag": "test_tag"},
   101  	}
   102  
   103  	assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))
   104  
   105  	expectedUlimits := map[string]*units.Ulimit{
   106  		"nofile": {
   107  			Name: "nofile",
   108  			Hard: 2048,
   109  			Soft: 1024,
   110  		},
   111  	}
   112  
   113  	assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
   114  }
   115  
   116  func TestDaemonConfigurationMergeShmSize(t *testing.T) {
   117  	data := `{"default-shm-size": "1g"}`
   118  
   119  	file := fs.NewFile(t, "docker-config", fs.WithContent(data))
   120  	defer file.Remove()
   121  
   122  	c := &Config{}
   123  
   124  	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
   125  	shmSize := opts.MemBytes(DefaultShmSize)
   126  	flags.Var(&shmSize, "default-shm-size", "")
   127  
   128  	cc, err := MergeDaemonConfigurations(c, flags, file.Path())
   129  	assert.NilError(t, err)
   130  
   131  	expectedValue := 1 * 1024 * 1024 * 1024
   132  	assert.Check(t, is.Equal(int64(expectedValue), cc.ShmSize.Value()))
   133  }
   134  
   135  func TestUnixValidateConfigurationErrors(t *testing.T) {
   136  	testCases := []struct {
   137  		config *Config
   138  	}{
   139  		// Can't override the stock runtime
   140  		{
   141  			config: &Config{
   142  				Runtimes: map[string]types.Runtime{
   143  					StockRuntimeName: {},
   144  				},
   145  			},
   146  		},
   147  		// Default runtime should be present in runtimes
   148  		{
   149  			config: &Config{
   150  				Runtimes: map[string]types.Runtime{
   151  					"foo": {},
   152  				},
   153  				CommonConfig: CommonConfig{
   154  					DefaultRuntime: "bar",
   155  				},
   156  			},
   157  		},
   158  	}
   159  	for _, tc := range testCases {
   160  		err := Validate(tc.config)
   161  		if err == nil {
   162  			t.Fatalf("expected error, got nil for config %v", tc.config)
   163  		}
   164  	}
   165  }
   166  
   167  func TestUnixGetInitPath(t *testing.T) {
   168  	testCases := []struct {
   169  		config           *Config
   170  		expectedInitPath string
   171  	}{
   172  		{
   173  			config: &Config{
   174  				InitPath: "some-init-path",
   175  			},
   176  			expectedInitPath: "some-init-path",
   177  		},
   178  		{
   179  			config: &Config{
   180  				DefaultInitBinary: "foo-init-bin",
   181  			},
   182  			expectedInitPath: "foo-init-bin",
   183  		},
   184  		{
   185  			config: &Config{
   186  				InitPath:          "init-path-A",
   187  				DefaultInitBinary: "init-path-B",
   188  			},
   189  			expectedInitPath: "init-path-A",
   190  		},
   191  		{
   192  			config:           &Config{},
   193  			expectedInitPath: "docker-init",
   194  		},
   195  	}
   196  	for _, tc := range testCases {
   197  		initPath := tc.config.GetInitPath()
   198  		if initPath != tc.expectedInitPath {
   199  			t.Fatalf("expected initPath to be %v, got %v", tc.expectedInitPath, initPath)
   200  		}
   201  	}
   202  }