-
Notifications
You must be signed in to change notification settings - Fork 18.6k
/
config_unix_test.go
135 lines (108 loc) · 3 KB
/
config_unix_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//go:build !windows
// +build !windows
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"
"gotest.tools/v3/fs"
)
func TestGetConflictFreeConfiguration(t *testing.T) {
configFileData := `
{
"debug": true,
"default-ulimits": {
"nofile": {
"Name": "nofile",
"Hard": 2048,
"Soft": 1024
}
},
"log-opts": {
"tag": "test_tag"
}
}`
file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
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", "")
cc, err := getConflictFreeConfiguration(file.Path(), 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) {
configFileData := `
{
"debug": true,
"default-ulimits": {
"nofile": {
"Name": "nofile",
"Hard": 2048,
"Soft": 1024
}
},
"log-opts": {
"tag": "test_tag"
}
}`
file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
defer file.Remove()
c := &Config{
CommonConfig: CommonConfig{
AutoRestart: true,
LogConfig: LogConfig{
Type: "syslog",
Config: map[string]string{"tag": "test"},
},
},
}
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", "")
cc, err := MergeDaemonConfigurations(c, flags, file.Path())
assert.NilError(t, err)
assert.Check(t, cc.Debug)
assert.Check(t, cc.AutoRestart)
expectedLogConfig := LogConfig{
Type: "syslog",
Config: map[string]string{"tag": "test_tag"},
}
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) {
data := `{"default-shm-size": "1g"}`
file := fs.NewFile(t, "docker-config", fs.WithContent(data))
defer file.Remove()
c := &Config{}
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
shmSize := opts.MemBytes(DefaultShmSize)
flags.Var(&shmSize, "default-shm-size", "")
cc, err := MergeDaemonConfigurations(c, flags, file.Path())
assert.NilError(t, err)
expectedValue := 1 * 1024 * 1024 * 1024
assert.Check(t, is.Equal(int64(expectedValue), cc.ShmSize.Value()))
}