moby/daemon/config/config_linux_test.go
Cory Snider 0f6eeecac0 daemon: consolidate runtimes config validation
The daemon has made a habit of mutating the DefaultRuntime and Runtimes
values in the Config struct to merge defaults. This would be fine if it
was a part of the regular configuration loading and merging process,
as is done with other config options. The trouble is it does so in
surprising places, such as in functions with 'verify' or 'validate' in
their name. It has been necessary in order to validate that the user has
not defined a custom runtime named "runc" which would shadow the
built-in runtime of the same name. Other daemon code depends on the
runtime named "runc" always being defined in the config, but merging it
with the user config at the same time as the other defaults are merged
would trip the validation. The root of the issue is that the daemon has
used the same config values for both validating the daemon runtime
configuration as supplied by the user and for keeping track of which
runtimes have been set up by the daemon. Now that a completely separate
value is used for the latter purpose, surprising contortions are no
longer required to make the validation work as intended.

Consolidate the validation of the runtimes config and merging of the
built-in runtimes into the daemon.setupRuntimes() function. Set the
result of merging the built-in runtimes config and default default
runtime on the returned runtimes struct, without back-propagating it
onto the config.Config argument.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-06-01 14:45:25 -04:00

156 lines
3.7 KiB
Go

package config // import "github.com/docker/docker/daemon/config"
import (
"testing"
"github.com/docker/docker/opts"
units "github.com/docker/go-units"
"github.com/spf13/pflag"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestGetConflictFreeConfiguration(t *testing.T) {
configFile := makeConfigFile(t, `
{
"debug": true,
"default-ulimits": {
"nofile": {
"Name": "nofile",
"Hard": 2048,
"Soft": 1024
}
},
"log-opts": {
"tag": "test_tag"
},
"default-network-opts": {
"overlay": {
"com.docker.network.driver.mtu": "1337"
}
}
}`)
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", "")
flags.Var(opts.NewNamedMapMapOpts("default-network-opts", nil, nil), "default-network-opt", "")
cc, err := getConflictFreeConfiguration(configFile, flags)
assert.NilError(t, err)
assert.Check(t, cc.Debug)
expectedUlimits := map[string]*units.Ulimit{
"nofile": {
Name: "nofile",
Hard: 2048,
Soft: 1024,
},
}
assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
}
func TestDaemonConfigurationMerge(t *testing.T) {
configFile := makeConfigFile(t, `
{
"debug": true,
"default-ulimits": {
"nofile": {
"Name": "nofile",
"Hard": 2048,
"Soft": 1024
}
}
}`)
conf, err := New()
assert.NilError(t, err)
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
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"))
cc, err := MergeDaemonConfigurations(conf, flags, configFile)
assert.NilError(t, err)
assert.Check(t, cc.Debug)
assert.Check(t, cc.AutoRestart)
expectedLogConfig := LogConfig{
Type: "syslog",
Config: map[string]string{"tag": "from_flag"},
}
assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))
expectedUlimits := map[string]*units.Ulimit{
"nofile": {
Name: "nofile",
Hard: 2048,
Soft: 1024,
},
}
assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
}
func TestDaemonConfigurationMergeShmSize(t *testing.T) {
configFile := makeConfigFile(t, `{"default-shm-size": "1g"}`)
c, err := New()
assert.NilError(t, err)
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
shmSize := opts.MemBytes(DefaultShmSize)
flags.Var(&shmSize, "default-shm-size", "")
cc, err := MergeDaemonConfigurations(c, flags, configFile)
assert.NilError(t, err)
expectedValue := 1 * 1024 * 1024 * 1024
assert.Check(t, is.Equal(int64(expectedValue), cc.ShmSize.Value()))
}
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 {
assert.Equal(t, tc.config.GetInitPath(), tc.expectedInitPath)
}
}