docker_cli_run_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "reflect"
  7. "regexp"
  8. "sort"
  9. "strings"
  10. "sync"
  11. "testing"
  12. )
  13. // "test123" should be printed by docker run
  14. func TestDockerRunEchoStdout(t *testing.T) {
  15. runCmd := exec.Command(dockerBinary, "run", "busybox", "echo", "test123")
  16. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  17. errorOut(err, t, out)
  18. if out != "test123\n" {
  19. t.Errorf("container should've printed 'test123'")
  20. }
  21. deleteAllContainers()
  22. logDone("run - echo test123")
  23. }
  24. // "test" should be printed
  25. func TestDockerRunEchoStdoutWithMemoryLimit(t *testing.T) {
  26. runCmd := exec.Command(dockerBinary, "run", "-m", "2786432", "busybox", "echo", "test")
  27. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  28. errorOut(err, t, out)
  29. if out != "test\n" {
  30. t.Errorf("container should've printed 'test'")
  31. }
  32. deleteAllContainers()
  33. logDone("run - echo with memory limit")
  34. }
  35. // "test" should be printed
  36. func TestDockerRunEchoStdoutWitCPULimit(t *testing.T) {
  37. runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "busybox", "echo", "test")
  38. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  39. errorOut(err, t, out)
  40. if out != "test\n" {
  41. t.Errorf("container should've printed 'test'")
  42. }
  43. deleteAllContainers()
  44. logDone("run - echo with CPU limit")
  45. }
  46. // "test" should be printed
  47. func TestDockerRunEchoStdoutWithCPUAndMemoryLimit(t *testing.T) {
  48. runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "-m", "2786432", "busybox", "echo", "test")
  49. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  50. errorOut(err, t, out)
  51. if out != "test\n" {
  52. t.Errorf("container should've printed 'test'")
  53. }
  54. deleteAllContainers()
  55. logDone("run - echo with CPU and memory limit")
  56. }
  57. // "test" should be printed
  58. func TestDockerRunEchoNamedContainer(t *testing.T) {
  59. runCmd := exec.Command(dockerBinary, "run", "--name", "testfoonamedcontainer", "busybox", "echo", "test")
  60. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  61. errorOut(err, t, out)
  62. if out != "test\n" {
  63. t.Errorf("container should've printed 'test'")
  64. }
  65. if err := deleteContainer("testfoonamedcontainer"); err != nil {
  66. t.Errorf("failed to remove the named container: %v", err)
  67. }
  68. deleteAllContainers()
  69. logDone("run - echo with named container")
  70. }
  71. // docker run should not leak file descriptors
  72. func TestDockerRunLeakyFileDescriptors(t *testing.T) {
  73. runCmd := exec.Command(dockerBinary, "run", "busybox", "ls", "-C", "/proc/self/fd")
  74. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  75. errorOut(err, t, out)
  76. // normally, we should only get 0, 1, and 2, but 3 gets created by "ls" when it does "opendir" on the "fd" directory
  77. if out != "0 1 2 3\n" {
  78. t.Errorf("container should've printed '0 1 2 3', not: %s", out)
  79. }
  80. deleteAllContainers()
  81. logDone("run - check file descriptor leakage")
  82. }
  83. // it should be possible to ping Google DNS resolver
  84. // this will fail when Internet access is unavailable
  85. func TestDockerRunPingGoogle(t *testing.T) {
  86. runCmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "8.8.8.8")
  87. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  88. errorOut(err, t, out)
  89. errorOut(err, t, "container should've been able to ping 8.8.8.8")
  90. deleteAllContainers()
  91. logDone("run - ping 8.8.8.8")
  92. }
  93. // the exit code should be 0
  94. // some versions of lxc might make this test fail
  95. func TestDockerRunExitCodeZero(t *testing.T) {
  96. runCmd := exec.Command(dockerBinary, "run", "busybox", "true")
  97. exitCode, err := runCommand(runCmd)
  98. errorOut(err, t, fmt.Sprintf("%s", err))
  99. if exitCode != 0 {
  100. t.Errorf("container should've exited with exit code 0")
  101. }
  102. deleteAllContainers()
  103. logDone("run - exit with 0")
  104. }
  105. // the exit code should be 1
  106. // some versions of lxc might make this test fail
  107. func TestDockerRunExitCodeOne(t *testing.T) {
  108. runCmd := exec.Command(dockerBinary, "run", "busybox", "false")
  109. exitCode, err := runCommand(runCmd)
  110. if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
  111. t.Fatal(err)
  112. }
  113. if exitCode != 1 {
  114. t.Errorf("container should've exited with exit code 1")
  115. }
  116. deleteAllContainers()
  117. logDone("run - exit with 1")
  118. }
  119. // it should be possible to pipe in data via stdin to a process running in a container
  120. // some versions of lxc might make this test fail
  121. func TestRunStdinPipe(t *testing.T) {
  122. runCmd := exec.Command("bash", "-c", `echo "blahblah" | docker run -i -a stdin busybox cat`)
  123. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  124. errorOut(err, t, out)
  125. out = stripTrailingCharacters(out)
  126. inspectCmd := exec.Command(dockerBinary, "inspect", out)
  127. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  128. errorOut(err, t, fmt.Sprintf("out should've been a container id: %s %s", out, inspectOut))
  129. waitCmd := exec.Command(dockerBinary, "wait", out)
  130. _, _, err = runCommandWithOutput(waitCmd)
  131. errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))
  132. logsCmd := exec.Command(dockerBinary, "logs", out)
  133. containerLogs, _, err := runCommandWithOutput(logsCmd)
  134. errorOut(err, t, fmt.Sprintf("error thrown while trying to get container logs: %s", err))
  135. containerLogs = stripTrailingCharacters(containerLogs)
  136. if containerLogs != "blahblah" {
  137. t.Errorf("logs didn't print the container's logs %s", containerLogs)
  138. }
  139. rmCmd := exec.Command(dockerBinary, "rm", out)
  140. _, _, err = runCommandWithOutput(rmCmd)
  141. errorOut(err, t, fmt.Sprintf("rm failed to remove container %s", err))
  142. deleteAllContainers()
  143. logDone("run - pipe in with -i -a stdin")
  144. }
  145. // the container's ID should be printed when starting a container in detached mode
  146. func TestDockerRunDetachedContainerIDPrinting(t *testing.T) {
  147. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  148. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  149. errorOut(err, t, out)
  150. out = stripTrailingCharacters(out)
  151. inspectCmd := exec.Command(dockerBinary, "inspect", out)
  152. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  153. errorOut(err, t, fmt.Sprintf("out should've been a container id: %s %s", out, inspectOut))
  154. waitCmd := exec.Command(dockerBinary, "wait", out)
  155. _, _, err = runCommandWithOutput(waitCmd)
  156. errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))
  157. rmCmd := exec.Command(dockerBinary, "rm", out)
  158. rmOut, _, err := runCommandWithOutput(rmCmd)
  159. errorOut(err, t, "rm failed to remove container")
  160. rmOut = stripTrailingCharacters(rmOut)
  161. if rmOut != out {
  162. t.Errorf("rm didn't print the container ID %s %s", out, rmOut)
  163. }
  164. deleteAllContainers()
  165. logDone("run - print container ID in detached mode")
  166. }
  167. // the working directory should be set correctly
  168. func TestDockerRunWorkingDirectory(t *testing.T) {
  169. runCmd := exec.Command(dockerBinary, "run", "-w", "/root", "busybox", "pwd")
  170. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  171. errorOut(err, t, out)
  172. out = stripTrailingCharacters(out)
  173. if out != "/root" {
  174. t.Errorf("-w failed to set working directory")
  175. }
  176. runCmd = exec.Command(dockerBinary, "run", "--workdir", "/root", "busybox", "pwd")
  177. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  178. errorOut(err, t, out)
  179. out = stripTrailingCharacters(out)
  180. if out != "/root" {
  181. t.Errorf("--workdir failed to set working directory")
  182. }
  183. deleteAllContainers()
  184. logDone("run - run with working directory set by -w")
  185. logDone("run - run with working directory set by --workdir")
  186. }
  187. // pinging Google's DNS resolver should fail when we disable the networking
  188. func TestDockerRunWithoutNetworking(t *testing.T) {
  189. runCmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "8.8.8.8")
  190. out, _, exitCode, err := runCommandWithStdoutStderr(runCmd)
  191. if err != nil && exitCode != 1 {
  192. t.Fatal(out, err)
  193. }
  194. if exitCode != 1 {
  195. t.Errorf("--net=none should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
  196. }
  197. runCmd = exec.Command(dockerBinary, "run", "-n=false", "busybox", "ping", "-c", "1", "8.8.8.8")
  198. out, _, exitCode, err = runCommandWithStdoutStderr(runCmd)
  199. if err != nil && exitCode != 1 {
  200. t.Fatal(out, err)
  201. }
  202. if exitCode != 1 {
  203. t.Errorf("-n=false should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
  204. }
  205. deleteAllContainers()
  206. logDone("run - disable networking with --net=none")
  207. logDone("run - disable networking with -n=false")
  208. }
  209. // Regression test for #4741
  210. func TestDockerRunWithVolumesAsFiles(t *testing.T) {
  211. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true")
  212. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  213. if err != nil && exitCode != 0 {
  214. t.Fatal("1", out, stderr, err)
  215. }
  216. runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/target-file")
  217. out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
  218. if err != nil && exitCode != 0 {
  219. t.Fatal("2", out, stderr, err)
  220. }
  221. deleteAllContainers()
  222. logDone("run - regression test for #4741 - volumes from as files")
  223. }
  224. // Regression test for #4979
  225. func TestDockerRunWithVolumesFromExited(t *testing.T) {
  226. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/some/dir", "busybox", "touch", "/some/dir/file")
  227. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  228. if err != nil && exitCode != 0 {
  229. t.Fatal("1", out, stderr, err)
  230. }
  231. runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/some/dir/file")
  232. out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
  233. if err != nil && exitCode != 0 {
  234. t.Fatal("2", out, stderr, err)
  235. }
  236. deleteAllContainers()
  237. logDone("run - regression test for #4979 - volumes-from on exited container")
  238. }
  239. // Regression test for #4830
  240. func TestDockerRunWithRelativePath(t *testing.T) {
  241. runCmd := exec.Command(dockerBinary, "run", "-v", "tmp:/other-tmp", "busybox", "true")
  242. if _, _, _, err := runCommandWithStdoutStderr(runCmd); err == nil {
  243. t.Fatalf("relative path should result in an error")
  244. }
  245. deleteAllContainers()
  246. logDone("run - volume with relative path")
  247. }
  248. func TestVolumesMountedAsReadonly(t *testing.T) {
  249. cmd := exec.Command(dockerBinary, "run", "-v", "/test:/test:ro", "busybox", "touch", "/test/somefile")
  250. if code, err := runCommand(cmd); err == nil || code == 0 {
  251. t.Fatalf("run should fail because volume is ro: exit code %d", code)
  252. }
  253. deleteAllContainers()
  254. logDone("run - volumes as readonly mount")
  255. }
  256. func TestVolumesFromInReadonlyMode(t *testing.T) {
  257. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true")
  258. if _, err := runCommand(cmd); err != nil {
  259. t.Fatal(err)
  260. }
  261. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:ro", "busybox", "touch", "/test/file")
  262. if code, err := runCommand(cmd); err == nil || code == 0 {
  263. t.Fatalf("run should fail because volume is ro: exit code %d", code)
  264. }
  265. deleteAllContainers()
  266. logDone("run - volumes from as readonly mount")
  267. }
  268. // Regression test for #1201
  269. func TestVolumesFromInReadWriteMode(t *testing.T) {
  270. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true")
  271. if _, err := runCommand(cmd); err != nil {
  272. t.Fatal(err)
  273. }
  274. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "busybox", "touch", "/test/file")
  275. if _, err := runCommand(cmd); err != nil {
  276. t.Fatal(err)
  277. }
  278. deleteAllContainers()
  279. logDone("run - volumes from as read write mount")
  280. }
  281. // Test for #1351
  282. func TestApplyVolumesFromBeforeVolumes(t *testing.T) {
  283. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "touch", "/test/foo")
  284. if _, err := runCommand(cmd); err != nil {
  285. t.Fatal(err)
  286. }
  287. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "-v", "/test", "busybox", "cat", "/test/foo")
  288. if _, err := runCommand(cmd); err != nil {
  289. t.Fatal(err)
  290. }
  291. deleteAllContainers()
  292. logDone("run - volumes from mounted first")
  293. }
  294. func TestMultipleVolumesFrom(t *testing.T) {
  295. cmd := exec.Command(dockerBinary, "run", "--name", "parent1", "-v", "/test", "busybox", "touch", "/test/foo")
  296. if _, err := runCommand(cmd); err != nil {
  297. t.Fatal(err)
  298. }
  299. cmd = exec.Command(dockerBinary, "run", "--name", "parent2", "-v", "/other", "busybox", "touch", "/other/bar")
  300. if _, err := runCommand(cmd); err != nil {
  301. t.Fatal(err)
  302. }
  303. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent1", "--volumes-from", "parent2",
  304. "busybox", "sh", "-c", "cat /test/foo && cat /other/bar")
  305. if _, err := runCommand(cmd); err != nil {
  306. t.Fatal(err)
  307. }
  308. deleteAllContainers()
  309. logDone("run - multiple volumes from")
  310. }
  311. // this tests verifies the ID format for the container
  312. func TestVerifyContainerID(t *testing.T) {
  313. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  314. out, exit, err := runCommandWithOutput(cmd)
  315. if err != nil {
  316. t.Fatal(err)
  317. }
  318. if exit != 0 {
  319. t.Fatalf("expected exit code 0 received %d", exit)
  320. }
  321. match, err := regexp.MatchString("^[0-9a-f]{64}$", strings.TrimSuffix(out, "\n"))
  322. if err != nil {
  323. t.Fatal(err)
  324. }
  325. if !match {
  326. t.Fatalf("Invalid container ID: %s", out)
  327. }
  328. deleteAllContainers()
  329. logDone("run - verify container ID")
  330. }
  331. // Test that creating a container with a volume doesn't crash. Regression test for #995.
  332. func TestCreateVolume(t *testing.T) {
  333. cmd := exec.Command(dockerBinary, "run", "-v", "/var/lib/data", "busybox", "true")
  334. if _, err := runCommand(cmd); err != nil {
  335. t.Fatal(err)
  336. }
  337. deleteAllContainers()
  338. logDone("run - create docker managed volume")
  339. }
  340. // Test that creating a volume with a symlink in its path works correctly. Test for #5152.
  341. // Note that this bug happens only with symlinks with a target that starts with '/'.
  342. func TestCreateVolumeWithSymlink(t *testing.T) {
  343. buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-createvolumewithsymlink", "-")
  344. buildCmd.Stdin = strings.NewReader(`FROM busybox
  345. RUN mkdir /foo && ln -s /foo /bar`)
  346. buildCmd.Dir = workingDirectory
  347. err := buildCmd.Run()
  348. if err != nil {
  349. t.Fatalf("could not build 'docker-test-createvolumewithsymlink': %v", err)
  350. }
  351. cmd := exec.Command(dockerBinary, "run", "-v", "/bar/foo", "--name", "test-createvolumewithsymlink", "docker-test-createvolumewithsymlink", "sh", "-c", "mount | grep -q /foo/foo")
  352. exitCode, err := runCommand(cmd)
  353. if err != nil || exitCode != 0 {
  354. t.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
  355. }
  356. var volPath string
  357. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{range .Volumes}}{{.}}{{end}}", "test-createvolumewithsymlink")
  358. volPath, exitCode, err = runCommandWithOutput(cmd)
  359. if err != nil || exitCode != 0 {
  360. t.Fatalf("[inspect] err: %v, exitcode: %d", err, exitCode)
  361. }
  362. cmd = exec.Command(dockerBinary, "rm", "-v", "test-createvolumewithsymlink")
  363. exitCode, err = runCommand(cmd)
  364. if err != nil || exitCode != 0 {
  365. t.Fatalf("[rm] err: %v, exitcode: %d", err, exitCode)
  366. }
  367. f, err := os.Open(volPath)
  368. defer f.Close()
  369. if !os.IsNotExist(err) {
  370. t.Fatalf("[open] (expecting 'file does not exist' error) err: %v, volPath: %s", err, volPath)
  371. }
  372. deleteImages("docker-test-createvolumewithsymlink")
  373. deleteAllContainers()
  374. logDone("run - create volume with symlink")
  375. }
  376. // Tests that a volume path that has a symlink exists in a container mounting it with `--volumes-from`.
  377. func TestVolumesFromSymlinkPath(t *testing.T) {
  378. buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-volumesfromsymlinkpath", "-")
  379. buildCmd.Stdin = strings.NewReader(`FROM busybox
  380. RUN mkdir /baz && ln -s /baz /foo
  381. VOLUME ["/foo/bar"]`)
  382. buildCmd.Dir = workingDirectory
  383. err := buildCmd.Run()
  384. if err != nil {
  385. t.Fatalf("could not build 'docker-test-volumesfromsymlinkpath': %v", err)
  386. }
  387. cmd := exec.Command(dockerBinary, "run", "--name", "test-volumesfromsymlinkpath", "docker-test-volumesfromsymlinkpath")
  388. exitCode, err := runCommand(cmd)
  389. if err != nil || exitCode != 0 {
  390. t.Fatalf("[run] (volume) err: %v, exitcode: %d", err, exitCode)
  391. }
  392. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-volumesfromsymlinkpath", "busybox", "sh", "-c", "ls /foo | grep -q bar")
  393. exitCode, err = runCommand(cmd)
  394. if err != nil || exitCode != 0 {
  395. t.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
  396. }
  397. deleteImages("docker-test-volumesfromsymlinkpath")
  398. deleteAllContainers()
  399. logDone("run - volumes-from symlink path")
  400. }
  401. func TestExitCode(t *testing.T) {
  402. cmd := exec.Command(dockerBinary, "run", "busybox", "/bin/sh", "-c", "exit 72")
  403. exit, err := runCommand(cmd)
  404. if err == nil {
  405. t.Fatal("should not have a non nil error")
  406. }
  407. if exit != 72 {
  408. t.Fatalf("expected exit code 72 received %d", exit)
  409. }
  410. deleteAllContainers()
  411. logDone("run - correct exit code")
  412. }
  413. func TestUserDefaultsToRoot(t *testing.T) {
  414. cmd := exec.Command(dockerBinary, "run", "busybox", "id")
  415. out, _, err := runCommandWithOutput(cmd)
  416. if err != nil {
  417. t.Fatal(err, out)
  418. }
  419. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  420. t.Fatalf("expected root user got %s", out)
  421. }
  422. deleteAllContainers()
  423. logDone("run - default user")
  424. }
  425. func TestUserByName(t *testing.T) {
  426. cmd := exec.Command(dockerBinary, "run", "-u", "root", "busybox", "id")
  427. out, _, err := runCommandWithOutput(cmd)
  428. if err != nil {
  429. t.Fatal(err, out)
  430. }
  431. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  432. t.Fatalf("expected root user got %s", out)
  433. }
  434. deleteAllContainers()
  435. logDone("run - user by name")
  436. }
  437. func TestUserByID(t *testing.T) {
  438. cmd := exec.Command(dockerBinary, "run", "-u", "1", "busybox", "id")
  439. out, _, err := runCommandWithOutput(cmd)
  440. if err != nil {
  441. t.Fatal(err, out)
  442. }
  443. if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
  444. t.Fatalf("expected daemon user got %s", out)
  445. }
  446. deleteAllContainers()
  447. logDone("run - user by id")
  448. }
  449. func TestUserByIDBig(t *testing.T) {
  450. cmd := exec.Command(dockerBinary, "run", "-u", "2147483648", "busybox", "id")
  451. out, _, err := runCommandWithOutput(cmd)
  452. if err == nil {
  453. t.Fatal("No error, but must be.", out)
  454. }
  455. if !strings.Contains(out, "Uids and gids must be in range") {
  456. t.Fatalf("expected error about uids range, got %s", out)
  457. }
  458. deleteAllContainers()
  459. logDone("run - user by id, id too big")
  460. }
  461. func TestUserByIDNegative(t *testing.T) {
  462. cmd := exec.Command(dockerBinary, "run", "-u", "-1", "busybox", "id")
  463. out, _, err := runCommandWithOutput(cmd)
  464. if err == nil {
  465. t.Fatal("No error, but must be.", out)
  466. }
  467. if !strings.Contains(out, "Uids and gids must be in range") {
  468. t.Fatalf("expected error about uids range, got %s", out)
  469. }
  470. deleteAllContainers()
  471. logDone("run - user by id, id negative")
  472. }
  473. func TestUserByIDZero(t *testing.T) {
  474. cmd := exec.Command(dockerBinary, "run", "-u", "0", "busybox", "id")
  475. out, _, err := runCommandWithOutput(cmd)
  476. if err != nil {
  477. t.Fatal(err, out)
  478. }
  479. if !strings.Contains(out, "uid=0(root) gid=0(root) groups=10(wheel)") {
  480. t.Fatalf("expected daemon user got %s", out)
  481. }
  482. deleteAllContainers()
  483. logDone("run - user by id, zero uid")
  484. }
  485. func TestUserNotFound(t *testing.T) {
  486. cmd := exec.Command(dockerBinary, "run", "-u", "notme", "busybox", "id")
  487. _, err := runCommand(cmd)
  488. if err == nil {
  489. t.Fatal("unknown user should cause container to fail")
  490. }
  491. deleteAllContainers()
  492. logDone("run - user not found")
  493. }
  494. func TestRunTwoConcurrentContainers(t *testing.T) {
  495. group := sync.WaitGroup{}
  496. group.Add(2)
  497. for i := 0; i < 2; i++ {
  498. go func() {
  499. defer group.Done()
  500. cmd := exec.Command(dockerBinary, "run", "busybox", "sleep", "2")
  501. if _, err := runCommand(cmd); err != nil {
  502. t.Fatal(err)
  503. }
  504. }()
  505. }
  506. group.Wait()
  507. deleteAllContainers()
  508. logDone("run - two concurrent containers")
  509. }
  510. func TestEnvironment(t *testing.T) {
  511. cmd := exec.Command(dockerBinary, "run", "-h", "testing", "-e=FALSE=true", "-e=TRUE", "-e=TRICKY", "busybox", "env")
  512. cmd.Env = append(os.Environ(),
  513. "TRUE=false",
  514. "TRICKY=tri\ncky\n",
  515. )
  516. out, _, err := runCommandWithOutput(cmd)
  517. if err != nil {
  518. t.Fatal(err, out)
  519. }
  520. actualEnv := strings.Split(out, "\n")
  521. if actualEnv[len(actualEnv)-1] == "" {
  522. actualEnv = actualEnv[:len(actualEnv)-1]
  523. }
  524. sort.Strings(actualEnv)
  525. goodEnv := []string{
  526. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  527. "HOME=/",
  528. "HOSTNAME=testing",
  529. "FALSE=true",
  530. "TRUE=false",
  531. "TRICKY=tri",
  532. "cky",
  533. "",
  534. }
  535. sort.Strings(goodEnv)
  536. if len(goodEnv) != len(actualEnv) {
  537. t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
  538. }
  539. for i := range goodEnv {
  540. if actualEnv[i] != goodEnv[i] {
  541. t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  542. }
  543. }
  544. deleteAllContainers()
  545. logDone("run - verify environment")
  546. }
  547. func TestContainerNetwork(t *testing.T) {
  548. cmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "127.0.0.1")
  549. if _, err := runCommand(cmd); err != nil {
  550. t.Fatal(err)
  551. }
  552. deleteAllContainers()
  553. logDone("run - test container network via ping")
  554. }
  555. // Issue #4681
  556. func TestLoopbackWhenNetworkDisabled(t *testing.T) {
  557. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1")
  558. if _, err := runCommand(cmd); err != nil {
  559. t.Fatal(err)
  560. }
  561. deleteAllContainers()
  562. logDone("run - test container loopback when networking disabled")
  563. }
  564. func TestLoopbackOnlyExistsWhenNetworkingDisabled(t *testing.T) {
  565. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up")
  566. out, _, err := runCommandWithOutput(cmd)
  567. if err != nil {
  568. t.Fatal(err, out)
  569. }
  570. var (
  571. count = 0
  572. parts = strings.Split(out, "\n")
  573. )
  574. for _, l := range parts {
  575. if l != "" {
  576. count++
  577. }
  578. }
  579. if count != 1 {
  580. t.Fatalf("Wrong interface count in container %d", count)
  581. }
  582. if !strings.HasPrefix(out, "1: lo") {
  583. t.Fatalf("Wrong interface in test container: expected [1: lo], got %s", out)
  584. }
  585. deleteAllContainers()
  586. logDone("run - test loopback only exists when networking disabled")
  587. }
  588. func TestPrivilegedCanMknod(t *testing.T) {
  589. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  590. out, _, err := runCommandWithOutput(cmd)
  591. if err != nil {
  592. t.Fatal(err)
  593. }
  594. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  595. t.Fatalf("expected output ok received %s", actual)
  596. }
  597. deleteAllContainers()
  598. logDone("run - test privileged can mknod")
  599. }
  600. func TestUnPrivilegedCanMknod(t *testing.T) {
  601. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  602. out, _, err := runCommandWithOutput(cmd)
  603. if err != nil {
  604. t.Fatal(err)
  605. }
  606. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  607. t.Fatalf("expected output ok received %s", actual)
  608. }
  609. deleteAllContainers()
  610. logDone("run - test un-privileged can mknod")
  611. }
  612. func TestPrivilegedCanMount(t *testing.T) {
  613. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  614. out, _, err := runCommandWithOutput(cmd)
  615. if err != nil {
  616. t.Fatal(err)
  617. }
  618. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  619. t.Fatalf("expected output ok received %s", actual)
  620. }
  621. deleteAllContainers()
  622. logDone("run - test privileged can mount")
  623. }
  624. func TestUnPrivilegedCannotMount(t *testing.T) {
  625. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  626. out, _, err := runCommandWithOutput(cmd)
  627. if err == nil {
  628. t.Fatal(err, out)
  629. }
  630. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  631. t.Fatalf("expected output not ok received %s", actual)
  632. }
  633. deleteAllContainers()
  634. logDone("run - test un-privileged cannot mount")
  635. }
  636. func TestSysNotWritableInNonPrivilegedContainers(t *testing.T) {
  637. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/sys/kernel/profiling")
  638. if code, err := runCommand(cmd); err == nil || code == 0 {
  639. t.Fatal("sys should not be writable in a non privileged container")
  640. }
  641. deleteAllContainers()
  642. logDone("run - sys not writable in non privileged container")
  643. }
  644. func TestSysWritableInPrivilegedContainers(t *testing.T) {
  645. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/sys/kernel/profiling")
  646. if code, err := runCommand(cmd); err != nil || code != 0 {
  647. t.Fatalf("sys should be writable in privileged container")
  648. }
  649. deleteAllContainers()
  650. logDone("run - sys writable in privileged container")
  651. }
  652. func TestProcNotWritableInNonPrivilegedContainers(t *testing.T) {
  653. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/proc/sysrq-trigger")
  654. if code, err := runCommand(cmd); err == nil || code == 0 {
  655. t.Fatal("proc should not be writable in a non privileged container")
  656. }
  657. deleteAllContainers()
  658. logDone("run - proc not writable in non privileged container")
  659. }
  660. func TestProcWritableInPrivilegedContainers(t *testing.T) {
  661. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/proc/sysrq-trigger")
  662. if code, err := runCommand(cmd); err != nil || code != 0 {
  663. t.Fatalf("proc should be writable in privileged container")
  664. }
  665. deleteAllContainers()
  666. logDone("run - proc writable in privileged container")
  667. }
  668. func TestRunWithCpuset(t *testing.T) {
  669. cmd := exec.Command(dockerBinary, "run", "--cpuset", "0", "busybox", "true")
  670. if code, err := runCommand(cmd); err != nil || code != 0 {
  671. t.Fatalf("container should run successfuly with cpuset of 0: %s", err)
  672. }
  673. deleteAllContainers()
  674. logDone("run - cpuset 0")
  675. }
  676. func TestDeviceNumbers(t *testing.T) {
  677. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "ls -l /dev/null")
  678. out, _, err := runCommandWithOutput(cmd)
  679. if err != nil {
  680. t.Fatal(err, out)
  681. }
  682. deviceLineFields := strings.Fields(out)
  683. deviceLineFields[6] = ""
  684. deviceLineFields[7] = ""
  685. deviceLineFields[8] = ""
  686. expected := []string{"crw-rw-rw-", "1", "root", "root", "1,", "3", "", "", "", "/dev/null"}
  687. if !(reflect.DeepEqual(deviceLineFields, expected)) {
  688. t.Fatalf("expected output\ncrw-rw-rw- 1 root root 1, 3 May 24 13:29 /dev/null\n received\n %s\n", out)
  689. }
  690. deleteAllContainers()
  691. logDone("run - test device numbers")
  692. }
  693. func TestThatCharacterDevicesActLikeCharacterDevices(t *testing.T) {
  694. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "dd if=/dev/zero of=/zero bs=1k count=5 2> /dev/null ; du -h /zero")
  695. out, _, err := runCommandWithOutput(cmd)
  696. if err != nil {
  697. t.Fatal(err, out)
  698. }
  699. if actual := strings.Trim(out, "\r\n"); actual[0] == '0' {
  700. t.Fatalf("expected a new file called /zero to be create that is greater than 0 bytes long, but du says: %s", actual)
  701. }
  702. deleteAllContainers()
  703. logDone("run - test that character devices work.")
  704. }
  705. func TestRunUnprivilegedWithChroot(t *testing.T) {
  706. cmd := exec.Command(dockerBinary, "run", "busybox", "chroot", "/", "true")
  707. if _, err := runCommand(cmd); err != nil {
  708. t.Fatal(err)
  709. }
  710. deleteAllContainers()
  711. logDone("run - unprivileged with chroot")
  712. }
  713. func TestModeHostname(t *testing.T) {
  714. cmd := exec.Command(dockerBinary, "run", "-h=testhostname", "busybox", "cat", "/etc/hostname")
  715. out, _, err := runCommandWithOutput(cmd)
  716. if err != nil {
  717. t.Fatal(err, out)
  718. }
  719. if actual := strings.Trim(out, "\r\n"); actual != "testhostname" {
  720. t.Fatalf("expected 'testhostname', but says: '%s'", actual)
  721. }
  722. cmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hostname")
  723. out, _, err = runCommandWithOutput(cmd)
  724. if err != nil {
  725. t.Fatal(err, out)
  726. }
  727. hostname, err := os.Hostname()
  728. if err != nil {
  729. t.Fatal(err)
  730. }
  731. if actual := strings.Trim(out, "\r\n"); actual != hostname {
  732. t.Fatalf("expected '%s', but says: '%s'", hostname, actual)
  733. }
  734. deleteAllContainers()
  735. logDone("run - hostname and several network modes")
  736. }
  737. func TestRootWorkdir(t *testing.T) {
  738. s, _, err := cmd(t, "run", "--workdir", "/", "busybox", "pwd")
  739. if err != nil {
  740. t.Fatal(s, err)
  741. }
  742. if s != "/\n" {
  743. t.Fatalf("pwd returned '%s' (expected /\\n)", s)
  744. }
  745. deleteAllContainers()
  746. logDone("run - workdir /")
  747. }
  748. func TestAllowBindMountingRoot(t *testing.T) {
  749. s, _, err := cmd(t, "run", "-v", "/:/host", "busybox", "ls", "/host")
  750. if err != nil {
  751. t.Fatal(s, err)
  752. }
  753. deleteAllContainers()
  754. logDone("run - bind mount / as volume")
  755. }
  756. func TestDisallowBindMountingRootToRoot(t *testing.T) {
  757. cmd := exec.Command(dockerBinary, "run", "-v", "/:/", "busybox", "ls", "/host")
  758. out, _, err := runCommandWithOutput(cmd)
  759. if err == nil {
  760. t.Fatal(out, err)
  761. }
  762. deleteAllContainers()
  763. logDone("run - bind mount /:/ as volume should fail")
  764. }