docker_cli_run_test.go 32 KB

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