docker_cli_daemon_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. "testing"
  12. "time"
  13. "github.com/docker/libtrust"
  14. )
  15. func TestDaemonRestartWithRunningContainersPorts(t *testing.T) {
  16. d := NewDaemon(t)
  17. if err := d.StartWithBusybox(); err != nil {
  18. t.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. t.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. t.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 c, shouldRun := range m {
  31. out, err := d.Cmd("ps")
  32. if err != nil {
  33. t.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, c) {
  41. t.Fatalf(format, prefix, c)
  42. }
  43. }
  44. }
  45. testRun(map[string]bool{"top1": true, "top2": true}, "")
  46. if err := d.Restart(); err != nil {
  47. t.Fatalf("Could not restart daemon: %v", err)
  48. }
  49. testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ")
  50. logDone("daemon - running containers on daemon restart")
  51. }
  52. func TestDaemonRestartWithVolumesRefs(t *testing.T) {
  53. d := NewDaemon(t)
  54. if err := d.StartWithBusybox(); err != nil {
  55. t.Fatal(err)
  56. }
  57. defer d.Stop()
  58. if out, err := d.Cmd("run", "-d", "--name", "volrestarttest1", "-v", "/foo", "busybox"); err != nil {
  59. t.Fatal(err, out)
  60. }
  61. if err := d.Restart(); err != nil {
  62. t.Fatal(err)
  63. }
  64. if _, err := d.Cmd("run", "-d", "--volumes-from", "volrestarttest1", "--name", "volrestarttest2", "busybox", "top"); err != nil {
  65. t.Fatal(err)
  66. }
  67. if out, err := d.Cmd("rm", "-fv", "volrestarttest2"); err != nil {
  68. t.Fatal(err, out)
  69. }
  70. v, err := d.Cmd("inspect", "--format", "{{ json .Volumes }}", "volrestarttest1")
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. volumes := make(map[string]string)
  75. json.Unmarshal([]byte(v), &volumes)
  76. if _, err := os.Stat(volumes["/foo"]); err != nil {
  77. t.Fatalf("Expected volume to exist: %s - %s", volumes["/foo"], err)
  78. }
  79. logDone("daemon - volume refs are restored")
  80. }
  81. func TestDaemonStartIptablesFalse(t *testing.T) {
  82. d := NewDaemon(t)
  83. if err := d.Start("--iptables=false"); err != nil {
  84. t.Fatalf("we should have been able to start the daemon with passing iptables=false: %v", err)
  85. }
  86. d.Stop()
  87. logDone("daemon - started daemon with iptables=false")
  88. }
  89. // Issue #8444: If docker0 bridge is modified (intentionally or unintentionally) and
  90. // no longer has an IP associated, we should gracefully handle that case and associate
  91. // an IP with it rather than fail daemon start
  92. func TestDaemonStartBridgeWithoutIPAssociation(t *testing.T) {
  93. d := NewDaemon(t)
  94. // rather than depending on brctl commands to verify docker0 is created and up
  95. // let's start the daemon and stop it, and then make a modification to run the
  96. // actual test
  97. if err := d.Start(); err != nil {
  98. t.Fatalf("Could not start daemon: %v", err)
  99. }
  100. if err := d.Stop(); err != nil {
  101. t.Fatalf("Could not stop daemon: %v", err)
  102. }
  103. // now we will remove the ip from docker0 and then try starting the daemon
  104. ipCmd := exec.Command("ip", "addr", "flush", "dev", "docker0")
  105. stdout, stderr, _, err := runCommandWithStdoutStderr(ipCmd)
  106. if err != nil {
  107. t.Fatalf("failed to remove docker0 IP association: %v, stdout: %q, stderr: %q", err, stdout, stderr)
  108. }
  109. if err := d.Start(); err != nil {
  110. warning := "**WARNING: Docker bridge network in bad state--delete docker0 bridge interface to fix"
  111. t.Fatalf("Could not start daemon when docker0 has no IP address: %v\n%s", err, warning)
  112. }
  113. // cleanup - stop the daemon if test passed
  114. if err := d.Stop(); err != nil {
  115. t.Fatalf("Could not stop daemon: %v", err)
  116. }
  117. logDone("daemon - successful daemon start when bridge has no IP association")
  118. }
  119. func TestDaemonIptablesClean(t *testing.T) {
  120. defer deleteAllContainers()
  121. d := NewDaemon(t)
  122. if err := d.StartWithBusybox(); err != nil {
  123. t.Fatalf("Could not start daemon with busybox: %v", err)
  124. }
  125. defer d.Stop()
  126. if out, err := d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
  127. t.Fatalf("Could not run top: %s, %v", out, err)
  128. }
  129. // get output from iptables with container running
  130. ipTablesSearchString := "tcp dpt:80"
  131. ipTablesCmd := exec.Command("iptables", "-nvL")
  132. out, _, err := runCommandWithOutput(ipTablesCmd)
  133. if err != nil {
  134. t.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  135. }
  136. if !strings.Contains(out, ipTablesSearchString) {
  137. t.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out)
  138. }
  139. if err := d.Stop(); err != nil {
  140. t.Fatalf("Could not stop daemon: %v", err)
  141. }
  142. // get output from iptables after restart
  143. ipTablesCmd = exec.Command("iptables", "-nvL")
  144. out, _, err = runCommandWithOutput(ipTablesCmd)
  145. if err != nil {
  146. t.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  147. }
  148. if strings.Contains(out, ipTablesSearchString) {
  149. t.Fatalf("iptables output should not have contained %q, but was %q", ipTablesSearchString, out)
  150. }
  151. logDone("daemon - run,iptables - iptables rules cleaned after daemon restart")
  152. }
  153. func TestDaemonIptablesCreate(t *testing.T) {
  154. defer deleteAllContainers()
  155. d := NewDaemon(t)
  156. if err := d.StartWithBusybox(); err != nil {
  157. t.Fatalf("Could not start daemon with busybox: %v", err)
  158. }
  159. defer d.Stop()
  160. if out, err := d.Cmd("run", "-d", "--name", "top", "--restart=always", "-p", "80", "busybox:latest", "top"); err != nil {
  161. t.Fatalf("Could not run top: %s, %v", out, err)
  162. }
  163. // get output from iptables with container running
  164. ipTablesSearchString := "tcp dpt:80"
  165. ipTablesCmd := exec.Command("iptables", "-nvL")
  166. out, _, err := runCommandWithOutput(ipTablesCmd)
  167. if err != nil {
  168. t.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  169. }
  170. if !strings.Contains(out, ipTablesSearchString) {
  171. t.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out)
  172. }
  173. if err := d.Restart(); err != nil {
  174. t.Fatalf("Could not restart daemon: %v", err)
  175. }
  176. // make sure the container is not running
  177. runningOut, err := d.Cmd("inspect", "--format='{{.State.Running}}'", "top")
  178. if err != nil {
  179. t.Fatalf("Could not inspect on container: %s, %v", out, err)
  180. }
  181. if strings.TrimSpace(runningOut) != "true" {
  182. t.Fatalf("Container should have been restarted after daemon restart. Status running should have been true but was: %q", strings.TrimSpace(runningOut))
  183. }
  184. // get output from iptables after restart
  185. ipTablesCmd = exec.Command("iptables", "-nvL")
  186. out, _, err = runCommandWithOutput(ipTablesCmd)
  187. if err != nil {
  188. t.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  189. }
  190. if !strings.Contains(out, ipTablesSearchString) {
  191. t.Fatalf("iptables output after restart should have contained %q, but was %q", ipTablesSearchString, out)
  192. }
  193. logDone("daemon - run,iptables - iptables rules for always restarted container created after daemon restart")
  194. }
  195. func TestDaemonLoggingLevel(t *testing.T) {
  196. d := NewDaemon(t)
  197. if err := d.Start("--log-level=bogus"); err == nil {
  198. t.Fatal("Daemon should not have been able to start")
  199. }
  200. d = NewDaemon(t)
  201. if err := d.Start("--log-level=debug"); err != nil {
  202. t.Fatal(err)
  203. }
  204. d.Stop()
  205. content, _ := ioutil.ReadFile(d.logFile.Name())
  206. if !strings.Contains(string(content), `level=debug`) {
  207. t.Fatalf(`Missing level="debug" in log file:\n%s`, string(content))
  208. }
  209. d = NewDaemon(t)
  210. if err := d.Start("--log-level=fatal"); err != nil {
  211. t.Fatal(err)
  212. }
  213. d.Stop()
  214. content, _ = ioutil.ReadFile(d.logFile.Name())
  215. if strings.Contains(string(content), `level=debug`) {
  216. t.Fatalf(`Should not have level="debug" in log file:\n%s`, string(content))
  217. }
  218. d = NewDaemon(t)
  219. if err := d.Start("-D"); err != nil {
  220. t.Fatal(err)
  221. }
  222. d.Stop()
  223. content, _ = ioutil.ReadFile(d.logFile.Name())
  224. if !strings.Contains(string(content), `level=debug`) {
  225. t.Fatalf(`Missing level="debug" in log file using -D:\n%s`, string(content))
  226. }
  227. d = NewDaemon(t)
  228. if err := d.Start("--debug"); err != nil {
  229. t.Fatal(err)
  230. }
  231. d.Stop()
  232. content, _ = ioutil.ReadFile(d.logFile.Name())
  233. if !strings.Contains(string(content), `level=debug`) {
  234. t.Fatalf(`Missing level="debug" in log file using --debug:\n%s`, string(content))
  235. }
  236. d = NewDaemon(t)
  237. if err := d.Start("--debug", "--log-level=fatal"); err != nil {
  238. t.Fatal(err)
  239. }
  240. d.Stop()
  241. content, _ = ioutil.ReadFile(d.logFile.Name())
  242. if !strings.Contains(string(content), `level=debug`) {
  243. t.Fatalf(`Missing level="debug" in log file when using both --debug and --log-level=fatal:\n%s`, string(content))
  244. }
  245. logDone("daemon - Logging Level")
  246. }
  247. func TestDaemonAllocatesListeningPort(t *testing.T) {
  248. listeningPorts := [][]string{
  249. {"0.0.0.0", "0.0.0.0", "5678"},
  250. {"127.0.0.1", "127.0.0.1", "1234"},
  251. {"localhost", "127.0.0.1", "1235"},
  252. }
  253. cmdArgs := []string{}
  254. for _, hostDirective := range listeningPorts {
  255. cmdArgs = append(cmdArgs, "--host", fmt.Sprintf("tcp://%s:%s", hostDirective[0], hostDirective[2]))
  256. }
  257. d := NewDaemon(t)
  258. if err := d.StartWithBusybox(cmdArgs...); err != nil {
  259. t.Fatalf("Could not start daemon with busybox: %v", err)
  260. }
  261. defer d.Stop()
  262. for _, hostDirective := range listeningPorts {
  263. output, err := d.Cmd("run", "-p", fmt.Sprintf("%s:%s:80", hostDirective[1], hostDirective[2]), "busybox", "true")
  264. if err == nil {
  265. t.Fatalf("Container should not start, expected port already allocated error: %q", output)
  266. } else if !strings.Contains(output, "port is already allocated") {
  267. t.Fatalf("Expected port is already allocated error: %q", output)
  268. }
  269. }
  270. logDone("daemon - daemon listening port is allocated")
  271. }
  272. // #9629
  273. func TestDaemonVolumesBindsRefs(t *testing.T) {
  274. d := NewDaemon(t)
  275. if err := d.StartWithBusybox(); err != nil {
  276. t.Fatal(err)
  277. }
  278. tmp, err := ioutil.TempDir(os.TempDir(), "")
  279. if err != nil {
  280. t.Fatal(err)
  281. }
  282. defer os.RemoveAll(tmp)
  283. if err := ioutil.WriteFile(tmp+"/test", []byte("testing"), 0655); err != nil {
  284. t.Fatal(err)
  285. }
  286. if out, err := d.Cmd("create", "-v", tmp+":/foo", "--name=voltest", "busybox"); err != nil {
  287. t.Fatal(err, out)
  288. }
  289. if err := d.Restart(); err != nil {
  290. t.Fatal(err)
  291. }
  292. if out, err := d.Cmd("run", "--volumes-from=voltest", "--name=consumer", "busybox", "/bin/sh", "-c", "[ -f /foo/test ]"); err != nil {
  293. t.Fatal(err, out)
  294. }
  295. logDone("daemon - bind refs in data-containers survive daemon restart")
  296. }
  297. func TestDaemonKeyGeneration(t *testing.T) {
  298. // TODO: skip or update for Windows daemon
  299. os.Remove("/etc/docker/key.json")
  300. d := NewDaemon(t)
  301. if err := d.Start(); err != nil {
  302. t.Fatalf("Could not start daemon: %v", err)
  303. }
  304. d.Stop()
  305. k, err := libtrust.LoadKeyFile("/etc/docker/key.json")
  306. if err != nil {
  307. t.Fatalf("Error opening key file")
  308. }
  309. kid := k.KeyID()
  310. // Test Key ID is a valid fingerprint (e.g. QQXN:JY5W:TBXI:MK3X:GX6P:PD5D:F56N:NHCS:LVRZ:JA46:R24J:XEFF)
  311. if len(kid) != 59 {
  312. t.Fatalf("Bad key ID: %s", kid)
  313. }
  314. logDone("daemon - key generation")
  315. }
  316. func TestDaemonKeyMigration(t *testing.T) {
  317. // TODO: skip or update for Windows daemon
  318. os.Remove("/etc/docker/key.json")
  319. k1, err := libtrust.GenerateECP256PrivateKey()
  320. if err != nil {
  321. t.Fatalf("Error generating private key: %s", err)
  322. }
  323. if err := os.MkdirAll(filepath.Join(os.Getenv("HOME"), ".docker"), 0755); err != nil {
  324. t.Fatalf("Error creating .docker directory: %s", err)
  325. }
  326. if err := libtrust.SaveKey(filepath.Join(os.Getenv("HOME"), ".docker", "key.json"), k1); err != nil {
  327. t.Fatalf("Error saving private key: %s", err)
  328. }
  329. d := NewDaemon(t)
  330. if err := d.Start(); err != nil {
  331. t.Fatalf("Could not start daemon: %v", err)
  332. }
  333. d.Stop()
  334. k2, err := libtrust.LoadKeyFile("/etc/docker/key.json")
  335. if err != nil {
  336. t.Fatalf("Error opening key file")
  337. }
  338. if k1.KeyID() != k2.KeyID() {
  339. t.Fatalf("Key not migrated")
  340. }
  341. logDone("daemon - key migration")
  342. }
  343. // Simulate an older daemon (pre 1.3) coming up with volumes specified in containers
  344. // without corresponding volume json
  345. func TestDaemonUpgradeWithVolumes(t *testing.T) {
  346. d := NewDaemon(t)
  347. graphDir := filepath.Join(os.TempDir(), "docker-test")
  348. defer os.RemoveAll(graphDir)
  349. if err := d.StartWithBusybox("-g", graphDir); err != nil {
  350. t.Fatal(err)
  351. }
  352. tmpDir := filepath.Join(os.TempDir(), "test")
  353. defer os.RemoveAll(tmpDir)
  354. if out, err := d.Cmd("create", "-v", tmpDir+":/foo", "--name=test", "busybox"); err != nil {
  355. t.Fatal(err, out)
  356. }
  357. if err := d.Stop(); err != nil {
  358. t.Fatal(err)
  359. }
  360. // Remove this since we're expecting the daemon to re-create it too
  361. if err := os.RemoveAll(tmpDir); err != nil {
  362. t.Fatal(err)
  363. }
  364. configDir := filepath.Join(graphDir, "volumes")
  365. if err := os.RemoveAll(configDir); err != nil {
  366. t.Fatal(err)
  367. }
  368. if err := d.Start("-g", graphDir); err != nil {
  369. t.Fatal(err)
  370. }
  371. if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
  372. t.Fatalf("expected volume path %s to exist but it does not", tmpDir)
  373. }
  374. dir, err := ioutil.ReadDir(configDir)
  375. if err != nil {
  376. t.Fatal(err)
  377. }
  378. if len(dir) == 0 {
  379. t.Fatalf("expected volumes config dir to contain data for new volume")
  380. }
  381. // Now with just removing the volume config and not the volume data
  382. if err := d.Stop(); err != nil {
  383. t.Fatal(err)
  384. }
  385. if err := os.RemoveAll(configDir); err != nil {
  386. t.Fatal(err)
  387. }
  388. if err := d.Start("-g", graphDir); err != nil {
  389. t.Fatal(err)
  390. }
  391. dir, err = ioutil.ReadDir(configDir)
  392. if err != nil {
  393. t.Fatal(err)
  394. }
  395. if len(dir) == 0 {
  396. t.Fatalf("expected volumes config dir to contain data for new volume")
  397. }
  398. logDone("daemon - volumes from old(pre 1.3) daemon work")
  399. }
  400. // GH#11320 - verify that the daemon exits on failure properly
  401. // Note that this explicitly tests the conflict of {-b,--bridge} and {--bip} options as the means
  402. // to get a daemon init failure; no other tests for -b/--bip conflict are therefore required
  403. func TestDaemonExitOnFailure(t *testing.T) {
  404. d := NewDaemon(t)
  405. defer d.Stop()
  406. //attempt to start daemon with incorrect flags (we know -b and --bip conflict)
  407. if err := d.Start("--bridge", "nosuchbridge", "--bip", "1.1.1.1"); err != nil {
  408. //verify we got the right error
  409. if !strings.Contains(err.Error(), "Daemon exited and never started") {
  410. t.Fatalf("Expected daemon not to start, got %v", err)
  411. }
  412. // look in the log and make sure we got the message that daemon is shutting down
  413. runCmd := exec.Command("grep", "Shutting down daemon due to", d.LogfileName())
  414. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  415. t.Fatalf("Expected 'shutting down daemon due to error' message; but doesn't exist in log: %q, err: %v", out, err)
  416. }
  417. } else {
  418. //if we didn't get an error and the daemon is running, this is a failure
  419. d.Stop()
  420. t.Fatal("Conflicting options should cause the daemon to error out with a failure")
  421. }
  422. logDone("daemon - verify no start on daemon init errors")
  423. }
  424. func TestDaemonUlimitDefaults(t *testing.T) {
  425. testRequires(t, NativeExecDriver)
  426. d := NewDaemon(t)
  427. if err := d.StartWithBusybox("--default-ulimit", "nofile=42:42", "--default-ulimit", "nproc=1024:1024"); err != nil {
  428. t.Fatal(err)
  429. }
  430. out, err := d.Cmd("run", "--ulimit", "nproc=2048", "--name=test", "busybox", "/bin/sh", "-c", "echo $(ulimit -n); echo $(ulimit -p)")
  431. if err != nil {
  432. t.Fatal(out, err)
  433. }
  434. outArr := strings.Split(out, "\n")
  435. if len(outArr) < 2 {
  436. t.Fatal("got unexpected output: %s", out)
  437. }
  438. nofile := strings.TrimSpace(outArr[0])
  439. nproc := strings.TrimSpace(outArr[1])
  440. if nofile != "42" {
  441. t.Fatalf("expected `ulimit -n` to be `42`, got: %s", nofile)
  442. }
  443. if nproc != "2048" {
  444. t.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  445. }
  446. // Now restart daemon with a new default
  447. if err := d.Restart("--default-ulimit", "nofile=43"); err != nil {
  448. t.Fatal(err)
  449. }
  450. out, err = d.Cmd("start", "-a", "test")
  451. if err != nil {
  452. t.Fatal(err)
  453. }
  454. outArr = strings.Split(out, "\n")
  455. if len(outArr) < 2 {
  456. t.Fatal("got unexpected output: %s", out)
  457. }
  458. nofile = strings.TrimSpace(outArr[0])
  459. nproc = strings.TrimSpace(outArr[1])
  460. if nofile != "43" {
  461. t.Fatalf("expected `ulimit -n` to be `43`, got: %s", nofile)
  462. }
  463. if nproc != "2048" {
  464. t.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  465. }
  466. logDone("daemon - default ulimits are applied")
  467. }
  468. // #11315
  469. func TestDaemonRestartRenameContainer(t *testing.T) {
  470. d := NewDaemon(t)
  471. if err := d.StartWithBusybox(); err != nil {
  472. t.Fatal(err)
  473. }
  474. if out, err := d.Cmd("run", "--name=test", "busybox"); err != nil {
  475. t.Fatal(err, out)
  476. }
  477. if out, err := d.Cmd("rename", "test", "test2"); err != nil {
  478. t.Fatal(err, out)
  479. }
  480. if err := d.Restart(); err != nil {
  481. t.Fatal(err)
  482. }
  483. if out, err := d.Cmd("start", "test2"); err != nil {
  484. t.Fatal(err, out)
  485. }
  486. logDone("daemon - rename persists through daemon restart")
  487. }
  488. func TestDaemonLoggingDriverDefault(t *testing.T) {
  489. d := NewDaemon(t)
  490. if err := d.StartWithBusybox(); err != nil {
  491. t.Fatal(err)
  492. }
  493. defer d.Stop()
  494. out, err := d.Cmd("run", "-d", "busybox", "echo", "testline")
  495. if err != nil {
  496. t.Fatal(out, err)
  497. }
  498. id := strings.TrimSpace(out)
  499. if out, err := d.Cmd("wait", id); err != nil {
  500. t.Fatal(out, err)
  501. }
  502. logPath := filepath.Join(d.folder, "graph", "containers", id, id+"-json.log")
  503. if _, err := os.Stat(logPath); err != nil {
  504. t.Fatal(err)
  505. }
  506. f, err := os.Open(logPath)
  507. if err != nil {
  508. t.Fatal(err)
  509. }
  510. var res struct {
  511. Log string `json:log`
  512. Stream string `json:stream`
  513. Time time.Time `json:time`
  514. }
  515. if err := json.NewDecoder(f).Decode(&res); err != nil {
  516. t.Fatal(err)
  517. }
  518. if res.Log != "testline\n" {
  519. t.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  520. }
  521. if res.Stream != "stdout" {
  522. t.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  523. }
  524. if !time.Now().After(res.Time) {
  525. t.Fatalf("Log time %v in future", res.Time)
  526. }
  527. logDone("daemon - default 'json-file' logging driver")
  528. }
  529. func TestDaemonLoggingDriverDefaultOverride(t *testing.T) {
  530. d := NewDaemon(t)
  531. if err := d.StartWithBusybox(); err != nil {
  532. t.Fatal(err)
  533. }
  534. defer d.Stop()
  535. out, err := d.Cmd("run", "-d", "--log-driver=none", "busybox", "echo", "testline")
  536. if err != nil {
  537. t.Fatal(out, err)
  538. }
  539. id := strings.TrimSpace(out)
  540. if out, err := d.Cmd("wait", id); err != nil {
  541. t.Fatal(out, err)
  542. }
  543. logPath := filepath.Join(d.folder, "graph", "containers", id, id+"-json.log")
  544. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  545. t.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  546. }
  547. logDone("daemon - default logging driver override in run")
  548. }
  549. func TestDaemonLoggingDriverNone(t *testing.T) {
  550. d := NewDaemon(t)
  551. if err := d.StartWithBusybox("--log-driver=none"); err != nil {
  552. t.Fatal(err)
  553. }
  554. defer d.Stop()
  555. out, err := d.Cmd("run", "-d", "busybox", "echo", "testline")
  556. if err != nil {
  557. t.Fatal(out, err)
  558. }
  559. id := strings.TrimSpace(out)
  560. if out, err := d.Cmd("wait", id); err != nil {
  561. t.Fatal(out, err)
  562. }
  563. logPath := filepath.Join(d.folder, "graph", "containers", id, id+"-json.log")
  564. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  565. t.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  566. }
  567. logDone("daemon - 'none' logging driver")
  568. }
  569. func TestDaemonLoggingDriverNoneOverride(t *testing.T) {
  570. d := NewDaemon(t)
  571. if err := d.StartWithBusybox("--log-driver=none"); err != nil {
  572. t.Fatal(err)
  573. }
  574. defer d.Stop()
  575. out, err := d.Cmd("run", "-d", "--log-driver=json-file", "busybox", "echo", "testline")
  576. if err != nil {
  577. t.Fatal(out, err)
  578. }
  579. id := strings.TrimSpace(out)
  580. if out, err := d.Cmd("wait", id); err != nil {
  581. t.Fatal(out, err)
  582. }
  583. logPath := filepath.Join(d.folder, "graph", "containers", id, id+"-json.log")
  584. if _, err := os.Stat(logPath); err != nil {
  585. t.Fatal(err)
  586. }
  587. f, err := os.Open(logPath)
  588. if err != nil {
  589. t.Fatal(err)
  590. }
  591. var res struct {
  592. Log string `json:log`
  593. Stream string `json:stream`
  594. Time time.Time `json:time`
  595. }
  596. if err := json.NewDecoder(f).Decode(&res); err != nil {
  597. t.Fatal(err)
  598. }
  599. if res.Log != "testline\n" {
  600. t.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  601. }
  602. if res.Stream != "stdout" {
  603. t.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  604. }
  605. if !time.Now().After(res.Time) {
  606. t.Fatalf("Log time %v in future", res.Time)
  607. }
  608. logDone("daemon - 'none' logging driver override in run")
  609. }
  610. func TestDaemonLoggingDriverNoneLogsError(t *testing.T) {
  611. d := NewDaemon(t)
  612. if err := d.StartWithBusybox("--log-driver=none"); err != nil {
  613. t.Fatal(err)
  614. }
  615. defer d.Stop()
  616. out, err := d.Cmd("run", "-d", "busybox", "echo", "testline")
  617. if err != nil {
  618. t.Fatal(out, err)
  619. }
  620. id := strings.TrimSpace(out)
  621. out, err = d.Cmd("logs", id)
  622. if err == nil {
  623. t.Fatalf("Logs should fail with \"none\" driver")
  624. }
  625. if !strings.Contains(out, `\"logs\" command is supported only for \"json-file\" logging driver`) {
  626. t.Fatalf("There should be error about non-json-file driver, got %s", out)
  627. }
  628. logDone("daemon - logs not available for non-json-file drivers")
  629. }