github.com/rumpl/bof@v23.0.0-rc.2+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  		}`
    68  
    69  	file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
    70  	defer file.Remove()
    71  
    72  	conf, err := New()
    73  	assert.NilError(t, err)
    74  
    75  	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
    76  	flags.BoolVarP(&conf.Debug, "debug", "D", false, "")
    77  	flags.BoolVarP(&conf.AutoRestart, "restart", "r", true, "")
    78  	flags.Var(opts.NewNamedUlimitOpt("default-ulimits", &conf.Ulimits), "default-ulimit", "")
    79  	flags.StringVar(&conf.LogConfig.Type, "log-driver", "json-file", "")
    80  	flags.Var(opts.NewNamedMapOpts("log-opts", conf.LogConfig.Config, nil), "log-opt", "")
    81  	assert.Check(t, flags.Set("restart", "true"))
    82  	assert.Check(t, flags.Set("log-driver", "syslog"))
    83  	assert.Check(t, flags.Set("log-opt", "tag=from_flag"))
    84  
    85  	cc, err := MergeDaemonConfigurations(conf, flags, file.Path())
    86  	assert.NilError(t, err)
    87  
    88  	assert.Check(t, cc.Debug)
    89  	assert.Check(t, cc.AutoRestart)
    90  
    91  	expectedLogConfig := LogConfig{
    92  		Type:   "syslog",
    93  		Config: map[string]string{"tag": "from_flag"},
    94  	}
    95  
    96  	assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))
    97  
    98  	expectedUlimits := map[string]*units.Ulimit{
    99  		"nofile": {
   100  			Name: "nofile",
   101  			Hard: 2048,
   102  			Soft: 1024,
   103  		},
   104  	}
   105  
   106  	assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
   107  }
   108  
   109  func TestDaemonConfigurationMergeShmSize(t *testing.T) {
   110  	data := `{"default-shm-size": "1g"}`
   111  
   112  	file := fs.NewFile(t, "docker-config", fs.WithContent(data))
   113  	defer file.Remove()
   114  
   115  	c := &Config{}
   116  
   117  	flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
   118  	shmSize := opts.MemBytes(DefaultShmSize)
   119  	flags.Var(&shmSize, "default-shm-size", "")
   120  
   121  	cc, err := MergeDaemonConfigurations(c, flags, file.Path())
   122  	assert.NilError(t, err)
   123  
   124  	expectedValue := 1 * 1024 * 1024 * 1024
   125  	assert.Check(t, is.Equal(int64(expectedValue), cc.ShmSize.Value()))
   126  }
   127  
   128  func TestUnixValidateConfigurationErrors(t *testing.T) {
   129  	testCases := []struct {
   130  		doc         string
   131  		config      *Config
   132  		expectedErr string
   133  	}{
   134  		{
   135  			doc: `cannot override the stock runtime`,
   136  			config: &Config{
   137  				Runtimes: map[string]types.Runtime{
   138  					StockRuntimeName: {},
   139  				},
   140  			},
   141  			expectedErr: `runtime name 'runc' is reserved`,
   142  		},
   143  	}
   144  	for _, tc := range testCases {
   145  		tc := tc
   146  		t.Run(tc.doc, func(t *testing.T) {
   147  			err := Validate(tc.config)
   148  			assert.ErrorContains(t, err, tc.expectedErr)
   149  		})
   150  	}
   151  }
   152  
   153  func TestUnixGetInitPath(t *testing.T) {
   154  	testCases := []struct {
   155  		config           *Config
   156  		expectedInitPath string
   157  	}{
   158  		{
   159  			config: &Config{
   160  				InitPath: "some-init-path",
   161  			},
   162  			expectedInitPath: "some-init-path",
   163  		},
   164  		{
   165  			config: &Config{
   166  				DefaultInitBinary: "foo-init-bin",
   167  			},
   168  			expectedInitPath: "foo-init-bin",
   169  		},
   170  		{
   171  			config: &Config{
   172  				InitPath:          "init-path-A",
   173  				DefaultInitBinary: "init-path-B",
   174  			},
   175  			expectedInitPath: "init-path-A",
   176  		},
   177  		{
   178  			config:           &Config{},
   179  			expectedInitPath: "docker-init",
   180  		},
   181  	}
   182  	for _, tc := range testCases {
   183  		assert.Equal(t, tc.config.GetInitPath(), tc.expectedInitPath)
   184  	}
   185  }