docker_cli_daemon_test.go 11 KB

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