docker_cli_daemon_test.go 27 KB

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