docker_cli_daemon_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. // +build daemon
  2. package main
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "github.com/docker/libtrust"
  13. "github.com/go-check/check"
  14. )
  15. func (s *DockerDaemonSuite) TestDaemonRestartWithRunningContainersPorts(c *check.C) {
  16. if err := s.d.StartWithBusybox(); err != nil {
  17. c.Fatalf("Could not start daemon with busybox: %v", err)
  18. }
  19. if out, err := s.d.Cmd("run", "-d", "--name", "top1", "-p", "1234:80", "--restart", "always", "busybox:latest", "top"); err != nil {
  20. c.Fatalf("Could not run top1: err=%v\n%s", err, out)
  21. }
  22. // --restart=no by default
  23. if out, err := s.d.Cmd("run", "-d", "--name", "top2", "-p", "80", "busybox:latest", "top"); err != nil {
  24. c.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 cont, shouldRun := range m {
  29. out, err := s.d.Cmd("ps")
  30. if err != nil {
  31. c.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, cont) {
  39. c.Fatalf(format, prefix, cont)
  40. }
  41. }
  42. }
  43. testRun(map[string]bool{"top1": true, "top2": true}, "")
  44. if err := s.d.Restart(); err != nil {
  45. c.Fatalf("Could not restart daemon: %v", err)
  46. }
  47. testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ")
  48. }
  49. func (s *DockerDaemonSuite) TestDaemonRestartWithVolumesRefs(c *check.C) {
  50. if err := s.d.StartWithBusybox(); err != nil {
  51. c.Fatal(err)
  52. }
  53. if out, err := s.d.Cmd("run", "-d", "--name", "volrestarttest1", "-v", "/foo", "busybox"); err != nil {
  54. c.Fatal(err, out)
  55. }
  56. if err := s.d.Restart(); err != nil {
  57. c.Fatal(err)
  58. }
  59. if _, err := s.d.Cmd("run", "-d", "--volumes-from", "volrestarttest1", "--name", "volrestarttest2", "busybox", "top"); err != nil {
  60. c.Fatal(err)
  61. }
  62. if out, err := s.d.Cmd("rm", "-fv", "volrestarttest2"); err != nil {
  63. c.Fatal(err, out)
  64. }
  65. v, err := s.d.Cmd("inspect", "--format", "{{ json .Volumes }}", "volrestarttest1")
  66. if err != nil {
  67. c.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. c.Fatalf("Expected volume to exist: %s - %s", volumes["/foo"], err)
  73. }
  74. }
  75. func (s *DockerDaemonSuite) TestDaemonStartIptablesFalse(c *check.C) {
  76. if err := s.d.Start("--iptables=false"); err != nil {
  77. c.Fatalf("we should have been able to start the daemon with passing iptables=false: %v", err)
  78. }
  79. }
  80. // Issue #8444: If docker0 bridge is modified (intentionally or unintentionally) and
  81. // no longer has an IP associated, we should gracefully handle that case and associate
  82. // an IP with it rather than fail daemon start
  83. func (s *DockerDaemonSuite) TestDaemonStartBridgeWithoutIPAssociation(c *check.C) {
  84. // rather than depending on brctl commands to verify docker0 is created and up
  85. // let's start the daemon and stop it, and then make a modification to run the
  86. // actual test
  87. if err := s.d.Start(); err != nil {
  88. c.Fatalf("Could not start daemon: %v", err)
  89. }
  90. if err := s.d.Stop(); err != nil {
  91. c.Fatalf("Could not stop daemon: %v", err)
  92. }
  93. // now we will remove the ip from docker0 and then try starting the daemon
  94. ipCmd := exec.Command("ip", "addr", "flush", "dev", "docker0")
  95. stdout, stderr, _, err := runCommandWithStdoutStderr(ipCmd)
  96. if err != nil {
  97. c.Fatalf("failed to remove docker0 IP association: %v, stdout: %q, stderr: %q", err, stdout, stderr)
  98. }
  99. if err := s.d.Start(); err != nil {
  100. warning := "**WARNING: Docker bridge network in bad state--delete docker0 bridge interface to fix"
  101. c.Fatalf("Could not start daemon when docker0 has no IP address: %v\n%s", err, warning)
  102. }
  103. }
  104. func (s *DockerDaemonSuite) TestDaemonIptablesClean(c *check.C) {
  105. if err := s.d.StartWithBusybox(); err != nil {
  106. c.Fatalf("Could not start daemon with busybox: %v", err)
  107. }
  108. if out, err := s.d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
  109. c.Fatalf("Could not run top: %s, %v", out, err)
  110. }
  111. // get output from iptables with container running
  112. ipTablesSearchString := "tcp dpt:80"
  113. ipTablesCmd := exec.Command("iptables", "-nvL")
  114. out, _, err := runCommandWithOutput(ipTablesCmd)
  115. if err != nil {
  116. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  117. }
  118. if !strings.Contains(out, ipTablesSearchString) {
  119. c.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out)
  120. }
  121. if err := s.d.Stop(); err != nil {
  122. c.Fatalf("Could not stop daemon: %v", err)
  123. }
  124. // get output from iptables after restart
  125. ipTablesCmd = exec.Command("iptables", "-nvL")
  126. out, _, err = runCommandWithOutput(ipTablesCmd)
  127. if err != nil {
  128. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  129. }
  130. if strings.Contains(out, ipTablesSearchString) {
  131. c.Fatalf("iptables output should not have contained %q, but was %q", ipTablesSearchString, out)
  132. }
  133. }
  134. func (s *DockerDaemonSuite) TestDaemonIptablesCreate(c *check.C) {
  135. if err := s.d.StartWithBusybox(); err != nil {
  136. c.Fatalf("Could not start daemon with busybox: %v", err)
  137. }
  138. if out, err := s.d.Cmd("run", "-d", "--name", "top", "--restart=always", "-p", "80", "busybox:latest", "top"); err != nil {
  139. c.Fatalf("Could not run top: %s, %v", out, err)
  140. }
  141. // get output from iptables with container running
  142. ipTablesSearchString := "tcp dpt:80"
  143. ipTablesCmd := exec.Command("iptables", "-nvL")
  144. out, _, err := runCommandWithOutput(ipTablesCmd)
  145. if err != nil {
  146. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  147. }
  148. if !strings.Contains(out, ipTablesSearchString) {
  149. c.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out)
  150. }
  151. if err := s.d.Restart(); err != nil {
  152. c.Fatalf("Could not restart daemon: %v", err)
  153. }
  154. // make sure the container is not running
  155. runningOut, err := s.d.Cmd("inspect", "--format='{{.State.Running}}'", "top")
  156. if err != nil {
  157. c.Fatalf("Could not inspect on container: %s, %v", out, err)
  158. }
  159. if strings.TrimSpace(runningOut) != "true" {
  160. c.Fatalf("Container should have been restarted after daemon restart. Status running should have been true but was: %q", strings.TrimSpace(runningOut))
  161. }
  162. // get output from iptables after restart
  163. ipTablesCmd = exec.Command("iptables", "-nvL")
  164. out, _, err = runCommandWithOutput(ipTablesCmd)
  165. if err != nil {
  166. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  167. }
  168. if !strings.Contains(out, ipTablesSearchString) {
  169. c.Fatalf("iptables output after restart should have contained %q, but was %q", ipTablesSearchString, out)
  170. }
  171. }
  172. func (s *DockerDaemonSuite) TestDaemonLogLevelWrong(c *check.C) {
  173. c.Assert(s.d.Start("--log-level=bogus"), check.NotNil, check.Commentf("Daemon shouldn't start with wrong log level"))
  174. }
  175. func (s *DockerDaemonSuite) TestDaemonLogLevelDebug(c *check.C) {
  176. if err := s.d.Start("--log-level=debug"); err != nil {
  177. c.Fatal(err)
  178. }
  179. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  180. if !strings.Contains(string(content), `level=debug`) {
  181. c.Fatalf(`Missing level="debug" in log file:\n%s`, string(content))
  182. }
  183. }
  184. func (s *DockerDaemonSuite) TestDaemonLogLevelFatal(c *check.C) {
  185. // we creating new daemons to create new logFile
  186. if err := s.d.Start("--log-level=fatal"); err != nil {
  187. c.Fatal(err)
  188. }
  189. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  190. if strings.Contains(string(content), `level=debug`) {
  191. c.Fatalf(`Should not have level="debug" in log file:\n%s`, string(content))
  192. }
  193. }
  194. func (s *DockerDaemonSuite) TestDaemonFlagD(c *check.C) {
  195. if err := s.d.Start("-D"); err != nil {
  196. c.Fatal(err)
  197. }
  198. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  199. if !strings.Contains(string(content), `level=debug`) {
  200. c.Fatalf(`Missing level="debug" in log file using -D:\n%s`, string(content))
  201. }
  202. }
  203. func (s *DockerDaemonSuite) TestDaemonFlagDebug(c *check.C) {
  204. if err := s.d.Start("--debug"); err != nil {
  205. c.Fatal(err)
  206. }
  207. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  208. if !strings.Contains(string(content), `level=debug`) {
  209. c.Fatalf(`Missing level="debug" in log file using --debug:\n%s`, string(content))
  210. }
  211. }
  212. func (s *DockerDaemonSuite) TestDaemonFlagDebugLogLevelFatal(c *check.C) {
  213. if err := s.d.Start("--debug", "--log-level=fatal"); err != nil {
  214. c.Fatal(err)
  215. }
  216. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  217. if !strings.Contains(string(content), `level=debug`) {
  218. c.Fatalf(`Missing level="debug" in log file when using both --debug and --log-level=fatal:\n%s`, string(content))
  219. }
  220. }
  221. func (s *DockerDaemonSuite) TestDaemonAllocatesListeningPort(c *check.C) {
  222. listeningPorts := [][]string{
  223. {"0.0.0.0", "0.0.0.0", "5678"},
  224. {"127.0.0.1", "127.0.0.1", "1234"},
  225. {"localhost", "127.0.0.1", "1235"},
  226. }
  227. cmdArgs := []string{}
  228. for _, hostDirective := range listeningPorts {
  229. cmdArgs = append(cmdArgs, "--host", fmt.Sprintf("tcp://%s:%s", hostDirective[0], hostDirective[2]))
  230. }
  231. if err := s.d.StartWithBusybox(cmdArgs...); err != nil {
  232. c.Fatalf("Could not start daemon with busybox: %v", err)
  233. }
  234. for _, hostDirective := range listeningPorts {
  235. output, err := s.d.Cmd("run", "-p", fmt.Sprintf("%s:%s:80", hostDirective[1], hostDirective[2]), "busybox", "true")
  236. if err == nil {
  237. c.Fatalf("Container should not start, expected port already allocated error: %q", output)
  238. } else if !strings.Contains(output, "port is already allocated") {
  239. c.Fatalf("Expected port is already allocated error: %q", output)
  240. }
  241. }
  242. }
  243. // #9629
  244. func (s *DockerDaemonSuite) TestDaemonVolumesBindsRefs(c *check.C) {
  245. if err := s.d.StartWithBusybox(); err != nil {
  246. c.Fatal(err)
  247. }
  248. tmp, err := ioutil.TempDir(os.TempDir(), "")
  249. if err != nil {
  250. c.Fatal(err)
  251. }
  252. defer os.RemoveAll(tmp)
  253. if err := ioutil.WriteFile(tmp+"/test", []byte("testing"), 0655); err != nil {
  254. c.Fatal(err)
  255. }
  256. if out, err := s.d.Cmd("create", "-v", tmp+":/foo", "--name=voltest", "busybox"); err != nil {
  257. c.Fatal(err, out)
  258. }
  259. if err := s.d.Restart(); err != nil {
  260. c.Fatal(err)
  261. }
  262. if out, err := s.d.Cmd("run", "--volumes-from=voltest", "--name=consumer", "busybox", "/bin/sh", "-c", "[ -f /foo/test ]"); err != nil {
  263. c.Fatal(err, out)
  264. }
  265. }
  266. func (s *DockerDaemonSuite) TestDaemonKeyGeneration(c *check.C) {
  267. // TODO: skip or update for Windows daemon
  268. os.Remove("/etc/docker/key.json")
  269. if err := s.d.Start(); err != nil {
  270. c.Fatalf("Could not start daemon: %v", err)
  271. }
  272. s.d.Stop()
  273. k, err := libtrust.LoadKeyFile("/etc/docker/key.json")
  274. if err != nil {
  275. c.Fatalf("Error opening key file")
  276. }
  277. kid := k.KeyID()
  278. // Test Key ID is a valid fingerprint (e.g. QQXN:JY5W:TBXI:MK3X:GX6P:PD5D:F56N:NHCS:LVRZ:JA46:R24J:XEFF)
  279. if len(kid) != 59 {
  280. c.Fatalf("Bad key ID: %s", kid)
  281. }
  282. }
  283. func (s *DockerDaemonSuite) TestDaemonKeyMigration(c *check.C) {
  284. // TODO: skip or update for Windows daemon
  285. os.Remove("/etc/docker/key.json")
  286. k1, err := libtrust.GenerateECP256PrivateKey()
  287. if err != nil {
  288. c.Fatalf("Error generating private key: %s", err)
  289. }
  290. if err := os.MkdirAll(filepath.Join(os.Getenv("HOME"), ".docker"), 0755); err != nil {
  291. c.Fatalf("Error creating .docker directory: %s", err)
  292. }
  293. if err := libtrust.SaveKey(filepath.Join(os.Getenv("HOME"), ".docker", "key.json"), k1); err != nil {
  294. c.Fatalf("Error saving private key: %s", err)
  295. }
  296. if err := s.d.Start(); err != nil {
  297. c.Fatalf("Could not start daemon: %v", err)
  298. }
  299. s.d.Stop()
  300. k2, err := libtrust.LoadKeyFile("/etc/docker/key.json")
  301. if err != nil {
  302. c.Fatalf("Error opening key file")
  303. }
  304. if k1.KeyID() != k2.KeyID() {
  305. c.Fatalf("Key not migrated")
  306. }
  307. }
  308. // Simulate an older daemon (pre 1.3) coming up with volumes specified in containers
  309. // without corresponding volume json
  310. func (s *DockerDaemonSuite) TestDaemonUpgradeWithVolumes(c *check.C) {
  311. graphDir := filepath.Join(os.TempDir(), "docker-test")
  312. defer os.RemoveAll(graphDir)
  313. if err := s.d.StartWithBusybox("-g", graphDir); err != nil {
  314. c.Fatal(err)
  315. }
  316. tmpDir := filepath.Join(os.TempDir(), "test")
  317. defer os.RemoveAll(tmpDir)
  318. if out, err := s.d.Cmd("create", "-v", tmpDir+":/foo", "--name=test", "busybox"); err != nil {
  319. c.Fatal(err, out)
  320. }
  321. if err := s.d.Stop(); err != nil {
  322. c.Fatal(err)
  323. }
  324. // Remove this since we're expecting the daemon to re-create it too
  325. if err := os.RemoveAll(tmpDir); err != nil {
  326. c.Fatal(err)
  327. }
  328. configDir := filepath.Join(graphDir, "volumes")
  329. if err := os.RemoveAll(configDir); err != nil {
  330. c.Fatal(err)
  331. }
  332. if err := s.d.Start("-g", graphDir); err != nil {
  333. c.Fatal(err)
  334. }
  335. if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
  336. c.Fatalf("expected volume path %s to exist but it does not", tmpDir)
  337. }
  338. dir, err := ioutil.ReadDir(configDir)
  339. if err != nil {
  340. c.Fatal(err)
  341. }
  342. if len(dir) == 0 {
  343. c.Fatalf("expected volumes config dir to contain data for new volume")
  344. }
  345. // Now with just removing the volume config and not the volume data
  346. if err := s.d.Stop(); err != nil {
  347. c.Fatal(err)
  348. }
  349. if err := os.RemoveAll(configDir); err != nil {
  350. c.Fatal(err)
  351. }
  352. if err := s.d.Start("-g", graphDir); err != nil {
  353. c.Fatal(err)
  354. }
  355. dir, err = ioutil.ReadDir(configDir)
  356. if err != nil {
  357. c.Fatal(err)
  358. }
  359. if len(dir) == 0 {
  360. c.Fatalf("expected volumes config dir to contain data for new volume")
  361. }
  362. }
  363. // GH#11320 - verify that the daemon exits on failure properly
  364. // Note that this explicitly tests the conflict of {-b,--bridge} and {--bip} options as the means
  365. // to get a daemon init failure; no other tests for -b/--bip conflict are therefore required
  366. func (s *DockerDaemonSuite) TestDaemonExitOnFailure(c *check.C) {
  367. //attempt to start daemon with incorrect flags (we know -b and --bip conflict)
  368. if err := s.d.Start("--bridge", "nosuchbridge", "--bip", "1.1.1.1"); err != nil {
  369. //verify we got the right error
  370. if !strings.Contains(err.Error(), "Daemon exited and never started") {
  371. c.Fatalf("Expected daemon not to start, got %v", err)
  372. }
  373. // look in the log and make sure we got the message that daemon is shutting down
  374. runCmd := exec.Command("grep", "Error starting daemon", s.d.LogfileName())
  375. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  376. c.Fatalf("Expected 'Error starting daemon' message; but doesn't exist in log: %q, err: %v", out, err)
  377. }
  378. } else {
  379. //if we didn't get an error and the daemon is running, this is a failure
  380. c.Fatal("Conflicting options should cause the daemon to error out with a failure")
  381. }
  382. }
  383. func (s *DockerDaemonSuite) TestDaemonUlimitDefaults(c *check.C) {
  384. testRequires(c, NativeExecDriver)
  385. if err := s.d.StartWithBusybox("--default-ulimit", "nofile=42:42", "--default-ulimit", "nproc=1024:1024"); err != nil {
  386. c.Fatal(err)
  387. }
  388. out, err := s.d.Cmd("run", "--ulimit", "nproc=2048", "--name=test", "busybox", "/bin/sh", "-c", "echo $(ulimit -n); echo $(ulimit -p)")
  389. if err != nil {
  390. c.Fatal(out, err)
  391. }
  392. outArr := strings.Split(out, "\n")
  393. if len(outArr) < 2 {
  394. c.Fatalf("got unexpected output: %s", out)
  395. }
  396. nofile := strings.TrimSpace(outArr[0])
  397. nproc := strings.TrimSpace(outArr[1])
  398. if nofile != "42" {
  399. c.Fatalf("expected `ulimit -n` to be `42`, got: %s", nofile)
  400. }
  401. if nproc != "2048" {
  402. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  403. }
  404. // Now restart daemon with a new default
  405. if err := s.d.Restart("--default-ulimit", "nofile=43"); err != nil {
  406. c.Fatal(err)
  407. }
  408. out, err = s.d.Cmd("start", "-a", "test")
  409. if err != nil {
  410. c.Fatal(err)
  411. }
  412. outArr = strings.Split(out, "\n")
  413. if len(outArr) < 2 {
  414. c.Fatalf("got unexpected output: %s", out)
  415. }
  416. nofile = strings.TrimSpace(outArr[0])
  417. nproc = strings.TrimSpace(outArr[1])
  418. if nofile != "43" {
  419. c.Fatalf("expected `ulimit -n` to be `43`, got: %s", nofile)
  420. }
  421. if nproc != "2048" {
  422. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  423. }
  424. }
  425. // #11315
  426. func (s *DockerDaemonSuite) TestDaemonRestartRenameContainer(c *check.C) {
  427. if err := s.d.StartWithBusybox(); err != nil {
  428. c.Fatal(err)
  429. }
  430. if out, err := s.d.Cmd("run", "--name=test", "busybox"); err != nil {
  431. c.Fatal(err, out)
  432. }
  433. if out, err := s.d.Cmd("rename", "test", "test2"); err != nil {
  434. c.Fatal(err, out)
  435. }
  436. if err := s.d.Restart(); err != nil {
  437. c.Fatal(err)
  438. }
  439. if out, err := s.d.Cmd("start", "test2"); err != nil {
  440. c.Fatal(err, out)
  441. }
  442. }
  443. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefault(c *check.C) {
  444. if err := s.d.StartWithBusybox(); err != nil {
  445. c.Fatal(err)
  446. }
  447. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  448. if err != nil {
  449. c.Fatal(out, err)
  450. }
  451. id := strings.TrimSpace(out)
  452. if out, err := s.d.Cmd("wait", id); err != nil {
  453. c.Fatal(out, err)
  454. }
  455. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  456. if _, err := os.Stat(logPath); err != nil {
  457. c.Fatal(err)
  458. }
  459. f, err := os.Open(logPath)
  460. if err != nil {
  461. c.Fatal(err)
  462. }
  463. var res struct {
  464. Log string `json:"log"`
  465. Stream string `json:"stream"`
  466. Time time.Time `json:"time"`
  467. }
  468. if err := json.NewDecoder(f).Decode(&res); err != nil {
  469. c.Fatal(err)
  470. }
  471. if res.Log != "testline\n" {
  472. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  473. }
  474. if res.Stream != "stdout" {
  475. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  476. }
  477. if !time.Now().After(res.Time) {
  478. c.Fatalf("Log time %v in future", res.Time)
  479. }
  480. }
  481. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefaultOverride(c *check.C) {
  482. if err := s.d.StartWithBusybox(); err != nil {
  483. c.Fatal(err)
  484. }
  485. out, err := s.d.Cmd("run", "-d", "--log-driver=none", "busybox", "echo", "testline")
  486. if err != nil {
  487. c.Fatal(out, err)
  488. }
  489. id := strings.TrimSpace(out)
  490. if out, err := s.d.Cmd("wait", id); err != nil {
  491. c.Fatal(out, err)
  492. }
  493. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  494. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  495. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  496. }
  497. }
  498. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNone(c *check.C) {
  499. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  500. c.Fatal(err)
  501. }
  502. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  503. if err != nil {
  504. c.Fatal(out, err)
  505. }
  506. id := strings.TrimSpace(out)
  507. if out, err := s.d.Cmd("wait", id); err != nil {
  508. c.Fatal(out, err)
  509. }
  510. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  511. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  512. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  513. }
  514. }
  515. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneOverride(c *check.C) {
  516. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  517. c.Fatal(err)
  518. }
  519. out, err := s.d.Cmd("run", "-d", "--log-driver=json-file", "busybox", "echo", "testline")
  520. if err != nil {
  521. c.Fatal(out, err)
  522. }
  523. id := strings.TrimSpace(out)
  524. if out, err := s.d.Cmd("wait", id); err != nil {
  525. c.Fatal(out, err)
  526. }
  527. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  528. if _, err := os.Stat(logPath); err != nil {
  529. c.Fatal(err)
  530. }
  531. f, err := os.Open(logPath)
  532. if err != nil {
  533. c.Fatal(err)
  534. }
  535. var res struct {
  536. Log string `json:"log"`
  537. Stream string `json:"stream"`
  538. Time time.Time `json:"time"`
  539. }
  540. if err := json.NewDecoder(f).Decode(&res); err != nil {
  541. c.Fatal(err)
  542. }
  543. if res.Log != "testline\n" {
  544. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  545. }
  546. if res.Stream != "stdout" {
  547. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  548. }
  549. if !time.Now().After(res.Time) {
  550. c.Fatalf("Log time %v in future", res.Time)
  551. }
  552. }
  553. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneLogsError(c *check.C) {
  554. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  555. c.Fatal(err)
  556. }
  557. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  558. if err != nil {
  559. c.Fatal(out, err)
  560. }
  561. id := strings.TrimSpace(out)
  562. out, err = s.d.Cmd("logs", id)
  563. if err == nil {
  564. c.Fatalf("Logs should fail with \"none\" driver")
  565. }
  566. if !strings.Contains(out, `\"logs\" command is supported only for \"json-file\" logging driver`) {
  567. c.Fatalf("There should be error about non-json-file driver, got %s", out)
  568. }
  569. }
  570. func (s *DockerDaemonSuite) TestDaemonDots(c *check.C) {
  571. if err := s.d.StartWithBusybox(); err != nil {
  572. c.Fatal(err)
  573. }
  574. // Now create 4 containers
  575. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  576. c.Fatalf("Error creating container: %q", err)
  577. }
  578. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  579. c.Fatalf("Error creating container: %q", err)
  580. }
  581. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  582. c.Fatalf("Error creating container: %q", err)
  583. }
  584. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  585. c.Fatalf("Error creating container: %q", err)
  586. }
  587. s.d.Stop()
  588. s.d.Start("--log-level=debug")
  589. s.d.Stop()
  590. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  591. if strings.Contains(string(content), "....") {
  592. c.Fatalf("Debug level should not have ....\n%s", string(content))
  593. }
  594. s.d.Start("--log-level=error")
  595. s.d.Stop()
  596. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  597. if strings.Contains(string(content), "....") {
  598. c.Fatalf("Error level should not have ....\n%s", string(content))
  599. }
  600. s.d.Start("--log-level=info")
  601. s.d.Stop()
  602. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  603. if !strings.Contains(string(content), "....") {
  604. c.Fatalf("Info level should have ....\n%s", string(content))
  605. }
  606. }
  607. func (s *DockerDaemonSuite) TestDaemonUnixSockCleanedUp(c *check.C) {
  608. dir, err := ioutil.TempDir("", "socket-cleanup-test")
  609. if err != nil {
  610. c.Fatal(err)
  611. }
  612. defer os.RemoveAll(dir)
  613. sockPath := filepath.Join(dir, "docker.sock")
  614. if err := s.d.Start("--host", "unix://"+sockPath); err != nil {
  615. c.Fatal(err)
  616. }
  617. if _, err := os.Stat(sockPath); err != nil {
  618. c.Fatal("socket does not exist")
  619. }
  620. if err := s.d.Stop(); err != nil {
  621. c.Fatal(err)
  622. }
  623. if _, err := os.Stat(sockPath); err == nil || !os.IsNotExist(err) {
  624. c.Fatal("unix socket is not cleaned up")
  625. }
  626. }
  627. func (s *DockerDaemonSuite) TestDaemonWithWrongkey(c *check.C) {
  628. type Config struct {
  629. Crv string `json:"crv"`
  630. D string `json:"d"`
  631. Kid string `json:"kid"`
  632. Kty string `json:"kty"`
  633. X string `json:"x"`
  634. Y string `json:"y"`
  635. }
  636. os.Remove("/etc/docker/key.json")
  637. if err := s.d.Start(); err != nil {
  638. c.Fatalf("Failed to start daemon: %v", err)
  639. }
  640. if err := s.d.Stop(); err != nil {
  641. c.Fatalf("Could not stop daemon: %v", err)
  642. }
  643. config := &Config{}
  644. bytes, err := ioutil.ReadFile("/etc/docker/key.json")
  645. if err != nil {
  646. c.Fatalf("Error reading key.json file: %s", err)
  647. }
  648. // byte[] to Data-Struct
  649. if err := json.Unmarshal(bytes, &config); err != nil {
  650. c.Fatalf("Error Unmarshal: %s", err)
  651. }
  652. //replace config.Kid with the fake value
  653. config.Kid = "VSAJ:FUYR:X3H2:B2VZ:KZ6U:CJD5:K7BX:ZXHY:UZXT:P4FT:MJWG:HRJ4"
  654. // NEW Data-Struct to byte[]
  655. newBytes, err := json.Marshal(&config)
  656. if err != nil {
  657. c.Fatalf("Error Marshal: %s", err)
  658. }
  659. // write back
  660. if err := ioutil.WriteFile("/etc/docker/key.json", newBytes, 0400); err != nil {
  661. c.Fatalf("Error ioutil.WriteFile: %s", err)
  662. }
  663. defer os.Remove("/etc/docker/key.json")
  664. if err := s.d.Start(); err == nil {
  665. c.Fatalf("It should not be successful to start daemon with wrong key: %v", err)
  666. }
  667. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  668. if !strings.Contains(string(content), "Public Key ID does not match") {
  669. c.Fatal("Missing KeyID message from daemon logs")
  670. }
  671. }
  672. func (s *DockerDaemonSuite) TestDaemonRestartKillWait(c *check.C) {
  673. if err := s.d.StartWithBusybox(); err != nil {
  674. c.Fatalf("Could not start daemon with busybox: %v", err)
  675. }
  676. out, err := s.d.Cmd("run", "-id", "busybox", "/bin/cat")
  677. if err != nil {
  678. c.Fatalf("Could not run /bin/cat: err=%v\n%s", err, out)
  679. }
  680. containerID := strings.TrimSpace(out)
  681. if out, err := s.d.Cmd("kill", containerID); err != nil {
  682. c.Fatalf("Could not kill %s: err=%v\n%s", containerID, err, out)
  683. }
  684. if err := s.d.Restart(); err != nil {
  685. c.Fatalf("Could not restart daemon: %v", err)
  686. }
  687. errchan := make(chan error)
  688. go func() {
  689. if out, err := s.d.Cmd("wait", containerID); err != nil {
  690. errchan <- fmt.Errorf("%v:\n%s", err, out)
  691. }
  692. close(errchan)
  693. }()
  694. select {
  695. case <-time.After(5 * time.Second):
  696. c.Fatal("Waiting on a stopped (killed) container timed out")
  697. case err := <-errchan:
  698. if err != nil {
  699. c.Fatal(err)
  700. }
  701. }
  702. }
  703. // TestHttpsInfo connects via two-way authenticated HTTPS to the info endpoint
  704. func (s *DockerDaemonSuite) TestHttpsInfo(c *check.C) {
  705. const (
  706. testDaemonHttpsAddr = "localhost:4271"
  707. )
  708. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  709. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHttpsAddr); err != nil {
  710. c.Fatalf("Could not start daemon with busybox: %v", err)
  711. }
  712. //force tcp protocol
  713. host := fmt.Sprintf("tcp://%s", testDaemonHttpsAddr)
  714. daemonArgs := []string{"--host", host, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-cert.pem", "--tlskey", "fixtures/https/client-key.pem"}
  715. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  716. if err != nil {
  717. c.Fatalf("Error Occurred: %s and output: %s", err, out)
  718. }
  719. }
  720. // TestHttpsInfoRogueCert connects via two-way authenticated HTTPS to the info endpoint
  721. // by using a rogue client certificate and checks that it fails with the expected error.
  722. func (s *DockerDaemonSuite) TestHttpsInfoRogueCert(c *check.C) {
  723. const (
  724. errBadCertificate = "remote error: bad certificate"
  725. testDaemonHttpsAddr = "localhost:4271"
  726. )
  727. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  728. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHttpsAddr); err != nil {
  729. c.Fatalf("Could not start daemon with busybox: %v", err)
  730. }
  731. //force tcp protocol
  732. host := fmt.Sprintf("tcp://%s", testDaemonHttpsAddr)
  733. daemonArgs := []string{"--host", host, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"}
  734. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  735. if err == nil || !strings.Contains(out, errBadCertificate) {
  736. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errBadCertificate, err, out)
  737. }
  738. }
  739. // TestHttpsInfoRogueServerCert connects via two-way authenticated HTTPS to the info endpoint
  740. // which provides a rogue server certificate and checks that it fails with the expected error
  741. func (s *DockerDaemonSuite) TestHttpsInfoRogueServerCert(c *check.C) {
  742. const (
  743. errCaUnknown = "x509: certificate signed by unknown authority"
  744. testDaemonRogueHttpsAddr = "localhost:4272"
  745. )
  746. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-rogue-cert.pem",
  747. "--tlskey", "fixtures/https/server-rogue-key.pem", "-H", testDaemonRogueHttpsAddr); err != nil {
  748. c.Fatalf("Could not start daemon with busybox: %v", err)
  749. }
  750. //force tcp protocol
  751. host := fmt.Sprintf("tcp://%s", testDaemonRogueHttpsAddr)
  752. daemonArgs := []string{"--host", host, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"}
  753. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  754. if err == nil || !strings.Contains(out, errCaUnknown) {
  755. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errCaUnknown, err, out)
  756. }
  757. }