docker_cli_daemon_test.go 9.6 KB

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