github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/daemon/daemon_test.go (about) 1 // +build !solaris 2 3 package daemon 4 5 import ( 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "reflect" 10 "testing" 11 "time" 12 13 containertypes "github.com/docker/docker/api/types/container" 14 "github.com/docker/docker/container" 15 "github.com/docker/docker/pkg/discovery" 16 _ "github.com/docker/docker/pkg/discovery/memory" 17 "github.com/docker/docker/pkg/registrar" 18 "github.com/docker/docker/pkg/truncindex" 19 "github.com/docker/docker/registry" 20 "github.com/docker/docker/volume" 21 volumedrivers "github.com/docker/docker/volume/drivers" 22 "github.com/docker/docker/volume/local" 23 "github.com/docker/docker/volume/store" 24 "github.com/docker/go-connections/nat" 25 ) 26 27 // 28 // https://github.com/docker/docker/issues/8069 29 // 30 31 func TestGetContainer(t *testing.T) { 32 c1 := &container.Container{ 33 CommonContainer: container.CommonContainer{ 34 ID: "5a4ff6a163ad4533d22d69a2b8960bf7fafdcba06e72d2febdba229008b0bf57", 35 Name: "tender_bardeen", 36 }, 37 } 38 39 c2 := &container.Container{ 40 CommonContainer: container.CommonContainer{ 41 ID: "3cdbd1aa394fd68559fd1441d6eff2ab7c1e6363582c82febfaa8045df3bd8de", 42 Name: "drunk_hawking", 43 }, 44 } 45 46 c3 := &container.Container{ 47 CommonContainer: container.CommonContainer{ 48 ID: "3cdbd1aa394fd68559fd1441d6eff2abfafdcba06e72d2febdba229008b0bf57", 49 Name: "3cdbd1aa", 50 }, 51 } 52 53 c4 := &container.Container{ 54 CommonContainer: container.CommonContainer{ 55 ID: "75fb0b800922abdbef2d27e60abcdfaf7fb0698b2a96d22d3354da361a6ff4a5", 56 Name: "5a4ff6a163ad4533d22d69a2b8960bf7fafdcba06e72d2febdba229008b0bf57", 57 }, 58 } 59 60 c5 := &container.Container{ 61 CommonContainer: container.CommonContainer{ 62 ID: "d22d69a2b8960bf7fafdcba06e72d2febdba960bf7fafdcba06e72d2f9008b060b", 63 Name: "d22d69a2b896", 64 }, 65 } 66 67 store := container.NewMemoryStore() 68 store.Add(c1.ID, c1) 69 store.Add(c2.ID, c2) 70 store.Add(c3.ID, c3) 71 store.Add(c4.ID, c4) 72 store.Add(c5.ID, c5) 73 74 index := truncindex.NewTruncIndex([]string{}) 75 index.Add(c1.ID) 76 index.Add(c2.ID) 77 index.Add(c3.ID) 78 index.Add(c4.ID) 79 index.Add(c5.ID) 80 81 daemon := &Daemon{ 82 containers: store, 83 idIndex: index, 84 nameIndex: registrar.NewRegistrar(), 85 } 86 87 daemon.reserveName(c1.ID, c1.Name) 88 daemon.reserveName(c2.ID, c2.Name) 89 daemon.reserveName(c3.ID, c3.Name) 90 daemon.reserveName(c4.ID, c4.Name) 91 daemon.reserveName(c5.ID, c5.Name) 92 93 if container, _ := daemon.GetContainer("3cdbd1aa394fd68559fd1441d6eff2ab7c1e6363582c82febfaa8045df3bd8de"); container != c2 { 94 t.Fatal("Should explicitly match full container IDs") 95 } 96 97 if container, _ := daemon.GetContainer("75fb0b8009"); container != c4 { 98 t.Fatal("Should match a partial ID") 99 } 100 101 if container, _ := daemon.GetContainer("drunk_hawking"); container != c2 { 102 t.Fatal("Should match a full name") 103 } 104 105 // c3.Name is a partial match for both c3.ID and c2.ID 106 if c, _ := daemon.GetContainer("3cdbd1aa"); c != c3 { 107 t.Fatal("Should match a full name even though it collides with another container's ID") 108 } 109 110 if container, _ := daemon.GetContainer("d22d69a2b896"); container != c5 { 111 t.Fatal("Should match a container where the provided prefix is an exact match to the its name, and is also a prefix for its ID") 112 } 113 114 if _, err := daemon.GetContainer("3cdbd1"); err == nil { 115 t.Fatal("Should return an error when provided a prefix that partially matches multiple container ID's") 116 } 117 118 if _, err := daemon.GetContainer("nothing"); err == nil { 119 t.Fatal("Should return an error when provided a prefix that is neither a name or a partial match to an ID") 120 } 121 } 122 123 func initDaemonWithVolumeStore(tmp string) (*Daemon, error) { 124 var err error 125 daemon := &Daemon{ 126 repository: tmp, 127 root: tmp, 128 } 129 daemon.volumes, err = store.New(tmp) 130 if err != nil { 131 return nil, err 132 } 133 134 volumesDriver, err := local.New(tmp, 0, 0) 135 if err != nil { 136 return nil, err 137 } 138 volumedrivers.Register(volumesDriver, volumesDriver.Name()) 139 140 return daemon, nil 141 } 142 143 func TestValidContainerNames(t *testing.T) { 144 invalidNames := []string{"-rm", "&sdfsfd", "safd%sd"} 145 validNames := []string{"word-word", "word_word", "1weoid"} 146 147 for _, name := range invalidNames { 148 if validContainerNamePattern.MatchString(name) { 149 t.Fatalf("%q is not a valid container name and was returned as valid.", name) 150 } 151 } 152 153 for _, name := range validNames { 154 if !validContainerNamePattern.MatchString(name) { 155 t.Fatalf("%q is a valid container name and was returned as invalid.", name) 156 } 157 } 158 } 159 160 func TestContainerInitDNS(t *testing.T) { 161 tmp, err := ioutil.TempDir("", "docker-container-test-") 162 if err != nil { 163 t.Fatal(err) 164 } 165 defer os.RemoveAll(tmp) 166 167 containerID := "d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e" 168 containerPath := filepath.Join(tmp, containerID) 169 if err := os.MkdirAll(containerPath, 0755); err != nil { 170 t.Fatal(err) 171 } 172 173 config := `{"State":{"Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":2464,"ExitCode":0, 174 "Error":"","StartedAt":"2015-05-26T16:48:53.869308965Z","FinishedAt":"0001-01-01T00:00:00Z"}, 175 "ID":"d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e","Created":"2015-05-26T16:48:53.7987917Z","Path":"top", 176 "Args":[],"Config":{"Hostname":"d59df5276e7b","Domainname":"","User":"","Memory":0,"MemorySwap":0,"CpuShares":0,"Cpuset":"", 177 "AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"PortSpecs":null,"ExposedPorts":null,"Tty":true,"OpenStdin":true, 178 "StdinOnce":false,"Env":null,"Cmd":["top"],"Image":"ubuntu:latest","Volumes":null,"WorkingDir":"","Entrypoint":null, 179 "NetworkDisabled":false,"MacAddress":"","OnBuild":null,"Labels":{}},"Image":"07f8e8c5e66084bef8f848877857537ffe1c47edd01a93af27e7161672ad0e95", 180 "NetworkSettings":{"IPAddress":"172.17.0.1","IPPrefixLen":16,"MacAddress":"02:42:ac:11:00:01","LinkLocalIPv6Address":"fe80::42:acff:fe11:1", 181 "LinkLocalIPv6PrefixLen":64,"GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"Gateway":"172.17.42.1","IPv6Gateway":"","Bridge":"docker0","Ports":{}}, 182 "ResolvConfPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/resolv.conf", 183 "HostnamePath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/hostname", 184 "HostsPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/hosts", 185 "LogPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e-json.log", 186 "Name":"/ubuntu","Driver":"aufs","MountLabel":"","ProcessLabel":"","AppArmorProfile":"","RestartCount":0, 187 "UpdateDns":false,"Volumes":{},"VolumesRW":{},"AppliedVolumesFrom":null}` 188 189 // Container struct only used to retrieve path to config file 190 container := &container.Container{CommonContainer: container.CommonContainer{Root: containerPath}} 191 configPath, err := container.ConfigPath() 192 if err != nil { 193 t.Fatal(err) 194 } 195 if err = ioutil.WriteFile(configPath, []byte(config), 0644); err != nil { 196 t.Fatal(err) 197 } 198 199 hostConfig := `{"Binds":[],"ContainerIDFile":"","Memory":0,"MemorySwap":0,"CpuShares":0,"CpusetCpus":"", 200 "Privileged":false,"PortBindings":{},"Links":null,"PublishAllPorts":false,"Dns":null,"DnsOptions":null,"DnsSearch":null,"ExtraHosts":null,"VolumesFrom":null, 201 "Devices":[],"NetworkMode":"bridge","IpcMode":"","PidMode":"","CapAdd":null,"CapDrop":null,"RestartPolicy":{"Name":"no","MaximumRetryCount":0}, 202 "SecurityOpt":null,"ReadonlyRootfs":false,"Ulimits":null,"LogConfig":{"Type":"","Config":null},"CgroupParent":""}` 203 204 hostConfigPath, err := container.HostConfigPath() 205 if err != nil { 206 t.Fatal(err) 207 } 208 if err = ioutil.WriteFile(hostConfigPath, []byte(hostConfig), 0644); err != nil { 209 t.Fatal(err) 210 } 211 212 daemon, err := initDaemonWithVolumeStore(tmp) 213 if err != nil { 214 t.Fatal(err) 215 } 216 defer volumedrivers.Unregister(volume.DefaultDriverName) 217 218 c, err := daemon.load(containerID) 219 if err != nil { 220 t.Fatal(err) 221 } 222 223 if c.HostConfig.DNS == nil { 224 t.Fatal("Expected container DNS to not be nil") 225 } 226 227 if c.HostConfig.DNSSearch == nil { 228 t.Fatal("Expected container DNSSearch to not be nil") 229 } 230 231 if c.HostConfig.DNSOptions == nil { 232 t.Fatal("Expected container DNSOptions to not be nil") 233 } 234 } 235 236 func newPortNoError(proto, port string) nat.Port { 237 p, _ := nat.NewPort(proto, port) 238 return p 239 } 240 241 func TestMerge(t *testing.T) { 242 volumesImage := make(map[string]struct{}) 243 volumesImage["/test1"] = struct{}{} 244 volumesImage["/test2"] = struct{}{} 245 portsImage := make(nat.PortSet) 246 portsImage[newPortNoError("tcp", "1111")] = struct{}{} 247 portsImage[newPortNoError("tcp", "2222")] = struct{}{} 248 configImage := &containertypes.Config{ 249 ExposedPorts: portsImage, 250 Env: []string{"VAR1=1", "VAR2=2"}, 251 Volumes: volumesImage, 252 } 253 254 portsUser := make(nat.PortSet) 255 portsUser[newPortNoError("tcp", "2222")] = struct{}{} 256 portsUser[newPortNoError("tcp", "3333")] = struct{}{} 257 volumesUser := make(map[string]struct{}) 258 volumesUser["/test3"] = struct{}{} 259 configUser := &containertypes.Config{ 260 ExposedPorts: portsUser, 261 Env: []string{"VAR2=3", "VAR3=3"}, 262 Volumes: volumesUser, 263 } 264 265 if err := merge(configUser, configImage); err != nil { 266 t.Error(err) 267 } 268 269 if len(configUser.ExposedPorts) != 3 { 270 t.Fatalf("Expected 3 ExposedPorts, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts)) 271 } 272 for portSpecs := range configUser.ExposedPorts { 273 if portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" { 274 t.Fatalf("Expected 1111 or 2222 or 3333, found %s", portSpecs) 275 } 276 } 277 if len(configUser.Env) != 3 { 278 t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env)) 279 } 280 for _, env := range configUser.Env { 281 if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" { 282 t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env) 283 } 284 } 285 286 if len(configUser.Volumes) != 3 { 287 t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes)) 288 } 289 for v := range configUser.Volumes { 290 if v != "/test1" && v != "/test2" && v != "/test3" { 291 t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v) 292 } 293 } 294 295 ports, _, err := nat.ParsePortSpecs([]string{"0000"}) 296 if err != nil { 297 t.Error(err) 298 } 299 configImage2 := &containertypes.Config{ 300 ExposedPorts: ports, 301 } 302 303 if err := merge(configUser, configImage2); err != nil { 304 t.Error(err) 305 } 306 307 if len(configUser.ExposedPorts) != 4 { 308 t.Fatalf("Expected 4 ExposedPorts, 0000, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts)) 309 } 310 for portSpecs := range configUser.ExposedPorts { 311 if portSpecs.Port() != "0" && portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" { 312 t.Fatalf("Expected %q or %q or %q or %q, found %s", 0, 1111, 2222, 3333, portSpecs) 313 } 314 } 315 } 316 317 func TestDaemonReloadLabels(t *testing.T) { 318 daemon := &Daemon{} 319 daemon.configStore = &Config{ 320 CommonConfig: CommonConfig{ 321 Labels: []string{"foo:bar"}, 322 }, 323 } 324 325 valuesSets := make(map[string]interface{}) 326 valuesSets["labels"] = "foo:baz" 327 newConfig := &Config{ 328 CommonConfig: CommonConfig{ 329 Labels: []string{"foo:baz"}, 330 valuesSet: valuesSets, 331 }, 332 } 333 334 if err := daemon.Reload(newConfig); err != nil { 335 t.Fatal(err) 336 } 337 338 label := daemon.configStore.Labels[0] 339 if label != "foo:baz" { 340 t.Fatalf("Expected daemon label `foo:baz`, got %s", label) 341 } 342 } 343 344 func TestDaemonReloadMirrors(t *testing.T) { 345 daemon := &Daemon{} 346 347 daemon.RegistryService = registry.NewService(registry.ServiceOptions{ 348 InsecureRegistries: []string{}, 349 Mirrors: []string{ 350 "https://mirror.test1.com", 351 "https://mirror.test2.com", // this will be removed when reloading 352 "https://mirror.test3.com", // this will be removed when reloading 353 }, 354 }) 355 356 daemon.configStore = &Config{} 357 358 type pair struct { 359 valid bool 360 mirrors []string 361 after []string 362 } 363 364 loadMirrors := []pair{ 365 { 366 valid: false, 367 mirrors: []string{"10.10.1.11:5000"}, // this mirror is invalid 368 after: []string{}, 369 }, 370 { 371 valid: false, 372 mirrors: []string{"mirror.test1.com"}, // this mirror is invalid 373 after: []string{}, 374 }, 375 { 376 valid: false, 377 mirrors: []string{"10.10.1.11:5000", "mirror.test1.com"}, // mirrors are invalid 378 after: []string{}, 379 }, 380 { 381 valid: true, 382 mirrors: []string{"https://mirror.test1.com", "https://mirror.test4.com"}, 383 after: []string{"https://mirror.test1.com/", "https://mirror.test4.com/"}, 384 }, 385 } 386 387 for _, value := range loadMirrors { 388 valuesSets := make(map[string]interface{}) 389 valuesSets["registry-mirrors"] = value.mirrors 390 391 newConfig := &Config{ 392 CommonConfig: CommonConfig{ 393 ServiceOptions: registry.ServiceOptions{ 394 Mirrors: value.mirrors, 395 }, 396 valuesSet: valuesSets, 397 }, 398 } 399 400 err := daemon.Reload(newConfig) 401 if !value.valid && err == nil { 402 // mirrors should be invalid, should be a non-nil error 403 t.Fatalf("Expected daemon reload error with invalid mirrors: %s, while get nil", value.mirrors) 404 } 405 406 if value.valid { 407 if err != nil { 408 // mirrors should be valid, should be no error 409 t.Fatal(err) 410 } 411 registryService := daemon.RegistryService.ServiceConfig() 412 413 if len(registryService.Mirrors) != len(value.after) { 414 t.Fatalf("Expected %d daemon mirrors %s while get %d with %s", 415 len(value.after), 416 value.after, 417 len(registryService.Mirrors), 418 registryService.Mirrors) 419 } 420 421 dataMap := map[string]struct{}{} 422 423 for _, mirror := range registryService.Mirrors { 424 if _, exist := dataMap[mirror]; !exist { 425 dataMap[mirror] = struct{}{} 426 } 427 } 428 429 for _, address := range value.after { 430 if _, exist := dataMap[address]; !exist { 431 t.Fatalf("Expected %s in daemon mirrors, while get none", address) 432 } 433 } 434 } 435 } 436 } 437 438 func TestDaemonReloadInsecureRegistries(t *testing.T) { 439 daemon := &Daemon{} 440 // initialize daemon with existing insecure registries: "127.0.0.0/8", "10.10.1.11:5000", "10.10.1.22:5000" 441 daemon.RegistryService = registry.NewService(registry.ServiceOptions{ 442 InsecureRegistries: []string{ 443 "127.0.0.0/8", 444 "10.10.1.11:5000", 445 "10.10.1.22:5000", // this will be removed when reloading 446 "docker1.com", 447 "docker2.com", // this will be removed when reloading 448 }, 449 }) 450 451 daemon.configStore = &Config{} 452 453 insecureRegistries := []string{ 454 "127.0.0.0/8", // this will be kept 455 "10.10.1.11:5000", // this will be kept 456 "10.10.1.33:5000", // this will be newly added 457 "docker1.com", // this will be kept 458 "docker3.com", // this will be newly added 459 } 460 461 valuesSets := make(map[string]interface{}) 462 valuesSets["insecure-registries"] = insecureRegistries 463 464 newConfig := &Config{ 465 CommonConfig: CommonConfig{ 466 ServiceOptions: registry.ServiceOptions{ 467 InsecureRegistries: insecureRegistries, 468 }, 469 valuesSet: valuesSets, 470 }, 471 } 472 473 if err := daemon.Reload(newConfig); err != nil { 474 t.Fatal(err) 475 } 476 477 // After Reload, daemon.RegistryService will be changed which is useful 478 // for registry communication in daemon. 479 registries := daemon.RegistryService.ServiceConfig() 480 481 // After Reload(), newConfig has come to registries.InsecureRegistryCIDRs and registries.IndexConfigs in daemon. 482 // Then collect registries.InsecureRegistryCIDRs in dataMap. 483 // When collecting, we need to convert CIDRS into string as a key, 484 // while the times of key appears as value. 485 dataMap := map[string]int{} 486 for _, value := range registries.InsecureRegistryCIDRs { 487 if _, ok := dataMap[value.String()]; !ok { 488 dataMap[value.String()] = 1 489 } else { 490 dataMap[value.String()]++ 491 } 492 } 493 494 for _, value := range registries.IndexConfigs { 495 if _, ok := dataMap[value.Name]; !ok { 496 dataMap[value.Name] = 1 497 } else { 498 dataMap[value.Name]++ 499 } 500 } 501 502 // Finally compare dataMap with the original insecureRegistries. 503 // Each value in insecureRegistries should appear in daemon's insecure registries, 504 // and each can only appear exactly ONCE. 505 for _, r := range insecureRegistries { 506 if value, ok := dataMap[r]; !ok { 507 t.Fatalf("Expected daemon insecure registry %s, got none", r) 508 } else if value != 1 { 509 t.Fatalf("Expected only 1 daemon insecure registry %s, got %d", r, value) 510 } 511 } 512 513 // assert if "10.10.1.22:5000" is removed when reloading 514 if value, ok := dataMap["10.10.1.22:5000"]; ok { 515 t.Fatalf("Expected no insecure registry of 10.10.1.22:5000, got %d", value) 516 } 517 518 // assert if "docker2.com" is removed when reloading 519 if value, ok := dataMap["docker2.com"]; ok { 520 t.Fatalf("Expected no insecure registry of docker2.com, got %d", value) 521 } 522 } 523 524 func TestDaemonReloadNotAffectOthers(t *testing.T) { 525 daemon := &Daemon{} 526 daemon.configStore = &Config{ 527 CommonConfig: CommonConfig{ 528 Labels: []string{"foo:bar"}, 529 Debug: true, 530 }, 531 } 532 533 valuesSets := make(map[string]interface{}) 534 valuesSets["labels"] = "foo:baz" 535 newConfig := &Config{ 536 CommonConfig: CommonConfig{ 537 Labels: []string{"foo:baz"}, 538 valuesSet: valuesSets, 539 }, 540 } 541 542 if err := daemon.Reload(newConfig); err != nil { 543 t.Fatal(err) 544 } 545 546 label := daemon.configStore.Labels[0] 547 if label != "foo:baz" { 548 t.Fatalf("Expected daemon label `foo:baz`, got %s", label) 549 } 550 debug := daemon.configStore.Debug 551 if !debug { 552 t.Fatalf("Expected debug 'enabled', got 'disabled'") 553 } 554 } 555 556 func TestDaemonDiscoveryReload(t *testing.T) { 557 daemon := &Daemon{} 558 daemon.configStore = &Config{ 559 CommonConfig: CommonConfig{ 560 ClusterStore: "memory://127.0.0.1", 561 ClusterAdvertise: "127.0.0.1:3333", 562 }, 563 } 564 565 if err := daemon.initDiscovery(daemon.configStore); err != nil { 566 t.Fatal(err) 567 } 568 569 expected := discovery.Entries{ 570 &discovery.Entry{Host: "127.0.0.1", Port: "3333"}, 571 } 572 573 select { 574 case <-time.After(10 * time.Second): 575 t.Fatal("timeout waiting for discovery") 576 case <-daemon.discoveryWatcher.ReadyCh(): 577 } 578 579 stopCh := make(chan struct{}) 580 defer close(stopCh) 581 ch, errCh := daemon.discoveryWatcher.Watch(stopCh) 582 583 select { 584 case <-time.After(1 * time.Second): 585 t.Fatal("failed to get discovery advertisements in time") 586 case e := <-ch: 587 if !reflect.DeepEqual(e, expected) { 588 t.Fatalf("expected %v, got %v\n", expected, e) 589 } 590 case e := <-errCh: 591 t.Fatal(e) 592 } 593 594 valuesSets := make(map[string]interface{}) 595 valuesSets["cluster-store"] = "memory://127.0.0.1:2222" 596 valuesSets["cluster-advertise"] = "127.0.0.1:5555" 597 newConfig := &Config{ 598 CommonConfig: CommonConfig{ 599 ClusterStore: "memory://127.0.0.1:2222", 600 ClusterAdvertise: "127.0.0.1:5555", 601 valuesSet: valuesSets, 602 }, 603 } 604 605 expected = discovery.Entries{ 606 &discovery.Entry{Host: "127.0.0.1", Port: "5555"}, 607 } 608 609 if err := daemon.Reload(newConfig); err != nil { 610 t.Fatal(err) 611 } 612 613 select { 614 case <-time.After(10 * time.Second): 615 t.Fatal("timeout waiting for discovery") 616 case <-daemon.discoveryWatcher.ReadyCh(): 617 } 618 619 ch, errCh = daemon.discoveryWatcher.Watch(stopCh) 620 621 select { 622 case <-time.After(1 * time.Second): 623 t.Fatal("failed to get discovery advertisements in time") 624 case e := <-ch: 625 if !reflect.DeepEqual(e, expected) { 626 t.Fatalf("expected %v, got %v\n", expected, e) 627 } 628 case e := <-errCh: 629 t.Fatal(e) 630 } 631 } 632 633 func TestDaemonDiscoveryReloadFromEmptyDiscovery(t *testing.T) { 634 daemon := &Daemon{} 635 daemon.configStore = &Config{} 636 637 valuesSet := make(map[string]interface{}) 638 valuesSet["cluster-store"] = "memory://127.0.0.1:2222" 639 valuesSet["cluster-advertise"] = "127.0.0.1:5555" 640 newConfig := &Config{ 641 CommonConfig: CommonConfig{ 642 ClusterStore: "memory://127.0.0.1:2222", 643 ClusterAdvertise: "127.0.0.1:5555", 644 valuesSet: valuesSet, 645 }, 646 } 647 648 expected := discovery.Entries{ 649 &discovery.Entry{Host: "127.0.0.1", Port: "5555"}, 650 } 651 652 if err := daemon.Reload(newConfig); err != nil { 653 t.Fatal(err) 654 } 655 656 select { 657 case <-time.After(10 * time.Second): 658 t.Fatal("timeout waiting for discovery") 659 case <-daemon.discoveryWatcher.ReadyCh(): 660 } 661 662 stopCh := make(chan struct{}) 663 defer close(stopCh) 664 ch, errCh := daemon.discoveryWatcher.Watch(stopCh) 665 666 select { 667 case <-time.After(1 * time.Second): 668 t.Fatal("failed to get discovery advertisements in time") 669 case e := <-ch: 670 if !reflect.DeepEqual(e, expected) { 671 t.Fatalf("expected %v, got %v\n", expected, e) 672 } 673 case e := <-errCh: 674 t.Fatal(e) 675 } 676 } 677 678 func TestDaemonDiscoveryReloadOnlyClusterAdvertise(t *testing.T) { 679 daemon := &Daemon{} 680 daemon.configStore = &Config{ 681 CommonConfig: CommonConfig{ 682 ClusterStore: "memory://127.0.0.1", 683 }, 684 } 685 valuesSets := make(map[string]interface{}) 686 valuesSets["cluster-advertise"] = "127.0.0.1:5555" 687 newConfig := &Config{ 688 CommonConfig: CommonConfig{ 689 ClusterAdvertise: "127.0.0.1:5555", 690 valuesSet: valuesSets, 691 }, 692 } 693 expected := discovery.Entries{ 694 &discovery.Entry{Host: "127.0.0.1", Port: "5555"}, 695 } 696 697 if err := daemon.Reload(newConfig); err != nil { 698 t.Fatal(err) 699 } 700 701 select { 702 case <-daemon.discoveryWatcher.ReadyCh(): 703 case <-time.After(10 * time.Second): 704 t.Fatal("Timeout waiting for discovery") 705 } 706 stopCh := make(chan struct{}) 707 defer close(stopCh) 708 ch, errCh := daemon.discoveryWatcher.Watch(stopCh) 709 710 select { 711 case <-time.After(1 * time.Second): 712 t.Fatal("failed to get discovery advertisements in time") 713 case e := <-ch: 714 if !reflect.DeepEqual(e, expected) { 715 t.Fatalf("expected %v, got %v\n", expected, e) 716 } 717 case e := <-errCh: 718 t.Fatal(e) 719 } 720 721 }