github.com/uppal0016/docker_new@v0.0.0-20240123060250-1c98be13ac2c/integration-cli/docker_cli_daemon_test.go (about) 1 // +build daemon,!windows 2 3 package main 4 5 import ( 6 "bytes" 7 "encoding/json" 8 "fmt" 9 "io" 10 "io/ioutil" 11 "net" 12 "os" 13 "os/exec" 14 "path" 15 "path/filepath" 16 "regexp" 17 "strconv" 18 "strings" 19 "sync" 20 "syscall" 21 "time" 22 23 "github.com/docker/docker/pkg/integration/checker" 24 "github.com/docker/go-units" 25 "github.com/docker/libnetwork/iptables" 26 "github.com/docker/libtrust" 27 "github.com/go-check/check" 28 "github.com/kr/pty" 29 ) 30 31 func (s *DockerDaemonSuite) TestDaemonRestartWithRunningContainersPorts(c *check.C) { 32 if err := s.d.StartWithBusybox(); err != nil { 33 c.Fatalf("Could not start daemon with busybox: %v", err) 34 } 35 36 if out, err := s.d.Cmd("run", "-d", "--name", "top1", "-p", "1234:80", "--restart", "always", "busybox:latest", "top"); err != nil { 37 c.Fatalf("Could not run top1: err=%v\n%s", err, out) 38 } 39 // --restart=no by default 40 if out, err := s.d.Cmd("run", "-d", "--name", "top2", "-p", "80", "busybox:latest", "top"); err != nil { 41 c.Fatalf("Could not run top2: err=%v\n%s", err, out) 42 } 43 44 testRun := func(m map[string]bool, prefix string) { 45 var format string 46 for cont, shouldRun := range m { 47 out, err := s.d.Cmd("ps") 48 if err != nil { 49 c.Fatalf("Could not run ps: err=%v\n%q", err, out) 50 } 51 if shouldRun { 52 format = "%scontainer %q is not running" 53 } else { 54 format = "%scontainer %q is running" 55 } 56 if shouldRun != strings.Contains(out, cont) { 57 c.Fatalf(format, prefix, cont) 58 } 59 } 60 } 61 62 testRun(map[string]bool{"top1": true, "top2": true}, "") 63 64 if err := s.d.Restart(); err != nil { 65 c.Fatalf("Could not restart daemon: %v", err) 66 } 67 testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ") 68 } 69 70 func (s *DockerDaemonSuite) TestDaemonRestartWithVolumesRefs(c *check.C) { 71 if err := s.d.StartWithBusybox(); err != nil { 72 c.Fatal(err) 73 } 74 75 if out, err := s.d.Cmd("run", "--name", "volrestarttest1", "-v", "/foo", "busybox"); err != nil { 76 c.Fatal(err, out) 77 } 78 79 if err := s.d.Restart(); err != nil { 80 c.Fatal(err) 81 } 82 83 if _, err := s.d.Cmd("run", "-d", "--volumes-from", "volrestarttest1", "--name", "volrestarttest2", "busybox", "top"); err != nil { 84 c.Fatal(err) 85 } 86 87 if out, err := s.d.Cmd("rm", "-fv", "volrestarttest2"); err != nil { 88 c.Fatal(err, out) 89 } 90 91 out, err := s.d.Cmd("inspect", "-f", "{{json .Mounts}}", "volrestarttest1") 92 c.Assert(err, check.IsNil) 93 94 if _, err := inspectMountPointJSON(out, "/foo"); err != nil { 95 c.Fatalf("Expected volume to exist: /foo, error: %v\n", err) 96 } 97 } 98 99 // #11008 100 func (s *DockerDaemonSuite) TestDaemonRestartUnlessStopped(c *check.C) { 101 err := s.d.StartWithBusybox() 102 c.Assert(err, check.IsNil) 103 104 out, err := s.d.Cmd("run", "-d", "--name", "top1", "--restart", "always", "busybox:latest", "top") 105 c.Assert(err, check.IsNil, check.Commentf("run top1: %v", out)) 106 107 out, err = s.d.Cmd("run", "-d", "--name", "top2", "--restart", "unless-stopped", "busybox:latest", "top") 108 c.Assert(err, check.IsNil, check.Commentf("run top2: %v", out)) 109 110 testRun := func(m map[string]bool, prefix string) { 111 var format string 112 for name, shouldRun := range m { 113 out, err := s.d.Cmd("ps") 114 c.Assert(err, check.IsNil, check.Commentf("run ps: %v", out)) 115 if shouldRun { 116 format = "%scontainer %q is not running" 117 } else { 118 format = "%scontainer %q is running" 119 } 120 c.Assert(strings.Contains(out, name), check.Equals, shouldRun, check.Commentf(format, prefix, name)) 121 } 122 } 123 124 // both running 125 testRun(map[string]bool{"top1": true, "top2": true}, "") 126 127 out, err = s.d.Cmd("stop", "top1") 128 c.Assert(err, check.IsNil, check.Commentf(out)) 129 130 out, err = s.d.Cmd("stop", "top2") 131 c.Assert(err, check.IsNil, check.Commentf(out)) 132 133 // both stopped 134 testRun(map[string]bool{"top1": false, "top2": false}, "") 135 136 err = s.d.Restart() 137 c.Assert(err, check.IsNil) 138 139 // restart=always running 140 testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ") 141 142 out, err = s.d.Cmd("start", "top2") 143 c.Assert(err, check.IsNil, check.Commentf("start top2: %v", out)) 144 145 err = s.d.Restart() 146 c.Assert(err, check.IsNil) 147 148 // both running 149 testRun(map[string]bool{"top1": true, "top2": true}, "After second daemon restart: ") 150 151 } 152 153 func (s *DockerDaemonSuite) TestDaemonRestartOnFailure(c *check.C) { 154 err := s.d.StartWithBusybox() 155 c.Assert(err, check.IsNil) 156 157 out, err := s.d.Cmd("run", "-d", "--name", "test1", "--restart", "on-failure:3", "busybox:latest", "false") 158 c.Assert(err, check.IsNil, check.Commentf("run top1: %v", out)) 159 160 // wait test1 to stop 161 hostArgs := []string{"--host", s.d.sock()} 162 err = waitInspectWithArgs("test1", "{{.State.Running}} {{.State.Restarting}}", "false false", 10*time.Second, hostArgs...) 163 c.Assert(err, checker.IsNil, check.Commentf("test1 should exit but not")) 164 165 // record last start time 166 out, err = s.d.Cmd("inspect", "-f={{.State.StartedAt}}", "test1") 167 c.Assert(err, checker.IsNil, check.Commentf("out: %v", out)) 168 lastStartTime := out 169 170 err = s.d.Restart() 171 c.Assert(err, check.IsNil) 172 173 // test1 shouldn't restart at all 174 err = waitInspectWithArgs("test1", "{{.State.Running}} {{.State.Restarting}}", "false false", 0, hostArgs...) 175 c.Assert(err, checker.IsNil, check.Commentf("test1 should exit but not")) 176 177 // make sure test1 isn't restarted when daemon restart 178 // if "StartAt" time updates, means test1 was once restarted. 179 out, err = s.d.Cmd("inspect", "-f={{.State.StartedAt}}", "test1") 180 c.Assert(err, checker.IsNil, check.Commentf("out: %v", out)) 181 c.Assert(out, checker.Equals, lastStartTime, check.Commentf("test1 shouldn't start after daemon restarts")) 182 } 183 184 func (s *DockerDaemonSuite) TestDaemonStartIptablesFalse(c *check.C) { 185 if err := s.d.Start("--iptables=false"); err != nil { 186 c.Fatalf("we should have been able to start the daemon with passing iptables=false: %v", err) 187 } 188 } 189 190 // Make sure we cannot shrink base device at daemon restart. 191 func (s *DockerDaemonSuite) TestDaemonRestartWithInvalidBasesize(c *check.C) { 192 testRequires(c, Devicemapper) 193 c.Assert(s.d.Start(), check.IsNil) 194 195 oldBasesizeBytes := s.d.getBaseDeviceSize(c) 196 var newBasesizeBytes int64 = 1073741824 //1GB in bytes 197 198 if newBasesizeBytes < oldBasesizeBytes { 199 err := s.d.Restart("--storage-opt", fmt.Sprintf("dm.basesize=%d", newBasesizeBytes)) 200 c.Assert(err, check.IsNil, check.Commentf("daemon should not have started as new base device size is less than existing base device size: %v", err)) 201 } 202 c.Assert(s.d.Stop(), check.IsNil) 203 } 204 205 // Make sure we can grow base device at daemon restart. 206 func (s *DockerDaemonSuite) TestDaemonRestartWithIncreasedBasesize(c *check.C) { 207 testRequires(c, Devicemapper) 208 c.Assert(s.d.Start(), check.IsNil) 209 210 oldBasesizeBytes := s.d.getBaseDeviceSize(c) 211 212 var newBasesizeBytes int64 = 53687091200 //50GB in bytes 213 214 if newBasesizeBytes < oldBasesizeBytes { 215 c.Skip(fmt.Sprintf("New base device size (%v) must be greater than (%s)", units.HumanSize(float64(newBasesizeBytes)), units.HumanSize(float64(oldBasesizeBytes)))) 216 } 217 218 err := s.d.Restart("--storage-opt", fmt.Sprintf("dm.basesize=%d", newBasesizeBytes)) 219 c.Assert(err, check.IsNil, check.Commentf("we should have been able to start the daemon with increased base device size: %v", err)) 220 221 basesizeAfterRestart := s.d.getBaseDeviceSize(c) 222 newBasesize, err := convertBasesize(newBasesizeBytes) 223 c.Assert(err, check.IsNil, check.Commentf("Error in converting base device size: %v", err)) 224 c.Assert(newBasesize, check.Equals, basesizeAfterRestart, check.Commentf("Basesize passed is not equal to Basesize set")) 225 c.Assert(s.d.Stop(), check.IsNil) 226 } 227 228 // Issue #8444: If docker0 bridge is modified (intentionally or unintentionally) and 229 // no longer has an IP associated, we should gracefully handle that case and associate 230 // an IP with it rather than fail daemon start 231 func (s *DockerDaemonSuite) TestDaemonStartBridgeWithoutIPAssociation(c *check.C) { 232 // rather than depending on brctl commands to verify docker0 is created and up 233 // let's start the daemon and stop it, and then make a modification to run the 234 // actual test 235 if err := s.d.Start(); err != nil { 236 c.Fatalf("Could not start daemon: %v", err) 237 } 238 if err := s.d.Stop(); err != nil { 239 c.Fatalf("Could not stop daemon: %v", err) 240 } 241 242 // now we will remove the ip from docker0 and then try starting the daemon 243 ipCmd := exec.Command("ip", "addr", "flush", "dev", "docker0") 244 stdout, stderr, _, err := runCommandWithStdoutStderr(ipCmd) 245 if err != nil { 246 c.Fatalf("failed to remove docker0 IP association: %v, stdout: %q, stderr: %q", err, stdout, stderr) 247 } 248 249 if err := s.d.Start(); err != nil { 250 warning := "**WARNING: Docker bridge network in bad state--delete docker0 bridge interface to fix" 251 c.Fatalf("Could not start daemon when docker0 has no IP address: %v\n%s", err, warning) 252 } 253 } 254 255 func (s *DockerDaemonSuite) TestDaemonIptablesClean(c *check.C) { 256 if err := s.d.StartWithBusybox(); err != nil { 257 c.Fatalf("Could not start daemon with busybox: %v", err) 258 } 259 260 if out, err := s.d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil { 261 c.Fatalf("Could not run top: %s, %v", out, err) 262 } 263 264 // get output from iptables with container running 265 ipTablesSearchString := "tcp dpt:80" 266 ipTablesCmd := exec.Command("iptables", "-nvL") 267 out, _, err := runCommandWithOutput(ipTablesCmd) 268 if err != nil { 269 c.Fatalf("Could not run iptables -nvL: %s, %v", out, err) 270 } 271 272 if !strings.Contains(out, ipTablesSearchString) { 273 c.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out) 274 } 275 276 if err := s.d.Stop(); err != nil { 277 c.Fatalf("Could not stop daemon: %v", err) 278 } 279 280 // get output from iptables after restart 281 ipTablesCmd = exec.Command("iptables", "-nvL") 282 out, _, err = runCommandWithOutput(ipTablesCmd) 283 if err != nil { 284 c.Fatalf("Could not run iptables -nvL: %s, %v", out, err) 285 } 286 287 if strings.Contains(out, ipTablesSearchString) { 288 c.Fatalf("iptables output should not have contained %q, but was %q", ipTablesSearchString, out) 289 } 290 } 291 292 func (s *DockerDaemonSuite) TestDaemonIptablesCreate(c *check.C) { 293 if err := s.d.StartWithBusybox(); err != nil { 294 c.Fatalf("Could not start daemon with busybox: %v", err) 295 } 296 297 if out, err := s.d.Cmd("run", "-d", "--name", "top", "--restart=always", "-p", "80", "busybox:latest", "top"); err != nil { 298 c.Fatalf("Could not run top: %s, %v", out, err) 299 } 300 301 // get output from iptables with container running 302 ipTablesSearchString := "tcp dpt:80" 303 ipTablesCmd := exec.Command("iptables", "-nvL") 304 out, _, err := runCommandWithOutput(ipTablesCmd) 305 if err != nil { 306 c.Fatalf("Could not run iptables -nvL: %s, %v", out, err) 307 } 308 309 if !strings.Contains(out, ipTablesSearchString) { 310 c.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out) 311 } 312 313 if err := s.d.Restart(); err != nil { 314 c.Fatalf("Could not restart daemon: %v", err) 315 } 316 317 // make sure the container is not running 318 runningOut, err := s.d.Cmd("inspect", "--format='{{.State.Running}}'", "top") 319 if err != nil { 320 c.Fatalf("Could not inspect on container: %s, %v", out, err) 321 } 322 if strings.TrimSpace(runningOut) != "true" { 323 c.Fatalf("Container should have been restarted after daemon restart. Status running should have been true but was: %q", strings.TrimSpace(runningOut)) 324 } 325 326 // get output from iptables after restart 327 ipTablesCmd = exec.Command("iptables", "-nvL") 328 out, _, err = runCommandWithOutput(ipTablesCmd) 329 if err != nil { 330 c.Fatalf("Could not run iptables -nvL: %s, %v", out, err) 331 } 332 333 if !strings.Contains(out, ipTablesSearchString) { 334 c.Fatalf("iptables output after restart should have contained %q, but was %q", ipTablesSearchString, out) 335 } 336 } 337 338 // TestDaemonIPv6Enabled checks that when the daemon is started with --ipv6=true that the docker0 bridge 339 // has the fe80::1 address and that a container is assigned a link-local address 340 func (s *DockerSuite) TestDaemonIPv6Enabled(c *check.C) { 341 testRequires(c, IPv6) 342 343 if err := setupV6(); err != nil { 344 c.Fatal("Could not set up host for IPv6 tests") 345 } 346 347 d := NewDaemon(c) 348 349 if err := d.StartWithBusybox("--ipv6"); err != nil { 350 c.Fatal(err) 351 } 352 defer d.Stop() 353 354 iface, err := net.InterfaceByName("docker0") 355 if err != nil { 356 c.Fatalf("Error getting docker0 interface: %v", err) 357 } 358 359 addrs, err := iface.Addrs() 360 if err != nil { 361 c.Fatalf("Error getting addresses for docker0 interface: %v", err) 362 } 363 364 var found bool 365 expected := "fe80::1/64" 366 367 for i := range addrs { 368 if addrs[i].String() == expected { 369 found = true 370 } 371 } 372 373 if !found { 374 c.Fatalf("Bridge does not have an IPv6 Address") 375 } 376 377 if out, err := d.Cmd("run", "-itd", "--name=ipv6test", "busybox:latest"); err != nil { 378 c.Fatalf("Could not run container: %s, %v", out, err) 379 } 380 381 out, err := d.Cmd("inspect", "--format", "'{{.NetworkSettings.Networks.bridge.LinkLocalIPv6Address}}'", "ipv6test") 382 out = strings.Trim(out, " \r\n'") 383 384 if err != nil { 385 c.Fatalf("Error inspecting container: %s, %v", out, err) 386 } 387 388 if ip := net.ParseIP(out); ip == nil { 389 c.Fatalf("Container should have a link-local IPv6 address") 390 } 391 392 out, err = d.Cmd("inspect", "--format", "'{{.NetworkSettings.Networks.bridge.GlobalIPv6Address}}'", "ipv6test") 393 out = strings.Trim(out, " \r\n'") 394 395 if err != nil { 396 c.Fatalf("Error inspecting container: %s, %v", out, err) 397 } 398 399 if ip := net.ParseIP(out); ip != nil { 400 c.Fatalf("Container should not have a global IPv6 address: %v", out) 401 } 402 403 if err := teardownV6(); err != nil { 404 c.Fatal("Could not perform teardown for IPv6 tests") 405 } 406 407 } 408 409 // TestDaemonIPv6FixedCIDR checks that when the daemon is started with --ipv6=true and a fixed CIDR 410 // that running containers are given a link-local and global IPv6 address 411 func (s *DockerDaemonSuite) TestDaemonIPv6FixedCIDR(c *check.C) { 412 // IPv6 setup is messing with local bridge address. 413 testRequires(c, SameHostDaemon) 414 err := setupV6() 415 c.Assert(err, checker.IsNil, check.Commentf("Could not set up host for IPv6 tests")) 416 417 err = s.d.StartWithBusybox("--ipv6", "--fixed-cidr-v6='2001:db8:2::/64'", "--default-gateway-v6='2001:db8:2::100'") 418 c.Assert(err, checker.IsNil, check.Commentf("Could not start daemon with busybox: %v", err)) 419 420 out, err := s.d.Cmd("run", "-itd", "--name=ipv6test", "busybox:latest") 421 c.Assert(err, checker.IsNil, check.Commentf("Could not run container: %s, %v", out, err)) 422 423 out, err = s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.Networks.bridge.GlobalIPv6Address}}'", "ipv6test") 424 out = strings.Trim(out, " \r\n'") 425 426 c.Assert(err, checker.IsNil, check.Commentf(out)) 427 428 ip := net.ParseIP(out) 429 c.Assert(ip, checker.NotNil, check.Commentf("Container should have a global IPv6 address")) 430 431 out, err = s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.Networks.bridge.IPv6Gateway}}'", "ipv6test") 432 c.Assert(err, checker.IsNil, check.Commentf(out)) 433 434 c.Assert(strings.Trim(out, " \r\n'"), checker.Equals, "2001:db8:2::100", check.Commentf("Container should have a global IPv6 gateway")) 435 436 err = teardownV6() 437 c.Assert(err, checker.IsNil, check.Commentf("Could not perform teardown for IPv6 tests")) 438 } 439 440 // TestDaemonIPv6FixedCIDRAndMac checks that when the daemon is started with ipv6 fixed CIDR 441 // the running containers are given a an IPv6 address derived from the MAC address and the ipv6 fixed CIDR 442 func (s *DockerDaemonSuite) TestDaemonIPv6FixedCIDRAndMac(c *check.C) { 443 // IPv6 setup is messing with local bridge address. 444 testRequires(c, SameHostDaemon) 445 err := setupV6() 446 c.Assert(err, checker.IsNil) 447 448 err = s.d.StartWithBusybox("--ipv6", "--fixed-cidr-v6='2001:db8:1::/64'") 449 c.Assert(err, checker.IsNil) 450 451 out, err := s.d.Cmd("run", "-itd", "--name=ipv6test", "--mac-address", "AA:BB:CC:DD:EE:FF", "busybox") 452 c.Assert(err, checker.IsNil) 453 454 out, err = s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.Networks.bridge.GlobalIPv6Address}}'", "ipv6test") 455 c.Assert(err, checker.IsNil) 456 c.Assert(strings.Trim(out, " \r\n'"), checker.Equals, "2001:db8:1::aabb:ccdd:eeff") 457 458 err = teardownV6() 459 c.Assert(err, checker.IsNil) 460 } 461 462 func (s *DockerDaemonSuite) TestDaemonLogLevelWrong(c *check.C) { 463 c.Assert(s.d.Start("--log-level=bogus"), check.NotNil, check.Commentf("Daemon shouldn't start with wrong log level")) 464 } 465 466 func (s *DockerDaemonSuite) TestDaemonLogLevelDebug(c *check.C) { 467 if err := s.d.Start("--log-level=debug"); err != nil { 468 c.Fatal(err) 469 } 470 content, _ := ioutil.ReadFile(s.d.logFile.Name()) 471 if !strings.Contains(string(content), `level=debug`) { 472 c.Fatalf(`Missing level="debug" in log file:\n%s`, string(content)) 473 } 474 } 475 476 func (s *DockerDaemonSuite) TestDaemonLogLevelFatal(c *check.C) { 477 // we creating new daemons to create new logFile 478 if err := s.d.Start("--log-level=fatal"); err != nil { 479 c.Fatal(err) 480 } 481 content, _ := ioutil.ReadFile(s.d.logFile.Name()) 482 if strings.Contains(string(content), `level=debug`) { 483 c.Fatalf(`Should not have level="debug" in log file:\n%s`, string(content)) 484 } 485 } 486 487 func (s *DockerDaemonSuite) TestDaemonFlagD(c *check.C) { 488 if err := s.d.Start("-D"); err != nil { 489 c.Fatal(err) 490 } 491 content, _ := ioutil.ReadFile(s.d.logFile.Name()) 492 if !strings.Contains(string(content), `level=debug`) { 493 c.Fatalf(`Should have level="debug" in log file using -D:\n%s`, string(content)) 494 } 495 } 496 497 func (s *DockerDaemonSuite) TestDaemonFlagDebug(c *check.C) { 498 if err := s.d.Start("--debug"); err != nil { 499 c.Fatal(err) 500 } 501 content, _ := ioutil.ReadFile(s.d.logFile.Name()) 502 if !strings.Contains(string(content), `level=debug`) { 503 c.Fatalf(`Should have level="debug" in log file using --debug:\n%s`, string(content)) 504 } 505 } 506 507 func (s *DockerDaemonSuite) TestDaemonFlagDebugLogLevelFatal(c *check.C) { 508 if err := s.d.Start("--debug", "--log-level=fatal"); err != nil { 509 c.Fatal(err) 510 } 511 content, _ := ioutil.ReadFile(s.d.logFile.Name()) 512 if !strings.Contains(string(content), `level=debug`) { 513 c.Fatalf(`Should have level="debug" in log file when using both --debug and --log-level=fatal:\n%s`, string(content)) 514 } 515 } 516 517 func (s *DockerDaemonSuite) TestDaemonAllocatesListeningPort(c *check.C) { 518 listeningPorts := [][]string{ 519 {"0.0.0.0", "0.0.0.0", "5678"}, 520 {"127.0.0.1", "127.0.0.1", "1234"}, 521 {"localhost", "127.0.0.1", "1235"}, 522 } 523 524 cmdArgs := make([]string, 0, len(listeningPorts)*2) 525 for _, hostDirective := range listeningPorts { 526 cmdArgs = append(cmdArgs, "--host", fmt.Sprintf("tcp://%s:%s", hostDirective[0], hostDirective[2])) 527 } 528 529 if err := s.d.StartWithBusybox(cmdArgs...); err != nil { 530 c.Fatalf("Could not start daemon with busybox: %v", err) 531 } 532 533 for _, hostDirective := range listeningPorts { 534 output, err := s.d.Cmd("run", "-p", fmt.Sprintf("%s:%s:80", hostDirective[1], hostDirective[2]), "busybox", "true") 535 if err == nil { 536 c.Fatalf("Container should not start, expected port already allocated error: %q", output) 537 } else if !strings.Contains(output, "port is already allocated") { 538 c.Fatalf("Expected port is already allocated error: %q", output) 539 } 540 } 541 } 542 543 func (s *DockerDaemonSuite) TestDaemonKeyGeneration(c *check.C) { 544 // TODO: skip or update for Windows daemon 545 os.Remove("/etc/docker/key.json") 546 if err := s.d.Start(); err != nil { 547 c.Fatalf("Could not start daemon: %v", err) 548 } 549 s.d.Stop() 550 551 k, err := libtrust.LoadKeyFile("/etc/docker/key.json") 552 if err != nil { 553 c.Fatalf("Error opening key file") 554 } 555 kid := k.KeyID() 556 // Test Key ID is a valid fingerprint (e.g. QQXN:JY5W:TBXI:MK3X:GX6P:PD5D:F56N:NHCS:LVRZ:JA46:R24J:XEFF) 557 if len(kid) != 59 { 558 c.Fatalf("Bad key ID: %s", kid) 559 } 560 } 561 562 func (s *DockerDaemonSuite) TestDaemonKeyMigration(c *check.C) { 563 // TODO: skip or update for Windows daemon 564 os.Remove("/etc/docker/key.json") 565 k1, err := libtrust.GenerateECP256PrivateKey() 566 if err != nil { 567 c.Fatalf("Error generating private key: %s", err) 568 } 569 if err := os.MkdirAll(filepath.Join(os.Getenv("HOME"), ".docker"), 0755); err != nil { 570 c.Fatalf("Error creating .docker directory: %s", err) 571 } 572 if err := libtrust.SaveKey(filepath.Join(os.Getenv("HOME"), ".docker", "key.json"), k1); err != nil { 573 c.Fatalf("Error saving private key: %s", err) 574 } 575 576 if err := s.d.Start(); err != nil { 577 c.Fatalf("Could not start daemon: %v", err) 578 } 579 s.d.Stop() 580 581 k2, err := libtrust.LoadKeyFile("/etc/docker/key.json") 582 if err != nil { 583 c.Fatalf("Error opening key file") 584 } 585 if k1.KeyID() != k2.KeyID() { 586 c.Fatalf("Key not migrated") 587 } 588 } 589 590 // GH#11320 - verify that the daemon exits on failure properly 591 // Note that this explicitly tests the conflict of {-b,--bridge} and {--bip} options as the means 592 // to get a daemon init failure; no other tests for -b/--bip conflict are therefore required 593 func (s *DockerDaemonSuite) TestDaemonExitOnFailure(c *check.C) { 594 //attempt to start daemon with incorrect flags (we know -b and --bip conflict) 595 if err := s.d.Start("--bridge", "nosuchbridge", "--bip", "1.1.1.1"); err != nil { 596 //verify we got the right error 597 if !strings.Contains(err.Error(), "Daemon exited") { 598 c.Fatalf("Expected daemon not to start, got %v", err) 599 } 600 // look in the log and make sure we got the message that daemon is shutting down 601 runCmd := exec.Command("grep", "Error starting daemon", s.d.LogFileName()) 602 if out, _, err := runCommandWithOutput(runCmd); err != nil { 603 c.Fatalf("Expected 'Error starting daemon' message; but doesn't exist in log: %q, err: %v", out, err) 604 } 605 } else { 606 //if we didn't get an error and the daemon is running, this is a failure 607 c.Fatal("Conflicting options should cause the daemon to error out with a failure") 608 } 609 } 610 611 func (s *DockerDaemonSuite) TestDaemonBridgeExternal(c *check.C) { 612 d := s.d 613 err := d.Start("--bridge", "nosuchbridge") 614 c.Assert(err, check.NotNil, check.Commentf("--bridge option with an invalid bridge should cause the daemon to fail")) 615 defer d.Restart() 616 617 bridgeName := "external-bridge" 618 bridgeIP := "192.169.1.1/24" 619 _, bridgeIPNet, _ := net.ParseCIDR(bridgeIP) 620 621 out, err := createInterface(c, "bridge", bridgeName, bridgeIP) 622 c.Assert(err, check.IsNil, check.Commentf(out)) 623 defer deleteInterface(c, bridgeName) 624 625 err = d.StartWithBusybox("--bridge", bridgeName) 626 c.Assert(err, check.IsNil) 627 628 ipTablesSearchString := bridgeIPNet.String() 629 ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL") 630 out, _, err = runCommandWithOutput(ipTablesCmd) 631 c.Assert(err, check.IsNil) 632 633 c.Assert(strings.Contains(out, ipTablesSearchString), check.Equals, true, 634 check.Commentf("iptables output should have contained %q, but was %q", 635 ipTablesSearchString, out)) 636 637 _, err = d.Cmd("run", "-d", "--name", "ExtContainer", "busybox", "top") 638 c.Assert(err, check.IsNil) 639 640 containerIP := d.findContainerIP("ExtContainer") 641 ip := net.ParseIP(containerIP) 642 c.Assert(bridgeIPNet.Contains(ip), check.Equals, true, 643 check.Commentf("Container IP-Address must be in the same subnet range : %s", 644 containerIP)) 645 } 646 647 func createInterface(c *check.C, ifType string, ifName string, ipNet string) (string, error) { 648 args := []string{"link", "add", "name", ifName, "type", ifType} 649 ipLinkCmd := exec.Command("ip", args...) 650 out, _, err := runCommandWithOutput(ipLinkCmd) 651 if err != nil { 652 return out, err 653 } 654 655 ifCfgCmd := exec.Command("ifconfig", ifName, ipNet, "up") 656 out, _, err = runCommandWithOutput(ifCfgCmd) 657 return out, err 658 } 659 660 func deleteInterface(c *check.C, ifName string) { 661 ifCmd := exec.Command("ip", "link", "delete", ifName) 662 out, _, err := runCommandWithOutput(ifCmd) 663 c.Assert(err, check.IsNil, check.Commentf(out)) 664 665 flushCmd := exec.Command("iptables", "-t", "nat", "--flush") 666 out, _, err = runCommandWithOutput(flushCmd) 667 c.Assert(err, check.IsNil, check.Commentf(out)) 668 669 flushCmd = exec.Command("iptables", "--flush") 670 out, _, err = runCommandWithOutput(flushCmd) 671 c.Assert(err, check.IsNil, check.Commentf(out)) 672 } 673 674 func (s *DockerDaemonSuite) TestDaemonBridgeIP(c *check.C) { 675 // TestDaemonBridgeIP Steps 676 // 1. Delete the existing docker0 Bridge 677 // 2. Set --bip daemon configuration and start the new Docker Daemon 678 // 3. Check if the bip config has taken effect using ifconfig and iptables commands 679 // 4. Launch a Container and make sure the IP-Address is in the expected subnet 680 // 5. Delete the docker0 Bridge 681 // 6. Restart the Docker Daemon (via deferred action) 682 // This Restart takes care of bringing docker0 interface back to auto-assigned IP 683 684 defaultNetworkBridge := "docker0" 685 deleteInterface(c, defaultNetworkBridge) 686 687 d := s.d 688 689 bridgeIP := "192.169.1.1/24" 690 ip, bridgeIPNet, _ := net.ParseCIDR(bridgeIP) 691 692 err := d.StartWithBusybox("--bip", bridgeIP) 693 c.Assert(err, check.IsNil) 694 defer d.Restart() 695 696 ifconfigSearchString := ip.String() 697 ifconfigCmd := exec.Command("ifconfig", defaultNetworkBridge) 698 out, _, _, err := runCommandWithStdoutStderr(ifconfigCmd) 699 c.Assert(err, check.IsNil) 700 701 c.Assert(strings.Contains(out, ifconfigSearchString), check.Equals, true, 702 check.Commentf("ifconfig output should have contained %q, but was %q", 703 ifconfigSearchString, out)) 704 705 ipTablesSearchString := bridgeIPNet.String() 706 ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL") 707 out, _, err = runCommandWithOutput(ipTablesCmd) 708 c.Assert(err, check.IsNil) 709 710 c.Assert(strings.Contains(out, ipTablesSearchString), check.Equals, true, 711 check.Commentf("iptables output should have contained %q, but was %q", 712 ipTablesSearchString, out)) 713 714 out, err = d.Cmd("run", "-d", "--name", "test", "busybox", "top") 715 c.Assert(err, check.IsNil) 716 717 containerIP := d.findContainerIP("test") 718 ip = net.ParseIP(containerIP) 719 c.Assert(bridgeIPNet.Contains(ip), check.Equals, true, 720 check.Commentf("Container IP-Address must be in the same subnet range : %s", 721 containerIP)) 722 deleteInterface(c, defaultNetworkBridge) 723 } 724 725 func (s *DockerDaemonSuite) TestDaemonRestartWithBridgeIPChange(c *check.C) { 726 if err := s.d.Start(); err != nil { 727 c.Fatalf("Could not start daemon: %v", err) 728 } 729 defer s.d.Restart() 730 if err := s.d.Stop(); err != nil { 731 c.Fatalf("Could not stop daemon: %v", err) 732 } 733 734 // now we will change the docker0's IP and then try starting the daemon 735 bridgeIP := "192.169.100.1/24" 736 _, bridgeIPNet, _ := net.ParseCIDR(bridgeIP) 737 738 ipCmd := exec.Command("ifconfig", "docker0", bridgeIP) 739 stdout, stderr, _, err := runCommandWithStdoutStderr(ipCmd) 740 if err != nil { 741 c.Fatalf("failed to change docker0's IP association: %v, stdout: %q, stderr: %q", err, stdout, stderr) 742 } 743 744 if err := s.d.Start("--bip", bridgeIP); err != nil { 745 c.Fatalf("Could not start daemon: %v", err) 746 } 747 748 //check if the iptables contains new bridgeIP MASQUERADE rule 749 ipTablesSearchString := bridgeIPNet.String() 750 ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL") 751 out, _, err := runCommandWithOutput(ipTablesCmd) 752 if err != nil { 753 c.Fatalf("Could not run iptables -nvL: %s, %v", out, err) 754 } 755 if !strings.Contains(out, ipTablesSearchString) { 756 c.Fatalf("iptables output should have contained new MASQUERADE rule with IP %q, but was %q", ipTablesSearchString, out) 757 } 758 } 759 760 func (s *DockerDaemonSuite) TestDaemonBridgeFixedCidr(c *check.C) { 761 d := s.d 762 763 bridgeName := "external-bridge" 764 bridgeIP := "192.169.1.1/24" 765 766 out, err := createInterface(c, "bridge", bridgeName, bridgeIP) 767 c.Assert(err, check.IsNil, check.Commentf(out)) 768 defer deleteInterface(c, bridgeName) 769 770 args := []string{"--bridge", bridgeName, "--fixed-cidr", "192.169.1.0/30"} 771 err = d.StartWithBusybox(args...) 772 c.Assert(err, check.IsNil) 773 defer d.Restart() 774 775 for i := 0; i < 4; i++ { 776 cName := "Container" + strconv.Itoa(i) 777 out, err := d.Cmd("run", "-d", "--name", cName, "busybox", "top") 778 if err != nil { 779 c.Assert(strings.Contains(out, "no available IPv4 addresses"), check.Equals, true, 780 check.Commentf("Could not run a Container : %s %s", err.Error(), out)) 781 } 782 } 783 } 784 785 func (s *DockerDaemonSuite) TestDaemonBridgeFixedCidr2(c *check.C) { 786 d := s.d 787 788 bridgeName := "external-bridge" 789 bridgeIP := "10.2.2.1/16" 790 791 out, err := createInterface(c, "bridge", bridgeName, bridgeIP) 792 c.Assert(err, check.IsNil, check.Commentf(out)) 793 defer deleteInterface(c, bridgeName) 794 795 err = d.StartWithBusybox("--bip", bridgeIP, "--fixed-cidr", "10.2.2.0/24") 796 c.Assert(err, check.IsNil) 797 defer s.d.Restart() 798 799 out, err = d.Cmd("run", "-d", "--name", "bb", "busybox", "top") 800 c.Assert(err, checker.IsNil, check.Commentf(out)) 801 defer d.Cmd("stop", "bb") 802 803 out, err = d.Cmd("exec", "bb", "/bin/sh", "-c", "ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'") 804 c.Assert(out, checker.Equals, "10.2.2.0\n") 805 806 out, err = d.Cmd("run", "--rm", "busybox", "/bin/sh", "-c", "ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'") 807 c.Assert(err, checker.IsNil, check.Commentf(out)) 808 c.Assert(out, checker.Equals, "10.2.2.2\n") 809 } 810 811 func (s *DockerDaemonSuite) TestDaemonBridgeFixedCIDREqualBridgeNetwork(c *check.C) { 812 d := s.d 813 814 bridgeName := "external-bridge" 815 bridgeIP := "172.27.42.1/16" 816 817 out, err := createInterface(c, "bridge", bridgeName, bridgeIP) 818 c.Assert(err, check.IsNil, check.Commentf(out)) 819 defer deleteInterface(c, bridgeName) 820 821 err = d.StartWithBusybox("--bridge", bridgeName, "--fixed-cidr", bridgeIP) 822 c.Assert(err, check.IsNil) 823 defer s.d.Restart() 824 825 out, err = d.Cmd("run", "-d", "busybox", "top") 826 c.Assert(err, check.IsNil, check.Commentf(out)) 827 cid1 := strings.TrimSpace(out) 828 defer d.Cmd("stop", cid1) 829 } 830 831 func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Implicit(c *check.C) { 832 defaultNetworkBridge := "docker0" 833 deleteInterface(c, defaultNetworkBridge) 834 835 d := s.d 836 837 bridgeIP := "192.169.1.1" 838 bridgeIPNet := fmt.Sprintf("%s/24", bridgeIP) 839 840 err := d.StartWithBusybox("--bip", bridgeIPNet) 841 c.Assert(err, check.IsNil) 842 defer d.Restart() 843 844 expectedMessage := fmt.Sprintf("default via %s dev", bridgeIP) 845 out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0") 846 c.Assert(strings.Contains(out, expectedMessage), check.Equals, true, 847 check.Commentf("Implicit default gateway should be bridge IP %s, but default route was '%s'", 848 bridgeIP, strings.TrimSpace(out))) 849 deleteInterface(c, defaultNetworkBridge) 850 } 851 852 func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Explicit(c *check.C) { 853 defaultNetworkBridge := "docker0" 854 deleteInterface(c, defaultNetworkBridge) 855 856 d := s.d 857 858 bridgeIP := "192.169.1.1" 859 bridgeIPNet := fmt.Sprintf("%s/24", bridgeIP) 860 gatewayIP := "192.169.1.254" 861 862 err := d.StartWithBusybox("--bip", bridgeIPNet, "--default-gateway", gatewayIP) 863 c.Assert(err, check.IsNil) 864 defer d.Restart() 865 866 expectedMessage := fmt.Sprintf("default via %s dev", gatewayIP) 867 out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0") 868 c.Assert(strings.Contains(out, expectedMessage), check.Equals, true, 869 check.Commentf("Explicit default gateway should be %s, but default route was '%s'", 870 gatewayIP, strings.TrimSpace(out))) 871 deleteInterface(c, defaultNetworkBridge) 872 } 873 874 func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4ExplicitOutsideContainerSubnet(c *check.C) { 875 defaultNetworkBridge := "docker0" 876 deleteInterface(c, defaultNetworkBridge) 877 878 // Program a custom default gateway outside of the container subnet, daemon should accept it and start 879 err := s.d.StartWithBusybox("--bip", "172.16.0.10/16", "--fixed-cidr", "172.16.1.0/24", "--default-gateway", "172.16.0.254") 880 c.Assert(err, check.IsNil) 881 882 deleteInterface(c, defaultNetworkBridge) 883 s.d.Restart() 884 } 885 886 func (s *DockerDaemonSuite) TestDaemonDefaultNetworkInvalidClusterConfig(c *check.C) { 887 testRequires(c, DaemonIsLinux, SameHostDaemon) 888 889 // Start daemon without docker0 bridge 890 defaultNetworkBridge := "docker0" 891 deleteInterface(c, defaultNetworkBridge) 892 893 d := NewDaemon(c) 894 discoveryBackend := "consul://consuladdr:consulport/some/path" 895 err := d.Start(fmt.Sprintf("--cluster-store=%s", discoveryBackend)) 896 c.Assert(err, checker.IsNil) 897 898 // Start daemon with docker0 bridge 899 ifconfigCmd := exec.Command("ifconfig", defaultNetworkBridge) 900 _, err = runCommand(ifconfigCmd) 901 c.Assert(err, check.IsNil) 902 903 err = d.Restart(fmt.Sprintf("--cluster-store=%s", discoveryBackend)) 904 c.Assert(err, checker.IsNil) 905 906 d.Stop() 907 } 908 909 func (s *DockerDaemonSuite) TestDaemonIP(c *check.C) { 910 d := s.d 911 912 ipStr := "192.170.1.1/24" 913 ip, _, _ := net.ParseCIDR(ipStr) 914 args := []string{"--ip", ip.String()} 915 err := d.StartWithBusybox(args...) 916 c.Assert(err, check.IsNil) 917 defer d.Restart() 918 919 out, err := d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top") 920 c.Assert(err, check.NotNil, 921 check.Commentf("Running a container must fail with an invalid --ip option")) 922 c.Assert(strings.Contains(out, "Error starting userland proxy"), check.Equals, true) 923 924 ifName := "dummy" 925 out, err = createInterface(c, "dummy", ifName, ipStr) 926 c.Assert(err, check.IsNil, check.Commentf(out)) 927 defer deleteInterface(c, ifName) 928 929 _, err = d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top") 930 c.Assert(err, check.IsNil) 931 932 ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL") 933 out, _, err = runCommandWithOutput(ipTablesCmd) 934 c.Assert(err, check.IsNil) 935 936 regex := fmt.Sprintf("DNAT.*%s.*dpt:8000", ip.String()) 937 matched, _ := regexp.MatchString(regex, out) 938 c.Assert(matched, check.Equals, true, 939 check.Commentf("iptables output should have contained %q, but was %q", regex, out)) 940 } 941 942 func (s *DockerDaemonSuite) TestDaemonICCPing(c *check.C) { 943 testRequires(c, bridgeNfIptables) 944 d := s.d 945 946 bridgeName := "external-bridge" 947 bridgeIP := "192.169.1.1/24" 948 949 out, err := createInterface(c, "bridge", bridgeName, bridgeIP) 950 c.Assert(err, check.IsNil, check.Commentf(out)) 951 defer deleteInterface(c, bridgeName) 952 953 args := []string{"--bridge", bridgeName, "--icc=false"} 954 err = d.StartWithBusybox(args...) 955 c.Assert(err, check.IsNil) 956 defer d.Restart() 957 958 ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD") 959 out, _, err = runCommandWithOutput(ipTablesCmd) 960 c.Assert(err, check.IsNil) 961 962 regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName) 963 matched, _ := regexp.MatchString(regex, out) 964 c.Assert(matched, check.Equals, true, 965 check.Commentf("iptables output should have contained %q, but was %q", regex, out)) 966 967 // Pinging another container must fail with --icc=false 968 pingContainers(c, d, true) 969 970 ipStr := "192.171.1.1/24" 971 ip, _, _ := net.ParseCIDR(ipStr) 972 ifName := "icc-dummy" 973 974 createInterface(c, "dummy", ifName, ipStr) 975 976 // But, Pinging external or a Host interface must succeed 977 pingCmd := fmt.Sprintf("ping -c 1 %s -W 1", ip.String()) 978 runArgs := []string{"--rm", "busybox", "sh", "-c", pingCmd} 979 _, err = d.Cmd("run", runArgs...) 980 c.Assert(err, check.IsNil) 981 } 982 983 func (s *DockerDaemonSuite) TestDaemonICCLinkExpose(c *check.C) { 984 d := s.d 985 986 bridgeName := "external-bridge" 987 bridgeIP := "192.169.1.1/24" 988 989 out, err := createInterface(c, "bridge", bridgeName, bridgeIP) 990 c.Assert(err, check.IsNil, check.Commentf(out)) 991 defer deleteInterface(c, bridgeName) 992 993 args := []string{"--bridge", bridgeName, "--icc=false"} 994 err = d.StartWithBusybox(args...) 995 c.Assert(err, check.IsNil) 996 defer d.Restart() 997 998 ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD") 999 out, _, err = runCommandWithOutput(ipTablesCmd) 1000 c.Assert(err, check.IsNil) 1001 1002 regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName) 1003 matched, _ := regexp.MatchString(regex, out) 1004 c.Assert(matched, check.Equals, true, 1005 check.Commentf("iptables output should have contained %q, but was %q", regex, out)) 1006 1007 out, err = d.Cmd("run", "-d", "--expose", "4567", "--name", "icc1", "busybox", "nc", "-l", "-p", "4567") 1008 c.Assert(err, check.IsNil, check.Commentf(out)) 1009 1010 out, err = d.Cmd("run", "--link", "icc1:icc1", "busybox", "nc", "icc1", "4567") 1011 c.Assert(err, check.IsNil, check.Commentf(out)) 1012 } 1013 1014 func (s *DockerDaemonSuite) TestDaemonLinksIpTablesRulesWhenLinkAndUnlink(c *check.C) { 1015 bridgeName := "external-bridge" 1016 bridgeIP := "192.169.1.1/24" 1017 1018 out, err := createInterface(c, "bridge", bridgeName, bridgeIP) 1019 c.Assert(err, check.IsNil, check.Commentf(out)) 1020 defer deleteInterface(c, bridgeName) 1021 1022 err = s.d.StartWithBusybox("--bridge", bridgeName, "--icc=false") 1023 c.Assert(err, check.IsNil) 1024 defer s.d.Restart() 1025 1026 _, err = s.d.Cmd("run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "top") 1027 c.Assert(err, check.IsNil) 1028 _, err = s.d.Cmd("run", "-d", "--name", "parent", "--link", "child:http", "busybox", "top") 1029 c.Assert(err, check.IsNil) 1030 1031 childIP := s.d.findContainerIP("child") 1032 parentIP := s.d.findContainerIP("parent") 1033 1034 sourceRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"} 1035 destinationRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"} 1036 if !iptables.Exists("filter", "DOCKER", sourceRule...) || !iptables.Exists("filter", "DOCKER", destinationRule...) { 1037 c.Fatal("Iptables rules not found") 1038 } 1039 1040 s.d.Cmd("rm", "--link", "parent/http") 1041 if iptables.Exists("filter", "DOCKER", sourceRule...) || iptables.Exists("filter", "DOCKER", destinationRule...) { 1042 c.Fatal("Iptables rules should be removed when unlink") 1043 } 1044 1045 s.d.Cmd("kill", "child") 1046 s.d.Cmd("kill", "parent") 1047 } 1048 1049 func (s *DockerDaemonSuite) TestDaemonUlimitDefaults(c *check.C) { 1050 testRequires(c, DaemonIsLinux) 1051 1052 if err := s.d.StartWithBusybox("--default-ulimit", "nofile=42:42", "--default-ulimit", "nproc=1024:1024"); err != nil { 1053 c.Fatal(err) 1054 } 1055 1056 out, err := s.d.Cmd("run", "--ulimit", "nproc=2048", "--name=test", "busybox", "/bin/sh", "-c", "echo $(ulimit -n); echo $(ulimit -p)") 1057 if err != nil { 1058 c.Fatal(out, err) 1059 } 1060 1061 outArr := strings.Split(out, "\n") 1062 if len(outArr) < 2 { 1063 c.Fatalf("got unexpected output: %s", out) 1064 } 1065 nofile := strings.TrimSpace(outArr[0]) 1066 nproc := strings.TrimSpace(outArr[1]) 1067 1068 if nofile != "42" { 1069 c.Fatalf("expected `ulimit -n` to be `42`, got: %s", nofile) 1070 } 1071 if nproc != "2048" { 1072 c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc) 1073 } 1074 1075 // Now restart daemon with a new default 1076 if err := s.d.Restart("--default-ulimit", "nofile=43"); err != nil { 1077 c.Fatal(err) 1078 } 1079 1080 out, err = s.d.Cmd("start", "-a", "test") 1081 if err != nil { 1082 c.Fatal(err) 1083 } 1084 1085 outArr = strings.Split(out, "\n") 1086 if len(outArr) < 2 { 1087 c.Fatalf("got unexpected output: %s", out) 1088 } 1089 nofile = strings.TrimSpace(outArr[0]) 1090 nproc = strings.TrimSpace(outArr[1]) 1091 1092 if nofile != "43" { 1093 c.Fatalf("expected `ulimit -n` to be `43`, got: %s", nofile) 1094 } 1095 if nproc != "2048" { 1096 c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc) 1097 } 1098 } 1099 1100 // #11315 1101 func (s *DockerDaemonSuite) TestDaemonRestartRenameContainer(c *check.C) { 1102 if err := s.d.StartWithBusybox(); err != nil { 1103 c.Fatal(err) 1104 } 1105 1106 if out, err := s.d.Cmd("run", "--name=test", "busybox"); err != nil { 1107 c.Fatal(err, out) 1108 } 1109 1110 if out, err := s.d.Cmd("rename", "test", "test2"); err != nil { 1111 c.Fatal(err, out) 1112 } 1113 1114 if err := s.d.Restart(); err != nil { 1115 c.Fatal(err) 1116 } 1117 1118 if out, err := s.d.Cmd("start", "test2"); err != nil { 1119 c.Fatal(err, out) 1120 } 1121 } 1122 1123 func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefault(c *check.C) { 1124 if err := s.d.StartWithBusybox(); err != nil { 1125 c.Fatal(err) 1126 } 1127 1128 out, err := s.d.Cmd("run", "--name=test", "busybox", "echo", "testline") 1129 c.Assert(err, check.IsNil, check.Commentf(out)) 1130 id, err := s.d.getIDByName("test") 1131 c.Assert(err, check.IsNil) 1132 1133 logPath := filepath.Join(s.d.root, "containers", id, id+"-json.log") 1134 1135 if _, err := os.Stat(logPath); err != nil { 1136 c.Fatal(err) 1137 } 1138 f, err := os.Open(logPath) 1139 if err != nil { 1140 c.Fatal(err) 1141 } 1142 var res struct { 1143 Log string `json:"log"` 1144 Stream string `json:"stream"` 1145 Time time.Time `json:"time"` 1146 } 1147 if err := json.NewDecoder(f).Decode(&res); err != nil { 1148 c.Fatal(err) 1149 } 1150 if res.Log != "testline\n" { 1151 c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n") 1152 } 1153 if res.Stream != "stdout" { 1154 c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout") 1155 } 1156 if !time.Now().After(res.Time) { 1157 c.Fatalf("Log time %v in future", res.Time) 1158 } 1159 } 1160 1161 func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefaultOverride(c *check.C) { 1162 if err := s.d.StartWithBusybox(); err != nil { 1163 c.Fatal(err) 1164 } 1165 1166 out, err := s.d.Cmd("run", "--name=test", "--log-driver=none", "busybox", "echo", "testline") 1167 if err != nil { 1168 c.Fatal(out, err) 1169 } 1170 id, err := s.d.getIDByName("test") 1171 c.Assert(err, check.IsNil) 1172 1173 logPath := filepath.Join(s.d.root, "containers", id, id+"-json.log") 1174 1175 if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) { 1176 c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err) 1177 } 1178 } 1179 1180 func (s *DockerDaemonSuite) TestDaemonLoggingDriverNone(c *check.C) { 1181 if err := s.d.StartWithBusybox("--log-driver=none"); err != nil { 1182 c.Fatal(err) 1183 } 1184 1185 out, err := s.d.Cmd("run", "--name=test", "busybox", "echo", "testline") 1186 if err != nil { 1187 c.Fatal(out, err) 1188 } 1189 id, err := s.d.getIDByName("test") 1190 c.Assert(err, check.IsNil) 1191 1192 logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log") 1193 1194 if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) { 1195 c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err) 1196 } 1197 } 1198 1199 func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneOverride(c *check.C) { 1200 if err := s.d.StartWithBusybox("--log-driver=none"); err != nil { 1201 c.Fatal(err) 1202 } 1203 1204 out, err := s.d.Cmd("run", "--name=test", "--log-driver=json-file", "busybox", "echo", "testline") 1205 if err != nil { 1206 c.Fatal(out, err) 1207 } 1208 id, err := s.d.getIDByName("test") 1209 c.Assert(err, check.IsNil) 1210 1211 logPath := filepath.Join(s.d.root, "containers", id, id+"-json.log") 1212 1213 if _, err := os.Stat(logPath); err != nil { 1214 c.Fatal(err) 1215 } 1216 f, err := os.Open(logPath) 1217 if err != nil { 1218 c.Fatal(err) 1219 } 1220 var res struct { 1221 Log string `json:"log"` 1222 Stream string `json:"stream"` 1223 Time time.Time `json:"time"` 1224 } 1225 if err := json.NewDecoder(f).Decode(&res); err != nil { 1226 c.Fatal(err) 1227 } 1228 if res.Log != "testline\n" { 1229 c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n") 1230 } 1231 if res.Stream != "stdout" { 1232 c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout") 1233 } 1234 if !time.Now().After(res.Time) { 1235 c.Fatalf("Log time %v in future", res.Time) 1236 } 1237 } 1238 1239 func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneLogsError(c *check.C) { 1240 c.Assert(s.d.StartWithBusybox("--log-driver=none"), checker.IsNil) 1241 1242 out, err := s.d.Cmd("run", "--name=test", "busybox", "echo", "testline") 1243 c.Assert(err, checker.IsNil, check.Commentf(out)) 1244 1245 out, err = s.d.Cmd("logs", "test") 1246 c.Assert(err, check.NotNil, check.Commentf("Logs should fail with 'none' driver")) 1247 expected := `"logs" command is supported only for "json-file" and "journald" logging drivers (got: none)` 1248 c.Assert(out, checker.Contains, expected) 1249 } 1250 1251 func (s *DockerDaemonSuite) TestDaemonDots(c *check.C) { 1252 if err := s.d.StartWithBusybox(); err != nil { 1253 c.Fatal(err) 1254 } 1255 1256 // Now create 4 containers 1257 if _, err := s.d.Cmd("create", "busybox"); err != nil { 1258 c.Fatalf("Error creating container: %q", err) 1259 } 1260 if _, err := s.d.Cmd("create", "busybox"); err != nil { 1261 c.Fatalf("Error creating container: %q", err) 1262 } 1263 if _, err := s.d.Cmd("create", "busybox"); err != nil { 1264 c.Fatalf("Error creating container: %q", err) 1265 } 1266 if _, err := s.d.Cmd("create", "busybox"); err != nil { 1267 c.Fatalf("Error creating container: %q", err) 1268 } 1269 1270 s.d.Stop() 1271 1272 s.d.Start("--log-level=debug") 1273 s.d.Stop() 1274 content, _ := ioutil.ReadFile(s.d.logFile.Name()) 1275 if strings.Contains(string(content), "....") { 1276 c.Fatalf("Debug level should not have ....\n%s", string(content)) 1277 } 1278 1279 s.d.Start("--log-level=error") 1280 s.d.Stop() 1281 content, _ = ioutil.ReadFile(s.d.logFile.Name()) 1282 if strings.Contains(string(content), "....") { 1283 c.Fatalf("Error level should not have ....\n%s", string(content)) 1284 } 1285 1286 s.d.Start("--log-level=info") 1287 s.d.Stop() 1288 content, _ = ioutil.ReadFile(s.d.logFile.Name()) 1289 if !strings.Contains(string(content), "....") { 1290 c.Fatalf("Info level should have ....\n%s", string(content)) 1291 } 1292 } 1293 1294 func (s *DockerDaemonSuite) TestDaemonUnixSockCleanedUp(c *check.C) { 1295 dir, err := ioutil.TempDir("", "socket-cleanup-test") 1296 if err != nil { 1297 c.Fatal(err) 1298 } 1299 defer os.RemoveAll(dir) 1300 1301 sockPath := filepath.Join(dir, "docker.sock") 1302 if err := s.d.Start("--host", "unix://"+sockPath); err != nil { 1303 c.Fatal(err) 1304 } 1305 1306 if _, err := os.Stat(sockPath); err != nil { 1307 c.Fatal("socket does not exist") 1308 } 1309 1310 if err := s.d.Stop(); err != nil { 1311 c.Fatal(err) 1312 } 1313 1314 if _, err := os.Stat(sockPath); err == nil || !os.IsNotExist(err) { 1315 c.Fatal("unix socket is not cleaned up") 1316 } 1317 } 1318 1319 func (s *DockerDaemonSuite) TestDaemonWithWrongkey(c *check.C) { 1320 type Config struct { 1321 Crv string `json:"crv"` 1322 D string `json:"d"` 1323 Kid string `json:"kid"` 1324 Kty string `json:"kty"` 1325 X string `json:"x"` 1326 Y string `json:"y"` 1327 } 1328 1329 os.Remove("/etc/docker/key.json") 1330 if err := s.d.Start(); err != nil { 1331 c.Fatalf("Failed to start daemon: %v", err) 1332 } 1333 1334 if err := s.d.Stop(); err != nil { 1335 c.Fatalf("Could not stop daemon: %v", err) 1336 } 1337 1338 config := &Config{} 1339 bytes, err := ioutil.ReadFile("/etc/docker/key.json") 1340 if err != nil { 1341 c.Fatalf("Error reading key.json file: %s", err) 1342 } 1343 1344 // byte[] to Data-Struct 1345 if err := json.Unmarshal(bytes, &config); err != nil { 1346 c.Fatalf("Error Unmarshal: %s", err) 1347 } 1348 1349 //replace config.Kid with the fake value 1350 config.Kid = "VSAJ:FUYR:X3H2:B2VZ:KZ6U:CJD5:K7BX:ZXHY:UZXT:P4FT:MJWG:HRJ4" 1351 1352 // NEW Data-Struct to byte[] 1353 newBytes, err := json.Marshal(&config) 1354 if err != nil { 1355 c.Fatalf("Error Marshal: %s", err) 1356 } 1357 1358 // write back 1359 if err := ioutil.WriteFile("/etc/docker/key.json", newBytes, 0400); err != nil { 1360 c.Fatalf("Error ioutil.WriteFile: %s", err) 1361 } 1362 1363 defer os.Remove("/etc/docker/key.json") 1364 1365 if err := s.d.Start(); err == nil { 1366 c.Fatalf("It should not be successful to start daemon with wrong key: %v", err) 1367 } 1368 1369 content, _ := ioutil.ReadFile(s.d.logFile.Name()) 1370 1371 if !strings.Contains(string(content), "Public Key ID does not match") { 1372 c.Fatal("Missing KeyID message from daemon logs") 1373 } 1374 } 1375 1376 func (s *DockerDaemonSuite) TestDaemonRestartKillWait(c *check.C) { 1377 if err := s.d.StartWithBusybox(); err != nil { 1378 c.Fatalf("Could not start daemon with busybox: %v", err) 1379 } 1380 1381 out, err := s.d.Cmd("run", "-id", "busybox", "/bin/cat") 1382 if err != nil { 1383 c.Fatalf("Could not run /bin/cat: err=%v\n%s", err, out) 1384 } 1385 containerID := strings.TrimSpace(out) 1386 1387 if out, err := s.d.Cmd("kill", containerID); err != nil { 1388 c.Fatalf("Could not kill %s: err=%v\n%s", containerID, err, out) 1389 } 1390 1391 if err := s.d.Restart(); err != nil { 1392 c.Fatalf("Could not restart daemon: %v", err) 1393 } 1394 1395 errchan := make(chan error) 1396 go func() { 1397 if out, err := s.d.Cmd("wait", containerID); err != nil { 1398 errchan <- fmt.Errorf("%v:\n%s", err, out) 1399 } 1400 close(errchan) 1401 }() 1402 1403 select { 1404 case <-time.After(5 * time.Second): 1405 c.Fatal("Waiting on a stopped (killed) container timed out") 1406 case err := <-errchan: 1407 if err != nil { 1408 c.Fatal(err) 1409 } 1410 } 1411 } 1412 1413 // TestHttpsInfo connects via two-way authenticated HTTPS to the info endpoint 1414 func (s *DockerDaemonSuite) TestHttpsInfo(c *check.C) { 1415 const ( 1416 testDaemonHTTPSAddr = "tcp://localhost:4271" 1417 ) 1418 1419 if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem", 1420 "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHTTPSAddr); err != nil { 1421 c.Fatalf("Could not start daemon with busybox: %v", err) 1422 } 1423 1424 daemonArgs := []string{"--host", testDaemonHTTPSAddr, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-cert.pem", "--tlskey", "fixtures/https/client-key.pem"} 1425 out, err := s.d.CmdWithArgs(daemonArgs, "info") 1426 if err != nil { 1427 c.Fatalf("Error Occurred: %s and output: %s", err, out) 1428 } 1429 } 1430 1431 // TestHttpsRun connects via two-way authenticated HTTPS to the create, attach, start, and wait endpoints. 1432 // https://github.com/docker/docker/issues/19280 1433 func (s *DockerDaemonSuite) TestHttpsRun(c *check.C) { 1434 const ( 1435 testDaemonHTTPSAddr = "tcp://localhost:4271" 1436 ) 1437 1438 if err := s.d.StartWithBusybox("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem", 1439 "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHTTPSAddr); err != nil { 1440 c.Fatalf("Could not start daemon with busybox: %v", err) 1441 } 1442 1443 daemonArgs := []string{"--host", testDaemonHTTPSAddr, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-cert.pem", "--tlskey", "fixtures/https/client-key.pem"} 1444 out, err := s.d.CmdWithArgs(daemonArgs, "run", "busybox", "echo", "TLS response") 1445 if err != nil { 1446 c.Fatalf("Error Occurred: %s and output: %s", err, out) 1447 } 1448 1449 if !strings.Contains(out, "TLS response") { 1450 c.Fatalf("expected output to include `TLS response`, got %v", out) 1451 } 1452 } 1453 1454 // TestTlsVerify verifies that --tlsverify=false turns on tls 1455 func (s *DockerDaemonSuite) TestTlsVerify(c *check.C) { 1456 out, err := exec.Command(dockerBinary, "daemon", "--tlsverify=false").CombinedOutput() 1457 if err == nil || !strings.Contains(string(out), "Could not load X509 key pair") { 1458 c.Fatalf("Daemon should not have started due to missing certs: %v\n%s", err, string(out)) 1459 } 1460 } 1461 1462 // TestHttpsInfoRogueCert connects via two-way authenticated HTTPS to the info endpoint 1463 // by using a rogue client certificate and checks that it fails with the expected error. 1464 func (s *DockerDaemonSuite) TestHttpsInfoRogueCert(c *check.C) { 1465 const ( 1466 errBadCertificate = "remote error: bad certificate" 1467 testDaemonHTTPSAddr = "tcp://localhost:4271" 1468 ) 1469 1470 if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem", 1471 "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHTTPSAddr); err != nil { 1472 c.Fatalf("Could not start daemon with busybox: %v", err) 1473 } 1474 1475 daemonArgs := []string{"--host", testDaemonHTTPSAddr, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"} 1476 out, err := s.d.CmdWithArgs(daemonArgs, "info") 1477 if err == nil || !strings.Contains(out, errBadCertificate) { 1478 c.Fatalf("Expected err: %s, got instead: %s and output: %s", errBadCertificate, err, out) 1479 } 1480 } 1481 1482 // TestHttpsInfoRogueServerCert connects via two-way authenticated HTTPS to the info endpoint 1483 // which provides a rogue server certificate and checks that it fails with the expected error 1484 func (s *DockerDaemonSuite) TestHttpsInfoRogueServerCert(c *check.C) { 1485 const ( 1486 errCaUnknown = "x509: certificate signed by unknown authority" 1487 testDaemonRogueHTTPSAddr = "tcp://localhost:4272" 1488 ) 1489 if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-rogue-cert.pem", 1490 "--tlskey", "fixtures/https/server-rogue-key.pem", "-H", testDaemonRogueHTTPSAddr); err != nil { 1491 c.Fatalf("Could not start daemon with busybox: %v", err) 1492 } 1493 1494 daemonArgs := []string{"--host", testDaemonRogueHTTPSAddr, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"} 1495 out, err := s.d.CmdWithArgs(daemonArgs, "info") 1496 if err == nil || !strings.Contains(out, errCaUnknown) { 1497 c.Fatalf("Expected err: %s, got instead: %s and output: %s", errCaUnknown, err, out) 1498 } 1499 } 1500 1501 func pingContainers(c *check.C, d *Daemon, expectFailure bool) { 1502 var dargs []string 1503 if d != nil { 1504 dargs = []string{"--host", d.sock()} 1505 } 1506 1507 args := append(dargs, "run", "-d", "--name", "container1", "busybox", "top") 1508 dockerCmd(c, args...) 1509 1510 args = append(dargs, "run", "--rm", "--link", "container1:alias1", "busybox", "sh", "-c") 1511 pingCmd := "ping -c 1 %s -W 1" 1512 args = append(args, fmt.Sprintf(pingCmd, "alias1")) 1513 _, _, err := dockerCmdWithError(args...) 1514 1515 if expectFailure { 1516 c.Assert(err, check.NotNil) 1517 } else { 1518 c.Assert(err, check.IsNil) 1519 } 1520 1521 args = append(dargs, "rm", "-f", "container1") 1522 dockerCmd(c, args...) 1523 } 1524 1525 func (s *DockerDaemonSuite) TestDaemonRestartWithSocketAsVolume(c *check.C) { 1526 c.Assert(s.d.StartWithBusybox(), check.IsNil) 1527 1528 socket := filepath.Join(s.d.folder, "docker.sock") 1529 1530 out, err := s.d.Cmd("run", "--restart=always", "-v", socket+":/sock", "busybox") 1531 c.Assert(err, check.IsNil, check.Commentf("Output: %s", out)) 1532 c.Assert(s.d.Restart(), check.IsNil) 1533 } 1534 1535 // os.Kill should kill daemon ungracefully, leaving behind container mounts. 1536 // A subsequent daemon restart shoud clean up said mounts. 1537 func (s *DockerDaemonSuite) TestCleanupMountsAfterDaemonAndContainerKill(c *check.C) { 1538 c.Assert(s.d.StartWithBusybox(), check.IsNil) 1539 1540 out, err := s.d.Cmd("run", "-d", "busybox", "top") 1541 c.Assert(err, check.IsNil, check.Commentf("Output: %s", out)) 1542 id := strings.TrimSpace(out) 1543 c.Assert(s.d.cmd.Process.Signal(os.Kill), check.IsNil) 1544 mountOut, err := ioutil.ReadFile("/proc/self/mountinfo") 1545 c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut)) 1546 1547 // container mounts should exist even after daemon has crashed. 1548 comment := check.Commentf("%s should stay mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut) 1549 c.Assert(strings.Contains(string(mountOut), id), check.Equals, true, comment) 1550 1551 // kill the container 1552 runCmd := exec.Command(ctrBinary, "--address", "/var/run/docker/libcontainerd/docker-containerd.sock", "containers", "kill", id) 1553 if out, ec, err := runCommandWithOutput(runCmd); err != nil { 1554 c.Fatalf("Failed to run ctr, ExitCode: %d, err: %v output: %s id: %s\n", ec, err, out, id) 1555 } 1556 1557 // restart daemon. 1558 if err := s.d.Restart(); err != nil { 1559 c.Fatal(err) 1560 } 1561 1562 // Now, container mounts should be gone. 1563 mountOut, err = ioutil.ReadFile("/proc/self/mountinfo") 1564 c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut)) 1565 comment = check.Commentf("%s is still mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut) 1566 c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, comment) 1567 } 1568 1569 // os.Interrupt should perform a graceful daemon shutdown and hence cleanup mounts. 1570 func (s *DockerDaemonSuite) TestCleanupMountsAfterGracefulShutdown(c *check.C) { 1571 c.Assert(s.d.StartWithBusybox(), check.IsNil) 1572 1573 out, err := s.d.Cmd("run", "-d", "busybox", "top") 1574 c.Assert(err, check.IsNil, check.Commentf("Output: %s", out)) 1575 id := strings.TrimSpace(out) 1576 1577 // Send SIGINT and daemon should clean up 1578 c.Assert(s.d.cmd.Process.Signal(os.Interrupt), check.IsNil) 1579 1580 // Wait a bit for the daemon to handle cleanups. 1581 time.Sleep(3 * time.Second) 1582 1583 mountOut, err := ioutil.ReadFile("/proc/self/mountinfo") 1584 c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut)) 1585 1586 comment := check.Commentf("%s is still mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut) 1587 c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, comment) 1588 } 1589 1590 func (s *DockerDaemonSuite) TestRunContainerWithBridgeNone(c *check.C) { 1591 testRequires(c, DaemonIsLinux, NotUserNamespace) 1592 c.Assert(s.d.StartWithBusybox("-b", "none"), check.IsNil) 1593 1594 out, err := s.d.Cmd("run", "--rm", "busybox", "ip", "l") 1595 c.Assert(err, check.IsNil, check.Commentf("Output: %s", out)) 1596 c.Assert(strings.Contains(out, "eth0"), check.Equals, false, 1597 check.Commentf("There shouldn't be eth0 in container in default(bridge) mode when bridge network is disabled: %s", out)) 1598 1599 out, err = s.d.Cmd("run", "--rm", "--net=bridge", "busybox", "ip", "l") 1600 c.Assert(err, check.IsNil, check.Commentf("Output: %s", out)) 1601 c.Assert(strings.Contains(out, "eth0"), check.Equals, false, 1602 check.Commentf("There shouldn't be eth0 in container in bridge mode when bridge network is disabled: %s", out)) 1603 // the extra grep and awk clean up the output of `ip` to only list the number and name of 1604 // interfaces, allowing for different versions of ip (e.g. inside and outside the container) to 1605 // be used while still verifying that the interface list is the exact same 1606 cmd := exec.Command("sh", "-c", "ip l | grep -E '^[0-9]+:' | awk -F: ' { print $1\":\"$2 } '") 1607 stdout := bytes.NewBuffer(nil) 1608 cmd.Stdout = stdout 1609 if err := cmd.Run(); err != nil { 1610 c.Fatal("Failed to get host network interface") 1611 } 1612 out, err = s.d.Cmd("run", "--rm", "--net=host", "busybox", "sh", "-c", "ip l | grep -E '^[0-9]+:' | awk -F: ' { print $1\":\"$2 } '") 1613 c.Assert(err, check.IsNil, check.Commentf("Output: %s", out)) 1614 c.Assert(out, check.Equals, fmt.Sprintf("%s", stdout), 1615 check.Commentf("The network interfaces in container should be the same with host when --net=host when bridge network is disabled: %s", out)) 1616 } 1617 1618 // os.Kill should kill daemon ungracefully, leaving behind container mounts. 1619 // A subsequent daemon restart shoud clean up said mounts. 1620 func (s *DockerDaemonSuite) TestCleanupMountsAfterDaemonKill(c *check.C) { 1621 testRequires(c, NotExperimentalDaemon) 1622 c.Assert(s.d.StartWithBusybox(), check.IsNil) 1623 1624 out, err := s.d.Cmd("run", "-d", "busybox", "top") 1625 c.Assert(err, check.IsNil, check.Commentf("Output: %s", out)) 1626 id := strings.TrimSpace(out) 1627 c.Assert(s.d.cmd.Process.Signal(os.Kill), check.IsNil) 1628 mountOut, err := ioutil.ReadFile("/proc/self/mountinfo") 1629 c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut)) 1630 1631 // container mounts should exist even after daemon has crashed. 1632 comment := check.Commentf("%s should stay mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut) 1633 c.Assert(strings.Contains(string(mountOut), id), check.Equals, true, comment) 1634 1635 // restart daemon. 1636 if err := s.d.Restart(); err != nil { 1637 c.Fatal(err) 1638 } 1639 1640 // Now, container mounts should be gone. 1641 mountOut, err = ioutil.ReadFile("/proc/self/mountinfo") 1642 c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut)) 1643 comment = check.Commentf("%s is still mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut) 1644 c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, comment) 1645 } 1646 1647 func (s *DockerDaemonSuite) TestDaemonRestartWithContainerRunning(t *check.C) { 1648 if err := s.d.StartWithBusybox(); err != nil { 1649 t.Fatal(err) 1650 } 1651 if out, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top"); err != nil { 1652 t.Fatal(out, err) 1653 } 1654 1655 if err := s.d.Restart(); err != nil { 1656 t.Fatal(err) 1657 } 1658 // Container 'test' should be removed without error 1659 if out, err := s.d.Cmd("rm", "test"); err != nil { 1660 t.Fatal(out, err) 1661 } 1662 } 1663 1664 func (s *DockerDaemonSuite) TestDaemonRestartCleanupNetns(c *check.C) { 1665 if err := s.d.StartWithBusybox(); err != nil { 1666 c.Fatal(err) 1667 } 1668 out, err := s.d.Cmd("run", "--name", "netns", "-d", "busybox", "top") 1669 if err != nil { 1670 c.Fatal(out, err) 1671 } 1672 1673 // Get sandbox key via inspect 1674 out, err = s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.SandboxKey}}'", "netns") 1675 if err != nil { 1676 c.Fatalf("Error inspecting container: %s, %v", out, err) 1677 } 1678 fileName := strings.Trim(out, " \r\n'") 1679 1680 if out, err := s.d.Cmd("stop", "netns"); err != nil { 1681 c.Fatal(out, err) 1682 } 1683 1684 // Test if the file still exists 1685 out, _, err = runCommandWithOutput(exec.Command("stat", "-c", "%n", fileName)) 1686 out = strings.TrimSpace(out) 1687 c.Assert(err, check.IsNil, check.Commentf("Output: %s", out)) 1688 c.Assert(out, check.Equals, fileName, check.Commentf("Output: %s", out)) 1689 1690 // Remove the container and restart the daemon 1691 if out, err := s.d.Cmd("rm", "netns"); err != nil { 1692 c.Fatal(out, err) 1693 } 1694 1695 if err := s.d.Restart(); err != nil { 1696 c.Fatal(err) 1697 } 1698 1699 // Test again and see now the netns file does not exist 1700 out, _, err = runCommandWithOutput(exec.Command("stat", "-c", "%n", fileName)) 1701 out = strings.TrimSpace(out) 1702 c.Assert(err, check.Not(check.IsNil), check.Commentf("Output: %s", out)) 1703 } 1704 1705 // tests regression detailed in #13964 where DOCKER_TLS_VERIFY env is ignored 1706 func (s *DockerDaemonSuite) TestDaemonNoTlsCliTlsVerifyWithEnv(c *check.C) { 1707 host := "tcp://localhost:4271" 1708 c.Assert(s.d.Start("-H", host), check.IsNil) 1709 cmd := exec.Command(dockerBinary, "-H", host, "info") 1710 cmd.Env = []string{"DOCKER_TLS_VERIFY=1", "DOCKER_CERT_PATH=fixtures/https"} 1711 out, _, err := runCommandWithOutput(cmd) 1712 c.Assert(err, check.Not(check.IsNil), check.Commentf("%s", out)) 1713 c.Assert(strings.Contains(out, "error occurred trying to connect"), check.Equals, true) 1714 1715 } 1716 1717 func setupV6() error { 1718 // Hack to get the right IPv6 address on docker0, which has already been created 1719 return exec.Command("ip", "addr", "add", "fe80::1/64", "dev", "docker0").Run() 1720 } 1721 1722 func teardownV6() error { 1723 return exec.Command("ip", "addr", "del", "fe80::1/64", "dev", "docker0").Run() 1724 } 1725 1726 func (s *DockerDaemonSuite) TestDaemonRestartWithContainerWithRestartPolicyAlways(c *check.C) { 1727 c.Assert(s.d.StartWithBusybox(), check.IsNil) 1728 1729 out, err := s.d.Cmd("run", "-d", "--restart", "always", "busybox", "top") 1730 c.Assert(err, check.IsNil) 1731 id := strings.TrimSpace(out) 1732 1733 _, err = s.d.Cmd("stop", id) 1734 c.Assert(err, check.IsNil) 1735 _, err = s.d.Cmd("wait", id) 1736 c.Assert(err, check.IsNil) 1737 1738 out, err = s.d.Cmd("ps", "-q") 1739 c.Assert(err, check.IsNil) 1740 c.Assert(out, check.Equals, "") 1741 1742 c.Assert(s.d.Restart(), check.IsNil) 1743 1744 out, err = s.d.Cmd("ps", "-q") 1745 c.Assert(err, check.IsNil) 1746 c.Assert(strings.TrimSpace(out), check.Equals, id[:12]) 1747 } 1748 1749 func (s *DockerDaemonSuite) TestDaemonWideLogConfig(c *check.C) { 1750 if err := s.d.StartWithBusybox("--log-driver=json-file", "--log-opt=max-size=1k"); err != nil { 1751 c.Fatal(err) 1752 } 1753 out, err := s.d.Cmd("run", "-d", "--name=logtest", "busybox", "top") 1754 c.Assert(err, check.IsNil, check.Commentf("Output: %s, err: %v", out, err)) 1755 out, err = s.d.Cmd("inspect", "-f", "{{ .HostConfig.LogConfig.Config }}", "logtest") 1756 c.Assert(err, check.IsNil, check.Commentf("Output: %s", out)) 1757 cfg := strings.TrimSpace(out) 1758 if cfg != "map[max-size:1k]" { 1759 c.Fatalf("Unexpected log-opt: %s, expected map[max-size:1k]", cfg) 1760 } 1761 } 1762 1763 func (s *DockerDaemonSuite) TestDaemonRestartWithPausedContainer(c *check.C) { 1764 if err := s.d.StartWithBusybox(); err != nil { 1765 c.Fatal(err) 1766 } 1767 if out, err := s.d.Cmd("run", "-i", "-d", "--name", "test", "busybox", "top"); err != nil { 1768 c.Fatal(err, out) 1769 } 1770 if out, err := s.d.Cmd("pause", "test"); err != nil { 1771 c.Fatal(err, out) 1772 } 1773 if err := s.d.Restart(); err != nil { 1774 c.Fatal(err) 1775 } 1776 1777 errchan := make(chan error) 1778 go func() { 1779 out, err := s.d.Cmd("start", "test") 1780 if err != nil { 1781 errchan <- fmt.Errorf("%v:\n%s", err, out) 1782 } 1783 name := strings.TrimSpace(out) 1784 if name != "test" { 1785 errchan <- fmt.Errorf("Paused container start error on docker daemon restart, expected 'test' but got '%s'", name) 1786 } 1787 close(errchan) 1788 }() 1789 1790 select { 1791 case <-time.After(5 * time.Second): 1792 c.Fatal("Waiting on start a container timed out") 1793 case err := <-errchan: 1794 if err != nil { 1795 c.Fatal(err) 1796 } 1797 } 1798 } 1799 1800 func (s *DockerDaemonSuite) TestDaemonRestartRmVolumeInUse(c *check.C) { 1801 c.Assert(s.d.StartWithBusybox(), check.IsNil) 1802 1803 out, err := s.d.Cmd("create", "-v", "test:/foo", "busybox") 1804 c.Assert(err, check.IsNil, check.Commentf(out)) 1805 1806 c.Assert(s.d.Restart(), check.IsNil) 1807 1808 out, err = s.d.Cmd("volume", "rm", "test") 1809 c.Assert(err, check.NotNil, check.Commentf("should not be able to remove in use volume after daemon restart")) 1810 c.Assert(out, checker.Contains, "in use") 1811 } 1812 1813 func (s *DockerDaemonSuite) TestDaemonRestartLocalVolumes(c *check.C) { 1814 c.Assert(s.d.Start(), check.IsNil) 1815 1816 _, err := s.d.Cmd("volume", "create", "--name", "test") 1817 c.Assert(err, check.IsNil) 1818 c.Assert(s.d.Restart(), check.IsNil) 1819 1820 _, err = s.d.Cmd("volume", "inspect", "test") 1821 c.Assert(err, check.IsNil) 1822 } 1823 1824 func (s *DockerDaemonSuite) TestDaemonCorruptedLogDriverAddress(c *check.C) { 1825 c.Assert(s.d.Start("--log-driver=syslog", "--log-opt", "syslog-address=corrupted:42"), check.NotNil) 1826 expected := "Failed to set log opts: syslog-address should be in form proto://address" 1827 runCmd := exec.Command("grep", expected, s.d.LogFileName()) 1828 if out, _, err := runCommandWithOutput(runCmd); err != nil { 1829 c.Fatalf("Expected %q message; but doesn't exist in log: %q, err: %v", expected, out, err) 1830 } 1831 } 1832 1833 func (s *DockerDaemonSuite) TestDaemonCorruptedFluentdAddress(c *check.C) { 1834 c.Assert(s.d.Start("--log-driver=fluentd", "--log-opt", "fluentd-address=corrupted:c"), check.NotNil) 1835 expected := "Failed to set log opts: invalid fluentd-address corrupted:c: " 1836 runCmd := exec.Command("grep", expected, s.d.LogFileName()) 1837 if out, _, err := runCommandWithOutput(runCmd); err != nil { 1838 c.Fatalf("Expected %q message; but doesn't exist in log: %q, err: %v", expected, out, err) 1839 } 1840 } 1841 1842 func (s *DockerDaemonSuite) TestDaemonStartWithoutHost(c *check.C) { 1843 s.d.useDefaultHost = true 1844 defer func() { 1845 s.d.useDefaultHost = false 1846 }() 1847 c.Assert(s.d.Start(), check.IsNil) 1848 } 1849 1850 func (s *DockerDaemonSuite) TestDaemonStartWithDefalutTlsHost(c *check.C) { 1851 s.d.useDefaultTLSHost = true 1852 defer func() { 1853 s.d.useDefaultTLSHost = false 1854 }() 1855 if err := s.d.Start( 1856 "--tlsverify", 1857 "--tlscacert", "fixtures/https/ca.pem", 1858 "--tlscert", "fixtures/https/server-cert.pem", 1859 "--tlskey", "fixtures/https/server-key.pem"); err != nil { 1860 c.Fatalf("Could not start daemon: %v", err) 1861 } 1862 1863 // The client with --tlsverify should also use default host localhost:2376 1864 tmpHost := os.Getenv("DOCKER_HOST") 1865 defer func() { 1866 os.Setenv("DOCKER_HOST", tmpHost) 1867 }() 1868 1869 os.Setenv("DOCKER_HOST", "") 1870 1871 out, _ := dockerCmd( 1872 c, 1873 "--tlsverify", 1874 "--tlscacert", "fixtures/https/ca.pem", 1875 "--tlscert", "fixtures/https/client-cert.pem", 1876 "--tlskey", "fixtures/https/client-key.pem", 1877 "version", 1878 ) 1879 if !strings.Contains(out, "Server") { 1880 c.Fatalf("docker version should return information of server side") 1881 } 1882 } 1883 1884 func (s *DockerDaemonSuite) TestBridgeIPIsExcludedFromAllocatorPool(c *check.C) { 1885 defaultNetworkBridge := "docker0" 1886 deleteInterface(c, defaultNetworkBridge) 1887 1888 bridgeIP := "192.169.1.1" 1889 bridgeRange := bridgeIP + "/30" 1890 1891 err := s.d.StartWithBusybox("--bip", bridgeRange) 1892 c.Assert(err, check.IsNil) 1893 defer s.d.Restart() 1894 1895 var cont int 1896 for { 1897 contName := fmt.Sprintf("container%d", cont) 1898 _, err = s.d.Cmd("run", "--name", contName, "-d", "busybox", "/bin/sleep", "2") 1899 if err != nil { 1900 // pool exhausted 1901 break 1902 } 1903 ip, err := s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.IPAddress}}'", contName) 1904 c.Assert(err, check.IsNil) 1905 1906 c.Assert(ip, check.Not(check.Equals), bridgeIP) 1907 cont++ 1908 } 1909 } 1910 1911 // Test daemon for no space left on device error 1912 func (s *DockerDaemonSuite) TestDaemonNoSpaceleftOnDeviceError(c *check.C) { 1913 testRequires(c, SameHostDaemon, DaemonIsLinux, Network) 1914 1915 // create a 2MiB image and mount it as graph root 1916 cmd := exec.Command("dd", "of=/tmp/testfs.img", "bs=1M", "seek=2", "count=0") 1917 if err := cmd.Run(); err != nil { 1918 c.Fatalf("dd failed: %v", err) 1919 } 1920 cmd = exec.Command("mkfs.ext4", "-F", "/tmp/testfs.img") 1921 if err := cmd.Run(); err != nil { 1922 c.Fatalf("mkfs.ext4 failed: %v", err) 1923 } 1924 cmd = exec.Command("mkdir", "-p", "/tmp/testfs-mount") 1925 if err := cmd.Run(); err != nil { 1926 c.Fatalf("mkdir failed: %v", err) 1927 } 1928 cmd = exec.Command("mount", "-t", "ext4", "-no", "loop,rw", "/tmp/testfs.img", "/tmp/testfs-mount") 1929 if err := cmd.Run(); err != nil { 1930 c.Fatalf("mount failed: %v", err) 1931 } 1932 err := s.d.Start("--graph", "/tmp/testfs-mount") 1933 c.Assert(err, check.IsNil) 1934 1935 // pull a repository large enough to fill the mount point 1936 out, err := s.d.Cmd("pull", "registry:2") 1937 c.Assert(out, checker.Contains, "no space left on device") 1938 } 1939 1940 // Test daemon restart with container links + auto restart 1941 func (s *DockerDaemonSuite) TestDaemonRestartContainerLinksRestart(c *check.C) { 1942 d := NewDaemon(c) 1943 defer d.Stop() 1944 err := d.StartWithBusybox() 1945 c.Assert(err, checker.IsNil) 1946 1947 parent1Args := []string{} 1948 parent2Args := []string{} 1949 wg := sync.WaitGroup{} 1950 maxChildren := 10 1951 chErr := make(chan error, maxChildren) 1952 1953 for i := 0; i < maxChildren; i++ { 1954 wg.Add(1) 1955 name := fmt.Sprintf("test%d", i) 1956 1957 if i < maxChildren/2 { 1958 parent1Args = append(parent1Args, []string{"--link", name}...) 1959 } else { 1960 parent2Args = append(parent2Args, []string{"--link", name}...) 1961 } 1962 1963 go func() { 1964 _, err = d.Cmd("run", "-d", "--name", name, "--restart=always", "busybox", "top") 1965 chErr <- err 1966 wg.Done() 1967 }() 1968 } 1969 1970 wg.Wait() 1971 close(chErr) 1972 for err := range chErr { 1973 c.Assert(err, check.IsNil) 1974 } 1975 1976 parent1Args = append([]string{"run", "-d"}, parent1Args...) 1977 parent1Args = append(parent1Args, []string{"--name=parent1", "--restart=always", "busybox", "top"}...) 1978 parent2Args = append([]string{"run", "-d"}, parent2Args...) 1979 parent2Args = append(parent2Args, []string{"--name=parent2", "--restart=always", "busybox", "top"}...) 1980 1981 _, err = d.Cmd(parent1Args[0], parent1Args[1:]...) 1982 c.Assert(err, check.IsNil) 1983 _, err = d.Cmd(parent2Args[0], parent2Args[1:]...) 1984 c.Assert(err, check.IsNil) 1985 1986 err = d.Stop() 1987 c.Assert(err, check.IsNil) 1988 // clear the log file -- we don't need any of it but may for the next part 1989 // can ignore the error here, this is just a cleanup 1990 os.Truncate(d.LogFileName(), 0) 1991 err = d.Start() 1992 c.Assert(err, check.IsNil) 1993 1994 for _, num := range []string{"1", "2"} { 1995 out, err := d.Cmd("inspect", "-f", "{{ .State.Running }}", "parent"+num) 1996 c.Assert(err, check.IsNil) 1997 if strings.TrimSpace(out) != "true" { 1998 log, _ := ioutil.ReadFile(d.LogFileName()) 1999 c.Fatalf("parent container is not running\n%s", string(log)) 2000 } 2001 } 2002 } 2003 2004 func (s *DockerDaemonSuite) TestDaemonCgroupParent(c *check.C) { 2005 testRequires(c, DaemonIsLinux) 2006 2007 cgroupParent := "test" 2008 name := "cgroup-test" 2009 2010 err := s.d.StartWithBusybox("--cgroup-parent", cgroupParent) 2011 c.Assert(err, check.IsNil) 2012 defer s.d.Restart() 2013 2014 out, err := s.d.Cmd("run", "--name", name, "busybox", "cat", "/proc/self/cgroup") 2015 c.Assert(err, checker.IsNil) 2016 cgroupPaths := parseCgroupPaths(string(out)) 2017 c.Assert(len(cgroupPaths), checker.Not(checker.Equals), 0, check.Commentf("unexpected output - %q", string(out))) 2018 out, err = s.d.Cmd("inspect", "-f", "{{.Id}}", name) 2019 c.Assert(err, checker.IsNil) 2020 id := strings.TrimSpace(string(out)) 2021 expectedCgroup := path.Join(cgroupParent, id) 2022 found := false 2023 for _, path := range cgroupPaths { 2024 if strings.HasSuffix(path, expectedCgroup) { 2025 found = true 2026 break 2027 } 2028 } 2029 c.Assert(found, checker.True, check.Commentf("Cgroup path for container (%s) doesn't found in cgroups file: %s", expectedCgroup, cgroupPaths)) 2030 } 2031 2032 func (s *DockerDaemonSuite) TestDaemonRestartWithLinks(c *check.C) { 2033 testRequires(c, DaemonIsLinux) // Windows does not support links 2034 err := s.d.StartWithBusybox() 2035 c.Assert(err, check.IsNil) 2036 2037 out, err := s.d.Cmd("run", "-d", "--name=test", "busybox", "top") 2038 c.Assert(err, check.IsNil, check.Commentf(out)) 2039 2040 out, err = s.d.Cmd("run", "--name=test2", "--link", "test:abc", "busybox", "sh", "-c", "ping -c 1 -w 1 abc") 2041 c.Assert(err, check.IsNil, check.Commentf(out)) 2042 2043 c.Assert(s.d.Restart(), check.IsNil) 2044 2045 // should fail since test is not running yet 2046 out, err = s.d.Cmd("start", "test2") 2047 c.Assert(err, check.NotNil, check.Commentf(out)) 2048 2049 out, err = s.d.Cmd("start", "test") 2050 c.Assert(err, check.IsNil, check.Commentf(out)) 2051 out, err = s.d.Cmd("start", "-a", "test2") 2052 c.Assert(err, check.IsNil, check.Commentf(out)) 2053 c.Assert(strings.Contains(out, "1 packets transmitted, 1 packets received"), check.Equals, true, check.Commentf(out)) 2054 } 2055 2056 func (s *DockerDaemonSuite) TestDaemonRestartWithNames(c *check.C) { 2057 testRequires(c, DaemonIsLinux) // Windows does not support links 2058 err := s.d.StartWithBusybox() 2059 c.Assert(err, check.IsNil) 2060 2061 out, err := s.d.Cmd("create", "--name=test", "busybox") 2062 c.Assert(err, check.IsNil, check.Commentf(out)) 2063 2064 out, err = s.d.Cmd("run", "-d", "--name=test2", "busybox", "top") 2065 c.Assert(err, check.IsNil, check.Commentf(out)) 2066 test2ID := strings.TrimSpace(out) 2067 2068 out, err = s.d.Cmd("run", "-d", "--name=test3", "--link", "test2:abc", "busybox", "top") 2069 test3ID := strings.TrimSpace(out) 2070 2071 c.Assert(s.d.Restart(), check.IsNil) 2072 2073 out, err = s.d.Cmd("create", "--name=test", "busybox") 2074 c.Assert(err, check.NotNil, check.Commentf("expected error trying to create container with duplicate name")) 2075 // this one is no longer needed, removing simplifies the remainder of the test 2076 out, err = s.d.Cmd("rm", "-f", "test") 2077 c.Assert(err, check.IsNil, check.Commentf(out)) 2078 2079 out, err = s.d.Cmd("ps", "-a", "--no-trunc") 2080 c.Assert(err, check.IsNil, check.Commentf(out)) 2081 2082 lines := strings.Split(strings.TrimSpace(out), "\n")[1:] 2083 2084 test2validated := false 2085 test3validated := false 2086 for _, line := range lines { 2087 fields := strings.Fields(line) 2088 names := fields[len(fields)-1] 2089 switch fields[0] { 2090 case test2ID: 2091 c.Assert(names, check.Equals, "test2,test3/abc") 2092 test2validated = true 2093 case test3ID: 2094 c.Assert(names, check.Equals, "test3") 2095 test3validated = true 2096 } 2097 } 2098 2099 c.Assert(test2validated, check.Equals, true) 2100 c.Assert(test3validated, check.Equals, true) 2101 } 2102 2103 // TestRunLinksChanged checks that creating a new container with the same name does not update links 2104 // this ensures that the old, pre gh#16032 functionality continues on 2105 func (s *DockerDaemonSuite) TestRunLinksChanged(c *check.C) { 2106 testRequires(c, DaemonIsLinux) // Windows does not support links 2107 err := s.d.StartWithBusybox() 2108 c.Assert(err, check.IsNil) 2109 2110 out, err := s.d.Cmd("run", "-d", "--name=test", "busybox", "top") 2111 c.Assert(err, check.IsNil, check.Commentf(out)) 2112 2113 out, err = s.d.Cmd("run", "--name=test2", "--link=test:abc", "busybox", "sh", "-c", "ping -c 1 abc") 2114 c.Assert(err, check.IsNil, check.Commentf(out)) 2115 c.Assert(out, checker.Contains, "1 packets transmitted, 1 packets received") 2116 2117 out, err = s.d.Cmd("rm", "-f", "test") 2118 c.Assert(err, check.IsNil, check.Commentf(out)) 2119 2120 out, err = s.d.Cmd("run", "-d", "--name=test", "busybox", "top") 2121 c.Assert(err, check.IsNil, check.Commentf(out)) 2122 out, err = s.d.Cmd("start", "-a", "test2") 2123 c.Assert(err, check.NotNil, check.Commentf(out)) 2124 c.Assert(out, check.Not(checker.Contains), "1 packets transmitted, 1 packets received") 2125 2126 err = s.d.Restart() 2127 c.Assert(err, check.IsNil) 2128 out, err = s.d.Cmd("start", "-a", "test2") 2129 c.Assert(err, check.NotNil, check.Commentf(out)) 2130 c.Assert(out, check.Not(checker.Contains), "1 packets transmitted, 1 packets received") 2131 } 2132 2133 func (s *DockerDaemonSuite) TestDaemonStartWithoutColors(c *check.C) { 2134 testRequires(c, DaemonIsLinux, NotPpc64le) 2135 newD := NewDaemon(c) 2136 2137 infoLog := "\x1b[34mINFO\x1b" 2138 2139 p, tty, err := pty.Open() 2140 c.Assert(err, checker.IsNil) 2141 defer func() { 2142 tty.Close() 2143 p.Close() 2144 }() 2145 2146 b := bytes.NewBuffer(nil) 2147 go io.Copy(b, p) 2148 2149 // Enable coloring explicitly 2150 newD.StartWithLogFile(tty, "--raw-logs=false") 2151 newD.Stop() 2152 c.Assert(b.String(), checker.Contains, infoLog) 2153 2154 b.Reset() 2155 2156 // Disable coloring explicitly 2157 newD.StartWithLogFile(tty, "--raw-logs=true") 2158 newD.Stop() 2159 c.Assert(b.String(), check.Not(checker.Contains), infoLog) 2160 } 2161 2162 func (s *DockerDaemonSuite) TestDaemonDebugLog(c *check.C) { 2163 testRequires(c, DaemonIsLinux, NotPpc64le) 2164 newD := NewDaemon(c) 2165 2166 debugLog := "\x1b[37mDEBU\x1b" 2167 2168 p, tty, err := pty.Open() 2169 c.Assert(err, checker.IsNil) 2170 defer func() { 2171 tty.Close() 2172 p.Close() 2173 }() 2174 2175 b := bytes.NewBuffer(nil) 2176 go io.Copy(b, p) 2177 2178 newD.StartWithLogFile(tty, "--debug") 2179 newD.Stop() 2180 c.Assert(b.String(), checker.Contains, debugLog) 2181 } 2182 2183 func (s *DockerSuite) TestDaemonDiscoveryBackendConfigReload(c *check.C) { 2184 testRequires(c, SameHostDaemon, DaemonIsLinux) 2185 2186 // daemon config file 2187 daemonConfig := `{ "debug" : false }` 2188 configFilePath := "test.json" 2189 2190 configFile, err := os.Create(configFilePath) 2191 c.Assert(err, checker.IsNil) 2192 fmt.Fprintf(configFile, "%s", daemonConfig) 2193 2194 d := NewDaemon(c) 2195 err = d.Start(fmt.Sprintf("--config-file=%s", configFilePath)) 2196 c.Assert(err, checker.IsNil) 2197 defer d.Stop() 2198 2199 // daemon config file 2200 daemonConfig = `{ 2201 "cluster-store": "consul://consuladdr:consulport/some/path", 2202 "cluster-advertise": "192.168.56.100:0", 2203 "debug" : false 2204 }` 2205 2206 configFile.Close() 2207 os.Remove(configFilePath) 2208 2209 configFile, err = os.Create(configFilePath) 2210 c.Assert(err, checker.IsNil) 2211 defer os.Remove(configFilePath) 2212 fmt.Fprintf(configFile, "%s", daemonConfig) 2213 configFile.Close() 2214 2215 syscall.Kill(d.cmd.Process.Pid, syscall.SIGHUP) 2216 2217 time.Sleep(3 * time.Second) 2218 2219 out, err := d.Cmd("info") 2220 c.Assert(err, checker.IsNil) 2221 c.Assert(out, checker.Contains, fmt.Sprintf("Cluster Store: consul://consuladdr:consulport/some/path")) 2222 c.Assert(out, checker.Contains, fmt.Sprintf("Cluster Advertise: 192.168.56.100:0")) 2223 }