github.com/akerouanton/docker@v1.11.0-rc3/daemon/daemon_test.go (about) 1 package daemon 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "reflect" 8 "testing" 9 "time" 10 11 "github.com/docker/docker/container" 12 "github.com/docker/docker/pkg/discovery" 13 _ "github.com/docker/docker/pkg/discovery/memory" 14 "github.com/docker/docker/pkg/registrar" 15 "github.com/docker/docker/pkg/truncindex" 16 "github.com/docker/docker/volume" 17 volumedrivers "github.com/docker/docker/volume/drivers" 18 "github.com/docker/docker/volume/local" 19 "github.com/docker/docker/volume/store" 20 containertypes "github.com/docker/engine-api/types/container" 21 "github.com/docker/go-connections/nat" 22 ) 23 24 // 25 // https://github.com/docker/docker/issues/8069 26 // 27 28 func TestGetContainer(t *testing.T) { 29 c1 := &container.Container{ 30 CommonContainer: container.CommonContainer{ 31 ID: "5a4ff6a163ad4533d22d69a2b8960bf7fafdcba06e72d2febdba229008b0bf57", 32 Name: "tender_bardeen", 33 }, 34 } 35 36 c2 := &container.Container{ 37 CommonContainer: container.CommonContainer{ 38 ID: "3cdbd1aa394fd68559fd1441d6eff2ab7c1e6363582c82febfaa8045df3bd8de", 39 Name: "drunk_hawking", 40 }, 41 } 42 43 c3 := &container.Container{ 44 CommonContainer: container.CommonContainer{ 45 ID: "3cdbd1aa394fd68559fd1441d6eff2abfafdcba06e72d2febdba229008b0bf57", 46 Name: "3cdbd1aa", 47 }, 48 } 49 50 c4 := &container.Container{ 51 CommonContainer: container.CommonContainer{ 52 ID: "75fb0b800922abdbef2d27e60abcdfaf7fb0698b2a96d22d3354da361a6ff4a5", 53 Name: "5a4ff6a163ad4533d22d69a2b8960bf7fafdcba06e72d2febdba229008b0bf57", 54 }, 55 } 56 57 c5 := &container.Container{ 58 CommonContainer: container.CommonContainer{ 59 ID: "d22d69a2b8960bf7fafdcba06e72d2febdba960bf7fafdcba06e72d2f9008b060b", 60 Name: "d22d69a2b896", 61 }, 62 } 63 64 store := container.NewMemoryStore() 65 store.Add(c1.ID, c1) 66 store.Add(c2.ID, c2) 67 store.Add(c3.ID, c3) 68 store.Add(c4.ID, c4) 69 store.Add(c5.ID, c5) 70 71 index := truncindex.NewTruncIndex([]string{}) 72 index.Add(c1.ID) 73 index.Add(c2.ID) 74 index.Add(c3.ID) 75 index.Add(c4.ID) 76 index.Add(c5.ID) 77 78 daemon := &Daemon{ 79 containers: store, 80 idIndex: index, 81 nameIndex: registrar.NewRegistrar(), 82 } 83 84 daemon.reserveName(c1.ID, c1.Name) 85 daemon.reserveName(c2.ID, c2.Name) 86 daemon.reserveName(c3.ID, c3.Name) 87 daemon.reserveName(c4.ID, c4.Name) 88 daemon.reserveName(c5.ID, c5.Name) 89 90 if container, _ := daemon.GetContainer("3cdbd1aa394fd68559fd1441d6eff2ab7c1e6363582c82febfaa8045df3bd8de"); container != c2 { 91 t.Fatal("Should explicitly match full container IDs") 92 } 93 94 if container, _ := daemon.GetContainer("75fb0b8009"); container != c4 { 95 t.Fatal("Should match a partial ID") 96 } 97 98 if container, _ := daemon.GetContainer("drunk_hawking"); container != c2 { 99 t.Fatal("Should match a full name") 100 } 101 102 // c3.Name is a partial match for both c3.ID and c2.ID 103 if c, _ := daemon.GetContainer("3cdbd1aa"); c != c3 { 104 t.Fatal("Should match a full name even though it collides with another container's ID") 105 } 106 107 if container, _ := daemon.GetContainer("d22d69a2b896"); container != c5 { 108 t.Fatal("Should match a container where the provided prefix is an exact match to the it's name, and is also a prefix for it's ID") 109 } 110 111 if _, err := daemon.GetContainer("3cdbd1"); err == nil { 112 t.Fatal("Should return an error when provided a prefix that partially matches multiple container ID's") 113 } 114 115 if _, err := daemon.GetContainer("nothing"); err == nil { 116 t.Fatal("Should return an error when provided a prefix that is neither a name or a partial match to an ID") 117 } 118 } 119 120 func initDaemonWithVolumeStore(tmp string) (*Daemon, error) { 121 var err error 122 daemon := &Daemon{ 123 repository: tmp, 124 root: tmp, 125 } 126 daemon.volumes, err = store.New(tmp) 127 if err != nil { 128 return nil, err 129 } 130 131 volumesDriver, err := local.New(tmp, 0, 0) 132 if err != nil { 133 return nil, err 134 } 135 volumedrivers.Register(volumesDriver, volumesDriver.Name()) 136 137 return daemon, nil 138 } 139 140 func TestValidContainerNames(t *testing.T) { 141 invalidNames := []string{"-rm", "&sdfsfd", "safd%sd"} 142 validNames := []string{"word-word", "word_word", "1weoid"} 143 144 for _, name := range invalidNames { 145 if validContainerNamePattern.MatchString(name) { 146 t.Fatalf("%q is not a valid container name and was returned as valid.", name) 147 } 148 } 149 150 for _, name := range validNames { 151 if !validContainerNamePattern.MatchString(name) { 152 t.Fatalf("%q is a valid container name and was returned as invalid.", name) 153 } 154 } 155 } 156 157 func TestContainerInitDNS(t *testing.T) { 158 tmp, err := ioutil.TempDir("", "docker-container-test-") 159 if err != nil { 160 t.Fatal(err) 161 } 162 defer os.RemoveAll(tmp) 163 164 containerID := "d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e" 165 containerPath := filepath.Join(tmp, containerID) 166 if err := os.MkdirAll(containerPath, 0755); err != nil { 167 t.Fatal(err) 168 } 169 170 config := `{"State":{"Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":2464,"ExitCode":0, 171 "Error":"","StartedAt":"2015-05-26T16:48:53.869308965Z","FinishedAt":"0001-01-01T00:00:00Z"}, 172 "ID":"d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e","Created":"2015-05-26T16:48:53.7987917Z","Path":"top", 173 "Args":[],"Config":{"Hostname":"d59df5276e7b","Domainname":"","User":"","Memory":0,"MemorySwap":0,"CpuShares":0,"Cpuset":"", 174 "AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"PortSpecs":null,"ExposedPorts":null,"Tty":true,"OpenStdin":true, 175 "StdinOnce":false,"Env":null,"Cmd":["top"],"Image":"ubuntu:latest","Volumes":null,"WorkingDir":"","Entrypoint":null, 176 "NetworkDisabled":false,"MacAddress":"","OnBuild":null,"Labels":{}},"Image":"07f8e8c5e66084bef8f848877857537ffe1c47edd01a93af27e7161672ad0e95", 177 "NetworkSettings":{"IPAddress":"172.17.0.1","IPPrefixLen":16,"MacAddress":"02:42:ac:11:00:01","LinkLocalIPv6Address":"fe80::42:acff:fe11:1", 178 "LinkLocalIPv6PrefixLen":64,"GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"Gateway":"172.17.42.1","IPv6Gateway":"","Bridge":"docker0","Ports":{}}, 179 "ResolvConfPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/resolv.conf", 180 "HostnamePath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/hostname", 181 "HostsPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/hosts", 182 "LogPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e-json.log", 183 "Name":"/ubuntu","Driver":"aufs","MountLabel":"","ProcessLabel":"","AppArmorProfile":"","RestartCount":0, 184 "UpdateDns":false,"Volumes":{},"VolumesRW":{},"AppliedVolumesFrom":null}` 185 186 // Container struct only used to retrieve path to config file 187 container := &container.Container{CommonContainer: container.CommonContainer{Root: containerPath}} 188 configPath, err := container.ConfigPath() 189 if err != nil { 190 t.Fatal(err) 191 } 192 if err = ioutil.WriteFile(configPath, []byte(config), 0644); err != nil { 193 t.Fatal(err) 194 } 195 196 hostConfig := `{"Binds":[],"ContainerIDFile":"","Memory":0,"MemorySwap":0,"CpuShares":0,"CpusetCpus":"", 197 "Privileged":false,"PortBindings":{},"Links":null,"PublishAllPorts":false,"Dns":null,"DnsOptions":null,"DnsSearch":null,"ExtraHosts":null,"VolumesFrom":null, 198 "Devices":[],"NetworkMode":"bridge","IpcMode":"","PidMode":"","CapAdd":null,"CapDrop":null,"RestartPolicy":{"Name":"no","MaximumRetryCount":0}, 199 "SecurityOpt":null,"ReadonlyRootfs":false,"Ulimits":null,"LogConfig":{"Type":"","Config":null},"CgroupParent":""}` 200 201 hostConfigPath, err := container.HostConfigPath() 202 if err != nil { 203 t.Fatal(err) 204 } 205 if err = ioutil.WriteFile(hostConfigPath, []byte(hostConfig), 0644); err != nil { 206 t.Fatal(err) 207 } 208 209 daemon, err := initDaemonWithVolumeStore(tmp) 210 if err != nil { 211 t.Fatal(err) 212 } 213 defer volumedrivers.Unregister(volume.DefaultDriverName) 214 215 c, err := daemon.load(containerID) 216 if err != nil { 217 t.Fatal(err) 218 } 219 220 if c.HostConfig.DNS == nil { 221 t.Fatal("Expected container DNS to not be nil") 222 } 223 224 if c.HostConfig.DNSSearch == nil { 225 t.Fatal("Expected container DNSSearch to not be nil") 226 } 227 228 if c.HostConfig.DNSOptions == nil { 229 t.Fatal("Expected container DNSOptions to not be nil") 230 } 231 } 232 233 func newPortNoError(proto, port string) nat.Port { 234 p, _ := nat.NewPort(proto, port) 235 return p 236 } 237 238 func TestMerge(t *testing.T) { 239 volumesImage := make(map[string]struct{}) 240 volumesImage["/test1"] = struct{}{} 241 volumesImage["/test2"] = struct{}{} 242 portsImage := make(nat.PortSet) 243 portsImage[newPortNoError("tcp", "1111")] = struct{}{} 244 portsImage[newPortNoError("tcp", "2222")] = struct{}{} 245 configImage := &containertypes.Config{ 246 ExposedPorts: portsImage, 247 Env: []string{"VAR1=1", "VAR2=2"}, 248 Volumes: volumesImage, 249 } 250 251 portsUser := make(nat.PortSet) 252 portsUser[newPortNoError("tcp", "2222")] = struct{}{} 253 portsUser[newPortNoError("tcp", "3333")] = struct{}{} 254 volumesUser := make(map[string]struct{}) 255 volumesUser["/test3"] = struct{}{} 256 configUser := &containertypes.Config{ 257 ExposedPorts: portsUser, 258 Env: []string{"VAR2=3", "VAR3=3"}, 259 Volumes: volumesUser, 260 } 261 262 if err := merge(configUser, configImage); err != nil { 263 t.Error(err) 264 } 265 266 if len(configUser.ExposedPorts) != 3 { 267 t.Fatalf("Expected 3 ExposedPorts, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts)) 268 } 269 for portSpecs := range configUser.ExposedPorts { 270 if portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" { 271 t.Fatalf("Expected 1111 or 2222 or 3333, found %s", portSpecs) 272 } 273 } 274 if len(configUser.Env) != 3 { 275 t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env)) 276 } 277 for _, env := range configUser.Env { 278 if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" { 279 t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env) 280 } 281 } 282 283 if len(configUser.Volumes) != 3 { 284 t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes)) 285 } 286 for v := range configUser.Volumes { 287 if v != "/test1" && v != "/test2" && v != "/test3" { 288 t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v) 289 } 290 } 291 292 ports, _, err := nat.ParsePortSpecs([]string{"0000"}) 293 if err != nil { 294 t.Error(err) 295 } 296 configImage2 := &containertypes.Config{ 297 ExposedPorts: ports, 298 } 299 300 if err := merge(configUser, configImage2); err != nil { 301 t.Error(err) 302 } 303 304 if len(configUser.ExposedPorts) != 4 { 305 t.Fatalf("Expected 4 ExposedPorts, 0000, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts)) 306 } 307 for portSpecs := range configUser.ExposedPorts { 308 if portSpecs.Port() != "0" && portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" { 309 t.Fatalf("Expected %q or %q or %q or %q, found %s", 0, 1111, 2222, 3333, portSpecs) 310 } 311 } 312 } 313 314 func TestDaemonReloadLabels(t *testing.T) { 315 daemon := &Daemon{} 316 daemon.configStore = &Config{ 317 CommonConfig: CommonConfig{ 318 Labels: []string{"foo:bar"}, 319 }, 320 } 321 322 valuesSets := make(map[string]interface{}) 323 valuesSets["label"] = "foo:baz" 324 newConfig := &Config{ 325 CommonConfig: CommonConfig{ 326 Labels: []string{"foo:baz"}, 327 valuesSet: valuesSets, 328 }, 329 } 330 331 daemon.Reload(newConfig) 332 label := daemon.configStore.Labels[0] 333 if label != "foo:baz" { 334 t.Fatalf("Expected daemon label `foo:baz`, got %s", label) 335 } 336 } 337 338 func TestDaemonReloadNotAffectOthers(t *testing.T) { 339 daemon := &Daemon{} 340 daemon.configStore = &Config{ 341 CommonConfig: CommonConfig{ 342 Labels: []string{"foo:bar"}, 343 Debug: true, 344 }, 345 } 346 347 valuesSets := make(map[string]interface{}) 348 valuesSets["label"] = "foo:baz" 349 newConfig := &Config{ 350 CommonConfig: CommonConfig{ 351 Labels: []string{"foo:baz"}, 352 valuesSet: valuesSets, 353 }, 354 } 355 356 daemon.Reload(newConfig) 357 label := daemon.configStore.Labels[0] 358 if label != "foo:baz" { 359 t.Fatalf("Expected daemon label `foo:baz`, got %s", label) 360 } 361 debug := daemon.configStore.Debug 362 if !debug { 363 t.Fatalf("Expected debug 'enabled', got 'disabled'") 364 } 365 } 366 367 func TestDaemonDiscoveryReload(t *testing.T) { 368 daemon := &Daemon{} 369 daemon.configStore = &Config{ 370 CommonConfig: CommonConfig{ 371 ClusterStore: "memory://127.0.0.1", 372 ClusterAdvertise: "127.0.0.1:3333", 373 }, 374 } 375 376 if err := daemon.initDiscovery(daemon.configStore); err != nil { 377 t.Fatal(err) 378 } 379 380 expected := discovery.Entries{ 381 &discovery.Entry{Host: "127.0.0.1", Port: "3333"}, 382 } 383 384 stopCh := make(chan struct{}) 385 defer close(stopCh) 386 ch, errCh := daemon.discoveryWatcher.Watch(stopCh) 387 388 select { 389 case <-time.After(1 * time.Second): 390 t.Fatal("failed to get discovery advertisements in time") 391 case e := <-ch: 392 if !reflect.DeepEqual(e, expected) { 393 t.Fatalf("expected %v, got %v\n", expected, e) 394 } 395 case e := <-errCh: 396 t.Fatal(e) 397 } 398 399 valuesSets := make(map[string]interface{}) 400 valuesSets["cluster-store"] = "memory://127.0.0.1:2222" 401 valuesSets["cluster-advertise"] = "127.0.0.1:5555" 402 newConfig := &Config{ 403 CommonConfig: CommonConfig{ 404 ClusterStore: "memory://127.0.0.1:2222", 405 ClusterAdvertise: "127.0.0.1:5555", 406 valuesSet: valuesSets, 407 }, 408 } 409 410 expected = discovery.Entries{ 411 &discovery.Entry{Host: "127.0.0.1", Port: "5555"}, 412 } 413 414 if err := daemon.Reload(newConfig); err != nil { 415 t.Fatal(err) 416 } 417 ch, errCh = daemon.discoveryWatcher.Watch(stopCh) 418 419 select { 420 case <-time.After(1 * time.Second): 421 t.Fatal("failed to get discovery advertisements in time") 422 case e := <-ch: 423 if !reflect.DeepEqual(e, expected) { 424 t.Fatalf("expected %v, got %v\n", expected, e) 425 } 426 case e := <-errCh: 427 t.Fatal(e) 428 } 429 } 430 431 func TestDaemonDiscoveryReloadFromEmptyDiscovery(t *testing.T) { 432 daemon := &Daemon{} 433 daemon.configStore = &Config{} 434 435 valuesSet := make(map[string]interface{}) 436 valuesSet["cluster-store"] = "memory://127.0.0.1:2222" 437 valuesSet["cluster-advertise"] = "127.0.0.1:5555" 438 newConfig := &Config{ 439 CommonConfig: CommonConfig{ 440 ClusterStore: "memory://127.0.0.1:2222", 441 ClusterAdvertise: "127.0.0.1:5555", 442 valuesSet: valuesSet, 443 }, 444 } 445 446 expected := discovery.Entries{ 447 &discovery.Entry{Host: "127.0.0.1", Port: "5555"}, 448 } 449 450 if err := daemon.Reload(newConfig); err != nil { 451 t.Fatal(err) 452 } 453 stopCh := make(chan struct{}) 454 defer close(stopCh) 455 ch, errCh := daemon.discoveryWatcher.Watch(stopCh) 456 457 select { 458 case <-time.After(1 * time.Second): 459 t.Fatal("failed to get discovery advertisements in time") 460 case e := <-ch: 461 if !reflect.DeepEqual(e, expected) { 462 t.Fatalf("expected %v, got %v\n", expected, e) 463 } 464 case e := <-errCh: 465 t.Fatal(e) 466 } 467 } 468 469 func TestDaemonDiscoveryReloadOnlyClusterAdvertise(t *testing.T) { 470 daemon := &Daemon{} 471 daemon.configStore = &Config{ 472 CommonConfig: CommonConfig{ 473 ClusterStore: "memory://127.0.0.1", 474 }, 475 } 476 valuesSets := make(map[string]interface{}) 477 valuesSets["cluster-advertise"] = "127.0.0.1:5555" 478 newConfig := &Config{ 479 CommonConfig: CommonConfig{ 480 ClusterAdvertise: "127.0.0.1:5555", 481 valuesSet: valuesSets, 482 }, 483 } 484 expected := discovery.Entries{ 485 &discovery.Entry{Host: "127.0.0.1", Port: "5555"}, 486 } 487 488 if err := daemon.Reload(newConfig); err != nil { 489 t.Fatal(err) 490 } 491 stopCh := make(chan struct{}) 492 defer close(stopCh) 493 ch, errCh := daemon.discoveryWatcher.Watch(stopCh) 494 495 select { 496 case <-time.After(1 * time.Second): 497 t.Fatal("failed to get discovery advertisements in time") 498 case e := <-ch: 499 if !reflect.DeepEqual(e, expected) { 500 t.Fatalf("expected %v, got %v\n", expected, e) 501 } 502 case e := <-errCh: 503 t.Fatal(e) 504 } 505 506 }