3
3
package config
4
4
5
5
import (
6
- "io/ioutil"
7
- "runtime"
8
-
9
6
"testing"
7
+
8
+ "github.com/docker/docker/opts"
9
+ "github.com/docker/docker/pkg/testutil/tempfile"
10
+ "github.com/docker/go-units"
11
+ "github.com/spf13/pflag"
12
+ "github.com/stretchr/testify/assert"
13
+ "github.com/stretchr/testify/require"
10
14
)
11
15
12
- func TestDaemonConfigurationMerge (t * testing.T ) {
13
- f , err := ioutil .TempFile ("" , "docker-config-" )
14
- if err != nil {
15
- t .Fatal (err )
16
+ func TestGetConflictFreeConfiguration (t * testing.T ) {
17
+ configFileData := string ([]byte (`
18
+ {
19
+ "debug": true,
20
+ "default-ulimits": {
21
+ "nofile": {
22
+ "Name": "nofile",
23
+ "Hard": 2048,
24
+ "Soft": 1024
25
+ }
26
+ },
27
+ "log-opts": {
28
+ "tag": "test_tag"
29
+ }
30
+ }` ))
31
+
32
+ file := tempfile .NewTempFile (t , "docker-config" , configFileData )
33
+ defer file .Remove ()
34
+
35
+ flags := pflag .NewFlagSet ("test" , pflag .ContinueOnError )
36
+ var debug bool
37
+ flags .BoolVarP (& debug , "debug" , "D" , false , "" )
38
+ flags .Var (opts .NewNamedUlimitOpt ("default-ulimits" , nil ), "default-ulimit" , "" )
39
+ flags .Var (opts .NewNamedMapOpts ("log-opts" , nil , nil ), "log-opt" , "" )
40
+
41
+ cc , err := getConflictFreeConfiguration (file .Name (), flags )
42
+ require .NoError (t , err )
43
+
44
+ assert .True (t , cc .Debug )
45
+
46
+ expectedUlimits := map [string ]* units.Ulimit {
47
+ "nofile" : {
48
+ Name : "nofile" ,
49
+ Hard : 2048 ,
50
+ Soft : 1024 ,
51
+ },
16
52
}
17
53
18
- configFile := f .Name ()
54
+ assert .Equal (t , expectedUlimits , cc .Ulimits )
55
+ }
19
56
20
- f .Write ([]byte (`
57
+ func TestDaemonConfigurationMerge (t * testing.T ) {
58
+ configFileData := string ([]byte (`
21
59
{
22
60
"debug": true,
23
61
"default-ulimits": {
@@ -32,7 +70,8 @@ func TestDaemonConfigurationMerge(t *testing.T) {
32
70
}
33
71
}` ))
34
72
35
- f .Close ()
73
+ file := tempfile .NewTempFile (t , "docker-config" , configFileData )
74
+ defer file .Remove ()
36
75
37
76
c := & Config {
38
77
CommonConfig : CommonConfig {
@@ -44,66 +83,55 @@ func TestDaemonConfigurationMerge(t *testing.T) {
44
83
},
45
84
}
46
85
47
- cc , err := MergeDaemonConfigurations (c , nil , configFile )
48
- if err != nil {
49
- t .Fatal (err )
50
- }
51
- if ! cc .Debug {
52
- t .Fatalf ("expected %v, got %v\n " , true , cc .Debug )
53
- }
54
- if ! cc .AutoRestart {
55
- t .Fatalf ("expected %v, got %v\n " , true , cc .AutoRestart )
56
- }
57
- if cc .LogConfig .Type != "syslog" {
58
- t .Fatalf ("expected syslog config, got %q\n " , cc .LogConfig )
59
- }
86
+ flags := pflag .NewFlagSet ("test" , pflag .ContinueOnError )
60
87
61
- if configValue , OK := cc .LogConfig .Config ["tag" ]; ! OK {
62
- t .Fatal ("expected syslog config attributes, got nil\n " )
63
- } else {
64
- if configValue != "test_tag" {
65
- t .Fatalf ("expected syslog config attributes 'tag=test_tag', got 'tag=%s'\n " , configValue )
66
- }
67
- }
88
+ var debug bool
89
+ flags .BoolVarP (& debug , "debug" , "D" , false , "" )
90
+ flags .Var (opts .NewNamedUlimitOpt ("default-ulimits" , nil ), "default-ulimit" , "" )
91
+ flags .Var (opts .NewNamedMapOpts ("log-opts" , nil , nil ), "log-opt" , "" )
68
92
69
- if cc .Ulimits == nil {
70
- t .Fatal ("expected default ulimit config, got nil\n " )
71
- } else {
72
- if _ , OK := cc .Ulimits ["nofile" ]; OK {
73
- if cc .Ulimits ["nofile" ].Name != "nofile" ||
74
- cc .Ulimits ["nofile" ].Hard != 2048 ||
75
- cc .Ulimits ["nofile" ].Soft != 1024 {
76
- t .Fatalf ("expected default ulimit name, hard and soft are nofile, 2048, 1024, got %s, %d, %d\n " , cc .Ulimits ["nofile" ].Name , cc .Ulimits ["nofile" ].Hard , cc .Ulimits ["nofile" ].Soft )
77
- }
78
- } else {
79
- t .Fatal ("expected default ulimit name nofile, got nil\n " )
80
- }
81
- }
82
- }
93
+ cc , err := MergeDaemonConfigurations (c , flags , file .Name ())
94
+ require .NoError (t , err )
83
95
84
- func TestDaemonConfigurationMergeShmSize (t * testing.T ) {
85
- if runtime .GOOS == "solaris" {
86
- t .Skip ("ShmSize not supported on Solaris\n " )
96
+ assert .True (t , cc .Debug )
97
+ assert .True (t , cc .AutoRestart )
98
+
99
+ expectedLogConfig := LogConfig {
100
+ Type : "syslog" ,
101
+ Config : map [string ]string {"tag" : "test_tag" },
87
102
}
88
- f , err := ioutil .TempFile ("" , "docker-config-" )
89
- if err != nil {
90
- t .Fatal (err )
103
+
104
+ assert .Equal (t , expectedLogConfig , cc .LogConfig )
105
+
106
+ expectedUlimits := map [string ]* units.Ulimit {
107
+ "nofile" : {
108
+ Name : "nofile" ,
109
+ Hard : 2048 ,
110
+ Soft : 1024 ,
111
+ },
91
112
}
92
113
93
- configFile := f .Name ()
114
+ assert .Equal (t , expectedUlimits , cc .Ulimits )
115
+ }
94
116
95
- f .Write ([]byte (`
117
+ func TestDaemonConfigurationMergeShmSize (t * testing.T ) {
118
+ data := string ([]byte (`
96
119
{
97
120
"default-shm-size": "1g"
98
121
}` ))
99
122
100
- f .Close ()
123
+ file := tempfile .NewTempFile (t , "docker-config" , data )
124
+ defer file .Remove ()
101
125
102
126
c := & Config {}
103
- cc , err := MergeDaemonConfigurations (c , nil , configFile )
104
- if err != nil {
105
- t .Fatal (err )
106
- }
127
+
128
+ flags := pflag .NewFlagSet ("test" , pflag .ContinueOnError )
129
+ shmSize := opts .MemBytes (DefaultShmSize )
130
+ flags .Var (& shmSize , "default-shm-size" , "" )
131
+
132
+ cc , err := MergeDaemonConfigurations (c , flags , file .Name ())
133
+ require .NoError (t , err )
134
+
107
135
expectedValue := 1 * 1024 * 1024 * 1024
108
136
if cc .ShmSize .Value () != int64 (expectedValue ) {
109
137
t .Fatalf ("expected default shm size %d, got %d" , expectedValue , cc .ShmSize .Value ())
0 commit comments