2016-08-10 06:52:06 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package config // import "github.com/docker/docker/daemon/config"
|
2016-08-10 06:52:06 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2017-04-12 03:26:42 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/opts"
|
2017-09-11 14:55:05 -04:00
|
|
|
units "github.com/docker/go-units"
|
2018-03-13 15:28:34 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-08-23 17:25:00 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/fs"
|
2017-04-12 03:26:42 -04:00
|
|
|
"github.com/spf13/pflag"
|
2016-08-10 06:52:06 -04:00
|
|
|
)
|
|
|
|
|
2017-04-12 03:26:42 -04:00
|
|
|
func TestGetConflictFreeConfiguration(t *testing.T) {
|
2017-09-11 14:55:05 -04:00
|
|
|
configFileData := `
|
2017-04-12 03:26:42 -04:00
|
|
|
{
|
|
|
|
"debug": true,
|
|
|
|
"default-ulimits": {
|
|
|
|
"nofile": {
|
|
|
|
"Name": "nofile",
|
|
|
|
"Hard": 2048,
|
|
|
|
"Soft": 1024
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"log-opts": {
|
|
|
|
"tag": "test_tag"
|
|
|
|
}
|
2017-09-11 14:55:05 -04:00
|
|
|
}`
|
2017-04-12 03:26:42 -04:00
|
|
|
|
2017-08-23 17:25:00 -04:00
|
|
|
file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
|
2017-04-12 03:26:42 -04:00
|
|
|
defer file.Remove()
|
|
|
|
|
|
|
|
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", "")
|
|
|
|
|
2017-08-23 17:25:00 -04:00
|
|
|
cc, err := getConflictFreeConfiguration(file.Path(), flags)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-04-12 03:26:42 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, cc.Debug)
|
2017-04-12 03:26:42 -04:00
|
|
|
|
|
|
|
expectedUlimits := map[string]*units.Ulimit{
|
|
|
|
"nofile": {
|
|
|
|
Name: "nofile",
|
|
|
|
Hard: 2048,
|
|
|
|
Soft: 1024,
|
|
|
|
},
|
2016-08-10 06:52:06 -04:00
|
|
|
}
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
|
2017-04-12 03:26:42 -04:00
|
|
|
}
|
2016-08-10 06:52:06 -04:00
|
|
|
|
2017-04-12 03:26:42 -04:00
|
|
|
func TestDaemonConfigurationMerge(t *testing.T) {
|
2017-09-11 14:55:05 -04:00
|
|
|
configFileData := `
|
2016-08-10 06:52:06 -04:00
|
|
|
{
|
|
|
|
"debug": true,
|
|
|
|
"default-ulimits": {
|
|
|
|
"nofile": {
|
|
|
|
"Name": "nofile",
|
|
|
|
"Hard": 2048,
|
|
|
|
"Soft": 1024
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"log-opts": {
|
|
|
|
"tag": "test_tag"
|
|
|
|
}
|
2017-09-11 14:55:05 -04:00
|
|
|
}`
|
2016-08-10 06:52:06 -04:00
|
|
|
|
2017-08-23 17:25:00 -04:00
|
|
|
file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
|
2017-04-12 03:26:42 -04:00
|
|
|
defer file.Remove()
|
2016-08-10 06:52:06 -04:00
|
|
|
|
|
|
|
c := &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
AutoRestart: true,
|
|
|
|
LogConfig: LogConfig{
|
|
|
|
Type: "syslog",
|
|
|
|
Config: map[string]string{"tag": "test"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-04-12 03:26:42 -04:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
2016-08-10 06:52:06 -04:00
|
|
|
|
2017-04-12 03:26:42 -04:00
|
|
|
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", "")
|
2016-08-10 06:52:06 -04:00
|
|
|
|
2017-08-23 17:25:00 -04:00
|
|
|
cc, err := MergeDaemonConfigurations(c, flags, file.Path())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2016-12-25 04:11:12 -05:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, cc.Debug)
|
|
|
|
assert.Check(t, cc.AutoRestart)
|
2017-04-12 03:26:42 -04:00
|
|
|
|
|
|
|
expectedLogConfig := LogConfig{
|
|
|
|
Type: "syslog",
|
|
|
|
Config: map[string]string{"tag": "test_tag"},
|
2016-12-25 04:11:12 -05:00
|
|
|
}
|
2017-04-12 03:26:42 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))
|
2017-04-12 03:26:42 -04:00
|
|
|
|
|
|
|
expectedUlimits := map[string]*units.Ulimit{
|
|
|
|
"nofile": {
|
|
|
|
Name: "nofile",
|
|
|
|
Hard: 2048,
|
|
|
|
Soft: 1024,
|
|
|
|
},
|
2016-12-25 04:11:12 -05:00
|
|
|
}
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
|
2017-04-12 03:26:42 -04:00
|
|
|
}
|
2016-12-25 04:11:12 -05:00
|
|
|
|
2017-04-12 03:26:42 -04:00
|
|
|
func TestDaemonConfigurationMergeShmSize(t *testing.T) {
|
2017-09-11 14:55:05 -04:00
|
|
|
data := `{"default-shm-size": "1g"}`
|
2016-12-25 04:11:12 -05:00
|
|
|
|
2017-08-23 17:25:00 -04:00
|
|
|
file := fs.NewFile(t, "docker-config", fs.WithContent(data))
|
2017-04-12 03:26:42 -04:00
|
|
|
defer file.Remove()
|
2016-12-25 04:11:12 -05:00
|
|
|
|
|
|
|
c := &Config{}
|
2017-04-12 03:26:42 -04:00
|
|
|
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
shmSize := opts.MemBytes(DefaultShmSize)
|
|
|
|
flags.Var(&shmSize, "default-shm-size", "")
|
|
|
|
|
2017-08-23 17:25:00 -04:00
|
|
|
cc, err := MergeDaemonConfigurations(c, flags, file.Path())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-04-12 03:26:42 -04:00
|
|
|
|
2016-12-25 04:11:12 -05:00
|
|
|
expectedValue := 1 * 1024 * 1024 * 1024
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(int64(expectedValue), cc.ShmSize.Value()))
|
2016-12-25 04:11:12 -05:00
|
|
|
}
|