docker_cli_run_test.go 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path"
  9. "path/filepath"
  10. "reflect"
  11. "regexp"
  12. "sort"
  13. "strings"
  14. "sync"
  15. "testing"
  16. "time"
  17. "github.com/docker/docker/pkg/mount"
  18. "github.com/docker/docker/pkg/networkfs/resolvconf"
  19. )
  20. // "test123" should be printed by docker run
  21. func TestDockerRunEchoStdout(t *testing.T) {
  22. runCmd := exec.Command(dockerBinary, "run", "busybox", "echo", "test123")
  23. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  24. errorOut(err, t, out)
  25. if out != "test123\n" {
  26. t.Errorf("container should've printed 'test123'")
  27. }
  28. deleteAllContainers()
  29. logDone("run - echo test123")
  30. }
  31. // "test" should be printed
  32. func TestDockerRunEchoStdoutWithMemoryLimit(t *testing.T) {
  33. runCmd := exec.Command(dockerBinary, "run", "-m", "2786432", "busybox", "echo", "test")
  34. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  35. errorOut(err, t, out)
  36. if out != "test\n" {
  37. t.Errorf("container should've printed 'test'")
  38. }
  39. deleteAllContainers()
  40. logDone("run - echo with memory limit")
  41. }
  42. // "test" should be printed
  43. func TestDockerRunEchoStdoutWitCPULimit(t *testing.T) {
  44. runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "busybox", "echo", "test")
  45. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  46. errorOut(err, t, out)
  47. if out != "test\n" {
  48. t.Errorf("container should've printed 'test'")
  49. }
  50. deleteAllContainers()
  51. logDone("run - echo with CPU limit")
  52. }
  53. // "test" should be printed
  54. func TestDockerRunEchoStdoutWithCPUAndMemoryLimit(t *testing.T) {
  55. runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "-m", "2786432", "busybox", "echo", "test")
  56. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  57. errorOut(err, t, out)
  58. if out != "test\n" {
  59. t.Errorf("container should've printed 'test'")
  60. }
  61. deleteAllContainers()
  62. logDone("run - echo with CPU and memory limit")
  63. }
  64. // "test" should be printed
  65. func TestDockerRunEchoNamedContainer(t *testing.T) {
  66. runCmd := exec.Command(dockerBinary, "run", "--name", "testfoonamedcontainer", "busybox", "echo", "test")
  67. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  68. errorOut(err, t, out)
  69. if out != "test\n" {
  70. t.Errorf("container should've printed 'test'")
  71. }
  72. if err := deleteContainer("testfoonamedcontainer"); err != nil {
  73. t.Errorf("failed to remove the named container: %v", err)
  74. }
  75. deleteAllContainers()
  76. logDone("run - echo with named container")
  77. }
  78. // docker run should not leak file descriptors
  79. func TestDockerRunLeakyFileDescriptors(t *testing.T) {
  80. runCmd := exec.Command(dockerBinary, "run", "busybox", "ls", "-C", "/proc/self/fd")
  81. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  82. errorOut(err, t, out)
  83. // normally, we should only get 0, 1, and 2, but 3 gets created by "ls" when it does "opendir" on the "fd" directory
  84. if out != "0 1 2 3\n" {
  85. t.Errorf("container should've printed '0 1 2 3', not: %s", out)
  86. }
  87. deleteAllContainers()
  88. logDone("run - check file descriptor leakage")
  89. }
  90. // it should be possible to ping Google DNS resolver
  91. // this will fail when Internet access is unavailable
  92. func TestDockerRunPingGoogle(t *testing.T) {
  93. runCmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "8.8.8.8")
  94. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  95. errorOut(err, t, out)
  96. errorOut(err, t, "container should've been able to ping 8.8.8.8")
  97. deleteAllContainers()
  98. logDone("run - ping 8.8.8.8")
  99. }
  100. // the exit code should be 0
  101. // some versions of lxc might make this test fail
  102. func TestDockerRunExitCodeZero(t *testing.T) {
  103. runCmd := exec.Command(dockerBinary, "run", "busybox", "true")
  104. exitCode, err := runCommand(runCmd)
  105. errorOut(err, t, fmt.Sprintf("%s", err))
  106. if exitCode != 0 {
  107. t.Errorf("container should've exited with exit code 0")
  108. }
  109. deleteAllContainers()
  110. logDone("run - exit with 0")
  111. }
  112. // the exit code should be 1
  113. // some versions of lxc might make this test fail
  114. func TestDockerRunExitCodeOne(t *testing.T) {
  115. runCmd := exec.Command(dockerBinary, "run", "busybox", "false")
  116. exitCode, err := runCommand(runCmd)
  117. if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
  118. t.Fatal(err)
  119. }
  120. if exitCode != 1 {
  121. t.Errorf("container should've exited with exit code 1")
  122. }
  123. deleteAllContainers()
  124. logDone("run - exit with 1")
  125. }
  126. // it should be possible to pipe in data via stdin to a process running in a container
  127. // some versions of lxc might make this test fail
  128. func TestRunStdinPipe(t *testing.T) {
  129. runCmd := exec.Command("bash", "-c", `echo "blahblah" | docker run -i -a stdin busybox cat`)
  130. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  131. errorOut(err, t, out)
  132. out = stripTrailingCharacters(out)
  133. inspectCmd := exec.Command(dockerBinary, "inspect", out)
  134. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  135. errorOut(err, t, fmt.Sprintf("out should've been a container id: %s %s", out, inspectOut))
  136. waitCmd := exec.Command(dockerBinary, "wait", out)
  137. _, _, err = runCommandWithOutput(waitCmd)
  138. errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))
  139. logsCmd := exec.Command(dockerBinary, "logs", out)
  140. containerLogs, _, err := runCommandWithOutput(logsCmd)
  141. errorOut(err, t, fmt.Sprintf("error thrown while trying to get container logs: %s", err))
  142. containerLogs = stripTrailingCharacters(containerLogs)
  143. if containerLogs != "blahblah" {
  144. t.Errorf("logs didn't print the container's logs %s", containerLogs)
  145. }
  146. rmCmd := exec.Command(dockerBinary, "rm", out)
  147. _, _, err = runCommandWithOutput(rmCmd)
  148. errorOut(err, t, fmt.Sprintf("rm failed to remove container %s", err))
  149. deleteAllContainers()
  150. logDone("run - pipe in with -i -a stdin")
  151. }
  152. // the container's ID should be printed when starting a container in detached mode
  153. func TestDockerRunDetachedContainerIDPrinting(t *testing.T) {
  154. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  155. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  156. errorOut(err, t, out)
  157. out = stripTrailingCharacters(out)
  158. inspectCmd := exec.Command(dockerBinary, "inspect", out)
  159. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  160. errorOut(err, t, fmt.Sprintf("out should've been a container id: %s %s", out, inspectOut))
  161. waitCmd := exec.Command(dockerBinary, "wait", out)
  162. _, _, err = runCommandWithOutput(waitCmd)
  163. errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))
  164. rmCmd := exec.Command(dockerBinary, "rm", out)
  165. rmOut, _, err := runCommandWithOutput(rmCmd)
  166. errorOut(err, t, "rm failed to remove container")
  167. rmOut = stripTrailingCharacters(rmOut)
  168. if rmOut != out {
  169. t.Errorf("rm didn't print the container ID %s %s", out, rmOut)
  170. }
  171. deleteAllContainers()
  172. logDone("run - print container ID in detached mode")
  173. }
  174. // the working directory should be set correctly
  175. func TestDockerRunWorkingDirectory(t *testing.T) {
  176. runCmd := exec.Command(dockerBinary, "run", "-w", "/root", "busybox", "pwd")
  177. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  178. errorOut(err, t, out)
  179. out = stripTrailingCharacters(out)
  180. if out != "/root" {
  181. t.Errorf("-w failed to set working directory")
  182. }
  183. runCmd = exec.Command(dockerBinary, "run", "--workdir", "/root", "busybox", "pwd")
  184. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  185. errorOut(err, t, out)
  186. out = stripTrailingCharacters(out)
  187. if out != "/root" {
  188. t.Errorf("--workdir failed to set working directory")
  189. }
  190. deleteAllContainers()
  191. logDone("run - run with working directory set by -w")
  192. logDone("run - run with working directory set by --workdir")
  193. }
  194. // pinging Google's DNS resolver should fail when we disable the networking
  195. func TestDockerRunWithoutNetworking(t *testing.T) {
  196. runCmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "8.8.8.8")
  197. out, _, exitCode, err := runCommandWithStdoutStderr(runCmd)
  198. if err != nil && exitCode != 1 {
  199. t.Fatal(out, err)
  200. }
  201. if exitCode != 1 {
  202. t.Errorf("--net=none should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
  203. }
  204. runCmd = exec.Command(dockerBinary, "run", "-n=false", "busybox", "ping", "-c", "1", "8.8.8.8")
  205. out, _, exitCode, err = runCommandWithStdoutStderr(runCmd)
  206. if err != nil && exitCode != 1 {
  207. t.Fatal(out, err)
  208. }
  209. if exitCode != 1 {
  210. t.Errorf("-n=false should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
  211. }
  212. deleteAllContainers()
  213. logDone("run - disable networking with --net=none")
  214. logDone("run - disable networking with -n=false")
  215. }
  216. // Regression test for #4741
  217. func TestDockerRunWithVolumesAsFiles(t *testing.T) {
  218. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true")
  219. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  220. if err != nil && exitCode != 0 {
  221. t.Fatal("1", out, stderr, err)
  222. }
  223. runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/target-file")
  224. out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
  225. if err != nil && exitCode != 0 {
  226. t.Fatal("2", out, stderr, err)
  227. }
  228. deleteAllContainers()
  229. logDone("run - regression test for #4741 - volumes from as files")
  230. }
  231. // Regression test for #4979
  232. func TestDockerRunWithVolumesFromExited(t *testing.T) {
  233. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/some/dir", "busybox", "touch", "/some/dir/file")
  234. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  235. if err != nil && exitCode != 0 {
  236. t.Fatal("1", out, stderr, err)
  237. }
  238. runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/some/dir/file")
  239. out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
  240. if err != nil && exitCode != 0 {
  241. t.Fatal("2", out, stderr, err)
  242. }
  243. deleteAllContainers()
  244. logDone("run - regression test for #4979 - volumes-from on exited container")
  245. }
  246. // Regression test for #4830
  247. func TestDockerRunWithRelativePath(t *testing.T) {
  248. runCmd := exec.Command(dockerBinary, "run", "-v", "tmp:/other-tmp", "busybox", "true")
  249. if _, _, _, err := runCommandWithStdoutStderr(runCmd); err == nil {
  250. t.Fatalf("relative path should result in an error")
  251. }
  252. deleteAllContainers()
  253. logDone("run - volume with relative path")
  254. }
  255. func TestVolumesMountedAsReadonly(t *testing.T) {
  256. cmd := exec.Command(dockerBinary, "run", "-v", "/test:/test:ro", "busybox", "touch", "/test/somefile")
  257. if code, err := runCommand(cmd); err == nil || code == 0 {
  258. t.Fatalf("run should fail because volume is ro: exit code %d", code)
  259. }
  260. deleteAllContainers()
  261. logDone("run - volumes as readonly mount")
  262. }
  263. func TestVolumesFromInReadonlyMode(t *testing.T) {
  264. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true")
  265. if _, err := runCommand(cmd); err != nil {
  266. t.Fatal(err)
  267. }
  268. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:ro", "busybox", "touch", "/test/file")
  269. if code, err := runCommand(cmd); err == nil || code == 0 {
  270. t.Fatalf("run should fail because volume is ro: exit code %d", code)
  271. }
  272. deleteAllContainers()
  273. logDone("run - volumes from as readonly mount")
  274. }
  275. // Regression test for #1201
  276. func TestVolumesFromInReadWriteMode(t *testing.T) {
  277. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true")
  278. if _, err := runCommand(cmd); err != nil {
  279. t.Fatal(err)
  280. }
  281. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "busybox", "touch", "/test/file")
  282. if _, err := runCommand(cmd); err != nil {
  283. t.Fatal(err)
  284. }
  285. deleteAllContainers()
  286. logDone("run - volumes from as read write mount")
  287. }
  288. // Test for #1351
  289. func TestApplyVolumesFromBeforeVolumes(t *testing.T) {
  290. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "touch", "/test/foo")
  291. if _, err := runCommand(cmd); err != nil {
  292. t.Fatal(err)
  293. }
  294. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "-v", "/test", "busybox", "cat", "/test/foo")
  295. if _, err := runCommand(cmd); err != nil {
  296. t.Fatal(err)
  297. }
  298. deleteAllContainers()
  299. logDone("run - volumes from mounted first")
  300. }
  301. func TestMultipleVolumesFrom(t *testing.T) {
  302. cmd := exec.Command(dockerBinary, "run", "--name", "parent1", "-v", "/test", "busybox", "touch", "/test/foo")
  303. if _, err := runCommand(cmd); err != nil {
  304. t.Fatal(err)
  305. }
  306. cmd = exec.Command(dockerBinary, "run", "--name", "parent2", "-v", "/other", "busybox", "touch", "/other/bar")
  307. if _, err := runCommand(cmd); err != nil {
  308. t.Fatal(err)
  309. }
  310. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent1", "--volumes-from", "parent2",
  311. "busybox", "sh", "-c", "cat /test/foo && cat /other/bar")
  312. if _, err := runCommand(cmd); err != nil {
  313. t.Fatal(err)
  314. }
  315. deleteAllContainers()
  316. logDone("run - multiple volumes from")
  317. }
  318. // this tests verifies the ID format for the container
  319. func TestVerifyContainerID(t *testing.T) {
  320. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  321. out, exit, err := runCommandWithOutput(cmd)
  322. if err != nil {
  323. t.Fatal(err)
  324. }
  325. if exit != 0 {
  326. t.Fatalf("expected exit code 0 received %d", exit)
  327. }
  328. match, err := regexp.MatchString("^[0-9a-f]{64}$", strings.TrimSuffix(out, "\n"))
  329. if err != nil {
  330. t.Fatal(err)
  331. }
  332. if !match {
  333. t.Fatalf("Invalid container ID: %s", out)
  334. }
  335. deleteAllContainers()
  336. logDone("run - verify container ID")
  337. }
  338. // Test that creating a container with a volume doesn't crash. Regression test for #995.
  339. func TestCreateVolume(t *testing.T) {
  340. cmd := exec.Command(dockerBinary, "run", "-v", "/var/lib/data", "busybox", "true")
  341. if _, err := runCommand(cmd); err != nil {
  342. t.Fatal(err)
  343. }
  344. deleteAllContainers()
  345. logDone("run - create docker managed volume")
  346. }
  347. // Test that creating a volume with a symlink in its path works correctly. Test for #5152.
  348. // Note that this bug happens only with symlinks with a target that starts with '/'.
  349. func TestCreateVolumeWithSymlink(t *testing.T) {
  350. buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-createvolumewithsymlink", "-")
  351. buildCmd.Stdin = strings.NewReader(`FROM busybox
  352. RUN mkdir /foo && ln -s /foo /bar`)
  353. buildCmd.Dir = workingDirectory
  354. err := buildCmd.Run()
  355. if err != nil {
  356. t.Fatalf("could not build 'docker-test-createvolumewithsymlink': %v", err)
  357. }
  358. cmd := exec.Command(dockerBinary, "run", "-v", "/bar/foo", "--name", "test-createvolumewithsymlink", "docker-test-createvolumewithsymlink", "sh", "-c", "mount | grep -q /foo/foo")
  359. exitCode, err := runCommand(cmd)
  360. if err != nil || exitCode != 0 {
  361. t.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
  362. }
  363. var volPath string
  364. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{range .Volumes}}{{.}}{{end}}", "test-createvolumewithsymlink")
  365. volPath, exitCode, err = runCommandWithOutput(cmd)
  366. if err != nil || exitCode != 0 {
  367. t.Fatalf("[inspect] err: %v, exitcode: %d", err, exitCode)
  368. }
  369. cmd = exec.Command(dockerBinary, "rm", "-v", "test-createvolumewithsymlink")
  370. exitCode, err = runCommand(cmd)
  371. if err != nil || exitCode != 0 {
  372. t.Fatalf("[rm] err: %v, exitcode: %d", err, exitCode)
  373. }
  374. f, err := os.Open(volPath)
  375. defer f.Close()
  376. if !os.IsNotExist(err) {
  377. t.Fatalf("[open] (expecting 'file does not exist' error) err: %v, volPath: %s", err, volPath)
  378. }
  379. deleteImages("docker-test-createvolumewithsymlink")
  380. deleteAllContainers()
  381. logDone("run - create volume with symlink")
  382. }
  383. // Tests that a volume path that has a symlink exists in a container mounting it with `--volumes-from`.
  384. func TestVolumesFromSymlinkPath(t *testing.T) {
  385. buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-volumesfromsymlinkpath", "-")
  386. buildCmd.Stdin = strings.NewReader(`FROM busybox
  387. RUN mkdir /baz && ln -s /baz /foo
  388. VOLUME ["/foo/bar"]`)
  389. buildCmd.Dir = workingDirectory
  390. err := buildCmd.Run()
  391. if err != nil {
  392. t.Fatalf("could not build 'docker-test-volumesfromsymlinkpath': %v", err)
  393. }
  394. cmd := exec.Command(dockerBinary, "run", "--name", "test-volumesfromsymlinkpath", "docker-test-volumesfromsymlinkpath")
  395. exitCode, err := runCommand(cmd)
  396. if err != nil || exitCode != 0 {
  397. t.Fatalf("[run] (volume) err: %v, exitcode: %d", err, exitCode)
  398. }
  399. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-volumesfromsymlinkpath", "busybox", "sh", "-c", "ls /foo | grep -q bar")
  400. exitCode, err = runCommand(cmd)
  401. if err != nil || exitCode != 0 {
  402. t.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
  403. }
  404. deleteImages("docker-test-volumesfromsymlinkpath")
  405. deleteAllContainers()
  406. logDone("run - volumes-from symlink path")
  407. }
  408. func TestExitCode(t *testing.T) {
  409. cmd := exec.Command(dockerBinary, "run", "busybox", "/bin/sh", "-c", "exit 72")
  410. exit, err := runCommand(cmd)
  411. if err == nil {
  412. t.Fatal("should not have a non nil error")
  413. }
  414. if exit != 72 {
  415. t.Fatalf("expected exit code 72 received %d", exit)
  416. }
  417. deleteAllContainers()
  418. logDone("run - correct exit code")
  419. }
  420. func TestUserDefaultsToRoot(t *testing.T) {
  421. cmd := exec.Command(dockerBinary, "run", "busybox", "id")
  422. out, _, err := runCommandWithOutput(cmd)
  423. if err != nil {
  424. t.Fatal(err, out)
  425. }
  426. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  427. t.Fatalf("expected root user got %s", out)
  428. }
  429. deleteAllContainers()
  430. logDone("run - default user")
  431. }
  432. func TestUserByName(t *testing.T) {
  433. cmd := exec.Command(dockerBinary, "run", "-u", "root", "busybox", "id")
  434. out, _, err := runCommandWithOutput(cmd)
  435. if err != nil {
  436. t.Fatal(err, out)
  437. }
  438. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  439. t.Fatalf("expected root user got %s", out)
  440. }
  441. deleteAllContainers()
  442. logDone("run - user by name")
  443. }
  444. func TestUserByID(t *testing.T) {
  445. cmd := exec.Command(dockerBinary, "run", "-u", "1", "busybox", "id")
  446. out, _, err := runCommandWithOutput(cmd)
  447. if err != nil {
  448. t.Fatal(err, out)
  449. }
  450. if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
  451. t.Fatalf("expected daemon user got %s", out)
  452. }
  453. deleteAllContainers()
  454. logDone("run - user by id")
  455. }
  456. func TestUserByIDBig(t *testing.T) {
  457. cmd := exec.Command(dockerBinary, "run", "-u", "2147483648", "busybox", "id")
  458. out, _, err := runCommandWithOutput(cmd)
  459. if err == nil {
  460. t.Fatal("No error, but must be.", out)
  461. }
  462. if !strings.Contains(out, "Uids and gids must be in range") {
  463. t.Fatalf("expected error about uids range, got %s", out)
  464. }
  465. deleteAllContainers()
  466. logDone("run - user by id, id too big")
  467. }
  468. func TestUserByIDNegative(t *testing.T) {
  469. cmd := exec.Command(dockerBinary, "run", "-u", "-1", "busybox", "id")
  470. out, _, err := runCommandWithOutput(cmd)
  471. if err == nil {
  472. t.Fatal("No error, but must be.", out)
  473. }
  474. if !strings.Contains(out, "Uids and gids must be in range") {
  475. t.Fatalf("expected error about uids range, got %s", out)
  476. }
  477. deleteAllContainers()
  478. logDone("run - user by id, id negative")
  479. }
  480. func TestUserByIDZero(t *testing.T) {
  481. cmd := exec.Command(dockerBinary, "run", "-u", "0", "busybox", "id")
  482. out, _, err := runCommandWithOutput(cmd)
  483. if err != nil {
  484. t.Fatal(err, out)
  485. }
  486. if !strings.Contains(out, "uid=0(root) gid=0(root) groups=10(wheel)") {
  487. t.Fatalf("expected daemon user got %s", out)
  488. }
  489. deleteAllContainers()
  490. logDone("run - user by id, zero uid")
  491. }
  492. func TestUserNotFound(t *testing.T) {
  493. cmd := exec.Command(dockerBinary, "run", "-u", "notme", "busybox", "id")
  494. _, err := runCommand(cmd)
  495. if err == nil {
  496. t.Fatal("unknown user should cause container to fail")
  497. }
  498. deleteAllContainers()
  499. logDone("run - user not found")
  500. }
  501. func TestRunTwoConcurrentContainers(t *testing.T) {
  502. group := sync.WaitGroup{}
  503. group.Add(2)
  504. for i := 0; i < 2; i++ {
  505. go func() {
  506. defer group.Done()
  507. cmd := exec.Command(dockerBinary, "run", "busybox", "sleep", "2")
  508. if _, err := runCommand(cmd); err != nil {
  509. t.Fatal(err)
  510. }
  511. }()
  512. }
  513. group.Wait()
  514. deleteAllContainers()
  515. logDone("run - two concurrent containers")
  516. }
  517. func TestEnvironment(t *testing.T) {
  518. cmd := exec.Command(dockerBinary, "run", "-h", "testing", "-e=FALSE=true", "-e=TRUE", "-e=TRICKY", "-e=HOME=", "busybox", "env")
  519. cmd.Env = append(os.Environ(),
  520. "TRUE=false",
  521. "TRICKY=tri\ncky\n",
  522. )
  523. out, _, err := runCommandWithOutput(cmd)
  524. if err != nil {
  525. t.Fatal(err, out)
  526. }
  527. actualEnv := strings.Split(out, "\n")
  528. if actualEnv[len(actualEnv)-1] == "" {
  529. actualEnv = actualEnv[:len(actualEnv)-1]
  530. }
  531. sort.Strings(actualEnv)
  532. goodEnv := []string{
  533. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  534. "HOSTNAME=testing",
  535. "FALSE=true",
  536. "TRUE=false",
  537. "TRICKY=tri",
  538. "cky",
  539. "",
  540. "HOME=/root",
  541. }
  542. sort.Strings(goodEnv)
  543. if len(goodEnv) != len(actualEnv) {
  544. t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
  545. }
  546. for i := range goodEnv {
  547. if actualEnv[i] != goodEnv[i] {
  548. t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  549. }
  550. }
  551. deleteAllContainers()
  552. logDone("run - verify environment")
  553. }
  554. func TestContainerNetwork(t *testing.T) {
  555. cmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "127.0.0.1")
  556. if _, err := runCommand(cmd); err != nil {
  557. t.Fatal(err)
  558. }
  559. deleteAllContainers()
  560. logDone("run - test container network via ping")
  561. }
  562. // Issue #4681
  563. func TestLoopbackWhenNetworkDisabled(t *testing.T) {
  564. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1")
  565. if _, err := runCommand(cmd); err != nil {
  566. t.Fatal(err)
  567. }
  568. deleteAllContainers()
  569. logDone("run - test container loopback when networking disabled")
  570. }
  571. func TestNetHostNotAllowedWithLinks(t *testing.T) {
  572. _, _, err := cmd(t, "run", "--name", "linked", "busybox", "true")
  573. cmd := exec.Command(dockerBinary, "run", "--net=host", "--link", "linked:linked", "busybox", "true")
  574. _, _, err = runCommandWithOutput(cmd)
  575. if err == nil {
  576. t.Fatal("Expected error")
  577. }
  578. deleteAllContainers()
  579. logDone("run - don't allow --net=host to be used with links")
  580. }
  581. func TestLoopbackOnlyExistsWhenNetworkingDisabled(t *testing.T) {
  582. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up")
  583. out, _, err := runCommandWithOutput(cmd)
  584. if err != nil {
  585. t.Fatal(err, out)
  586. }
  587. var (
  588. count = 0
  589. parts = strings.Split(out, "\n")
  590. )
  591. for _, l := range parts {
  592. if l != "" {
  593. count++
  594. }
  595. }
  596. if count != 1 {
  597. t.Fatalf("Wrong interface count in container %d", count)
  598. }
  599. if !strings.HasPrefix(out, "1: lo") {
  600. t.Fatalf("Wrong interface in test container: expected [1: lo], got %s", out)
  601. }
  602. deleteAllContainers()
  603. logDone("run - test loopback only exists when networking disabled")
  604. }
  605. func TestPrivilegedCanMknod(t *testing.T) {
  606. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  607. out, _, err := runCommandWithOutput(cmd)
  608. if err != nil {
  609. t.Fatal(err)
  610. }
  611. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  612. t.Fatalf("expected output ok received %s", actual)
  613. }
  614. deleteAllContainers()
  615. logDone("run - test privileged can mknod")
  616. }
  617. func TestUnPrivilegedCanMknod(t *testing.T) {
  618. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  619. out, _, err := runCommandWithOutput(cmd)
  620. if err != nil {
  621. t.Fatal(err)
  622. }
  623. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  624. t.Fatalf("expected output ok received %s", actual)
  625. }
  626. deleteAllContainers()
  627. logDone("run - test un-privileged can mknod")
  628. }
  629. func TestCapDropInvalid(t *testing.T) {
  630. cmd := exec.Command(dockerBinary, "run", "--cap-drop=CHPASS", "busybox", "ls")
  631. out, _, err := runCommandWithOutput(cmd)
  632. if err == nil {
  633. t.Fatal(err, out)
  634. }
  635. logDone("run - test --cap-drop=CHPASS invalid")
  636. }
  637. func TestCapDropCannotMknod(t *testing.T) {
  638. cmd := exec.Command(dockerBinary, "run", "--cap-drop=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  639. out, _, err := runCommandWithOutput(cmd)
  640. if err == nil {
  641. t.Fatal(err, out)
  642. }
  643. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  644. t.Fatalf("expected output not ok received %s", actual)
  645. }
  646. deleteAllContainers()
  647. logDone("run - test --cap-drop=MKNOD cannot mknod")
  648. }
  649. func TestCapDropCannotMknodLowerCase(t *testing.T) {
  650. cmd := exec.Command(dockerBinary, "run", "--cap-drop=mknod", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  651. out, _, err := runCommandWithOutput(cmd)
  652. if err == nil {
  653. t.Fatal(err, out)
  654. }
  655. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  656. t.Fatalf("expected output not ok received %s", actual)
  657. }
  658. deleteAllContainers()
  659. logDone("run - test --cap-drop=mknod cannot mknod lowercase")
  660. }
  661. func TestCapDropALLCannotMknod(t *testing.T) {
  662. cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  663. out, _, err := runCommandWithOutput(cmd)
  664. if err == nil {
  665. t.Fatal(err, out)
  666. }
  667. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  668. t.Fatalf("expected output not ok received %s", actual)
  669. }
  670. deleteAllContainers()
  671. logDone("run - test --cap-drop=ALL cannot mknod")
  672. }
  673. func TestCapDropALLAddMknodCannotMknod(t *testing.T) {
  674. cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "--cap-add=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  675. out, _, err := runCommandWithOutput(cmd)
  676. if err != nil {
  677. t.Fatal(err, out)
  678. }
  679. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  680. t.Fatalf("expected output ok received %s", actual)
  681. }
  682. deleteAllContainers()
  683. logDone("run - test --cap-drop=ALL --cap-add=MKNOD can mknod")
  684. }
  685. func TestCapAddInvalid(t *testing.T) {
  686. cmd := exec.Command(dockerBinary, "run", "--cap-add=CHPASS", "busybox", "ls")
  687. out, _, err := runCommandWithOutput(cmd)
  688. if err == nil {
  689. t.Fatal(err, out)
  690. }
  691. logDone("run - test --cap-add=CHPASS invalid")
  692. }
  693. func TestCapAddCanDownInterface(t *testing.T) {
  694. cmd := exec.Command(dockerBinary, "run", "--cap-add=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  695. out, _, err := runCommandWithOutput(cmd)
  696. if err != nil {
  697. t.Fatal(err, out)
  698. }
  699. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  700. t.Fatalf("expected output ok received %s", actual)
  701. }
  702. deleteAllContainers()
  703. logDone("run - test --cap-add=NET_ADMIN can set eth0 down")
  704. }
  705. func TestCapAddALLCanDownInterface(t *testing.T) {
  706. cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  707. out, _, err := runCommandWithOutput(cmd)
  708. if err != nil {
  709. t.Fatal(err, out)
  710. }
  711. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  712. t.Fatalf("expected output ok received %s", actual)
  713. }
  714. deleteAllContainers()
  715. logDone("run - test --cap-add=ALL can set eth0 down")
  716. }
  717. func TestCapAddALLDropNetAdminCanDownInterface(t *testing.T) {
  718. cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "--cap-drop=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  719. out, _, err := runCommandWithOutput(cmd)
  720. if err == nil {
  721. t.Fatal(err, out)
  722. }
  723. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  724. t.Fatalf("expected output not ok received %s", actual)
  725. }
  726. deleteAllContainers()
  727. logDone("run - test --cap-add=ALL --cap-drop=NET_ADMIN cannot set eth0 down")
  728. }
  729. func TestPrivilegedCanMount(t *testing.T) {
  730. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  731. out, _, err := runCommandWithOutput(cmd)
  732. if err != nil {
  733. t.Fatal(err)
  734. }
  735. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  736. t.Fatalf("expected output ok received %s", actual)
  737. }
  738. deleteAllContainers()
  739. logDone("run - test privileged can mount")
  740. }
  741. func TestUnPrivilegedCannotMount(t *testing.T) {
  742. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  743. out, _, err := runCommandWithOutput(cmd)
  744. if err == nil {
  745. t.Fatal(err, out)
  746. }
  747. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  748. t.Fatalf("expected output not ok received %s", actual)
  749. }
  750. deleteAllContainers()
  751. logDone("run - test un-privileged cannot mount")
  752. }
  753. func TestSysNotWritableInNonPrivilegedContainers(t *testing.T) {
  754. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/sys/kernel/profiling")
  755. if code, err := runCommand(cmd); err == nil || code == 0 {
  756. t.Fatal("sys should not be writable in a non privileged container")
  757. }
  758. deleteAllContainers()
  759. logDone("run - sys not writable in non privileged container")
  760. }
  761. func TestSysWritableInPrivilegedContainers(t *testing.T) {
  762. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/sys/kernel/profiling")
  763. if code, err := runCommand(cmd); err != nil || code != 0 {
  764. t.Fatalf("sys should be writable in privileged container")
  765. }
  766. deleteAllContainers()
  767. logDone("run - sys writable in privileged container")
  768. }
  769. func TestProcNotWritableInNonPrivilegedContainers(t *testing.T) {
  770. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/proc/sysrq-trigger")
  771. if code, err := runCommand(cmd); err == nil || code == 0 {
  772. t.Fatal("proc should not be writable in a non privileged container")
  773. }
  774. deleteAllContainers()
  775. logDone("run - proc not writable in non privileged container")
  776. }
  777. func TestProcWritableInPrivilegedContainers(t *testing.T) {
  778. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/proc/sysrq-trigger")
  779. if code, err := runCommand(cmd); err != nil || code != 0 {
  780. t.Fatalf("proc should be writable in privileged container")
  781. }
  782. deleteAllContainers()
  783. logDone("run - proc writable in privileged container")
  784. }
  785. func TestRunWithCpuset(t *testing.T) {
  786. cmd := exec.Command(dockerBinary, "run", "--cpuset", "0", "busybox", "true")
  787. if code, err := runCommand(cmd); err != nil || code != 0 {
  788. t.Fatalf("container should run successfuly with cpuset of 0: %s", err)
  789. }
  790. deleteAllContainers()
  791. logDone("run - cpuset 0")
  792. }
  793. func TestDeviceNumbers(t *testing.T) {
  794. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "ls -l /dev/null")
  795. out, _, err := runCommandWithOutput(cmd)
  796. if err != nil {
  797. t.Fatal(err, out)
  798. }
  799. deviceLineFields := strings.Fields(out)
  800. deviceLineFields[6] = ""
  801. deviceLineFields[7] = ""
  802. deviceLineFields[8] = ""
  803. expected := []string{"crw-rw-rw-", "1", "root", "root", "1,", "3", "", "", "", "/dev/null"}
  804. if !(reflect.DeepEqual(deviceLineFields, expected)) {
  805. t.Fatalf("expected output\ncrw-rw-rw- 1 root root 1, 3 May 24 13:29 /dev/null\n received\n %s\n", out)
  806. }
  807. deleteAllContainers()
  808. logDone("run - test device numbers")
  809. }
  810. func TestThatCharacterDevicesActLikeCharacterDevices(t *testing.T) {
  811. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "dd if=/dev/zero of=/zero bs=1k count=5 2> /dev/null ; du -h /zero")
  812. out, _, err := runCommandWithOutput(cmd)
  813. if err != nil {
  814. t.Fatal(err, out)
  815. }
  816. if actual := strings.Trim(out, "\r\n"); actual[0] == '0' {
  817. t.Fatalf("expected a new file called /zero to be create that is greater than 0 bytes long, but du says: %s", actual)
  818. }
  819. deleteAllContainers()
  820. logDone("run - test that character devices work.")
  821. }
  822. func TestRunUnprivilegedWithChroot(t *testing.T) {
  823. cmd := exec.Command(dockerBinary, "run", "busybox", "chroot", "/", "true")
  824. if _, err := runCommand(cmd); err != nil {
  825. t.Fatal(err)
  826. }
  827. deleteAllContainers()
  828. logDone("run - unprivileged with chroot")
  829. }
  830. func TestAddingOptionalDevices(t *testing.T) {
  831. cmd := exec.Command(dockerBinary, "run", "--device", "/dev/zero:/dev/nulo", "busybox", "sh", "-c", "ls /dev/nulo")
  832. out, _, err := runCommandWithOutput(cmd)
  833. if err != nil {
  834. t.Fatal(err, out)
  835. }
  836. if actual := strings.Trim(out, "\r\n"); actual != "/dev/nulo" {
  837. t.Fatalf("expected output /dev/nulo, received %s", actual)
  838. }
  839. deleteAllContainers()
  840. logDone("run - test --device argument")
  841. }
  842. func TestModeHostname(t *testing.T) {
  843. cmd := exec.Command(dockerBinary, "run", "-h=testhostname", "busybox", "cat", "/etc/hostname")
  844. out, _, err := runCommandWithOutput(cmd)
  845. if err != nil {
  846. t.Fatal(err, out)
  847. }
  848. if actual := strings.Trim(out, "\r\n"); actual != "testhostname" {
  849. t.Fatalf("expected 'testhostname', but says: '%s'", actual)
  850. }
  851. cmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hostname")
  852. out, _, err = runCommandWithOutput(cmd)
  853. if err != nil {
  854. t.Fatal(err, out)
  855. }
  856. hostname, err := os.Hostname()
  857. if err != nil {
  858. t.Fatal(err)
  859. }
  860. if actual := strings.Trim(out, "\r\n"); actual != hostname {
  861. t.Fatalf("expected '%s', but says: '%s'", hostname, actual)
  862. }
  863. deleteAllContainers()
  864. logDone("run - hostname and several network modes")
  865. }
  866. func TestRootWorkdir(t *testing.T) {
  867. s, _, err := cmd(t, "run", "--workdir", "/", "busybox", "pwd")
  868. if err != nil {
  869. t.Fatal(s, err)
  870. }
  871. if s != "/\n" {
  872. t.Fatalf("pwd returned '%s' (expected /\\n)", s)
  873. }
  874. deleteAllContainers()
  875. logDone("run - workdir /")
  876. }
  877. func TestAllowBindMountingRoot(t *testing.T) {
  878. s, _, err := cmd(t, "run", "-v", "/:/host", "busybox", "ls", "/host")
  879. if err != nil {
  880. t.Fatal(s, err)
  881. }
  882. deleteAllContainers()
  883. logDone("run - bind mount / as volume")
  884. }
  885. func TestDisallowBindMountingRootToRoot(t *testing.T) {
  886. cmd := exec.Command(dockerBinary, "run", "-v", "/:/", "busybox", "ls", "/host")
  887. out, _, err := runCommandWithOutput(cmd)
  888. if err == nil {
  889. t.Fatal(out, err)
  890. }
  891. deleteAllContainers()
  892. logDone("run - bind mount /:/ as volume should fail")
  893. }
  894. // Test recursive bind mount works by default
  895. func TestDockerRunWithVolumesIsRecursive(t *testing.T) {
  896. tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
  897. if err != nil {
  898. t.Fatal(err)
  899. }
  900. defer os.RemoveAll(tmpDir)
  901. // Create a temporary tmpfs mount.
  902. tmpfsDir := filepath.Join(tmpDir, "tmpfs")
  903. if err := os.MkdirAll(tmpfsDir, 0777); err != nil {
  904. t.Fatalf("failed to mkdir at %s - %s", tmpfsDir, err)
  905. }
  906. if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
  907. t.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
  908. }
  909. f, err := ioutil.TempFile(tmpfsDir, "touch-me")
  910. if err != nil {
  911. t.Fatal(err)
  912. }
  913. defer f.Close()
  914. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
  915. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  916. if err != nil && exitCode != 0 {
  917. t.Fatal(out, stderr, err)
  918. }
  919. if !strings.Contains(out, filepath.Base(f.Name())) {
  920. t.Fatal("Recursive bind mount test failed. Expected file not found")
  921. }
  922. deleteAllContainers()
  923. logDone("run - volumes are bind mounted recuursively")
  924. }
  925. func TestDnsDefaultOptions(t *testing.T) {
  926. cmd := exec.Command(dockerBinary, "run", "busybox", "cat", "/etc/resolv.conf")
  927. actual, _, err := runCommandWithOutput(cmd)
  928. if err != nil {
  929. t.Fatal(err, actual)
  930. }
  931. resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
  932. if os.IsNotExist(err) {
  933. t.Fatalf("/etc/resolv.conf does not exist")
  934. }
  935. if actual != string(resolvConf) {
  936. t.Fatalf("expected resolv.conf is not the same of actual")
  937. }
  938. deleteAllContainers()
  939. logDone("run - dns default options")
  940. }
  941. func TestDnsOptions(t *testing.T) {
  942. cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
  943. out, _, err := runCommandWithOutput(cmd)
  944. if err != nil {
  945. t.Fatal(err, out)
  946. }
  947. actual := strings.Replace(strings.Trim(out, "\r\n"), "\n", " ", -1)
  948. if actual != "nameserver 127.0.0.1 search mydomain" {
  949. t.Fatalf("expected 'nameserver 127.0.0.1 search mydomain', but says: '%s'", actual)
  950. }
  951. cmd = exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=.", "busybox", "cat", "/etc/resolv.conf")
  952. out, _, err = runCommandWithOutput(cmd)
  953. if err != nil {
  954. t.Fatal(err, out)
  955. }
  956. actual = strings.Replace(strings.Trim(strings.Trim(out, "\r\n"), " "), "\n", " ", -1)
  957. if actual != "nameserver 127.0.0.1" {
  958. t.Fatalf("expected 'nameserver 127.0.0.1', but says: '%s'", actual)
  959. }
  960. logDone("run - dns options")
  961. }
  962. func TestDnsOptionsBasedOnHostResolvConf(t *testing.T) {
  963. resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
  964. if os.IsNotExist(err) {
  965. t.Fatalf("/etc/resolv.conf does not exist")
  966. }
  967. hostNamservers := resolvconf.GetNameservers(resolvConf)
  968. hostSearch := resolvconf.GetSearchDomains(resolvConf)
  969. cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "busybox", "cat", "/etc/resolv.conf")
  970. out, _, err := runCommandWithOutput(cmd)
  971. if err != nil {
  972. t.Fatal(err, out)
  973. }
  974. if actualNameservers := resolvconf.GetNameservers([]byte(out)); string(actualNameservers[0]) != "127.0.0.1" {
  975. t.Fatalf("expected '127.0.0.1', but says: '%s'", string(actualNameservers[0]))
  976. }
  977. actualSearch := resolvconf.GetSearchDomains([]byte(out))
  978. if len(actualSearch) != len(hostSearch) {
  979. t.Fatalf("expected '%s' search domain(s), but it has: '%s'", len(hostSearch), len(actualSearch))
  980. }
  981. for i := range actualSearch {
  982. if actualSearch[i] != hostSearch[i] {
  983. t.Fatalf("expected '%s' domain, but says: '%s'", actualSearch[i], hostSearch[i])
  984. }
  985. }
  986. cmd = exec.Command(dockerBinary, "run", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
  987. out, _, err = runCommandWithOutput(cmd)
  988. if err != nil {
  989. t.Fatal(err, out)
  990. }
  991. actualNameservers := resolvconf.GetNameservers([]byte(out))
  992. if len(actualNameservers) != len(hostNamservers) {
  993. t.Fatalf("expected '%s' nameserver(s), but it has: '%s'", len(hostNamservers), len(actualNameservers))
  994. }
  995. for i := range actualNameservers {
  996. if actualNameservers[i] != hostNamservers[i] {
  997. t.Fatalf("expected '%s' nameserver, but says: '%s'", actualNameservers[i], hostNamservers[i])
  998. }
  999. }
  1000. if actualSearch = resolvconf.GetSearchDomains([]byte(out)); string(actualSearch[0]) != "mydomain" {
  1001. t.Fatalf("expected 'mydomain', but says: '%s'", string(actualSearch[0]))
  1002. }
  1003. deleteAllContainers()
  1004. logDone("run - dns options based on host resolv.conf")
  1005. }
  1006. // Regression test for #6983
  1007. func TestAttachStdErrOnlyTTYMode(t *testing.T) {
  1008. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stderr", "busybox", "true")
  1009. exitCode, err := runCommand(cmd)
  1010. if err != nil {
  1011. t.Fatal(err)
  1012. } else if exitCode != 0 {
  1013. t.Fatalf("Container should have exited with error code 0")
  1014. }
  1015. deleteAllContainers()
  1016. logDone("run - Attach stderr only with -t")
  1017. }
  1018. // Regression test for #6983
  1019. func TestAttachStdOutOnlyTTYMode(t *testing.T) {
  1020. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "busybox", "true")
  1021. exitCode, err := runCommand(cmd)
  1022. if err != nil {
  1023. t.Fatal(err)
  1024. } else if exitCode != 0 {
  1025. t.Fatalf("Container should have exited with error code 0")
  1026. }
  1027. deleteAllContainers()
  1028. logDone("run - Attach stdout only with -t")
  1029. }
  1030. // Regression test for #6983
  1031. func TestAttachStdOutAndErrTTYMode(t *testing.T) {
  1032. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "-a", "stderr", "busybox", "true")
  1033. exitCode, err := runCommand(cmd)
  1034. if err != nil {
  1035. t.Fatal(err)
  1036. } else if exitCode != 0 {
  1037. t.Fatalf("Container should have exited with error code 0")
  1038. }
  1039. deleteAllContainers()
  1040. logDone("run - Attach stderr and stdout with -t")
  1041. }
  1042. func TestState(t *testing.T) {
  1043. defer deleteAllContainers()
  1044. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  1045. out, _, err := runCommandWithOutput(cmd)
  1046. if err != nil {
  1047. t.Fatal(err, out)
  1048. }
  1049. id := strings.TrimSpace(out)
  1050. state, err := inspectField(id, "State.Running")
  1051. if err != nil {
  1052. t.Fatal(err)
  1053. }
  1054. if state != "true" {
  1055. t.Fatal("Container state is 'not running'")
  1056. }
  1057. pid1, err := inspectField(id, "State.Pid")
  1058. if err != nil {
  1059. t.Fatal(err)
  1060. }
  1061. if pid1 == "0" {
  1062. t.Fatal("Container state Pid 0")
  1063. }
  1064. cmd = exec.Command(dockerBinary, "stop", id)
  1065. out, _, err = runCommandWithOutput(cmd)
  1066. if err != nil {
  1067. t.Fatal(err, out)
  1068. }
  1069. state, err = inspectField(id, "State.Running")
  1070. if err != nil {
  1071. t.Fatal(err)
  1072. }
  1073. if state != "false" {
  1074. t.Fatal("Container state is 'running'")
  1075. }
  1076. pid2, err := inspectField(id, "State.Pid")
  1077. if err != nil {
  1078. t.Fatal(err)
  1079. }
  1080. if pid2 == pid1 {
  1081. t.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
  1082. }
  1083. cmd = exec.Command(dockerBinary, "start", id)
  1084. out, _, err = runCommandWithOutput(cmd)
  1085. if err != nil {
  1086. t.Fatal(err, out)
  1087. }
  1088. state, err = inspectField(id, "State.Running")
  1089. if err != nil {
  1090. t.Fatal(err)
  1091. }
  1092. if state != "true" {
  1093. t.Fatal("Container state is 'not running'")
  1094. }
  1095. pid3, err := inspectField(id, "State.Pid")
  1096. if err != nil {
  1097. t.Fatal(err)
  1098. }
  1099. if pid3 == pid1 {
  1100. t.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
  1101. }
  1102. logDone("run - test container state.")
  1103. }
  1104. // Test for #1737
  1105. func TestCopyVolumeUidGid(t *testing.T) {
  1106. name := "testrunvolumesuidgid"
  1107. defer deleteImages(name)
  1108. defer deleteAllContainers()
  1109. _, err := buildImage(name,
  1110. `FROM busybox
  1111. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  1112. RUN echo 'dockerio:x:1001:' >> /etc/group
  1113. RUN mkdir -p /hello && touch /hello/test && chown dockerio.dockerio /hello`,
  1114. true)
  1115. if err != nil {
  1116. t.Fatal(err)
  1117. }
  1118. // Test that the uid and gid is copied from the image to the volume
  1119. cmd := exec.Command(dockerBinary, "run", "--rm", "-v", "/hello", name, "sh", "-c", "ls -l / | grep hello | awk '{print $3\":\"$4}'")
  1120. out, _, err := runCommandWithOutput(cmd)
  1121. if err != nil {
  1122. t.Fatal(err, out)
  1123. }
  1124. out = strings.TrimSpace(out)
  1125. if out != "dockerio:dockerio" {
  1126. t.Fatalf("Wrong /hello ownership: %s, expected dockerio:dockerio", out)
  1127. }
  1128. logDone("run - copy uid/gid for volume")
  1129. }
  1130. // Test for #1582
  1131. func TestCopyVolumeContent(t *testing.T) {
  1132. name := "testruncopyvolumecontent"
  1133. defer deleteImages(name)
  1134. defer deleteAllContainers()
  1135. _, err := buildImage(name,
  1136. `FROM busybox
  1137. RUN mkdir -p /hello/local && echo hello > /hello/local/world`,
  1138. true)
  1139. if err != nil {
  1140. t.Fatal(err)
  1141. }
  1142. // Test that the content is copied from the image to the volume
  1143. cmd := exec.Command(dockerBinary, "run", "--rm", "-v", "/hello", name, "sh", "-c", "find", "/hello")
  1144. out, _, err := runCommandWithOutput(cmd)
  1145. if err != nil {
  1146. t.Fatal(err, out)
  1147. }
  1148. if !(strings.Contains(out, "/hello/local/world") && strings.Contains(out, "/hello/local")) {
  1149. t.Fatal("Container failed to transfer content to volume")
  1150. }
  1151. logDone("run - copy volume content")
  1152. }
  1153. func TestRunCleanupCmdOnEntrypoint(t *testing.T) {
  1154. name := "testrunmdcleanuponentrypoint"
  1155. defer deleteImages(name)
  1156. defer deleteAllContainers()
  1157. if _, err := buildImage(name,
  1158. `FROM busybox
  1159. ENTRYPOINT ["echo"]
  1160. CMD ["testingpoint"]`,
  1161. true); err != nil {
  1162. t.Fatal(err)
  1163. }
  1164. runCmd := exec.Command(dockerBinary, "run", "--entrypoint", "whoami", name)
  1165. out, exit, err := runCommandWithOutput(runCmd)
  1166. if err != nil {
  1167. t.Fatalf("Error: %v, out: %q", err, out)
  1168. }
  1169. if exit != 0 {
  1170. t.Fatalf("expected exit code 0 received %d, out: %q", exit, out)
  1171. }
  1172. out = strings.TrimSpace(out)
  1173. if out != "root" {
  1174. t.Fatalf("Expected output root, got %q", out)
  1175. }
  1176. logDone("run - cleanup cmd on --entrypoint")
  1177. }
  1178. // TestRunWorkdirExistsAndIsFile checks that if 'docker run -w' with existing file can be detected
  1179. func TestRunWorkdirExistsAndIsFile(t *testing.T) {
  1180. defer deleteAllContainers()
  1181. runCmd := exec.Command(dockerBinary, "run", "-w", "/bin/cat", "busybox")
  1182. out, exit, err := runCommandWithOutput(runCmd)
  1183. if !(err != nil && exit == 1 && strings.Contains(out, "Cannot mkdir: /bin/cat is not a directory")) {
  1184. t.Fatalf("Docker must complains about making dir, but we got out: %s, exit: %d, err: %s", out, exit, err)
  1185. }
  1186. logDone("run - error on existing file for workdir")
  1187. }
  1188. func TestRunExitOnStdinClose(t *testing.T) {
  1189. name := "testrunexitonstdinclose"
  1190. defer deleteAllContainers()
  1191. runCmd := exec.Command(dockerBinary, "run", "--name", name, "-i", "busybox", "/bin/cat")
  1192. stdin, err := runCmd.StdinPipe()
  1193. if err != nil {
  1194. t.Fatal(err)
  1195. }
  1196. stdout, err := runCmd.StdoutPipe()
  1197. if err != nil {
  1198. t.Fatal(err)
  1199. }
  1200. if err := runCmd.Start(); err != nil {
  1201. t.Fatal(err)
  1202. }
  1203. if _, err := stdin.Write([]byte("hello\n")); err != nil {
  1204. t.Fatal(err)
  1205. }
  1206. r := bufio.NewReader(stdout)
  1207. line, err := r.ReadString('\n')
  1208. if err != nil {
  1209. t.Fatal(err)
  1210. }
  1211. line = strings.TrimSpace(line)
  1212. if line != "hello" {
  1213. t.Fatalf("Output should be 'hello', got '%q'", line)
  1214. }
  1215. if err := stdin.Close(); err != nil {
  1216. t.Fatal(err)
  1217. }
  1218. finish := make(chan struct{})
  1219. go func() {
  1220. if err := runCmd.Wait(); err != nil {
  1221. t.Fatal(err)
  1222. }
  1223. close(finish)
  1224. }()
  1225. select {
  1226. case <-finish:
  1227. case <-time.After(1 * time.Second):
  1228. t.Fatal("docker run failed to exit on stdin close")
  1229. }
  1230. state, err := inspectField(name, "State.Running")
  1231. if err != nil {
  1232. t.Fatal(err)
  1233. }
  1234. if state != "false" {
  1235. t.Fatal("Container must be stopped after stdin closing")
  1236. }
  1237. logDone("run - exit on stdin closing")
  1238. }
  1239. // Test for #2267
  1240. func TestWriteHostsFileAndNotCommit(t *testing.T) {
  1241. name := "writehosts"
  1242. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/hosts && cat /etc/hosts")
  1243. out, _, err := runCommandWithOutput(cmd)
  1244. if err != nil {
  1245. t.Fatal(err, out)
  1246. }
  1247. if !strings.Contains(out, "test2267") {
  1248. t.Fatal("/etc/hosts should contain 'test2267'")
  1249. }
  1250. cmd = exec.Command(dockerBinary, "diff", name)
  1251. if err != nil {
  1252. t.Fatal(err, out)
  1253. }
  1254. out, _, err = runCommandWithOutput(cmd)
  1255. if err != nil {
  1256. t.Fatal(err, out)
  1257. }
  1258. if len(strings.Trim(out, "\r\n")) != 0 {
  1259. t.Fatal("diff should be empty")
  1260. }
  1261. logDone("run - write to /etc/hosts and not commited")
  1262. }
  1263. // Test for #2267
  1264. func TestWriteHostnameFileAndNotCommit(t *testing.T) {
  1265. name := "writehostname"
  1266. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/hostname && cat /etc/hostname")
  1267. out, _, err := runCommandWithOutput(cmd)
  1268. if err != nil {
  1269. t.Fatal(err, out)
  1270. }
  1271. if !strings.Contains(out, "test2267") {
  1272. t.Fatal("/etc/hostname should contain 'test2267'")
  1273. }
  1274. cmd = exec.Command(dockerBinary, "diff", name)
  1275. if err != nil {
  1276. t.Fatal(err, out)
  1277. }
  1278. out, _, err = runCommandWithOutput(cmd)
  1279. if err != nil {
  1280. t.Fatal(err, out)
  1281. }
  1282. if len(strings.Trim(out, "\r\n")) != 0 {
  1283. t.Fatal("diff should be empty")
  1284. }
  1285. logDone("run - write to /etc/hostname and not commited")
  1286. }
  1287. // Test for #2267
  1288. func TestWriteResolvFileAndNotCommit(t *testing.T) {
  1289. name := "writeresolv"
  1290. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/resolv.conf && cat /etc/resolv.conf")
  1291. out, _, err := runCommandWithOutput(cmd)
  1292. if err != nil {
  1293. t.Fatal(err, out)
  1294. }
  1295. if !strings.Contains(out, "test2267") {
  1296. t.Fatal("/etc/resolv.conf should contain 'test2267'")
  1297. }
  1298. cmd = exec.Command(dockerBinary, "diff", name)
  1299. if err != nil {
  1300. t.Fatal(err, out)
  1301. }
  1302. out, _, err = runCommandWithOutput(cmd)
  1303. if err != nil {
  1304. t.Fatal(err, out)
  1305. }
  1306. if len(strings.Trim(out, "\r\n")) != 0 {
  1307. t.Fatal("diff should be empty")
  1308. }
  1309. logDone("run - write to /etc/resolv.conf and not commited")
  1310. }
  1311. func TestRunWithBadDevice(t *testing.T) {
  1312. name := "baddevice"
  1313. cmd := exec.Command(dockerBinary, "run", "--name", name, "--device", "/etc", "busybox", "true")
  1314. out, _, err := runCommandWithOutput(cmd)
  1315. if err == nil {
  1316. t.Fatal("Run should fail with bad device")
  1317. }
  1318. expected := `"/etc": not a device node`
  1319. if !strings.Contains(out, expected) {
  1320. t.Fatalf("Output should contain %q, actual out: %q", expected, out)
  1321. }
  1322. logDone("run - error with bad device")
  1323. }
  1324. func TestEntrypoint(t *testing.T) {
  1325. name := "entrypoint"
  1326. cmd := exec.Command(dockerBinary, "run", "--name", name, "--entrypoint", "/bin/echo", "busybox", "-n", "foobar")
  1327. out, _, err := runCommandWithOutput(cmd)
  1328. if err != nil {
  1329. t.Fatal(err, out)
  1330. }
  1331. expected := "foobar"
  1332. if out != expected {
  1333. t.Fatalf("Output should be %q, actual out: %q", expected, out)
  1334. }
  1335. logDone("run - entrypoint")
  1336. }
  1337. func TestBindMounts(t *testing.T) {
  1338. tmpDir, err := ioutil.TempDir("", "docker-test-container")
  1339. if err != nil {
  1340. t.Fatal(err)
  1341. }
  1342. defer os.RemoveAll(tmpDir)
  1343. writeFile(path.Join(tmpDir, "touch-me"), "", t)
  1344. // Test reading from a read-only bind mount
  1345. cmd := exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox", "ls", "/tmp")
  1346. out, _, err := runCommandWithOutput(cmd)
  1347. if err != nil {
  1348. t.Fatal(err, out)
  1349. }
  1350. if !strings.Contains(out, "touch-me") {
  1351. t.Fatal("Container failed to read from bind mount")
  1352. }
  1353. // test writing to bind mount
  1354. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "busybox", "touch", "/tmp/holla")
  1355. out, _, err = runCommandWithOutput(cmd)
  1356. if err != nil {
  1357. t.Fatal(err, out)
  1358. }
  1359. readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1360. // test mounting to an illegal destination directory
  1361. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:.", tmpDir), "busybox", "ls", ".")
  1362. _, err = runCommand(cmd)
  1363. if err == nil {
  1364. t.Fatal("Container bind mounted illegal directory")
  1365. }
  1366. // test mount a file
  1367. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "busybox", "sh", "-c", "echo -n 'yotta' > /tmp/holla")
  1368. _, err = runCommand(cmd)
  1369. if err != nil {
  1370. t.Fatal(err, out)
  1371. }
  1372. content := readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1373. expected := "yotta"
  1374. if content != expected {
  1375. t.Fatalf("Output should be %q, actual out: %q", expected, content)
  1376. }
  1377. }