docker_cli_run_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "regexp"
  6. "strings"
  7. "testing"
  8. )
  9. // "test123" should be printed by docker run
  10. func TestDockerRunEchoStdout(t *testing.T) {
  11. runCmd := exec.Command(dockerBinary, "run", "busybox", "echo", "test123")
  12. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  13. errorOut(err, t, out)
  14. if out != "test123\n" {
  15. t.Errorf("container should've printed 'test123'")
  16. }
  17. deleteAllContainers()
  18. logDone("run - echo test123")
  19. }
  20. // "test" should be printed
  21. func TestDockerRunEchoStdoutWithMemoryLimit(t *testing.T) {
  22. runCmd := exec.Command(dockerBinary, "run", "-m", "2786432", "busybox", "echo", "test")
  23. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  24. errorOut(err, t, out)
  25. if out != "test\n" {
  26. t.Errorf("container should've printed 'test'")
  27. }
  28. deleteAllContainers()
  29. logDone("run - echo with memory limit")
  30. }
  31. // "test" should be printed
  32. func TestDockerRunEchoStdoutWitCPULimit(t *testing.T) {
  33. runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "busybox", "echo", "test")
  34. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  35. errorOut(err, t, out)
  36. if out != "test\n" {
  37. t.Errorf("container should've printed 'test'")
  38. }
  39. deleteAllContainers()
  40. logDone("run - echo with CPU limit")
  41. }
  42. // "test" should be printed
  43. func TestDockerRunEchoStdoutWithCPUAndMemoryLimit(t *testing.T) {
  44. runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "-m", "2786432", "busybox", "echo", "test")
  45. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  46. errorOut(err, t, out)
  47. if out != "test\n" {
  48. t.Errorf("container should've printed 'test'")
  49. }
  50. deleteAllContainers()
  51. logDone("run - echo with CPU and memory limit")
  52. }
  53. // "test" should be printed
  54. func TestDockerRunEchoNamedContainer(t *testing.T) {
  55. runCmd := exec.Command(dockerBinary, "run", "--name", "testfoonamedcontainer", "busybox", "echo", "test")
  56. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  57. errorOut(err, t, out)
  58. if out != "test\n" {
  59. t.Errorf("container should've printed 'test'")
  60. }
  61. if err := deleteContainer("testfoonamedcontainer"); err != nil {
  62. t.Errorf("failed to remove the named container: %v", err)
  63. }
  64. deleteAllContainers()
  65. logDone("run - echo with named container")
  66. }
  67. // it should be possible to ping Google DNS resolver
  68. // this will fail when Internet access is unavailable
  69. func TestDockerRunPingGoogle(t *testing.T) {
  70. runCmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "8.8.8.8")
  71. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  72. errorOut(err, t, out)
  73. errorOut(err, t, "container should've been able to ping 8.8.8.8")
  74. deleteAllContainers()
  75. logDone("run - ping 8.8.8.8")
  76. }
  77. // the exit code should be 0
  78. // some versions of lxc might make this test fail
  79. func TestDockerRunExitCodeZero(t *testing.T) {
  80. runCmd := exec.Command(dockerBinary, "run", "busybox", "true")
  81. exitCode, err := runCommand(runCmd)
  82. errorOut(err, t, fmt.Sprintf("%s", err))
  83. if exitCode != 0 {
  84. t.Errorf("container should've exited with exit code 0")
  85. }
  86. deleteAllContainers()
  87. logDone("run - exit with 0")
  88. }
  89. // the exit code should be 1
  90. // some versions of lxc might make this test fail
  91. func TestDockerRunExitCodeOne(t *testing.T) {
  92. runCmd := exec.Command(dockerBinary, "run", "busybox", "false")
  93. exitCode, err := runCommand(runCmd)
  94. if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
  95. t.Fatal(err)
  96. }
  97. if exitCode != 1 {
  98. t.Errorf("container should've exited with exit code 1")
  99. }
  100. deleteAllContainers()
  101. logDone("run - exit with 1")
  102. }
  103. // it should be possible to pipe in data via stdin to a process running in a container
  104. // some versions of lxc might make this test fail
  105. func TestRunStdinPipe(t *testing.T) {
  106. runCmd := exec.Command("bash", "-c", `echo "blahblah" | docker run -i -a stdin busybox cat`)
  107. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  108. errorOut(err, t, out)
  109. out = stripTrailingCharacters(out)
  110. inspectCmd := exec.Command(dockerBinary, "inspect", out)
  111. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  112. errorOut(err, t, fmt.Sprintf("out should've been a container id: %s %s", out, inspectOut))
  113. waitCmd := exec.Command(dockerBinary, "wait", out)
  114. _, _, err = runCommandWithOutput(waitCmd)
  115. errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))
  116. logsCmd := exec.Command(dockerBinary, "logs", out)
  117. containerLogs, _, err := runCommandWithOutput(logsCmd)
  118. errorOut(err, t, fmt.Sprintf("error thrown while trying to get container logs: %s", err))
  119. containerLogs = stripTrailingCharacters(containerLogs)
  120. if containerLogs != "blahblah" {
  121. t.Errorf("logs didn't print the container's logs %s", containerLogs)
  122. }
  123. rmCmd := exec.Command(dockerBinary, "rm", out)
  124. _, _, err = runCommandWithOutput(rmCmd)
  125. errorOut(err, t, fmt.Sprintf("rm failed to remove container %s", err))
  126. deleteAllContainers()
  127. logDone("run - pipe in with -i -a stdin")
  128. }
  129. // the container's ID should be printed when starting a container in detached mode
  130. func TestDockerRunDetachedContainerIDPrinting(t *testing.T) {
  131. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  132. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  133. errorOut(err, t, out)
  134. out = stripTrailingCharacters(out)
  135. inspectCmd := exec.Command(dockerBinary, "inspect", out)
  136. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  137. errorOut(err, t, fmt.Sprintf("out should've been a container id: %s %s", out, inspectOut))
  138. waitCmd := exec.Command(dockerBinary, "wait", out)
  139. _, _, err = runCommandWithOutput(waitCmd)
  140. errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))
  141. rmCmd := exec.Command(dockerBinary, "rm", out)
  142. rmOut, _, err := runCommandWithOutput(rmCmd)
  143. errorOut(err, t, "rm failed to remove container")
  144. rmOut = stripTrailingCharacters(rmOut)
  145. if rmOut != out {
  146. t.Errorf("rm didn't print the container ID %s %s", out, rmOut)
  147. }
  148. deleteAllContainers()
  149. logDone("run - print container ID in detached mode")
  150. }
  151. // the working directory should be set correctly
  152. func TestDockerRunWorkingDirectory(t *testing.T) {
  153. runCmd := exec.Command(dockerBinary, "run", "-w", "/root", "busybox", "pwd")
  154. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  155. errorOut(err, t, out)
  156. out = stripTrailingCharacters(out)
  157. if out != "/root" {
  158. t.Errorf("-w failed to set working directory")
  159. }
  160. runCmd = exec.Command(dockerBinary, "run", "--workdir", "/root", "busybox", "pwd")
  161. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  162. errorOut(err, t, out)
  163. out = stripTrailingCharacters(out)
  164. if out != "/root" {
  165. t.Errorf("--workdir failed to set working directory")
  166. }
  167. deleteAllContainers()
  168. logDone("run - run with working directory set by -w")
  169. logDone("run - run with working directory set by --workdir")
  170. }
  171. // pinging Google's DNS resolver should fail when we disable the networking
  172. func TestDockerRunWithoutNetworking(t *testing.T) {
  173. runCmd := exec.Command(dockerBinary, "run", "--networking=false", "busybox", "ping", "-c", "1", "8.8.8.8")
  174. out, _, exitCode, err := runCommandWithStdoutStderr(runCmd)
  175. if err != nil && exitCode != 1 {
  176. t.Fatal(out, err)
  177. }
  178. if exitCode != 1 {
  179. t.Errorf("--networking=false should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
  180. }
  181. runCmd = exec.Command(dockerBinary, "run", "-n=false", "busybox", "ping", "-c", "1", "8.8.8.8")
  182. out, _, exitCode, err = runCommandWithStdoutStderr(runCmd)
  183. if err != nil && exitCode != 1 {
  184. t.Fatal(out, err)
  185. }
  186. if exitCode != 1 {
  187. t.Errorf("-n=false should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
  188. }
  189. deleteAllContainers()
  190. logDone("run - disable networking with --networking=false")
  191. logDone("run - disable networking with -n=false")
  192. }
  193. // Regression test for #4741
  194. func TestDockerRunWithVolumesAsFiles(t *testing.T) {
  195. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true")
  196. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  197. if err != nil && exitCode != 0 {
  198. t.Fatal("1", out, stderr, err)
  199. }
  200. runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/target-file")
  201. out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
  202. if err != nil && exitCode != 0 {
  203. t.Fatal("2", out, stderr, err)
  204. }
  205. deleteAllContainers()
  206. logDone("run - regression test for #4741 - volumes from as files")
  207. }
  208. // Regression test for #4979
  209. func TestDockerRunWithVolumesFromExited(t *testing.T) {
  210. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/some/dir", "busybox", "touch", "/some/dir/file")
  211. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  212. if err != nil && exitCode != 0 {
  213. t.Fatal("1", out, stderr, err)
  214. }
  215. runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/some/dir/file")
  216. out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
  217. if err != nil && exitCode != 0 {
  218. t.Fatal("2", out, stderr, err)
  219. }
  220. deleteAllContainers()
  221. logDone("run - regression test for #4979 - volumes-from on exited container")
  222. }
  223. // Regression test for #4830
  224. func TestDockerRunWithRelativePath(t *testing.T) {
  225. runCmd := exec.Command(dockerBinary, "run", "-v", "tmp:/other-tmp", "busybox", "true")
  226. if _, _, _, err := runCommandWithStdoutStderr(runCmd); err == nil {
  227. t.Fatalf("relative path should result in an error")
  228. }
  229. deleteAllContainers()
  230. logDone("run - volume with relative path")
  231. }
  232. func TestVolumesMountedAsReadonly(t *testing.T) {
  233. cmd := exec.Command(dockerBinary, "run", "-v", "/test:/test:ro", "busybox", "touch", "/test/somefile")
  234. if code, err := runCommand(cmd); err == nil || code == 0 {
  235. t.Fatalf("run should fail because volume is ro: exit code %d", code)
  236. }
  237. deleteAllContainers()
  238. logDone("run - volumes as readonly mount")
  239. }
  240. func TestVolumesFromInReadonlyMode(t *testing.T) {
  241. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true")
  242. if _, err := runCommand(cmd); err != nil {
  243. t.Fatal(err)
  244. }
  245. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:ro", "busybox", "touch", "/test/file")
  246. if code, err := runCommand(cmd); err == nil || code == 0 {
  247. t.Fatalf("run should fail because volume is ro: exit code %d", code)
  248. }
  249. deleteAllContainers()
  250. logDone("run - volumes from as readonly mount")
  251. }
  252. // Regression test for #1201
  253. func TestVolumesFromInReadWriteMode(t *testing.T) {
  254. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true")
  255. if _, err := runCommand(cmd); err != nil {
  256. t.Fatal(err)
  257. }
  258. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "busybox", "touch", "/test/file")
  259. if _, err := runCommand(cmd); err != nil {
  260. t.Fatal(err)
  261. }
  262. deleteAllContainers()
  263. logDone("run - volumes from as read write mount")
  264. }
  265. // Test for #1351
  266. func TestApplyVolumesFromBeforeVolumes(t *testing.T) {
  267. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "touch", "/test/foo")
  268. if _, err := runCommand(cmd); err != nil {
  269. t.Fatal(err)
  270. }
  271. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "-v", "/test", "busybox", "cat", "/test/foo")
  272. if _, err := runCommand(cmd); err != nil {
  273. t.Fatal(err)
  274. }
  275. deleteAllContainers()
  276. logDone("run - volumes from mounted first")
  277. }
  278. func TestMultipleVolumesFrom(t *testing.T) {
  279. cmd := exec.Command(dockerBinary, "run", "--name", "parent1", "-v", "/test", "busybox", "touch", "/test/foo")
  280. if _, err := runCommand(cmd); err != nil {
  281. t.Fatal(err)
  282. }
  283. cmd = exec.Command(dockerBinary, "run", "--name", "parent2", "-v", "/other", "busybox", "touch", "/other/bar")
  284. if _, err := runCommand(cmd); err != nil {
  285. t.Fatal(err)
  286. }
  287. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent1", "--volumes-from", "parent2",
  288. "busybox", "sh", "-c", "cat /test/foo && cat /other/bar")
  289. if _, err := runCommand(cmd); err != nil {
  290. t.Fatal(err)
  291. }
  292. deleteAllContainers()
  293. logDone("run - multiple volumes from")
  294. }
  295. // this tests verifies the ID format for the container
  296. func TestVerifyContainerID(t *testing.T) {
  297. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  298. out, exit, err := runCommandWithOutput(cmd)
  299. if err != nil {
  300. t.Fatal(err)
  301. }
  302. if exit != 0 {
  303. t.Fatalf("expected exit code 0 received %d", exit)
  304. }
  305. match, err := regexp.MatchString("^[0-9a-f]{64}$", strings.TrimSuffix(out, "\n"))
  306. if err != nil {
  307. t.Fatal(err)
  308. }
  309. if !match {
  310. t.Fatalf("Invalid container ID: %s", out)
  311. }
  312. deleteAllContainers()
  313. logDone("run - verify container ID")
  314. }