2018-02-05 21:05:59 +00:00
|
|
|
package config // import "github.com/docker/docker/daemon/config"
|
2016-08-10 10:52:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2017-04-12 07:26:42 +00:00
|
|
|
|
2021-07-10 12:52:16 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-04-12 07:26:42 +00:00
|
|
|
"github.com/docker/docker/opts"
|
2019-08-05 14:37:47 +00:00
|
|
|
units "github.com/docker/go-units"
|
2022-08-17 12:31:25 +00:00
|
|
|
"github.com/imdario/mergo"
|
2017-04-12 07:26:42 +00:00
|
|
|
"github.com/spf13/pflag"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-08-10 10:52:06 +00:00
|
|
|
)
|
|
|
|
|
2017-04-12 07:26:42 +00:00
|
|
|
func TestGetConflictFreeConfiguration(t *testing.T) {
|
2023-01-09 23:42:13 +00:00
|
|
|
configFile := makeConfigFile(t, `
|
2017-04-12 07:26:42 +00:00
|
|
|
{
|
|
|
|
"debug": true,
|
|
|
|
"default-ulimits": {
|
|
|
|
"nofile": {
|
|
|
|
"Name": "nofile",
|
|
|
|
"Hard": 2048,
|
|
|
|
"Soft": 1024
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"log-opts": {
|
|
|
|
"tag": "test_tag"
|
2023-04-03 19:35:21 +00:00
|
|
|
},
|
|
|
|
"default-network-opts": {
|
|
|
|
"overlay": {
|
|
|
|
"com.docker.network.driver.mtu": "1337"
|
|
|
|
}
|
2017-04-12 07:26:42 +00:00
|
|
|
}
|
2023-01-09 23:42:13 +00:00
|
|
|
}`)
|
2017-04-12 07:26:42 +00:00
|
|
|
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
var debug bool
|
|
|
|
flags.BoolVarP(&debug, "debug", "D", false, "")
|
|
|
|
flags.Var(opts.NewNamedUlimitOpt("default-ulimits", nil), "default-ulimit", "")
|
|
|
|
flags.Var(opts.NewNamedMapOpts("log-opts", nil, nil), "log-opt", "")
|
2023-04-03 19:35:21 +00:00
|
|
|
flags.Var(opts.NewNamedMapMapOpts("default-network-opts", nil, nil), "default-network-opt", "")
|
2017-04-12 07:26:42 +00:00
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
cc, err := getConflictFreeConfiguration(configFile, flags)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2017-04-12 07:26:42 +00:00
|
|
|
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, cc.Debug)
|
2017-04-12 07:26:42 +00:00
|
|
|
|
|
|
|
expectedUlimits := map[string]*units.Ulimit{
|
|
|
|
"nofile": {
|
|
|
|
Name: "nofile",
|
|
|
|
Hard: 2048,
|
|
|
|
Soft: 1024,
|
|
|
|
},
|
2016-08-10 10:52:06 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
|
2017-04-12 07:26:42 +00:00
|
|
|
}
|
2016-08-10 10:52:06 +00:00
|
|
|
|
2017-04-12 07:26:42 +00:00
|
|
|
func TestDaemonConfigurationMerge(t *testing.T) {
|
2023-01-09 23:42:13 +00:00
|
|
|
configFile := makeConfigFile(t, `
|
2016-08-10 10:52:06 +00:00
|
|
|
{
|
|
|
|
"debug": true,
|
|
|
|
"default-ulimits": {
|
|
|
|
"nofile": {
|
|
|
|
"Name": "nofile",
|
|
|
|
"Hard": 2048,
|
|
|
|
"Soft": 1024
|
|
|
|
}
|
|
|
|
}
|
2023-01-09 23:42:13 +00:00
|
|
|
}`)
|
2016-08-10 10:52:06 +00:00
|
|
|
|
2022-06-06 19:11:08 +00:00
|
|
|
conf, err := New()
|
|
|
|
assert.NilError(t, err)
|
2016-08-10 10:52:06 +00:00
|
|
|
|
2017-04-12 07:26:42 +00:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
2022-06-06 16:55:05 +00:00
|
|
|
flags.BoolVarP(&conf.Debug, "debug", "D", false, "")
|
|
|
|
flags.BoolVarP(&conf.AutoRestart, "restart", "r", true, "")
|
|
|
|
flags.Var(opts.NewNamedUlimitOpt("default-ulimits", &conf.Ulimits), "default-ulimit", "")
|
|
|
|
flags.StringVar(&conf.LogConfig.Type, "log-driver", "json-file", "")
|
|
|
|
flags.Var(opts.NewNamedMapOpts("log-opts", conf.LogConfig.Config, nil), "log-opt", "")
|
|
|
|
assert.Check(t, flags.Set("restart", "true"))
|
|
|
|
assert.Check(t, flags.Set("log-driver", "syslog"))
|
|
|
|
assert.Check(t, flags.Set("log-opt", "tag=from_flag"))
|
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
cc, err := MergeDaemonConfigurations(conf, flags, configFile)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 09:11:12 +00:00
|
|
|
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, cc.Debug)
|
|
|
|
assert.Check(t, cc.AutoRestart)
|
2017-04-12 07:26:42 +00:00
|
|
|
|
|
|
|
expectedLogConfig := LogConfig{
|
|
|
|
Type: "syslog",
|
2022-06-06 16:55:05 +00:00
|
|
|
Config: map[string]string{"tag": "from_flag"},
|
2016-12-25 09:11:12 +00:00
|
|
|
}
|
2017-04-12 07:26:42 +00:00
|
|
|
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))
|
2017-04-12 07:26:42 +00:00
|
|
|
|
|
|
|
expectedUlimits := map[string]*units.Ulimit{
|
|
|
|
"nofile": {
|
|
|
|
Name: "nofile",
|
|
|
|
Hard: 2048,
|
|
|
|
Soft: 1024,
|
|
|
|
},
|
2016-12-25 09:11:12 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
|
2017-04-12 07:26:42 +00:00
|
|
|
}
|
2016-12-25 09:11:12 +00:00
|
|
|
|
2017-04-12 07:26:42 +00:00
|
|
|
func TestDaemonConfigurationMergeShmSize(t *testing.T) {
|
2023-01-09 23:42:13 +00:00
|
|
|
configFile := makeConfigFile(t, `{"default-shm-size": "1g"}`)
|
2016-12-25 09:11:12 +00:00
|
|
|
|
2022-08-17 12:31:25 +00:00
|
|
|
c, err := New()
|
|
|
|
assert.NilError(t, err)
|
2017-04-12 07:26:42 +00:00
|
|
|
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
shmSize := opts.MemBytes(DefaultShmSize)
|
|
|
|
flags.Var(&shmSize, "default-shm-size", "")
|
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
cc, err := MergeDaemonConfigurations(c, flags, configFile)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2017-04-12 07:26:42 +00:00
|
|
|
|
2016-12-25 09:11:12 +00:00
|
|
|
expectedValue := 1 * 1024 * 1024 * 1024
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.Equal(int64(expectedValue), cc.ShmSize.Value()))
|
2016-12-25 09:11:12 +00:00
|
|
|
}
|
2021-07-10 12:52:16 +00:00
|
|
|
|
|
|
|
func TestUnixValidateConfigurationErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2022-06-22 13:02:23 +00:00
|
|
|
doc string
|
|
|
|
config *Config
|
|
|
|
expectedErr string
|
2021-07-10 12:52:16 +00:00
|
|
|
}{
|
|
|
|
{
|
2022-06-22 13:02:23 +00:00
|
|
|
doc: `cannot override the stock runtime`,
|
2021-07-10 12:52:16 +00:00
|
|
|
config: &Config{
|
|
|
|
Runtimes: map[string]types.Runtime{
|
|
|
|
StockRuntimeName: {},
|
|
|
|
},
|
|
|
|
},
|
2022-06-22 13:02:23 +00:00
|
|
|
expectedErr: `runtime name 'runc' is reserved`,
|
2021-07-10 12:52:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2022-06-22 13:02:23 +00:00
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
2022-08-17 12:31:25 +00:00
|
|
|
cfg, err := New()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, mergo.Merge(cfg, tc.config, mergo.WithOverride))
|
|
|
|
err = Validate(cfg)
|
2022-06-22 13:02:23 +00:00
|
|
|
assert.ErrorContains(t, err, tc.expectedErr)
|
|
|
|
})
|
2021-07-10 12:52:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnixGetInitPath(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
config *Config
|
|
|
|
expectedInitPath string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
config: &Config{
|
|
|
|
InitPath: "some-init-path",
|
|
|
|
},
|
|
|
|
expectedInitPath: "some-init-path",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: &Config{
|
|
|
|
DefaultInitBinary: "foo-init-bin",
|
|
|
|
},
|
|
|
|
expectedInitPath: "foo-init-bin",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: &Config{
|
|
|
|
InitPath: "init-path-A",
|
|
|
|
DefaultInitBinary: "init-path-B",
|
|
|
|
},
|
|
|
|
expectedInitPath: "init-path-A",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: &Config{},
|
|
|
|
expectedInitPath: "docker-init",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2022-06-22 13:03:06 +00:00
|
|
|
assert.Equal(t, tc.config.GetInitPath(), tc.expectedInitPath)
|
2021-07-10 12:52:16 +00:00
|
|
|
}
|
|
|
|
}
|