github.com/adityamillind98/moby@v23.0.0-rc.4+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 ) 13 14 func TestGetConflictFreeConfiguration(t *testing.T) { 15 configFile := makeConfigFile(t, ` 16 { 17 "debug": true, 18 "default-ulimits": { 19 "nofile": { 20 "Name": "nofile", 21 "Hard": 2048, 22 "Soft": 1024 23 } 24 }, 25 "log-opts": { 26 "tag": "test_tag" 27 } 28 }`) 29 30 flags := pflag.NewFlagSet("test", pflag.ContinueOnError) 31 var debug bool 32 flags.BoolVarP(&debug, "debug", "D", false, "") 33 flags.Var(opts.NewNamedUlimitOpt("default-ulimits", nil), "default-ulimit", "") 34 flags.Var(opts.NewNamedMapOpts("log-opts", nil, nil), "log-opt", "") 35 36 cc, err := getConflictFreeConfiguration(configFile, flags) 37 assert.NilError(t, err) 38 39 assert.Check(t, cc.Debug) 40 41 expectedUlimits := map[string]*units.Ulimit{ 42 "nofile": { 43 Name: "nofile", 44 Hard: 2048, 45 Soft: 1024, 46 }, 47 } 48 49 assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits)) 50 } 51 52 func TestDaemonConfigurationMerge(t *testing.T) { 53 configFile := makeConfigFile(t, ` 54 { 55 "debug": true, 56 "default-ulimits": { 57 "nofile": { 58 "Name": "nofile", 59 "Hard": 2048, 60 "Soft": 1024 61 } 62 } 63 }`) 64 65 conf, err := New() 66 assert.NilError(t, err) 67 68 flags := pflag.NewFlagSet("test", pflag.ContinueOnError) 69 flags.BoolVarP(&conf.Debug, "debug", "D", false, "") 70 flags.BoolVarP(&conf.AutoRestart, "restart", "r", true, "") 71 flags.Var(opts.NewNamedUlimitOpt("default-ulimits", &conf.Ulimits), "default-ulimit", "") 72 flags.StringVar(&conf.LogConfig.Type, "log-driver", "json-file", "") 73 flags.Var(opts.NewNamedMapOpts("log-opts", conf.LogConfig.Config, nil), "log-opt", "") 74 assert.Check(t, flags.Set("restart", "true")) 75 assert.Check(t, flags.Set("log-driver", "syslog")) 76 assert.Check(t, flags.Set("log-opt", "tag=from_flag")) 77 78 cc, err := MergeDaemonConfigurations(conf, flags, configFile) 79 assert.NilError(t, err) 80 81 assert.Check(t, cc.Debug) 82 assert.Check(t, cc.AutoRestart) 83 84 expectedLogConfig := LogConfig{ 85 Type: "syslog", 86 Config: map[string]string{"tag": "from_flag"}, 87 } 88 89 assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig)) 90 91 expectedUlimits := map[string]*units.Ulimit{ 92 "nofile": { 93 Name: "nofile", 94 Hard: 2048, 95 Soft: 1024, 96 }, 97 } 98 99 assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits)) 100 } 101 102 func TestDaemonConfigurationMergeShmSize(t *testing.T) { 103 configFile := makeConfigFile(t, `{"default-shm-size": "1g"}`) 104 105 c := &Config{} 106 107 flags := pflag.NewFlagSet("test", pflag.ContinueOnError) 108 shmSize := opts.MemBytes(DefaultShmSize) 109 flags.Var(&shmSize, "default-shm-size", "") 110 111 cc, err := MergeDaemonConfigurations(c, flags, configFile) 112 assert.NilError(t, err) 113 114 expectedValue := 1 * 1024 * 1024 * 1024 115 assert.Check(t, is.Equal(int64(expectedValue), cc.ShmSize.Value())) 116 } 117 118 func TestUnixValidateConfigurationErrors(t *testing.T) { 119 testCases := []struct { 120 doc string 121 config *Config 122 expectedErr string 123 }{ 124 { 125 doc: `cannot override the stock runtime`, 126 config: &Config{ 127 Runtimes: map[string]types.Runtime{ 128 StockRuntimeName: {}, 129 }, 130 }, 131 expectedErr: `runtime name 'runc' is reserved`, 132 }, 133 } 134 for _, tc := range testCases { 135 tc := tc 136 t.Run(tc.doc, func(t *testing.T) { 137 err := Validate(tc.config) 138 assert.ErrorContains(t, err, tc.expectedErr) 139 }) 140 } 141 } 142 143 func TestUnixGetInitPath(t *testing.T) { 144 testCases := []struct { 145 config *Config 146 expectedInitPath string 147 }{ 148 { 149 config: &Config{ 150 InitPath: "some-init-path", 151 }, 152 expectedInitPath: "some-init-path", 153 }, 154 { 155 config: &Config{ 156 DefaultInitBinary: "foo-init-bin", 157 }, 158 expectedInitPath: "foo-init-bin", 159 }, 160 { 161 config: &Config{ 162 InitPath: "init-path-A", 163 DefaultInitBinary: "init-path-B", 164 }, 165 expectedInitPath: "init-path-A", 166 }, 167 { 168 config: &Config{}, 169 expectedInitPath: "docker-init", 170 }, 171 } 172 for _, tc := range testCases { 173 assert.Equal(t, tc.config.GetInitPath(), tc.expectedInitPath) 174 } 175 }