docker_cli_daemon_test.go 8.6 KB

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