docker_cli_run_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. func TestExitCode(t *testing.T) {
  377. cmd := exec.Command(dockerBinary, "run", "busybox", "/bin/sh", "-c", "exit 72")
  378. exit, err := runCommand(cmd)
  379. if err == nil {
  380. t.Fatal("should not have a non nil error")
  381. }
  382. if exit != 72 {
  383. t.Fatalf("expected exit code 72 received %d", exit)
  384. }
  385. deleteAllContainers()
  386. logDone("run - correct exit code")
  387. }
  388. func TestUserDefaultsToRoot(t *testing.T) {
  389. cmd := exec.Command(dockerBinary, "run", "busybox", "id")
  390. out, _, err := runCommandWithOutput(cmd)
  391. if err != nil {
  392. t.Fatal(err, out)
  393. }
  394. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  395. t.Fatalf("expected root user got %s", out)
  396. }
  397. deleteAllContainers()
  398. logDone("run - default user")
  399. }
  400. func TestUserByName(t *testing.T) {
  401. cmd := exec.Command(dockerBinary, "run", "-u", "root", "busybox", "id")
  402. out, _, err := runCommandWithOutput(cmd)
  403. if err != nil {
  404. t.Fatal(err, out)
  405. }
  406. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  407. t.Fatalf("expected root user got %s", out)
  408. }
  409. deleteAllContainers()
  410. logDone("run - user by name")
  411. }
  412. func TestUserByID(t *testing.T) {
  413. cmd := exec.Command(dockerBinary, "run", "-u", "1", "busybox", "id")
  414. out, _, err := runCommandWithOutput(cmd)
  415. if err != nil {
  416. t.Fatal(err, out)
  417. }
  418. if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
  419. t.Fatalf("expected daemon user got %s", out)
  420. }
  421. deleteAllContainers()
  422. logDone("run - user by id")
  423. }
  424. func TestUserByIDBig(t *testing.T) {
  425. cmd := exec.Command(dockerBinary, "run", "-u", "2147483648", "busybox", "id")
  426. out, _, err := runCommandWithOutput(cmd)
  427. if err == nil {
  428. t.Fatal("No error, but must be.", out)
  429. }
  430. if !strings.Contains(out, "Uids and gids must be in range") {
  431. t.Fatalf("expected error about uids range, got %s", out)
  432. }
  433. deleteAllContainers()
  434. logDone("run - user by id, id too big")
  435. }
  436. func TestUserByIDNegative(t *testing.T) {
  437. cmd := exec.Command(dockerBinary, "run", "-u", "-1", "busybox", "id")
  438. out, _, err := runCommandWithOutput(cmd)
  439. if err == nil {
  440. t.Fatal("No error, but must be.", out)
  441. }
  442. if !strings.Contains(out, "Uids and gids must be in range") {
  443. t.Fatalf("expected error about uids range, got %s", out)
  444. }
  445. deleteAllContainers()
  446. logDone("run - user by id, id negative")
  447. }
  448. func TestUserByIDZero(t *testing.T) {
  449. cmd := exec.Command(dockerBinary, "run", "-u", "0", "busybox", "id")
  450. out, _, err := runCommandWithOutput(cmd)
  451. if err != nil {
  452. t.Fatal(err, out)
  453. }
  454. if !strings.Contains(out, "uid=0(root) gid=0(root) groups=10(wheel)") {
  455. t.Fatalf("expected daemon user got %s", out)
  456. }
  457. deleteAllContainers()
  458. logDone("run - user by id, zero uid")
  459. }
  460. func TestUserNotFound(t *testing.T) {
  461. cmd := exec.Command(dockerBinary, "run", "-u", "notme", "busybox", "id")
  462. _, err := runCommand(cmd)
  463. if err == nil {
  464. t.Fatal("unknown user should cause container to fail")
  465. }
  466. deleteAllContainers()
  467. logDone("run - user not found")
  468. }
  469. func TestRunTwoConcurrentContainers(t *testing.T) {
  470. group := sync.WaitGroup{}
  471. group.Add(2)
  472. for i := 0; i < 2; i++ {
  473. go func() {
  474. defer group.Done()
  475. cmd := exec.Command(dockerBinary, "run", "busybox", "sleep", "2")
  476. if _, err := runCommand(cmd); err != nil {
  477. t.Fatal(err)
  478. }
  479. }()
  480. }
  481. group.Wait()
  482. deleteAllContainers()
  483. logDone("run - two concurrent containers")
  484. }
  485. func TestEnvironment(t *testing.T) {
  486. cmd := exec.Command(dockerBinary, "run", "-h", "testing", "-e=FALSE=true", "-e=TRUE", "-e=TRICKY", "busybox", "env")
  487. cmd.Env = append(os.Environ(),
  488. "TRUE=false",
  489. "TRICKY=tri\ncky\n",
  490. )
  491. out, _, err := runCommandWithOutput(cmd)
  492. if err != nil {
  493. t.Fatal(err, out)
  494. }
  495. actualEnv := strings.Split(out, "\n")
  496. if actualEnv[len(actualEnv)-1] == "" {
  497. actualEnv = actualEnv[:len(actualEnv)-1]
  498. }
  499. sort.Strings(actualEnv)
  500. goodEnv := []string{
  501. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  502. "HOME=/",
  503. "HOSTNAME=testing",
  504. "FALSE=true",
  505. "TRUE=false",
  506. "TRICKY=tri",
  507. "cky",
  508. "",
  509. }
  510. sort.Strings(goodEnv)
  511. if len(goodEnv) != len(actualEnv) {
  512. t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
  513. }
  514. for i := range goodEnv {
  515. if actualEnv[i] != goodEnv[i] {
  516. t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  517. }
  518. }
  519. deleteAllContainers()
  520. logDone("run - verify environment")
  521. }
  522. func TestContainerNetwork(t *testing.T) {
  523. cmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "127.0.0.1")
  524. if _, err := runCommand(cmd); err != nil {
  525. t.Fatal(err)
  526. }
  527. deleteAllContainers()
  528. logDone("run - test container network via ping")
  529. }
  530. // Issue #4681
  531. func TestLoopbackWhenNetworkDisabled(t *testing.T) {
  532. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1")
  533. if _, err := runCommand(cmd); err != nil {
  534. t.Fatal(err)
  535. }
  536. deleteAllContainers()
  537. logDone("run - test container loopback when networking disabled")
  538. }
  539. func TestLoopbackOnlyExistsWhenNetworkingDisabled(t *testing.T) {
  540. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up")
  541. out, _, err := runCommandWithOutput(cmd)
  542. if err != nil {
  543. t.Fatal(err, out)
  544. }
  545. var (
  546. count = 0
  547. parts = strings.Split(out, "\n")
  548. )
  549. for _, l := range parts {
  550. if l != "" {
  551. count++
  552. }
  553. }
  554. if count != 1 {
  555. t.Fatalf("Wrong interface count in container %d", count)
  556. }
  557. if !strings.HasPrefix(out, "1: lo") {
  558. t.Fatalf("Wrong interface in test container: expected [1: lo], got %s", out)
  559. }
  560. deleteAllContainers()
  561. logDone("run - test loopback only exists when networking disabled")
  562. }
  563. func TestPrivilegedCanMknod(t *testing.T) {
  564. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  565. out, _, err := runCommandWithOutput(cmd)
  566. if err != nil {
  567. t.Fatal(err)
  568. }
  569. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  570. t.Fatalf("expected output ok received %s", actual)
  571. }
  572. deleteAllContainers()
  573. logDone("run - test privileged can mknod")
  574. }
  575. func TestUnPrivilegedCanMknod(t *testing.T) {
  576. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  577. out, _, err := runCommandWithOutput(cmd)
  578. if err != nil {
  579. t.Fatal(err)
  580. }
  581. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  582. t.Fatalf("expected output ok received %s", actual)
  583. }
  584. deleteAllContainers()
  585. logDone("run - test un-privileged can mknod")
  586. }
  587. func TestPrivilegedCanMount(t *testing.T) {
  588. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  589. out, _, err := runCommandWithOutput(cmd)
  590. if err != nil {
  591. t.Fatal(err)
  592. }
  593. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  594. t.Fatalf("expected output ok received %s", actual)
  595. }
  596. deleteAllContainers()
  597. logDone("run - test privileged can mount")
  598. }
  599. func TestUnPrivilegedCannotMount(t *testing.T) {
  600. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  601. out, _, err := runCommandWithOutput(cmd)
  602. if err == nil {
  603. t.Fatal(err, out)
  604. }
  605. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  606. t.Fatalf("expected output not ok received %s", actual)
  607. }
  608. deleteAllContainers()
  609. logDone("run - test un-privileged cannot mount")
  610. }
  611. func TestSysNotWritableInNonPrivilegedContainers(t *testing.T) {
  612. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/sys/kernel/profiling")
  613. if code, err := runCommand(cmd); err == nil || code == 0 {
  614. t.Fatal("sys should not be writable in a non privileged container")
  615. }
  616. deleteAllContainers()
  617. logDone("run - sys not writable in non privileged container")
  618. }
  619. func TestSysWritableInPrivilegedContainers(t *testing.T) {
  620. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/sys/kernel/profiling")
  621. if code, err := runCommand(cmd); err != nil || code != 0 {
  622. t.Fatalf("sys should be writable in privileged container")
  623. }
  624. deleteAllContainers()
  625. logDone("run - sys writable in privileged container")
  626. }
  627. func TestProcNotWritableInNonPrivilegedContainers(t *testing.T) {
  628. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/proc/sysrq-trigger")
  629. if code, err := runCommand(cmd); err == nil || code == 0 {
  630. t.Fatal("proc should not be writable in a non privileged container")
  631. }
  632. deleteAllContainers()
  633. logDone("run - proc not writable in non privileged container")
  634. }
  635. func TestProcWritableInPrivilegedContainers(t *testing.T) {
  636. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/proc/sysrq-trigger")
  637. if code, err := runCommand(cmd); err != nil || code != 0 {
  638. t.Fatalf("proc should be writable in privileged container")
  639. }
  640. deleteAllContainers()
  641. logDone("run - proc writable in privileged container")
  642. }
  643. func TestRunWithCpuset(t *testing.T) {
  644. cmd := exec.Command(dockerBinary, "run", "--cpuset", "0", "busybox", "true")
  645. if code, err := runCommand(cmd); err != nil || code != 0 {
  646. t.Fatalf("container should run successfuly with cpuset of 0: %s", err)
  647. }
  648. deleteAllContainers()
  649. logDone("run - cpuset 0")
  650. }
  651. func TestDeviceNumbers(t *testing.T) {
  652. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "ls -l /dev/null")
  653. out, _, err := runCommandWithOutput(cmd)
  654. if err != nil {
  655. t.Fatal(err, out)
  656. }
  657. deviceLineFields := strings.Fields(out)
  658. deviceLineFields[6] = ""
  659. deviceLineFields[7] = ""
  660. deviceLineFields[8] = ""
  661. expected := []string{"crw-rw-rw-", "1", "root", "root", "1,", "3", "", "", "", "/dev/null"}
  662. if !(reflect.DeepEqual(deviceLineFields, expected)) {
  663. t.Fatalf("expected output\ncrw-rw-rw- 1 root root 1, 3 May 24 13:29 /dev/null\n received\n %s\n", out)
  664. }
  665. deleteAllContainers()
  666. logDone("run - test device numbers")
  667. }
  668. func TestThatCharacterDevicesActLikeCharacterDevices(t *testing.T) {
  669. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "dd if=/dev/zero of=/zero bs=1k count=5 2> /dev/null ; du -h /zero")
  670. out, _, err := runCommandWithOutput(cmd)
  671. if err != nil {
  672. t.Fatal(err, out)
  673. }
  674. if actual := strings.Trim(out, "\r\n"); actual[0] == '0' {
  675. t.Fatalf("expected a new file called /zero to be create that is greater than 0 bytes long, but du says: %s", actual)
  676. }
  677. deleteAllContainers()
  678. logDone("run - test that character devices work.")
  679. }
  680. func TestRunUnprivilegedWithChroot(t *testing.T) {
  681. cmd := exec.Command(dockerBinary, "run", "busybox", "chroot", "/", "true")
  682. if _, err := runCommand(cmd); err != nil {
  683. t.Fatal(err)
  684. }
  685. deleteAllContainers()
  686. logDone("run - unprivileged with chroot")
  687. }
  688. func TestModeHostname(t *testing.T) {
  689. cmd := exec.Command(dockerBinary, "run", "-h=testhostname", "busybox", "cat", "/etc/hostname")
  690. out, _, err := runCommandWithOutput(cmd)
  691. if err != nil {
  692. t.Fatal(err, out)
  693. }
  694. if actual := strings.Trim(out, "\r\n"); actual != "testhostname" {
  695. t.Fatalf("expected 'testhostname', but says: '%s'", actual)
  696. }
  697. cmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hostname")
  698. out, _, err = runCommandWithOutput(cmd)
  699. if err != nil {
  700. t.Fatal(err, out)
  701. }
  702. hostname, err := os.Hostname()
  703. if err != nil {
  704. t.Fatal(err)
  705. }
  706. if actual := strings.Trim(out, "\r\n"); actual != hostname {
  707. t.Fatalf("expected '%s', but says: '%s'", hostname, actual)
  708. }
  709. deleteAllContainers()
  710. logDone("run - hostname and several network modes")
  711. }