github.com/ruphin/docker@v1.10.1/docker/daemon_test.go (about) 1 // +build daemon 2 3 package main 4 5 import ( 6 "io/ioutil" 7 "strings" 8 "testing" 9 10 "github.com/Sirupsen/logrus" 11 "github.com/docker/docker/cli" 12 "github.com/docker/docker/daemon" 13 "github.com/docker/docker/opts" 14 "github.com/docker/docker/pkg/mflag" 15 "github.com/docker/go-connections/tlsconfig" 16 ) 17 18 func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) { 19 c := &daemon.Config{} 20 common := &cli.CommonFlags{ 21 Debug: true, 22 } 23 24 flags := mflag.NewFlagSet("test", mflag.ContinueOnError) 25 loadedConfig, err := loadDaemonCliConfig(c, flags, common, "/tmp/fooobarbaz") 26 if err != nil { 27 t.Fatal(err) 28 } 29 if loadedConfig == nil { 30 t.Fatalf("expected configuration %v, got nil", c) 31 } 32 if !loadedConfig.Debug { 33 t.Fatalf("expected debug to be copied from the common flags, got false") 34 } 35 } 36 37 func TestLoadDaemonCliConfigWithTLS(t *testing.T) { 38 c := &daemon.Config{} 39 common := &cli.CommonFlags{ 40 TLS: true, 41 TLSOptions: &tlsconfig.Options{ 42 CAFile: "/tmp/ca.pem", 43 }, 44 } 45 46 flags := mflag.NewFlagSet("test", mflag.ContinueOnError) 47 loadedConfig, err := loadDaemonCliConfig(c, flags, common, "/tmp/fooobarbaz") 48 if err != nil { 49 t.Fatal(err) 50 } 51 if loadedConfig == nil { 52 t.Fatalf("expected configuration %v, got nil", c) 53 } 54 if loadedConfig.CommonTLSOptions.CAFile != "/tmp/ca.pem" { 55 t.Fatalf("expected /tmp/ca.pem, got %s: %q", loadedConfig.CommonTLSOptions.CAFile, loadedConfig) 56 } 57 } 58 59 func TestLoadDaemonCliConfigWithConflicts(t *testing.T) { 60 c := &daemon.Config{} 61 common := &cli.CommonFlags{} 62 f, err := ioutil.TempFile("", "docker-config-") 63 if err != nil { 64 t.Fatal(err) 65 } 66 67 configFile := f.Name() 68 f.Write([]byte(`{"labels": ["l3=foo"]}`)) 69 f.Close() 70 71 var labels []string 72 73 flags := mflag.NewFlagSet("test", mflag.ContinueOnError) 74 flags.String([]string{daemonConfigFileFlag}, "", "") 75 flags.Var(opts.NewNamedListOptsRef("labels", &labels, opts.ValidateLabel), []string{"-label"}, "") 76 77 flags.Set(daemonConfigFileFlag, configFile) 78 if err := flags.Set("-label", "l1=bar"); err != nil { 79 t.Fatal(err) 80 } 81 if err := flags.Set("-label", "l2=baz"); err != nil { 82 t.Fatal(err) 83 } 84 85 _, err = loadDaemonCliConfig(c, flags, common, configFile) 86 if err == nil { 87 t.Fatalf("expected configuration error, got nil") 88 } 89 if !strings.Contains(err.Error(), "labels") { 90 t.Fatalf("expected labels conflict, got %v", err) 91 } 92 } 93 94 func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) { 95 c := &daemon.Config{} 96 common := &cli.CommonFlags{ 97 TLSOptions: &tlsconfig.Options{ 98 CAFile: "/tmp/ca.pem", 99 }, 100 } 101 102 f, err := ioutil.TempFile("", "docker-config-") 103 if err != nil { 104 t.Fatal(err) 105 } 106 107 configFile := f.Name() 108 f.Write([]byte(`{"tlsverify": true}`)) 109 f.Close() 110 111 flags := mflag.NewFlagSet("test", mflag.ContinueOnError) 112 flags.Bool([]string{"-tlsverify"}, false, "") 113 loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) 114 if err != nil { 115 t.Fatal(err) 116 } 117 if loadedConfig == nil { 118 t.Fatalf("expected configuration %v, got nil", c) 119 } 120 121 if !loadedConfig.TLS { 122 t.Fatalf("expected TLS enabled, got %q", loadedConfig) 123 } 124 } 125 126 func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) { 127 c := &daemon.Config{} 128 common := &cli.CommonFlags{ 129 TLSOptions: &tlsconfig.Options{ 130 CAFile: "/tmp/ca.pem", 131 }, 132 } 133 134 f, err := ioutil.TempFile("", "docker-config-") 135 if err != nil { 136 t.Fatal(err) 137 } 138 139 configFile := f.Name() 140 f.Write([]byte(`{"tlsverify": false}`)) 141 f.Close() 142 143 flags := mflag.NewFlagSet("test", mflag.ContinueOnError) 144 flags.Bool([]string{"-tlsverify"}, false, "") 145 loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) 146 if err != nil { 147 t.Fatal(err) 148 } 149 if loadedConfig == nil { 150 t.Fatalf("expected configuration %v, got nil", c) 151 } 152 153 if !loadedConfig.TLS { 154 t.Fatalf("expected TLS enabled, got %q", loadedConfig) 155 } 156 } 157 158 func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) { 159 c := &daemon.Config{} 160 common := &cli.CommonFlags{ 161 TLSOptions: &tlsconfig.Options{ 162 CAFile: "/tmp/ca.pem", 163 }, 164 } 165 166 f, err := ioutil.TempFile("", "docker-config-") 167 if err != nil { 168 t.Fatal(err) 169 } 170 171 configFile := f.Name() 172 f.Write([]byte(`{}`)) 173 f.Close() 174 175 flags := mflag.NewFlagSet("test", mflag.ContinueOnError) 176 loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) 177 if err != nil { 178 t.Fatal(err) 179 } 180 if loadedConfig == nil { 181 t.Fatalf("expected configuration %v, got nil", c) 182 } 183 184 if loadedConfig.TLS { 185 t.Fatalf("expected TLS disabled, got %q", loadedConfig) 186 } 187 } 188 189 func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) { 190 c := &daemon.Config{} 191 common := &cli.CommonFlags{} 192 193 f, err := ioutil.TempFile("", "docker-config-") 194 if err != nil { 195 t.Fatal(err) 196 } 197 198 configFile := f.Name() 199 f.Write([]byte(`{"log-level": "warn"}`)) 200 f.Close() 201 202 flags := mflag.NewFlagSet("test", mflag.ContinueOnError) 203 flags.String([]string{"-log-level"}, "", "") 204 loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) 205 if err != nil { 206 t.Fatal(err) 207 } 208 if loadedConfig == nil { 209 t.Fatalf("expected configuration %v, got nil", c) 210 } 211 if loadedConfig.LogLevel != "warn" { 212 t.Fatalf("expected warn log level, got %v", loadedConfig.LogLevel) 213 } 214 215 if logrus.GetLevel() != logrus.WarnLevel { 216 t.Fatalf("expected warn log level, got %v", logrus.GetLevel()) 217 } 218 } 219 220 func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) { 221 c := &daemon.Config{} 222 common := &cli.CommonFlags{} 223 flags := mflag.NewFlagSet("test", mflag.ContinueOnError) 224 flags.String([]string{"-tlscacert"}, "", "") 225 flags.String([]string{"-log-driver"}, "", "") 226 227 f, err := ioutil.TempFile("", "docker-config-") 228 if err != nil { 229 t.Fatal(err) 230 } 231 232 configFile := f.Name() 233 f.Write([]byte(`{"tlscacert": "/etc/certs/ca.pem", "log-driver": "syslog"}`)) 234 f.Close() 235 236 loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) 237 if err != nil { 238 t.Fatal(err) 239 } 240 if loadedConfig == nil { 241 t.Fatal("expected configuration, got nil") 242 } 243 if loadedConfig.CommonTLSOptions.CAFile != "/etc/certs/ca.pem" { 244 t.Fatalf("expected CA file path /etc/certs/ca.pem, got %v", loadedConfig.CommonTLSOptions.CAFile) 245 } 246 if loadedConfig.LogConfig.Type != "syslog" { 247 t.Fatalf("expected LogConfig type syslog, got %v", loadedConfig.LogConfig.Type) 248 } 249 } 250 251 func TestLoadDaemonConfigWithMapOptions(t *testing.T) { 252 c := &daemon.Config{} 253 common := &cli.CommonFlags{} 254 flags := mflag.NewFlagSet("test", mflag.ContinueOnError) 255 256 flags.Var(opts.NewNamedMapOpts("cluster-store-opts", c.ClusterOpts, nil), []string{"-cluster-store-opt"}, "") 257 flags.Var(opts.NewNamedMapOpts("log-opts", c.LogConfig.Config, nil), []string{"-log-opt"}, "") 258 259 f, err := ioutil.TempFile("", "docker-config-") 260 if err != nil { 261 t.Fatal(err) 262 } 263 264 configFile := f.Name() 265 f.Write([]byte(`{ 266 "cluster-store-opts": {"kv.cacertfile": "/var/lib/docker/discovery_certs/ca.pem"}, 267 "log-opts": {"tag": "test"} 268 }`)) 269 f.Close() 270 271 loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) 272 if err != nil { 273 t.Fatal(err) 274 } 275 if loadedConfig == nil { 276 t.Fatal("expected configuration, got nil") 277 } 278 if loadedConfig.ClusterOpts == nil { 279 t.Fatal("expected cluster options, got nil") 280 } 281 282 expectedPath := "/var/lib/docker/discovery_certs/ca.pem" 283 if caPath := loadedConfig.ClusterOpts["kv.cacertfile"]; caPath != expectedPath { 284 t.Fatalf("expected %s, got %s", expectedPath, caPath) 285 } 286 287 if loadedConfig.LogConfig.Config == nil { 288 t.Fatal("expected log config options, got nil") 289 } 290 if tag := loadedConfig.LogConfig.Config["tag"]; tag != "test" { 291 t.Fatalf("expected log tag `test`, got %s", tag) 292 } 293 }