docker_cli_run_test.go 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  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. func TestVolumesFromInheritsReadOnly(t *testing.T) {
  289. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test:/test:ro", "busybox", "true")
  290. if _, err := runCommand(cmd); err != nil {
  291. t.Fatal(err)
  292. }
  293. // Expect this "rw" mode to be be ignored since the inheritted volume is "ro"
  294. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:rw", "busybox", "touch", "/test/file")
  295. if _, err := runCommand(cmd); err == nil {
  296. t.Fatal("Expected to inherit read-only volume even when passing in `rw`")
  297. }
  298. deleteAllContainers()
  299. logDone("run - volumes from ignores `rw` if inherrited volume is `ro`")
  300. }
  301. // Test for #1351
  302. func TestApplyVolumesFromBeforeVolumes(t *testing.T) {
  303. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "touch", "/test/foo")
  304. if _, err := runCommand(cmd); err != nil {
  305. t.Fatal(err)
  306. }
  307. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "-v", "/test", "busybox", "cat", "/test/foo")
  308. if _, err := runCommand(cmd); err != nil {
  309. t.Fatal(err)
  310. }
  311. deleteAllContainers()
  312. logDone("run - volumes from mounted first")
  313. }
  314. func TestMultipleVolumesFrom(t *testing.T) {
  315. cmd := exec.Command(dockerBinary, "run", "--name", "parent1", "-v", "/test", "busybox", "touch", "/test/foo")
  316. if _, err := runCommand(cmd); err != nil {
  317. t.Fatal(err)
  318. }
  319. cmd = exec.Command(dockerBinary, "run", "--name", "parent2", "-v", "/other", "busybox", "touch", "/other/bar")
  320. if _, err := runCommand(cmd); err != nil {
  321. t.Fatal(err)
  322. }
  323. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent1", "--volumes-from", "parent2",
  324. "busybox", "sh", "-c", "cat /test/foo && cat /other/bar")
  325. if _, err := runCommand(cmd); err != nil {
  326. t.Fatal(err)
  327. }
  328. deleteAllContainers()
  329. logDone("run - multiple volumes from")
  330. }
  331. // this tests verifies the ID format for the container
  332. func TestVerifyContainerID(t *testing.T) {
  333. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  334. out, exit, err := runCommandWithOutput(cmd)
  335. if err != nil {
  336. t.Fatal(err)
  337. }
  338. if exit != 0 {
  339. t.Fatalf("expected exit code 0 received %d", exit)
  340. }
  341. match, err := regexp.MatchString("^[0-9a-f]{64}$", strings.TrimSuffix(out, "\n"))
  342. if err != nil {
  343. t.Fatal(err)
  344. }
  345. if !match {
  346. t.Fatalf("Invalid container ID: %s", out)
  347. }
  348. deleteAllContainers()
  349. logDone("run - verify container ID")
  350. }
  351. // Test that creating a container with a volume doesn't crash. Regression test for #995.
  352. func TestCreateVolume(t *testing.T) {
  353. cmd := exec.Command(dockerBinary, "run", "-v", "/var/lib/data", "busybox", "true")
  354. if _, err := runCommand(cmd); err != nil {
  355. t.Fatal(err)
  356. }
  357. deleteAllContainers()
  358. logDone("run - create docker managed volume")
  359. }
  360. // Test that creating a volume with a symlink in its path works correctly. Test for #5152.
  361. // Note that this bug happens only with symlinks with a target that starts with '/'.
  362. func TestCreateVolumeWithSymlink(t *testing.T) {
  363. buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-createvolumewithsymlink", "-")
  364. buildCmd.Stdin = strings.NewReader(`FROM busybox
  365. RUN mkdir /foo && ln -s /foo /bar`)
  366. buildCmd.Dir = workingDirectory
  367. err := buildCmd.Run()
  368. if err != nil {
  369. t.Fatalf("could not build 'docker-test-createvolumewithsymlink': %v", err)
  370. }
  371. cmd := exec.Command(dockerBinary, "run", "-v", "/bar/foo", "--name", "test-createvolumewithsymlink", "docker-test-createvolumewithsymlink", "sh", "-c", "mount | grep -q /foo/foo")
  372. exitCode, err := runCommand(cmd)
  373. if err != nil || exitCode != 0 {
  374. t.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
  375. }
  376. var volPath string
  377. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{range .Volumes}}{{.}}{{end}}", "test-createvolumewithsymlink")
  378. volPath, exitCode, err = runCommandWithOutput(cmd)
  379. if err != nil || exitCode != 0 {
  380. t.Fatalf("[inspect] err: %v, exitcode: %d", err, exitCode)
  381. }
  382. cmd = exec.Command(dockerBinary, "rm", "-v", "test-createvolumewithsymlink")
  383. exitCode, err = runCommand(cmd)
  384. if err != nil || exitCode != 0 {
  385. t.Fatalf("[rm] err: %v, exitcode: %d", err, exitCode)
  386. }
  387. f, err := os.Open(volPath)
  388. defer f.Close()
  389. if !os.IsNotExist(err) {
  390. t.Fatalf("[open] (expecting 'file does not exist' error) err: %v, volPath: %s", err, volPath)
  391. }
  392. deleteImages("docker-test-createvolumewithsymlink")
  393. deleteAllContainers()
  394. logDone("run - create volume with symlink")
  395. }
  396. // Tests that a volume path that has a symlink exists in a container mounting it with `--volumes-from`.
  397. func TestVolumesFromSymlinkPath(t *testing.T) {
  398. buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-volumesfromsymlinkpath", "-")
  399. buildCmd.Stdin = strings.NewReader(`FROM busybox
  400. RUN mkdir /baz && ln -s /baz /foo
  401. VOLUME ["/foo/bar"]`)
  402. buildCmd.Dir = workingDirectory
  403. err := buildCmd.Run()
  404. if err != nil {
  405. t.Fatalf("could not build 'docker-test-volumesfromsymlinkpath': %v", err)
  406. }
  407. cmd := exec.Command(dockerBinary, "run", "--name", "test-volumesfromsymlinkpath", "docker-test-volumesfromsymlinkpath")
  408. exitCode, err := runCommand(cmd)
  409. if err != nil || exitCode != 0 {
  410. t.Fatalf("[run] (volume) err: %v, exitcode: %d", err, exitCode)
  411. }
  412. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-volumesfromsymlinkpath", "busybox", "sh", "-c", "ls /foo | grep -q bar")
  413. exitCode, err = runCommand(cmd)
  414. if err != nil || exitCode != 0 {
  415. t.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
  416. }
  417. deleteImages("docker-test-volumesfromsymlinkpath")
  418. deleteAllContainers()
  419. logDone("run - volumes-from symlink path")
  420. }
  421. func TestExitCode(t *testing.T) {
  422. cmd := exec.Command(dockerBinary, "run", "busybox", "/bin/sh", "-c", "exit 72")
  423. exit, err := runCommand(cmd)
  424. if err == nil {
  425. t.Fatal("should not have a non nil error")
  426. }
  427. if exit != 72 {
  428. t.Fatalf("expected exit code 72 received %d", exit)
  429. }
  430. deleteAllContainers()
  431. logDone("run - correct exit code")
  432. }
  433. func TestUserDefaultsToRoot(t *testing.T) {
  434. cmd := exec.Command(dockerBinary, "run", "busybox", "id")
  435. out, _, err := runCommandWithOutput(cmd)
  436. if err != nil {
  437. t.Fatal(err, out)
  438. }
  439. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  440. t.Fatalf("expected root user got %s", out)
  441. }
  442. deleteAllContainers()
  443. logDone("run - default user")
  444. }
  445. func TestUserByName(t *testing.T) {
  446. cmd := exec.Command(dockerBinary, "run", "-u", "root", "busybox", "id")
  447. out, _, err := runCommandWithOutput(cmd)
  448. if err != nil {
  449. t.Fatal(err, out)
  450. }
  451. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  452. t.Fatalf("expected root user got %s", out)
  453. }
  454. deleteAllContainers()
  455. logDone("run - user by name")
  456. }
  457. func TestUserByID(t *testing.T) {
  458. cmd := exec.Command(dockerBinary, "run", "-u", "1", "busybox", "id")
  459. out, _, err := runCommandWithOutput(cmd)
  460. if err != nil {
  461. t.Fatal(err, out)
  462. }
  463. if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
  464. t.Fatalf("expected daemon user got %s", out)
  465. }
  466. deleteAllContainers()
  467. logDone("run - user by id")
  468. }
  469. func TestUserByIDBig(t *testing.T) {
  470. cmd := exec.Command(dockerBinary, "run", "-u", "2147483648", "busybox", "id")
  471. out, _, err := runCommandWithOutput(cmd)
  472. if err == nil {
  473. t.Fatal("No error, but must be.", out)
  474. }
  475. if !strings.Contains(out, "Uids and gids must be in range") {
  476. t.Fatalf("expected error about uids range, got %s", out)
  477. }
  478. deleteAllContainers()
  479. logDone("run - user by id, id too big")
  480. }
  481. func TestUserByIDNegative(t *testing.T) {
  482. cmd := exec.Command(dockerBinary, "run", "-u", "-1", "busybox", "id")
  483. out, _, err := runCommandWithOutput(cmd)
  484. if err == nil {
  485. t.Fatal("No error, but must be.", out)
  486. }
  487. if !strings.Contains(out, "Uids and gids must be in range") {
  488. t.Fatalf("expected error about uids range, got %s", out)
  489. }
  490. deleteAllContainers()
  491. logDone("run - user by id, id negative")
  492. }
  493. func TestUserByIDZero(t *testing.T) {
  494. cmd := exec.Command(dockerBinary, "run", "-u", "0", "busybox", "id")
  495. out, _, err := runCommandWithOutput(cmd)
  496. if err != nil {
  497. t.Fatal(err, out)
  498. }
  499. if !strings.Contains(out, "uid=0(root) gid=0(root) groups=10(wheel)") {
  500. t.Fatalf("expected daemon user got %s", out)
  501. }
  502. deleteAllContainers()
  503. logDone("run - user by id, zero uid")
  504. }
  505. func TestUserNotFound(t *testing.T) {
  506. cmd := exec.Command(dockerBinary, "run", "-u", "notme", "busybox", "id")
  507. _, err := runCommand(cmd)
  508. if err == nil {
  509. t.Fatal("unknown user should cause container to fail")
  510. }
  511. deleteAllContainers()
  512. logDone("run - user not found")
  513. }
  514. func TestRunTwoConcurrentContainers(t *testing.T) {
  515. group := sync.WaitGroup{}
  516. group.Add(2)
  517. for i := 0; i < 2; i++ {
  518. go func() {
  519. defer group.Done()
  520. cmd := exec.Command(dockerBinary, "run", "busybox", "sleep", "2")
  521. if _, err := runCommand(cmd); err != nil {
  522. t.Fatal(err)
  523. }
  524. }()
  525. }
  526. group.Wait()
  527. deleteAllContainers()
  528. logDone("run - two concurrent containers")
  529. }
  530. func TestEnvironment(t *testing.T) {
  531. cmd := exec.Command(dockerBinary, "run", "-h", "testing", "-e=FALSE=true", "-e=TRUE", "-e=TRICKY", "-e=HOME=", "busybox", "env")
  532. cmd.Env = append(os.Environ(),
  533. "TRUE=false",
  534. "TRICKY=tri\ncky\n",
  535. )
  536. out, _, err := runCommandWithOutput(cmd)
  537. if err != nil {
  538. t.Fatal(err, out)
  539. }
  540. actualEnv := strings.Split(out, "\n")
  541. if actualEnv[len(actualEnv)-1] == "" {
  542. actualEnv = actualEnv[:len(actualEnv)-1]
  543. }
  544. sort.Strings(actualEnv)
  545. goodEnv := []string{
  546. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  547. "HOSTNAME=testing",
  548. "FALSE=true",
  549. "TRUE=false",
  550. "TRICKY=tri",
  551. "cky",
  552. "",
  553. "HOME=/root",
  554. }
  555. sort.Strings(goodEnv)
  556. if len(goodEnv) != len(actualEnv) {
  557. t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
  558. }
  559. for i := range goodEnv {
  560. if actualEnv[i] != goodEnv[i] {
  561. t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  562. }
  563. }
  564. deleteAllContainers()
  565. logDone("run - verify environment")
  566. }
  567. func TestContainerNetwork(t *testing.T) {
  568. cmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "127.0.0.1")
  569. if _, err := runCommand(cmd); err != nil {
  570. t.Fatal(err)
  571. }
  572. deleteAllContainers()
  573. logDone("run - test container network via ping")
  574. }
  575. // Issue #4681
  576. func TestLoopbackWhenNetworkDisabled(t *testing.T) {
  577. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1")
  578. if _, err := runCommand(cmd); err != nil {
  579. t.Fatal(err)
  580. }
  581. deleteAllContainers()
  582. logDone("run - test container loopback when networking disabled")
  583. }
  584. func TestNetHostNotAllowedWithLinks(t *testing.T) {
  585. _, _, err := cmd(t, "run", "--name", "linked", "busybox", "true")
  586. cmd := exec.Command(dockerBinary, "run", "--net=host", "--link", "linked:linked", "busybox", "true")
  587. _, _, err = runCommandWithOutput(cmd)
  588. if err == nil {
  589. t.Fatal("Expected error")
  590. }
  591. deleteAllContainers()
  592. logDone("run - don't allow --net=host to be used with links")
  593. }
  594. func TestLoopbackOnlyExistsWhenNetworkingDisabled(t *testing.T) {
  595. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up")
  596. out, _, err := runCommandWithOutput(cmd)
  597. if err != nil {
  598. t.Fatal(err, out)
  599. }
  600. var (
  601. count = 0
  602. parts = strings.Split(out, "\n")
  603. )
  604. for _, l := range parts {
  605. if l != "" {
  606. count++
  607. }
  608. }
  609. if count != 1 {
  610. t.Fatalf("Wrong interface count in container %d", count)
  611. }
  612. if !strings.HasPrefix(out, "1: lo") {
  613. t.Fatalf("Wrong interface in test container: expected [1: lo], got %s", out)
  614. }
  615. deleteAllContainers()
  616. logDone("run - test loopback only exists when networking disabled")
  617. }
  618. func TestPrivilegedCanMknod(t *testing.T) {
  619. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  620. out, _, err := runCommandWithOutput(cmd)
  621. if err != nil {
  622. t.Fatal(err)
  623. }
  624. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  625. t.Fatalf("expected output ok received %s", actual)
  626. }
  627. deleteAllContainers()
  628. logDone("run - test privileged can mknod")
  629. }
  630. func TestUnPrivilegedCanMknod(t *testing.T) {
  631. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  632. out, _, err := runCommandWithOutput(cmd)
  633. if err != nil {
  634. t.Fatal(err)
  635. }
  636. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  637. t.Fatalf("expected output ok received %s", actual)
  638. }
  639. deleteAllContainers()
  640. logDone("run - test un-privileged can mknod")
  641. }
  642. func TestCapDropInvalid(t *testing.T) {
  643. cmd := exec.Command(dockerBinary, "run", "--cap-drop=CHPASS", "busybox", "ls")
  644. out, _, err := runCommandWithOutput(cmd)
  645. if err == nil {
  646. t.Fatal(err, out)
  647. }
  648. logDone("run - test --cap-drop=CHPASS invalid")
  649. }
  650. func TestCapDropCannotMknod(t *testing.T) {
  651. cmd := exec.Command(dockerBinary, "run", "--cap-drop=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && 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 --cap-drop=MKNOD cannot mknod")
  661. }
  662. func TestCapDropCannotMknodLowerCase(t *testing.T) {
  663. cmd := exec.Command(dockerBinary, "run", "--cap-drop=mknod", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  664. out, _, err := runCommandWithOutput(cmd)
  665. if err == nil {
  666. t.Fatal(err, out)
  667. }
  668. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  669. t.Fatalf("expected output not ok received %s", actual)
  670. }
  671. deleteAllContainers()
  672. logDone("run - test --cap-drop=mknod cannot mknod lowercase")
  673. }
  674. func TestCapDropALLCannotMknod(t *testing.T) {
  675. cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  676. out, _, err := runCommandWithOutput(cmd)
  677. if err == nil {
  678. t.Fatal(err, out)
  679. }
  680. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  681. t.Fatalf("expected output not ok received %s", actual)
  682. }
  683. deleteAllContainers()
  684. logDone("run - test --cap-drop=ALL cannot mknod")
  685. }
  686. func TestCapDropALLAddMknodCannotMknod(t *testing.T) {
  687. cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "--cap-add=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  688. out, _, err := runCommandWithOutput(cmd)
  689. if err != nil {
  690. t.Fatal(err, out)
  691. }
  692. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  693. t.Fatalf("expected output ok received %s", actual)
  694. }
  695. deleteAllContainers()
  696. logDone("run - test --cap-drop=ALL --cap-add=MKNOD can mknod")
  697. }
  698. func TestCapAddInvalid(t *testing.T) {
  699. cmd := exec.Command(dockerBinary, "run", "--cap-add=CHPASS", "busybox", "ls")
  700. out, _, err := runCommandWithOutput(cmd)
  701. if err == nil {
  702. t.Fatal(err, out)
  703. }
  704. logDone("run - test --cap-add=CHPASS invalid")
  705. }
  706. func TestCapAddCanDownInterface(t *testing.T) {
  707. cmd := exec.Command(dockerBinary, "run", "--cap-add=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  708. out, _, err := runCommandWithOutput(cmd)
  709. if err != nil {
  710. t.Fatal(err, out)
  711. }
  712. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  713. t.Fatalf("expected output ok received %s", actual)
  714. }
  715. deleteAllContainers()
  716. logDone("run - test --cap-add=NET_ADMIN can set eth0 down")
  717. }
  718. func TestCapAddALLCanDownInterface(t *testing.T) {
  719. cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  720. out, _, err := runCommandWithOutput(cmd)
  721. if err != nil {
  722. t.Fatal(err, out)
  723. }
  724. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  725. t.Fatalf("expected output ok received %s", actual)
  726. }
  727. deleteAllContainers()
  728. logDone("run - test --cap-add=ALL can set eth0 down")
  729. }
  730. func TestCapAddALLDropNetAdminCanDownInterface(t *testing.T) {
  731. cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "--cap-drop=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  732. out, _, err := runCommandWithOutput(cmd)
  733. if err == nil {
  734. t.Fatal(err, out)
  735. }
  736. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  737. t.Fatalf("expected output not ok received %s", actual)
  738. }
  739. deleteAllContainers()
  740. logDone("run - test --cap-add=ALL --cap-drop=NET_ADMIN cannot set eth0 down")
  741. }
  742. func TestPrivilegedCanMount(t *testing.T) {
  743. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  744. out, _, err := runCommandWithOutput(cmd)
  745. if err != nil {
  746. t.Fatal(err)
  747. }
  748. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  749. t.Fatalf("expected output ok received %s", actual)
  750. }
  751. deleteAllContainers()
  752. logDone("run - test privileged can mount")
  753. }
  754. func TestUnPrivilegedCannotMount(t *testing.T) {
  755. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  756. out, _, err := runCommandWithOutput(cmd)
  757. if err == nil {
  758. t.Fatal(err, out)
  759. }
  760. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  761. t.Fatalf("expected output not ok received %s", actual)
  762. }
  763. deleteAllContainers()
  764. logDone("run - test un-privileged cannot mount")
  765. }
  766. func TestSysNotWritableInNonPrivilegedContainers(t *testing.T) {
  767. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/sys/kernel/profiling")
  768. if code, err := runCommand(cmd); err == nil || code == 0 {
  769. t.Fatal("sys should not be writable in a non privileged container")
  770. }
  771. deleteAllContainers()
  772. logDone("run - sys not writable in non privileged container")
  773. }
  774. func TestSysWritableInPrivilegedContainers(t *testing.T) {
  775. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/sys/kernel/profiling")
  776. if code, err := runCommand(cmd); err != nil || code != 0 {
  777. t.Fatalf("sys should be writable in privileged container")
  778. }
  779. deleteAllContainers()
  780. logDone("run - sys writable in privileged container")
  781. }
  782. func TestProcNotWritableInNonPrivilegedContainers(t *testing.T) {
  783. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/proc/sysrq-trigger")
  784. if code, err := runCommand(cmd); err == nil || code == 0 {
  785. t.Fatal("proc should not be writable in a non privileged container")
  786. }
  787. deleteAllContainers()
  788. logDone("run - proc not writable in non privileged container")
  789. }
  790. func TestProcWritableInPrivilegedContainers(t *testing.T) {
  791. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/proc/sysrq-trigger")
  792. if code, err := runCommand(cmd); err != nil || code != 0 {
  793. t.Fatalf("proc should be writable in privileged container")
  794. }
  795. deleteAllContainers()
  796. logDone("run - proc writable in privileged container")
  797. }
  798. func TestRunWithCpuset(t *testing.T) {
  799. cmd := exec.Command(dockerBinary, "run", "--cpuset", "0", "busybox", "true")
  800. if code, err := runCommand(cmd); err != nil || code != 0 {
  801. t.Fatalf("container should run successfuly with cpuset of 0: %s", err)
  802. }
  803. deleteAllContainers()
  804. logDone("run - cpuset 0")
  805. }
  806. func TestDeviceNumbers(t *testing.T) {
  807. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "ls -l /dev/null")
  808. out, _, err := runCommandWithOutput(cmd)
  809. if err != nil {
  810. t.Fatal(err, out)
  811. }
  812. deviceLineFields := strings.Fields(out)
  813. deviceLineFields[6] = ""
  814. deviceLineFields[7] = ""
  815. deviceLineFields[8] = ""
  816. expected := []string{"crw-rw-rw-", "1", "root", "root", "1,", "3", "", "", "", "/dev/null"}
  817. if !(reflect.DeepEqual(deviceLineFields, expected)) {
  818. t.Fatalf("expected output\ncrw-rw-rw- 1 root root 1, 3 May 24 13:29 /dev/null\n received\n %s\n", out)
  819. }
  820. deleteAllContainers()
  821. logDone("run - test device numbers")
  822. }
  823. func TestThatCharacterDevicesActLikeCharacterDevices(t *testing.T) {
  824. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "dd if=/dev/zero of=/zero bs=1k count=5 2> /dev/null ; du -h /zero")
  825. out, _, err := runCommandWithOutput(cmd)
  826. if err != nil {
  827. t.Fatal(err, out)
  828. }
  829. if actual := strings.Trim(out, "\r\n"); actual[0] == '0' {
  830. t.Fatalf("expected a new file called /zero to be create that is greater than 0 bytes long, but du says: %s", actual)
  831. }
  832. deleteAllContainers()
  833. logDone("run - test that character devices work.")
  834. }
  835. func TestRunUnprivilegedWithChroot(t *testing.T) {
  836. cmd := exec.Command(dockerBinary, "run", "busybox", "chroot", "/", "true")
  837. if _, err := runCommand(cmd); err != nil {
  838. t.Fatal(err)
  839. }
  840. deleteAllContainers()
  841. logDone("run - unprivileged with chroot")
  842. }
  843. func TestAddingOptionalDevices(t *testing.T) {
  844. cmd := exec.Command(dockerBinary, "run", "--device", "/dev/zero:/dev/nulo", "busybox", "sh", "-c", "ls /dev/nulo")
  845. out, _, err := runCommandWithOutput(cmd)
  846. if err != nil {
  847. t.Fatal(err, out)
  848. }
  849. if actual := strings.Trim(out, "\r\n"); actual != "/dev/nulo" {
  850. t.Fatalf("expected output /dev/nulo, received %s", actual)
  851. }
  852. deleteAllContainers()
  853. logDone("run - test --device argument")
  854. }
  855. func TestModeHostname(t *testing.T) {
  856. cmd := exec.Command(dockerBinary, "run", "-h=testhostname", "busybox", "cat", "/etc/hostname")
  857. out, _, err := runCommandWithOutput(cmd)
  858. if err != nil {
  859. t.Fatal(err, out)
  860. }
  861. if actual := strings.Trim(out, "\r\n"); actual != "testhostname" {
  862. t.Fatalf("expected 'testhostname', but says: '%s'", actual)
  863. }
  864. cmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hostname")
  865. out, _, err = runCommandWithOutput(cmd)
  866. if err != nil {
  867. t.Fatal(err, out)
  868. }
  869. hostname, err := os.Hostname()
  870. if err != nil {
  871. t.Fatal(err)
  872. }
  873. if actual := strings.Trim(out, "\r\n"); actual != hostname {
  874. t.Fatalf("expected '%s', but says: '%s'", hostname, actual)
  875. }
  876. deleteAllContainers()
  877. logDone("run - hostname and several network modes")
  878. }
  879. func TestRootWorkdir(t *testing.T) {
  880. s, _, err := cmd(t, "run", "--workdir", "/", "busybox", "pwd")
  881. if err != nil {
  882. t.Fatal(s, err)
  883. }
  884. if s != "/\n" {
  885. t.Fatalf("pwd returned '%s' (expected /\\n)", s)
  886. }
  887. deleteAllContainers()
  888. logDone("run - workdir /")
  889. }
  890. func TestAllowBindMountingRoot(t *testing.T) {
  891. s, _, err := cmd(t, "run", "-v", "/:/host", "busybox", "ls", "/host")
  892. if err != nil {
  893. t.Fatal(s, err)
  894. }
  895. deleteAllContainers()
  896. logDone("run - bind mount / as volume")
  897. }
  898. func TestDisallowBindMountingRootToRoot(t *testing.T) {
  899. cmd := exec.Command(dockerBinary, "run", "-v", "/:/", "busybox", "ls", "/host")
  900. out, _, err := runCommandWithOutput(cmd)
  901. if err == nil {
  902. t.Fatal(out, err)
  903. }
  904. deleteAllContainers()
  905. logDone("run - bind mount /:/ as volume should fail")
  906. }
  907. // Test recursive bind mount works by default
  908. func TestDockerRunWithVolumesIsRecursive(t *testing.T) {
  909. tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
  910. if err != nil {
  911. t.Fatal(err)
  912. }
  913. defer os.RemoveAll(tmpDir)
  914. // Create a temporary tmpfs mount.
  915. tmpfsDir := filepath.Join(tmpDir, "tmpfs")
  916. if err := os.MkdirAll(tmpfsDir, 0777); err != nil {
  917. t.Fatalf("failed to mkdir at %s - %s", tmpfsDir, err)
  918. }
  919. if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
  920. t.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
  921. }
  922. f, err := ioutil.TempFile(tmpfsDir, "touch-me")
  923. if err != nil {
  924. t.Fatal(err)
  925. }
  926. defer f.Close()
  927. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
  928. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  929. if err != nil && exitCode != 0 {
  930. t.Fatal(out, stderr, err)
  931. }
  932. if !strings.Contains(out, filepath.Base(f.Name())) {
  933. t.Fatal("Recursive bind mount test failed. Expected file not found")
  934. }
  935. deleteAllContainers()
  936. logDone("run - volumes are bind mounted recursively")
  937. }
  938. func TestDnsDefaultOptions(t *testing.T) {
  939. cmd := exec.Command(dockerBinary, "run", "busybox", "cat", "/etc/resolv.conf")
  940. actual, _, err := runCommandWithOutput(cmd)
  941. if err != nil {
  942. t.Fatal(err, actual)
  943. }
  944. resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
  945. if os.IsNotExist(err) {
  946. t.Fatalf("/etc/resolv.conf does not exist")
  947. }
  948. if actual != string(resolvConf) {
  949. t.Fatalf("expected resolv.conf is not the same of actual")
  950. }
  951. deleteAllContainers()
  952. logDone("run - dns default options")
  953. }
  954. func TestDnsOptions(t *testing.T) {
  955. cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
  956. out, _, err := runCommandWithOutput(cmd)
  957. if err != nil {
  958. t.Fatal(err, out)
  959. }
  960. actual := strings.Replace(strings.Trim(out, "\r\n"), "\n", " ", -1)
  961. if actual != "nameserver 127.0.0.1 search mydomain" {
  962. t.Fatalf("expected 'nameserver 127.0.0.1 search mydomain', but says: '%s'", actual)
  963. }
  964. cmd = exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=.", "busybox", "cat", "/etc/resolv.conf")
  965. out, _, err = runCommandWithOutput(cmd)
  966. if err != nil {
  967. t.Fatal(err, out)
  968. }
  969. actual = strings.Replace(strings.Trim(strings.Trim(out, "\r\n"), " "), "\n", " ", -1)
  970. if actual != "nameserver 127.0.0.1" {
  971. t.Fatalf("expected 'nameserver 127.0.0.1', but says: '%s'", actual)
  972. }
  973. logDone("run - dns options")
  974. }
  975. func TestDnsOptionsBasedOnHostResolvConf(t *testing.T) {
  976. resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
  977. if os.IsNotExist(err) {
  978. t.Fatalf("/etc/resolv.conf does not exist")
  979. }
  980. hostNamservers := resolvconf.GetNameservers(resolvConf)
  981. hostSearch := resolvconf.GetSearchDomains(resolvConf)
  982. cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "busybox", "cat", "/etc/resolv.conf")
  983. out, _, err := runCommandWithOutput(cmd)
  984. if err != nil {
  985. t.Fatal(err, out)
  986. }
  987. if actualNameservers := resolvconf.GetNameservers([]byte(out)); string(actualNameservers[0]) != "127.0.0.1" {
  988. t.Fatalf("expected '127.0.0.1', but says: '%s'", string(actualNameservers[0]))
  989. }
  990. actualSearch := resolvconf.GetSearchDomains([]byte(out))
  991. if len(actualSearch) != len(hostSearch) {
  992. t.Fatalf("expected '%s' search domain(s), but it has: '%s'", len(hostSearch), len(actualSearch))
  993. }
  994. for i := range actualSearch {
  995. if actualSearch[i] != hostSearch[i] {
  996. t.Fatalf("expected '%s' domain, but says: '%s'", actualSearch[i], hostSearch[i])
  997. }
  998. }
  999. cmd = exec.Command(dockerBinary, "run", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
  1000. out, _, err = runCommandWithOutput(cmd)
  1001. if err != nil {
  1002. t.Fatal(err, out)
  1003. }
  1004. actualNameservers := resolvconf.GetNameservers([]byte(out))
  1005. if len(actualNameservers) != len(hostNamservers) {
  1006. t.Fatalf("expected '%s' nameserver(s), but it has: '%s'", len(hostNamservers), len(actualNameservers))
  1007. }
  1008. for i := range actualNameservers {
  1009. if actualNameservers[i] != hostNamservers[i] {
  1010. t.Fatalf("expected '%s' nameserver, but says: '%s'", actualNameservers[i], hostNamservers[i])
  1011. }
  1012. }
  1013. if actualSearch = resolvconf.GetSearchDomains([]byte(out)); string(actualSearch[0]) != "mydomain" {
  1014. t.Fatalf("expected 'mydomain', but says: '%s'", string(actualSearch[0]))
  1015. }
  1016. deleteAllContainers()
  1017. logDone("run - dns options based on host resolv.conf")
  1018. }
  1019. // Regression test for #6983
  1020. func TestAttachStdErrOnlyTTYMode(t *testing.T) {
  1021. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stderr", "busybox", "true")
  1022. exitCode, err := runCommand(cmd)
  1023. if err != nil {
  1024. t.Fatal(err)
  1025. } else if exitCode != 0 {
  1026. t.Fatalf("Container should have exited with error code 0")
  1027. }
  1028. deleteAllContainers()
  1029. logDone("run - Attach stderr only with -t")
  1030. }
  1031. // Regression test for #6983
  1032. func TestAttachStdOutOnlyTTYMode(t *testing.T) {
  1033. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "busybox", "true")
  1034. exitCode, err := runCommand(cmd)
  1035. if err != nil {
  1036. t.Fatal(err)
  1037. } else if exitCode != 0 {
  1038. t.Fatalf("Container should have exited with error code 0")
  1039. }
  1040. deleteAllContainers()
  1041. logDone("run - Attach stdout only with -t")
  1042. }
  1043. // Regression test for #6983
  1044. func TestAttachStdOutAndErrTTYMode(t *testing.T) {
  1045. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "-a", "stderr", "busybox", "true")
  1046. exitCode, err := runCommand(cmd)
  1047. if err != nil {
  1048. t.Fatal(err)
  1049. } else if exitCode != 0 {
  1050. t.Fatalf("Container should have exited with error code 0")
  1051. }
  1052. deleteAllContainers()
  1053. logDone("run - Attach stderr and stdout with -t")
  1054. }
  1055. func TestState(t *testing.T) {
  1056. defer deleteAllContainers()
  1057. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  1058. out, _, err := runCommandWithOutput(cmd)
  1059. if err != nil {
  1060. t.Fatal(err, out)
  1061. }
  1062. id := strings.TrimSpace(out)
  1063. state, err := inspectField(id, "State.Running")
  1064. if err != nil {
  1065. t.Fatal(err)
  1066. }
  1067. if state != "true" {
  1068. t.Fatal("Container state is 'not running'")
  1069. }
  1070. pid1, err := inspectField(id, "State.Pid")
  1071. if err != nil {
  1072. t.Fatal(err)
  1073. }
  1074. if pid1 == "0" {
  1075. t.Fatal("Container state Pid 0")
  1076. }
  1077. cmd = exec.Command(dockerBinary, "stop", id)
  1078. out, _, err = runCommandWithOutput(cmd)
  1079. if err != nil {
  1080. t.Fatal(err, out)
  1081. }
  1082. state, err = inspectField(id, "State.Running")
  1083. if err != nil {
  1084. t.Fatal(err)
  1085. }
  1086. if state != "false" {
  1087. t.Fatal("Container state is 'running'")
  1088. }
  1089. pid2, err := inspectField(id, "State.Pid")
  1090. if err != nil {
  1091. t.Fatal(err)
  1092. }
  1093. if pid2 == pid1 {
  1094. t.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
  1095. }
  1096. cmd = exec.Command(dockerBinary, "start", id)
  1097. out, _, err = runCommandWithOutput(cmd)
  1098. if err != nil {
  1099. t.Fatal(err, out)
  1100. }
  1101. state, err = inspectField(id, "State.Running")
  1102. if err != nil {
  1103. t.Fatal(err)
  1104. }
  1105. if state != "true" {
  1106. t.Fatal("Container state is 'not running'")
  1107. }
  1108. pid3, err := inspectField(id, "State.Pid")
  1109. if err != nil {
  1110. t.Fatal(err)
  1111. }
  1112. if pid3 == pid1 {
  1113. t.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
  1114. }
  1115. logDone("run - test container state.")
  1116. }
  1117. // Test for #1737
  1118. func TestCopyVolumeUidGid(t *testing.T) {
  1119. name := "testrunvolumesuidgid"
  1120. defer deleteImages(name)
  1121. defer deleteAllContainers()
  1122. _, err := buildImage(name,
  1123. `FROM busybox
  1124. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  1125. RUN echo 'dockerio:x:1001:' >> /etc/group
  1126. RUN mkdir -p /hello && touch /hello/test && chown dockerio.dockerio /hello`,
  1127. true)
  1128. if err != nil {
  1129. t.Fatal(err)
  1130. }
  1131. // Test that the uid and gid is copied from the image to the volume
  1132. cmd := exec.Command(dockerBinary, "run", "--rm", "-v", "/hello", name, "sh", "-c", "ls -l / | grep hello | awk '{print $3\":\"$4}'")
  1133. out, _, err := runCommandWithOutput(cmd)
  1134. if err != nil {
  1135. t.Fatal(err, out)
  1136. }
  1137. out = strings.TrimSpace(out)
  1138. if out != "dockerio:dockerio" {
  1139. t.Fatalf("Wrong /hello ownership: %s, expected dockerio:dockerio", out)
  1140. }
  1141. logDone("run - copy uid/gid for volume")
  1142. }
  1143. // Test for #1582
  1144. func TestCopyVolumeContent(t *testing.T) {
  1145. name := "testruncopyvolumecontent"
  1146. defer deleteImages(name)
  1147. defer deleteAllContainers()
  1148. _, err := buildImage(name,
  1149. `FROM busybox
  1150. RUN mkdir -p /hello/local && echo hello > /hello/local/world`,
  1151. true)
  1152. if err != nil {
  1153. t.Fatal(err)
  1154. }
  1155. // Test that the content is copied from the image to the volume
  1156. cmd := exec.Command(dockerBinary, "run", "--rm", "-v", "/hello", name, "sh", "-c", "find", "/hello")
  1157. out, _, err := runCommandWithOutput(cmd)
  1158. if err != nil {
  1159. t.Fatal(err, out)
  1160. }
  1161. if !(strings.Contains(out, "/hello/local/world") && strings.Contains(out, "/hello/local")) {
  1162. t.Fatal("Container failed to transfer content to volume")
  1163. }
  1164. logDone("run - copy volume content")
  1165. }
  1166. func TestRunCleanupCmdOnEntrypoint(t *testing.T) {
  1167. name := "testrunmdcleanuponentrypoint"
  1168. defer deleteImages(name)
  1169. defer deleteAllContainers()
  1170. if _, err := buildImage(name,
  1171. `FROM busybox
  1172. ENTRYPOINT ["echo"]
  1173. CMD ["testingpoint"]`,
  1174. true); err != nil {
  1175. t.Fatal(err)
  1176. }
  1177. runCmd := exec.Command(dockerBinary, "run", "--entrypoint", "whoami", name)
  1178. out, exit, err := runCommandWithOutput(runCmd)
  1179. if err != nil {
  1180. t.Fatalf("Error: %v, out: %q", err, out)
  1181. }
  1182. if exit != 0 {
  1183. t.Fatalf("expected exit code 0 received %d, out: %q", exit, out)
  1184. }
  1185. out = strings.TrimSpace(out)
  1186. if out != "root" {
  1187. t.Fatalf("Expected output root, got %q", out)
  1188. }
  1189. logDone("run - cleanup cmd on --entrypoint")
  1190. }
  1191. // TestRunWorkdirExistsAndIsFile checks that if 'docker run -w' with existing file can be detected
  1192. func TestRunWorkdirExistsAndIsFile(t *testing.T) {
  1193. defer deleteAllContainers()
  1194. runCmd := exec.Command(dockerBinary, "run", "-w", "/bin/cat", "busybox")
  1195. out, exit, err := runCommandWithOutput(runCmd)
  1196. if !(err != nil && exit == 1 && strings.Contains(out, "Cannot mkdir: /bin/cat is not a directory")) {
  1197. t.Fatalf("Docker must complains about making dir, but we got out: %s, exit: %d, err: %s", out, exit, err)
  1198. }
  1199. logDone("run - error on existing file for workdir")
  1200. }
  1201. func TestRunExitOnStdinClose(t *testing.T) {
  1202. name := "testrunexitonstdinclose"
  1203. defer deleteAllContainers()
  1204. runCmd := exec.Command(dockerBinary, "run", "--name", name, "-i", "busybox", "/bin/cat")
  1205. stdin, err := runCmd.StdinPipe()
  1206. if err != nil {
  1207. t.Fatal(err)
  1208. }
  1209. stdout, err := runCmd.StdoutPipe()
  1210. if err != nil {
  1211. t.Fatal(err)
  1212. }
  1213. if err := runCmd.Start(); err != nil {
  1214. t.Fatal(err)
  1215. }
  1216. if _, err := stdin.Write([]byte("hello\n")); err != nil {
  1217. t.Fatal(err)
  1218. }
  1219. r := bufio.NewReader(stdout)
  1220. line, err := r.ReadString('\n')
  1221. if err != nil {
  1222. t.Fatal(err)
  1223. }
  1224. line = strings.TrimSpace(line)
  1225. if line != "hello" {
  1226. t.Fatalf("Output should be 'hello', got '%q'", line)
  1227. }
  1228. if err := stdin.Close(); err != nil {
  1229. t.Fatal(err)
  1230. }
  1231. finish := make(chan struct{})
  1232. go func() {
  1233. if err := runCmd.Wait(); err != nil {
  1234. t.Fatal(err)
  1235. }
  1236. close(finish)
  1237. }()
  1238. select {
  1239. case <-finish:
  1240. case <-time.After(1 * time.Second):
  1241. t.Fatal("docker run failed to exit on stdin close")
  1242. }
  1243. state, err := inspectField(name, "State.Running")
  1244. if err != nil {
  1245. t.Fatal(err)
  1246. }
  1247. if state != "false" {
  1248. t.Fatal("Container must be stopped after stdin closing")
  1249. }
  1250. logDone("run - exit on stdin closing")
  1251. }
  1252. // Test for #2267
  1253. func TestWriteHostsFileAndNotCommit(t *testing.T) {
  1254. name := "writehosts"
  1255. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/hosts && cat /etc/hosts")
  1256. out, _, err := runCommandWithOutput(cmd)
  1257. if err != nil {
  1258. t.Fatal(err, out)
  1259. }
  1260. if !strings.Contains(out, "test2267") {
  1261. t.Fatal("/etc/hosts should contain 'test2267'")
  1262. }
  1263. cmd = exec.Command(dockerBinary, "diff", name)
  1264. if err != nil {
  1265. t.Fatal(err, out)
  1266. }
  1267. out, _, err = runCommandWithOutput(cmd)
  1268. if err != nil {
  1269. t.Fatal(err, out)
  1270. }
  1271. if len(strings.Trim(out, "\r\n")) != 0 {
  1272. t.Fatal("diff should be empty")
  1273. }
  1274. logDone("run - write to /etc/hosts and not commited")
  1275. }
  1276. // Test for #2267
  1277. func TestWriteHostnameFileAndNotCommit(t *testing.T) {
  1278. name := "writehostname"
  1279. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/hostname && cat /etc/hostname")
  1280. out, _, err := runCommandWithOutput(cmd)
  1281. if err != nil {
  1282. t.Fatal(err, out)
  1283. }
  1284. if !strings.Contains(out, "test2267") {
  1285. t.Fatal("/etc/hostname should contain 'test2267'")
  1286. }
  1287. cmd = exec.Command(dockerBinary, "diff", name)
  1288. if err != nil {
  1289. t.Fatal(err, out)
  1290. }
  1291. out, _, err = runCommandWithOutput(cmd)
  1292. if err != nil {
  1293. t.Fatal(err, out)
  1294. }
  1295. if len(strings.Trim(out, "\r\n")) != 0 {
  1296. t.Fatal("diff should be empty")
  1297. }
  1298. logDone("run - write to /etc/hostname and not commited")
  1299. }
  1300. // Test for #2267
  1301. func TestWriteResolvFileAndNotCommit(t *testing.T) {
  1302. name := "writeresolv"
  1303. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/resolv.conf && cat /etc/resolv.conf")
  1304. out, _, err := runCommandWithOutput(cmd)
  1305. if err != nil {
  1306. t.Fatal(err, out)
  1307. }
  1308. if !strings.Contains(out, "test2267") {
  1309. t.Fatal("/etc/resolv.conf should contain 'test2267'")
  1310. }
  1311. cmd = exec.Command(dockerBinary, "diff", name)
  1312. if err != nil {
  1313. t.Fatal(err, out)
  1314. }
  1315. out, _, err = runCommandWithOutput(cmd)
  1316. if err != nil {
  1317. t.Fatal(err, out)
  1318. }
  1319. if len(strings.Trim(out, "\r\n")) != 0 {
  1320. t.Fatal("diff should be empty")
  1321. }
  1322. logDone("run - write to /etc/resolv.conf and not commited")
  1323. }
  1324. func TestRunWithBadDevice(t *testing.T) {
  1325. name := "baddevice"
  1326. cmd := exec.Command(dockerBinary, "run", "--name", name, "--device", "/etc", "busybox", "true")
  1327. out, _, err := runCommandWithOutput(cmd)
  1328. if err == nil {
  1329. t.Fatal("Run should fail with bad device")
  1330. }
  1331. expected := `"/etc": not a device node`
  1332. if !strings.Contains(out, expected) {
  1333. t.Fatalf("Output should contain %q, actual out: %q", expected, out)
  1334. }
  1335. logDone("run - error with bad device")
  1336. }
  1337. func TestEntrypoint(t *testing.T) {
  1338. name := "entrypoint"
  1339. cmd := exec.Command(dockerBinary, "run", "--name", name, "--entrypoint", "/bin/echo", "busybox", "-n", "foobar")
  1340. out, _, err := runCommandWithOutput(cmd)
  1341. if err != nil {
  1342. t.Fatal(err, out)
  1343. }
  1344. expected := "foobar"
  1345. if out != expected {
  1346. t.Fatalf("Output should be %q, actual out: %q", expected, out)
  1347. }
  1348. logDone("run - entrypoint")
  1349. }
  1350. func TestBindMounts(t *testing.T) {
  1351. tmpDir, err := ioutil.TempDir("", "docker-test-container")
  1352. if err != nil {
  1353. t.Fatal(err)
  1354. }
  1355. defer os.RemoveAll(tmpDir)
  1356. writeFile(path.Join(tmpDir, "touch-me"), "", t)
  1357. // Test reading from a read-only bind mount
  1358. cmd := exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox", "ls", "/tmp")
  1359. out, _, err := runCommandWithOutput(cmd)
  1360. if err != nil {
  1361. t.Fatal(err, out)
  1362. }
  1363. if !strings.Contains(out, "touch-me") {
  1364. t.Fatal("Container failed to read from bind mount")
  1365. }
  1366. // test writing to bind mount
  1367. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "busybox", "touch", "/tmp/holla")
  1368. out, _, err = runCommandWithOutput(cmd)
  1369. if err != nil {
  1370. t.Fatal(err, out)
  1371. }
  1372. readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1373. // test mounting to an illegal destination directory
  1374. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:.", tmpDir), "busybox", "ls", ".")
  1375. _, err = runCommand(cmd)
  1376. if err == nil {
  1377. t.Fatal("Container bind mounted illegal directory")
  1378. }
  1379. // test mount a file
  1380. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "busybox", "sh", "-c", "echo -n 'yotta' > /tmp/holla")
  1381. _, err = runCommand(cmd)
  1382. if err != nil {
  1383. t.Fatal(err, out)
  1384. }
  1385. content := readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1386. expected := "yotta"
  1387. if content != expected {
  1388. t.Fatalf("Output should be %q, actual out: %q", expected, content)
  1389. }
  1390. logDone("run - bind mounts")
  1391. }
  1392. func TestHostsLinkedContainerUpdate(t *testing.T) {
  1393. tmpdir, err := ioutil.TempDir("", "docker-integration")
  1394. if err != nil {
  1395. t.Fatal(err)
  1396. }
  1397. defer os.RemoveAll(tmpdir)
  1398. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sleep", "5"))
  1399. if err != nil {
  1400. t.Fatal(err, out)
  1401. }
  1402. // TODO fix docker cp and /etc/hosts
  1403. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--link", "c1:c1", "--name", "c2", "busybox", "sh", "-c", "while true;do cp /etc/hosts /hosts; done"))
  1404. if err != nil {
  1405. t.Fatal(err, out)
  1406. }
  1407. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "cp", "c2:/hosts", tmpdir+"/1"))
  1408. if err != nil {
  1409. t.Fatal(err, out)
  1410. }
  1411. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "restart", "-t", "0", "c1"))
  1412. if err != nil {
  1413. t.Fatal(err, out)
  1414. }
  1415. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "cp", "c2:/hosts", tmpdir+"/2"))
  1416. if err != nil {
  1417. t.Fatal(err, out)
  1418. }
  1419. out, _, _, err = runCommandWithStdoutStderr(exec.Command("diff", tmpdir+"/1", tmpdir+"/2"))
  1420. if err == nil {
  1421. t.Fatalf("Expecting error, got none")
  1422. }
  1423. out = stripTrailingCharacters(out)
  1424. if out == "" {
  1425. t.Fatalf("expected /etc/hosts to be updated, but wasn't")
  1426. }
  1427. deleteAllContainers()
  1428. logDone("run - /etc/hosts updated in parent when restart")
  1429. }
  1430. // Ensure that CIDFile gets deleted if it's empty
  1431. // Perform this test by making `docker run` fail
  1432. func TestRunCidFileCleanupIfEmpty(t *testing.T) {
  1433. tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
  1434. if err != nil {
  1435. t.Fatal(err)
  1436. }
  1437. defer os.RemoveAll(tmpDir)
  1438. tmpCidFile := path.Join(tmpDir, "cid")
  1439. cmd := exec.Command(dockerBinary, "run", "--cidfile", tmpCidFile, "scratch")
  1440. out, _, err := runCommandWithOutput(cmd)
  1441. t.Log(out)
  1442. if err == nil {
  1443. t.Fatal("Run without command must fail")
  1444. }
  1445. if _, err := os.Stat(tmpCidFile); err == nil {
  1446. t.Fatalf("empty CIDFile '%s' should've been deleted", tmpCidFile)
  1447. }
  1448. deleteAllContainers()
  1449. logDone("run - cleanup empty cidfile on fail")
  1450. }
  1451. // #2098 - Docker cidFiles only contain short version of the containerId
  1452. //sudo docker run --cidfile /tmp/docker_test.cid ubuntu echo "test"
  1453. // TestRunCidFile tests that run --cidfile returns the longid
  1454. func TestRunCidFileCheckIDLength(t *testing.T) {
  1455. tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
  1456. if err != nil {
  1457. t.Fatal(err)
  1458. }
  1459. tmpCidFile := path.Join(tmpDir, "cid")
  1460. defer os.RemoveAll(tmpDir)
  1461. cmd := exec.Command(dockerBinary, "run", "-d", "--cidfile", tmpCidFile, "busybox", "true")
  1462. out, _, err := runCommandWithOutput(cmd)
  1463. if err != nil {
  1464. t.Fatal(err)
  1465. }
  1466. id := strings.TrimSpace(out)
  1467. buffer, err := ioutil.ReadFile(tmpCidFile)
  1468. if err != nil {
  1469. t.Fatal(err)
  1470. }
  1471. cid := string(buffer)
  1472. if len(cid) != 64 {
  1473. t.Fatalf("--cidfile should be a long id, not '%s'", id)
  1474. }
  1475. if cid != id {
  1476. t.Fatalf("cid must be equal to %s, got %s", id, cid)
  1477. }
  1478. deleteAllContainers()
  1479. logDone("run - cidfile contains long id")
  1480. }