docker_cli_daemon_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package main
  2. import (
  3. "encoding/json"
  4. "os"
  5. "os/exec"
  6. "strings"
  7. "testing"
  8. )
  9. func TestDaemonRestartWithRunningContainersPorts(t *testing.T) {
  10. d := NewDaemon(t)
  11. if err := d.StartWithBusybox(); err != nil {
  12. t.Fatalf("Could not start daemon with busybox: %v", err)
  13. }
  14. defer d.Stop()
  15. if out, err := d.Cmd("run", "-d", "--name", "top1", "-p", "1234:80", "--restart", "always", "busybox:latest", "top"); err != nil {
  16. t.Fatalf("Could not run top1: err=%v\n%s", err, out)
  17. }
  18. // --restart=no by default
  19. if out, err := d.Cmd("run", "-d", "--name", "top2", "-p", "80", "busybox:latest", "top"); err != nil {
  20. t.Fatalf("Could not run top2: err=%v\n%s", err, out)
  21. }
  22. testRun := func(m map[string]bool, prefix string) {
  23. var format string
  24. for c, shouldRun := range m {
  25. out, err := d.Cmd("ps")
  26. if err != nil {
  27. t.Fatalf("Could not run ps: err=%v\n%q", err, out)
  28. }
  29. if shouldRun {
  30. format = "%scontainer %q is not running"
  31. } else {
  32. format = "%scontainer %q is running"
  33. }
  34. if shouldRun != strings.Contains(out, c) {
  35. t.Fatalf(format, prefix, c)
  36. }
  37. }
  38. }
  39. testRun(map[string]bool{"top1": true, "top2": true}, "")
  40. if err := d.Restart(); err != nil {
  41. t.Fatalf("Could not restart daemon: %v", err)
  42. }
  43. testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ")
  44. logDone("daemon - running containers on daemon restart")
  45. }
  46. func TestDaemonRestartWithVolumesRefs(t *testing.T) {
  47. d := NewDaemon(t)
  48. if err := d.StartWithBusybox(); err != nil {
  49. t.Fatal(err)
  50. }
  51. defer d.Stop()
  52. if out, err := d.Cmd("run", "-d", "--name", "volrestarttest1", "-v", "/foo", "busybox"); err != nil {
  53. t.Fatal(err, out)
  54. }
  55. if err := d.Restart(); err != nil {
  56. t.Fatal(err)
  57. }
  58. if _, err := d.Cmd("run", "-d", "--volumes-from", "volrestarttest1", "--name", "volrestarttest2", "busybox"); err != nil {
  59. t.Fatal(err)
  60. }
  61. if out, err := d.Cmd("rm", "-fv", "volrestarttest2"); err != nil {
  62. t.Fatal(err, out)
  63. }
  64. v, err := d.Cmd("inspect", "--format", "{{ json .Volumes }}", "volrestarttest1")
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. volumes := make(map[string]string)
  69. json.Unmarshal([]byte(v), &volumes)
  70. if _, err := os.Stat(volumes["/foo"]); err != nil {
  71. t.Fatalf("Expected volume to exist: %s - %s", volumes["/foo"], err)
  72. }
  73. logDone("daemon - volume refs are restored")
  74. }
  75. func TestDaemonStartIptablesFalse(t *testing.T) {
  76. d := NewDaemon(t)
  77. if err := d.Start("--iptables=false"); err != nil {
  78. t.Fatalf("we should have been able to start the daemon with passing iptables=false: %v", err)
  79. }
  80. d.Stop()
  81. logDone("daemon - started daemon with iptables=false")
  82. }
  83. // Issue #8444: If docker0 bridge is modified (intentionally or unintentionally) and
  84. // no longer has an IP associated, we should gracefully handle that case and associate
  85. // an IP with it rather than fail daemon start
  86. func TestDaemonStartBridgeWithoutIPAssociation(t *testing.T) {
  87. d := NewDaemon(t)
  88. // rather than depending on brctl commands to verify docker0 is created and up
  89. // let's start the daemon and stop it, and then make a modification to run the
  90. // actual test
  91. if err := d.Start(); err != nil {
  92. t.Fatalf("Could not start daemon: %v", err)
  93. }
  94. if err := d.Stop(); err != nil {
  95. t.Fatalf("Could not stop daemon: %v", err)
  96. }
  97. // now we will remove the ip from docker0 and then try starting the daemon
  98. ipCmd := exec.Command("ip", "addr", "flush", "dev", "docker0")
  99. stdout, stderr, _, err := runCommandWithStdoutStderr(ipCmd)
  100. if err != nil {
  101. t.Fatalf("failed to remove docker0 IP association: %v, stdout: %q, stderr: %q", err, stdout, stderr)
  102. }
  103. if err := d.Start(); err != nil {
  104. warning := "**WARNING: Docker bridge network in bad state--delete docker0 bridge interface to fix"
  105. t.Fatalf("Could not start daemon when docker0 has no IP address: %v\n%s", err, warning)
  106. }
  107. // cleanup - stop the daemon if test passed
  108. if err := d.Stop(); err != nil {
  109. t.Fatalf("Could not stop daemon: %v", err)
  110. }
  111. logDone("daemon - successful daemon start when bridge has no IP association")
  112. }
  113. func TestDaemonIptablesClean(t *testing.T) {
  114. d := NewDaemon(t)
  115. if err := d.StartWithBusybox(); err != nil {
  116. t.Fatalf("Could not start daemon with busybox: %v", err)
  117. }
  118. defer d.Stop()
  119. if out, err := d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
  120. t.Fatalf("Could not run top: %s, %v", out, err)
  121. }
  122. // get output from iptables with container running
  123. ipTablesSearchString := "tcp dpt:80"
  124. ipTablesCmd := exec.Command("iptables", "-nvL")
  125. out, _, err := runCommandWithOutput(ipTablesCmd)
  126. if err != nil {
  127. t.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  128. }
  129. if !strings.Contains(out, ipTablesSearchString) {
  130. t.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out)
  131. }
  132. if err := d.Stop(); err != nil {
  133. t.Fatalf("Could not stop daemon: %v", err)
  134. }
  135. // get output from iptables after restart
  136. ipTablesCmd = exec.Command("iptables", "-nvL")
  137. out, _, err = runCommandWithOutput(ipTablesCmd)
  138. if err != nil {
  139. t.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  140. }
  141. if strings.Contains(out, ipTablesSearchString) {
  142. t.Fatalf("iptables output should not have contained %q, but was %q", ipTablesSearchString, out)
  143. }
  144. deleteAllContainers()
  145. logDone("run,iptables - iptables rules cleaned after daemon restart")
  146. }
  147. func TestDaemonIptablesCreate(t *testing.T) {
  148. d := NewDaemon(t)
  149. if err := d.StartWithBusybox(); err != nil {
  150. t.Fatalf("Could not start daemon with busybox: %v", err)
  151. }
  152. defer d.Stop()
  153. if out, err := d.Cmd("run", "-d", "--name", "top", "--restart=always", "-p", "80", "busybox:latest", "top"); err != nil {
  154. t.Fatalf("Could not run top: %s, %v", out, err)
  155. }
  156. // get output from iptables with container running
  157. ipTablesSearchString := "tcp dpt:80"
  158. ipTablesCmd := exec.Command("iptables", "-nvL")
  159. out, _, err := runCommandWithOutput(ipTablesCmd)
  160. if err != nil {
  161. t.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  162. }
  163. if !strings.Contains(out, ipTablesSearchString) {
  164. t.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out)
  165. }
  166. if err := d.Restart(); err != nil {
  167. t.Fatalf("Could not restart daemon: %v", err)
  168. }
  169. // make sure the container is not running
  170. runningOut, err := d.Cmd("inspect", "--format='{{.State.Running}}'", "top")
  171. if err != nil {
  172. t.Fatalf("Could not inspect on container: %s, %v", out, err)
  173. }
  174. if strings.TrimSpace(runningOut) != "true" {
  175. t.Fatalf("Container should have been restarted after daemon restart. Status running should have been true but was: %q", strings.TrimSpace(runningOut))
  176. }
  177. // get output from iptables after restart
  178. ipTablesCmd = exec.Command("iptables", "-nvL")
  179. out, _, err = runCommandWithOutput(ipTablesCmd)
  180. if err != nil {
  181. t.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  182. }
  183. if !strings.Contains(out, ipTablesSearchString) {
  184. t.Fatalf("iptables output after restart should have contained %q, but was %q", ipTablesSearchString, out)
  185. }
  186. deleteAllContainers()
  187. logDone("run,iptables - iptables rules for always restarted container created after daemon restart")
  188. }