docker_cli_run_test.go 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  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. }
  1391. func TestHostsLinkedContainerUpdate(t *testing.T) {
  1392. tmpdir, err := ioutil.TempDir("", "docker-integration")
  1393. if err != nil {
  1394. t.Fatal(err)
  1395. }
  1396. defer os.RemoveAll(tmpdir)
  1397. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sleep", "5"))
  1398. if err != nil {
  1399. t.Fatal(err, out)
  1400. }
  1401. // TODO fix docker cp and /etc/hosts
  1402. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--link", "c1:c1", "--name", "c2", "busybox", "sh", "-c", "while true;do cp /etc/hosts /hosts; done"))
  1403. if err != nil {
  1404. t.Fatal(err, out)
  1405. }
  1406. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "cp", "c2:/hosts", tmpdir+"/1"))
  1407. if err != nil {
  1408. t.Fatal(err, out)
  1409. }
  1410. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "restart", "-t", "0", "c1"))
  1411. if err != nil {
  1412. t.Fatal(err, out)
  1413. }
  1414. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "cp", "c2:/hosts", tmpdir+"/2"))
  1415. if err != nil {
  1416. t.Fatal(err, out)
  1417. }
  1418. out, _, _, err = runCommandWithStdoutStderr(exec.Command("diff", tmpdir+"/1", tmpdir+"/2"))
  1419. if err == nil {
  1420. t.Fatalf("Expecting error, got none")
  1421. }
  1422. out = stripTrailingCharacters(out)
  1423. if out == "" {
  1424. t.Fatalf("expected /etc/hosts to be updated, but wasn't")
  1425. }
  1426. deleteAllContainers()
  1427. logDone("run - /etc/hosts updated in parent when restart")
  1428. }