docker_cli_run_test.go 27 KB

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