docker_cli_daemon_test.go 9.7 KB

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