docker_cli_run_test.go 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io/ioutil"
  6. "net"
  7. "os"
  8. "os/exec"
  9. "path"
  10. "path/filepath"
  11. "reflect"
  12. "regexp"
  13. "sort"
  14. "strings"
  15. "sync"
  16. "testing"
  17. "time"
  18. "github.com/docker/docker/pkg/mount"
  19. "github.com/docker/docker/pkg/networkfs/resolvconf"
  20. "github.com/kr/pty"
  21. )
  22. // "test123" should be printed by docker run
  23. func TestRunEchoStdout(t *testing.T) {
  24. runCmd := exec.Command(dockerBinary, "run", "busybox", "echo", "test123")
  25. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  26. if err != nil {
  27. t.Fatalf("failed to run container: %v, output: %q", err, out)
  28. }
  29. if out != "test123\n" {
  30. t.Errorf("container should've printed 'test123'")
  31. }
  32. deleteAllContainers()
  33. logDone("run - echo test123")
  34. }
  35. // "test" should be printed
  36. func TestRunEchoStdoutWithMemoryLimit(t *testing.T) {
  37. runCmd := exec.Command(dockerBinary, "run", "-m", "4m", "busybox", "echo", "test")
  38. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  39. if err != nil {
  40. t.Fatalf("failed to run container: %v, output: %q", err, out)
  41. }
  42. out = strings.Trim(out, "\r\n")
  43. if expected := "test"; out != expected {
  44. t.Errorf("container should've printed %q but printed %q", expected, out)
  45. }
  46. deleteAllContainers()
  47. logDone("run - echo with memory limit")
  48. }
  49. // "test" should be printed
  50. func TestRunEchoStdoutWitCPULimit(t *testing.T) {
  51. runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "busybox", "echo", "test")
  52. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  53. if err != nil {
  54. t.Fatalf("failed to run container: %v, output: %q", err, out)
  55. }
  56. if out != "test\n" {
  57. t.Errorf("container should've printed 'test'")
  58. }
  59. deleteAllContainers()
  60. logDone("run - echo with CPU limit")
  61. }
  62. // "test" should be printed
  63. func TestRunEchoStdoutWithCPUAndMemoryLimit(t *testing.T) {
  64. runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "-m", "4m", "busybox", "echo", "test")
  65. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  66. if err != nil {
  67. t.Fatalf("failed to run container: %v, output: %q", err, out)
  68. }
  69. if out != "test\n" {
  70. t.Errorf("container should've printed 'test', got %q instead", out)
  71. }
  72. deleteAllContainers()
  73. logDone("run - echo with CPU and memory limit")
  74. }
  75. // "test" should be printed
  76. func TestRunEchoNamedContainer(t *testing.T) {
  77. runCmd := exec.Command(dockerBinary, "run", "--name", "testfoonamedcontainer", "busybox", "echo", "test")
  78. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  79. if err != nil {
  80. t.Fatalf("failed to run container: %v, output: %q", err, out)
  81. }
  82. if out != "test\n" {
  83. t.Errorf("container should've printed 'test'")
  84. }
  85. if err := deleteContainer("testfoonamedcontainer"); err != nil {
  86. t.Errorf("failed to remove the named container: %v", err)
  87. }
  88. deleteAllContainers()
  89. logDone("run - echo with named container")
  90. }
  91. // docker run should not leak file descriptors
  92. func TestRunLeakyFileDescriptors(t *testing.T) {
  93. runCmd := exec.Command(dockerBinary, "run", "busybox", "ls", "-C", "/proc/self/fd")
  94. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  95. if err != nil {
  96. t.Fatalf("failed to run container: %v, output: %q", err, out)
  97. }
  98. // normally, we should only get 0, 1, and 2, but 3 gets created by "ls" when it does "opendir" on the "fd" directory
  99. if out != "0 1 2 3\n" {
  100. t.Errorf("container should've printed '0 1 2 3', not: %s", out)
  101. }
  102. deleteAllContainers()
  103. logDone("run - check file descriptor leakage")
  104. }
  105. // it should be possible to ping Google DNS resolver
  106. // this will fail when Internet access is unavailable
  107. func TestRunPingGoogle(t *testing.T) {
  108. runCmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "8.8.8.8")
  109. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  110. if err != nil {
  111. t.Fatalf("failed to run container: %v, output: %q", err, out)
  112. }
  113. errorOut(err, t, "container should've been able to ping 8.8.8.8")
  114. deleteAllContainers()
  115. logDone("run - ping 8.8.8.8")
  116. }
  117. // the exit code should be 0
  118. // some versions of lxc might make this test fail
  119. func TestRunExitCodeZero(t *testing.T) {
  120. runCmd := exec.Command(dockerBinary, "run", "busybox", "true")
  121. exitCode, err := runCommand(runCmd)
  122. errorOut(err, t, fmt.Sprintf("%s", err))
  123. if exitCode != 0 {
  124. t.Errorf("container should've exited with exit code 0")
  125. }
  126. deleteAllContainers()
  127. logDone("run - exit with 0")
  128. }
  129. // the exit code should be 1
  130. // some versions of lxc might make this test fail
  131. func TestRunExitCodeOne(t *testing.T) {
  132. runCmd := exec.Command(dockerBinary, "run", "busybox", "false")
  133. exitCode, err := runCommand(runCmd)
  134. if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
  135. t.Fatal(err)
  136. }
  137. if exitCode != 1 {
  138. t.Errorf("container should've exited with exit code 1")
  139. }
  140. deleteAllContainers()
  141. logDone("run - exit with 1")
  142. }
  143. // it should be possible to pipe in data via stdin to a process running in a container
  144. // some versions of lxc might make this test fail
  145. func TestRunStdinPipe(t *testing.T) {
  146. runCmd := exec.Command("bash", "-c", `echo "blahblah" | docker run -i -a stdin busybox cat`)
  147. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  148. if err != nil {
  149. t.Fatalf("failed to run container: %v, output: %q", err, out)
  150. }
  151. out = stripTrailingCharacters(out)
  152. inspectCmd := exec.Command(dockerBinary, "inspect", out)
  153. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  154. errorOut(err, t, fmt.Sprintf("out should've been a container id: %s %s", out, inspectOut))
  155. waitCmd := exec.Command(dockerBinary, "wait", out)
  156. _, _, err = runCommandWithOutput(waitCmd)
  157. errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))
  158. logsCmd := exec.Command(dockerBinary, "logs", out)
  159. containerLogs, _, err := runCommandWithOutput(logsCmd)
  160. errorOut(err, t, fmt.Sprintf("error thrown while trying to get container logs: %s", err))
  161. containerLogs = stripTrailingCharacters(containerLogs)
  162. if containerLogs != "blahblah" {
  163. t.Errorf("logs didn't print the container's logs %s", containerLogs)
  164. }
  165. rmCmd := exec.Command(dockerBinary, "rm", out)
  166. _, _, err = runCommandWithOutput(rmCmd)
  167. errorOut(err, t, fmt.Sprintf("rm failed to remove container %s", err))
  168. deleteAllContainers()
  169. logDone("run - pipe in with -i -a stdin")
  170. }
  171. // the container's ID should be printed when starting a container in detached mode
  172. func TestRunDetachedContainerIDPrinting(t *testing.T) {
  173. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  174. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  175. if err != nil {
  176. t.Fatalf("failed to run container: %v, output: %q", err, out)
  177. }
  178. out = stripTrailingCharacters(out)
  179. inspectCmd := exec.Command(dockerBinary, "inspect", out)
  180. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  181. errorOut(err, t, fmt.Sprintf("out should've been a container id: %s %s", out, inspectOut))
  182. waitCmd := exec.Command(dockerBinary, "wait", out)
  183. _, _, err = runCommandWithOutput(waitCmd)
  184. errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))
  185. rmCmd := exec.Command(dockerBinary, "rm", out)
  186. rmOut, _, err := runCommandWithOutput(rmCmd)
  187. errorOut(err, t, "rm failed to remove container")
  188. rmOut = stripTrailingCharacters(rmOut)
  189. if rmOut != out {
  190. t.Errorf("rm didn't print the container ID %s %s", out, rmOut)
  191. }
  192. deleteAllContainers()
  193. logDone("run - print container ID in detached mode")
  194. }
  195. // the working directory should be set correctly
  196. func TestRunWorkingDirectory(t *testing.T) {
  197. runCmd := exec.Command(dockerBinary, "run", "-w", "/root", "busybox", "pwd")
  198. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  199. if err != nil {
  200. t.Fatalf("failed to run container: %v, output: %q", err, out)
  201. }
  202. out = stripTrailingCharacters(out)
  203. if out != "/root" {
  204. t.Errorf("-w failed to set working directory")
  205. }
  206. runCmd = exec.Command(dockerBinary, "run", "--workdir", "/root", "busybox", "pwd")
  207. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  208. errorOut(err, t, out)
  209. out = stripTrailingCharacters(out)
  210. if out != "/root" {
  211. t.Errorf("--workdir failed to set working directory")
  212. }
  213. deleteAllContainers()
  214. logDone("run - run with working directory set by -w")
  215. logDone("run - run with working directory set by --workdir")
  216. }
  217. // pinging Google's DNS resolver should fail when we disable the networking
  218. func TestRunWithoutNetworking(t *testing.T) {
  219. runCmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "8.8.8.8")
  220. out, _, exitCode, err := runCommandWithStdoutStderr(runCmd)
  221. if err != nil && exitCode != 1 {
  222. t.Fatal(out, err)
  223. }
  224. if exitCode != 1 {
  225. t.Errorf("--net=none should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
  226. }
  227. runCmd = exec.Command(dockerBinary, "run", "-n=false", "busybox", "ping", "-c", "1", "8.8.8.8")
  228. out, _, exitCode, err = runCommandWithStdoutStderr(runCmd)
  229. if err != nil && exitCode != 1 {
  230. t.Fatal(out, err)
  231. }
  232. if exitCode != 1 {
  233. t.Errorf("-n=false should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
  234. }
  235. deleteAllContainers()
  236. logDone("run - disable networking with --net=none")
  237. logDone("run - disable networking with -n=false")
  238. }
  239. // Regression test for #4741
  240. func TestRunWithVolumesAsFiles(t *testing.T) {
  241. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true")
  242. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  243. if err != nil && exitCode != 0 {
  244. t.Fatal("1", out, stderr, err)
  245. }
  246. runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/target-file")
  247. out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
  248. if err != nil && exitCode != 0 {
  249. t.Fatal("2", out, stderr, err)
  250. }
  251. deleteAllContainers()
  252. logDone("run - regression test for #4741 - volumes from as files")
  253. }
  254. // Regression test for #4979
  255. func TestRunWithVolumesFromExited(t *testing.T) {
  256. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/some/dir", "busybox", "touch", "/some/dir/file")
  257. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  258. if err != nil && exitCode != 0 {
  259. t.Fatal("1", out, stderr, err)
  260. }
  261. runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/some/dir/file")
  262. out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
  263. if err != nil && exitCode != 0 {
  264. t.Fatal("2", out, stderr, err)
  265. }
  266. deleteAllContainers()
  267. logDone("run - regression test for #4979 - volumes-from on exited container")
  268. }
  269. // Regression test for #4830
  270. func TestRunWithRelativePath(t *testing.T) {
  271. runCmd := exec.Command(dockerBinary, "run", "-v", "tmp:/other-tmp", "busybox", "true")
  272. if _, _, _, err := runCommandWithStdoutStderr(runCmd); err == nil {
  273. t.Fatalf("relative path should result in an error")
  274. }
  275. deleteAllContainers()
  276. logDone("run - volume with relative path")
  277. }
  278. func TestRunVolumesMountedAsReadonly(t *testing.T) {
  279. cmd := exec.Command(dockerBinary, "run", "-v", "/test:/test:ro", "busybox", "touch", "/test/somefile")
  280. if code, err := runCommand(cmd); err == nil || code == 0 {
  281. t.Fatalf("run should fail because volume is ro: exit code %d", code)
  282. }
  283. deleteAllContainers()
  284. logDone("run - volumes as readonly mount")
  285. }
  286. func TestRunVolumesFromInReadonlyMode(t *testing.T) {
  287. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true")
  288. if _, err := runCommand(cmd); err != nil {
  289. t.Fatal(err)
  290. }
  291. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:ro", "busybox", "touch", "/test/file")
  292. if code, err := runCommand(cmd); err == nil || code == 0 {
  293. t.Fatalf("run should fail because volume is ro: exit code %d", code)
  294. }
  295. deleteAllContainers()
  296. logDone("run - volumes from as readonly mount")
  297. }
  298. // Regression test for #1201
  299. func TestRunVolumesFromInReadWriteMode(t *testing.T) {
  300. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true")
  301. if _, err := runCommand(cmd); err != nil {
  302. t.Fatal(err)
  303. }
  304. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:rw", "busybox", "touch", "/test/file")
  305. if out, _, err := runCommandWithOutput(cmd); err != nil {
  306. t.Fatalf("running --volumes-from parent:rw failed with output: %q\nerror: %v", out, err)
  307. }
  308. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:bar", "busybox", "touch", "/test/file")
  309. if out, _, err := runCommandWithOutput(cmd); err == nil || !strings.Contains(out, "Invalid mode for volumes-from: bar") {
  310. t.Fatalf("running --volumes-from foo:bar should have failed with invalid mount mode: %q", out)
  311. }
  312. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "busybox", "touch", "/test/file")
  313. if out, _, err := runCommandWithOutput(cmd); err != nil {
  314. t.Fatalf("running --volumes-from parent failed with output: %q\nerror: %v", out, err)
  315. }
  316. deleteAllContainers()
  317. logDone("run - volumes from as read write mount")
  318. }
  319. func TestVolumesFromGetsProperMode(t *testing.T) {
  320. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test:/test:ro", "busybox", "true")
  321. if _, err := runCommand(cmd); err != nil {
  322. t.Fatal(err)
  323. }
  324. // Expect this "rw" mode to be be ignored since the inheritted volume is "ro"
  325. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:rw", "busybox", "touch", "/test/file")
  326. if _, err := runCommand(cmd); err == nil {
  327. t.Fatal("Expected volumes-from to inherit read-only volume even when passing in `rw`")
  328. }
  329. cmd = exec.Command(dockerBinary, "run", "--name", "parent2", "-v", "/test:/test:ro", "busybox", "true")
  330. if _, err := runCommand(cmd); err != nil {
  331. t.Fatal(err)
  332. }
  333. // Expect this to be read-only since both are "ro"
  334. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent2:ro", "busybox", "touch", "/test/file")
  335. if _, err := runCommand(cmd); err == nil {
  336. t.Fatal("Expected volumes-from to inherit read-only volume even when passing in `ro`")
  337. }
  338. deleteAllContainers()
  339. logDone("run - volumes from ignores `rw` if inherrited volume is `ro`")
  340. }
  341. // Test for #1351
  342. func TestRunApplyVolumesFromBeforeVolumes(t *testing.T) {
  343. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "touch", "/test/foo")
  344. if _, err := runCommand(cmd); err != nil {
  345. t.Fatal(err)
  346. }
  347. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "-v", "/test", "busybox", "cat", "/test/foo")
  348. if out, _, err := runCommandWithOutput(cmd); err != nil {
  349. t.Fatal(out, err)
  350. }
  351. deleteAllContainers()
  352. logDone("run - volumes from mounted first")
  353. }
  354. func TestRunMultipleVolumesFrom(t *testing.T) {
  355. cmd := exec.Command(dockerBinary, "run", "--name", "parent1", "-v", "/test", "busybox", "touch", "/test/foo")
  356. if _, err := runCommand(cmd); err != nil {
  357. t.Fatal(err)
  358. }
  359. cmd = exec.Command(dockerBinary, "run", "--name", "parent2", "-v", "/other", "busybox", "touch", "/other/bar")
  360. if _, err := runCommand(cmd); err != nil {
  361. t.Fatal(err)
  362. }
  363. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent1", "--volumes-from", "parent2",
  364. "busybox", "sh", "-c", "cat /test/foo && cat /other/bar")
  365. if _, err := runCommand(cmd); err != nil {
  366. t.Fatal(err)
  367. }
  368. deleteAllContainers()
  369. logDone("run - multiple volumes from")
  370. }
  371. // this tests verifies the ID format for the container
  372. func TestRunVerifyContainerID(t *testing.T) {
  373. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  374. out, exit, err := runCommandWithOutput(cmd)
  375. if err != nil {
  376. t.Fatal(err)
  377. }
  378. if exit != 0 {
  379. t.Fatalf("expected exit code 0 received %d", exit)
  380. }
  381. match, err := regexp.MatchString("^[0-9a-f]{64}$", strings.TrimSuffix(out, "\n"))
  382. if err != nil {
  383. t.Fatal(err)
  384. }
  385. if !match {
  386. t.Fatalf("Invalid container ID: %s", out)
  387. }
  388. deleteAllContainers()
  389. logDone("run - verify container ID")
  390. }
  391. // Test that creating a container with a volume doesn't crash. Regression test for #995.
  392. func TestRunCreateVolume(t *testing.T) {
  393. cmd := exec.Command(dockerBinary, "run", "-v", "/var/lib/data", "busybox", "true")
  394. if _, err := runCommand(cmd); err != nil {
  395. t.Fatal(err)
  396. }
  397. deleteAllContainers()
  398. logDone("run - create docker managed volume")
  399. }
  400. // Test that creating a volume with a symlink in its path works correctly. Test for #5152.
  401. // Note that this bug happens only with symlinks with a target that starts with '/'.
  402. func TestRunCreateVolumeWithSymlink(t *testing.T) {
  403. buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-createvolumewithsymlink", "-")
  404. buildCmd.Stdin = strings.NewReader(`FROM busybox
  405. RUN mkdir /foo && ln -s /foo /bar`)
  406. buildCmd.Dir = workingDirectory
  407. err := buildCmd.Run()
  408. if err != nil {
  409. t.Fatalf("could not build 'docker-test-createvolumewithsymlink': %v", err)
  410. }
  411. cmd := exec.Command(dockerBinary, "run", "-v", "/bar/foo", "--name", "test-createvolumewithsymlink", "docker-test-createvolumewithsymlink", "sh", "-c", "mount | grep -q /foo/foo")
  412. exitCode, err := runCommand(cmd)
  413. if err != nil || exitCode != 0 {
  414. t.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
  415. }
  416. var volPath string
  417. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{range .Volumes}}{{.}}{{end}}", "test-createvolumewithsymlink")
  418. volPath, exitCode, err = runCommandWithOutput(cmd)
  419. if err != nil || exitCode != 0 {
  420. t.Fatalf("[inspect] err: %v, exitcode: %d", err, exitCode)
  421. }
  422. cmd = exec.Command(dockerBinary, "rm", "-v", "test-createvolumewithsymlink")
  423. exitCode, err = runCommand(cmd)
  424. if err != nil || exitCode != 0 {
  425. t.Fatalf("[rm] err: %v, exitcode: %d", err, exitCode)
  426. }
  427. f, err := os.Open(volPath)
  428. defer f.Close()
  429. if !os.IsNotExist(err) {
  430. t.Fatalf("[open] (expecting 'file does not exist' error) err: %v, volPath: %s", err, volPath)
  431. }
  432. deleteImages("docker-test-createvolumewithsymlink")
  433. deleteAllContainers()
  434. logDone("run - create volume with symlink")
  435. }
  436. // Tests that a volume path that has a symlink exists in a container mounting it with `--volumes-from`.
  437. func TestRunVolumesFromSymlinkPath(t *testing.T) {
  438. buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-volumesfromsymlinkpath", "-")
  439. buildCmd.Stdin = strings.NewReader(`FROM busybox
  440. RUN mkdir /baz && ln -s /baz /foo
  441. VOLUME ["/foo/bar"]`)
  442. buildCmd.Dir = workingDirectory
  443. err := buildCmd.Run()
  444. if err != nil {
  445. t.Fatalf("could not build 'docker-test-volumesfromsymlinkpath': %v", err)
  446. }
  447. cmd := exec.Command(dockerBinary, "run", "--name", "test-volumesfromsymlinkpath", "docker-test-volumesfromsymlinkpath")
  448. exitCode, err := runCommand(cmd)
  449. if err != nil || exitCode != 0 {
  450. t.Fatalf("[run] (volume) err: %v, exitcode: %d", err, exitCode)
  451. }
  452. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-volumesfromsymlinkpath", "busybox", "sh", "-c", "ls /foo | grep -q bar")
  453. exitCode, err = runCommand(cmd)
  454. if err != nil || exitCode != 0 {
  455. t.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
  456. }
  457. deleteImages("docker-test-volumesfromsymlinkpath")
  458. deleteAllContainers()
  459. logDone("run - volumes-from symlink path")
  460. }
  461. func TestRunExitCode(t *testing.T) {
  462. cmd := exec.Command(dockerBinary, "run", "busybox", "/bin/sh", "-c", "exit 72")
  463. exit, err := runCommand(cmd)
  464. if err == nil {
  465. t.Fatal("should not have a non nil error")
  466. }
  467. if exit != 72 {
  468. t.Fatalf("expected exit code 72 received %d", exit)
  469. }
  470. deleteAllContainers()
  471. logDone("run - correct exit code")
  472. }
  473. func TestRunUserDefaultsToRoot(t *testing.T) {
  474. cmd := exec.Command(dockerBinary, "run", "busybox", "id")
  475. out, _, err := runCommandWithOutput(cmd)
  476. if err != nil {
  477. t.Fatal(err, out)
  478. }
  479. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  480. t.Fatalf("expected root user got %s", out)
  481. }
  482. deleteAllContainers()
  483. logDone("run - default user")
  484. }
  485. func TestRunUserByName(t *testing.T) {
  486. cmd := exec.Command(dockerBinary, "run", "-u", "root", "busybox", "id")
  487. out, _, err := runCommandWithOutput(cmd)
  488. if err != nil {
  489. t.Fatal(err, out)
  490. }
  491. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  492. t.Fatalf("expected root user got %s", out)
  493. }
  494. deleteAllContainers()
  495. logDone("run - user by name")
  496. }
  497. func TestRunUserByID(t *testing.T) {
  498. cmd := exec.Command(dockerBinary, "run", "-u", "1", "busybox", "id")
  499. out, _, err := runCommandWithOutput(cmd)
  500. if err != nil {
  501. t.Fatal(err, out)
  502. }
  503. if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
  504. t.Fatalf("expected daemon user got %s", out)
  505. }
  506. deleteAllContainers()
  507. logDone("run - user by id")
  508. }
  509. func TestRunUserByIDBig(t *testing.T) {
  510. cmd := exec.Command(dockerBinary, "run", "-u", "2147483648", "busybox", "id")
  511. out, _, err := runCommandWithOutput(cmd)
  512. if err == nil {
  513. t.Fatal("No error, but must be.", out)
  514. }
  515. if !strings.Contains(out, "Uids and gids must be in range") {
  516. t.Fatalf("expected error about uids range, got %s", out)
  517. }
  518. deleteAllContainers()
  519. logDone("run - user by id, id too big")
  520. }
  521. func TestRunUserByIDNegative(t *testing.T) {
  522. cmd := exec.Command(dockerBinary, "run", "-u", "-1", "busybox", "id")
  523. out, _, err := runCommandWithOutput(cmd)
  524. if err == nil {
  525. t.Fatal("No error, but must be.", out)
  526. }
  527. if !strings.Contains(out, "Uids and gids must be in range") {
  528. t.Fatalf("expected error about uids range, got %s", out)
  529. }
  530. deleteAllContainers()
  531. logDone("run - user by id, id negative")
  532. }
  533. func TestRunUserByIDZero(t *testing.T) {
  534. cmd := exec.Command(dockerBinary, "run", "-u", "0", "busybox", "id")
  535. out, _, err := runCommandWithOutput(cmd)
  536. if err != nil {
  537. t.Fatal(err, out)
  538. }
  539. if !strings.Contains(out, "uid=0(root) gid=0(root) groups=10(wheel)") {
  540. t.Fatalf("expected daemon user got %s", out)
  541. }
  542. deleteAllContainers()
  543. logDone("run - user by id, zero uid")
  544. }
  545. func TestRunUserNotFound(t *testing.T) {
  546. cmd := exec.Command(dockerBinary, "run", "-u", "notme", "busybox", "id")
  547. _, err := runCommand(cmd)
  548. if err == nil {
  549. t.Fatal("unknown user should cause container to fail")
  550. }
  551. deleteAllContainers()
  552. logDone("run - user not found")
  553. }
  554. func TestRunTwoConcurrentContainers(t *testing.T) {
  555. group := sync.WaitGroup{}
  556. group.Add(2)
  557. for i := 0; i < 2; i++ {
  558. go func() {
  559. defer group.Done()
  560. cmd := exec.Command(dockerBinary, "run", "busybox", "sleep", "2")
  561. if _, err := runCommand(cmd); err != nil {
  562. t.Fatal(err)
  563. }
  564. }()
  565. }
  566. group.Wait()
  567. deleteAllContainers()
  568. logDone("run - two concurrent containers")
  569. }
  570. func TestRunEnvironment(t *testing.T) {
  571. cmd := exec.Command(dockerBinary, "run", "-h", "testing", "-e=FALSE=true", "-e=TRUE", "-e=TRICKY", "-e=HOME=", "busybox", "env")
  572. cmd.Env = append(os.Environ(),
  573. "TRUE=false",
  574. "TRICKY=tri\ncky\n",
  575. )
  576. out, _, err := runCommandWithOutput(cmd)
  577. if err != nil {
  578. t.Fatal(err, out)
  579. }
  580. actualEnv := strings.Split(out, "\n")
  581. if actualEnv[len(actualEnv)-1] == "" {
  582. actualEnv = actualEnv[:len(actualEnv)-1]
  583. }
  584. sort.Strings(actualEnv)
  585. goodEnv := []string{
  586. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  587. "HOSTNAME=testing",
  588. "FALSE=true",
  589. "TRUE=false",
  590. "TRICKY=tri",
  591. "cky",
  592. "",
  593. "HOME=/root",
  594. }
  595. sort.Strings(goodEnv)
  596. if len(goodEnv) != len(actualEnv) {
  597. t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
  598. }
  599. for i := range goodEnv {
  600. if actualEnv[i] != goodEnv[i] {
  601. t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  602. }
  603. }
  604. deleteAllContainers()
  605. logDone("run - verify environment")
  606. }
  607. func TestRunContainerNetwork(t *testing.T) {
  608. cmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "127.0.0.1")
  609. if _, err := runCommand(cmd); err != nil {
  610. t.Fatal(err)
  611. }
  612. deleteAllContainers()
  613. logDone("run - test container network via ping")
  614. }
  615. // Issue #4681
  616. func TestRunLoopbackWhenNetworkDisabled(t *testing.T) {
  617. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1")
  618. if _, err := runCommand(cmd); err != nil {
  619. t.Fatal(err)
  620. }
  621. deleteAllContainers()
  622. logDone("run - test container loopback when networking disabled")
  623. }
  624. func TestRunNetHostNotAllowedWithLinks(t *testing.T) {
  625. _, _, err := cmd(t, "run", "--name", "linked", "busybox", "true")
  626. cmd := exec.Command(dockerBinary, "run", "--net=host", "--link", "linked:linked", "busybox", "true")
  627. _, _, err = runCommandWithOutput(cmd)
  628. if err == nil {
  629. t.Fatal("Expected error")
  630. }
  631. deleteAllContainers()
  632. logDone("run - don't allow --net=host to be used with links")
  633. }
  634. func TestRunLoopbackOnlyExistsWhenNetworkingDisabled(t *testing.T) {
  635. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up")
  636. out, _, err := runCommandWithOutput(cmd)
  637. if err != nil {
  638. t.Fatal(err, out)
  639. }
  640. var (
  641. count = 0
  642. parts = strings.Split(out, "\n")
  643. )
  644. for _, l := range parts {
  645. if l != "" {
  646. count++
  647. }
  648. }
  649. if count != 1 {
  650. t.Fatalf("Wrong interface count in container %d", count)
  651. }
  652. if !strings.HasPrefix(out, "1: lo") {
  653. t.Fatalf("Wrong interface in test container: expected [1: lo], got %s", out)
  654. }
  655. deleteAllContainers()
  656. logDone("run - test loopback only exists when networking disabled")
  657. }
  658. // #7851 hostname outside container shows FQDN, inside only shortname
  659. // For testing purposes it is not required to set host's hostname directly
  660. // and use "--net=host" (as the original issue submitter did), as the same
  661. // codepath is executed with "docker run -h <hostname>". Both were manually
  662. // tested, but this testcase takes the simpler path of using "run -h .."
  663. func TestRunFullHostnameSet(t *testing.T) {
  664. cmd := exec.Command(dockerBinary, "run", "-h", "foo.bar.baz", "busybox", "hostname")
  665. out, _, err := runCommandWithOutput(cmd)
  666. if err != nil {
  667. t.Fatal(err, out)
  668. }
  669. if actual := strings.Trim(out, "\r\n"); actual != "foo.bar.baz" {
  670. t.Fatalf("expected hostname 'foo.bar.baz', received %s", actual)
  671. }
  672. deleteAllContainers()
  673. logDone("run - test fully qualified hostname set with -h")
  674. }
  675. func TestRunPrivilegedCanMknod(t *testing.T) {
  676. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  677. out, _, err := runCommandWithOutput(cmd)
  678. if err != nil {
  679. t.Fatal(err)
  680. }
  681. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  682. t.Fatalf("expected output ok received %s", actual)
  683. }
  684. deleteAllContainers()
  685. logDone("run - test privileged can mknod")
  686. }
  687. func TestRunUnPrivilegedCanMknod(t *testing.T) {
  688. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  689. out, _, err := runCommandWithOutput(cmd)
  690. if err != nil {
  691. t.Fatal(err)
  692. }
  693. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  694. t.Fatalf("expected output ok received %s", actual)
  695. }
  696. deleteAllContainers()
  697. logDone("run - test un-privileged can mknod")
  698. }
  699. func TestRunCapDropInvalid(t *testing.T) {
  700. cmd := exec.Command(dockerBinary, "run", "--cap-drop=CHPASS", "busybox", "ls")
  701. out, _, err := runCommandWithOutput(cmd)
  702. if err == nil {
  703. t.Fatal(err, out)
  704. }
  705. logDone("run - test --cap-drop=CHPASS invalid")
  706. }
  707. func TestRunCapDropCannotMknod(t *testing.T) {
  708. cmd := exec.Command(dockerBinary, "run", "--cap-drop=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  709. out, _, err := runCommandWithOutput(cmd)
  710. if err == nil {
  711. t.Fatal(err, out)
  712. }
  713. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  714. t.Fatalf("expected output not ok received %s", actual)
  715. }
  716. deleteAllContainers()
  717. logDone("run - test --cap-drop=MKNOD cannot mknod")
  718. }
  719. func TestRunCapDropCannotMknodLowerCase(t *testing.T) {
  720. cmd := exec.Command(dockerBinary, "run", "--cap-drop=mknod", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  721. out, _, err := runCommandWithOutput(cmd)
  722. if err == nil {
  723. t.Fatal(err, out)
  724. }
  725. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  726. t.Fatalf("expected output not ok received %s", actual)
  727. }
  728. deleteAllContainers()
  729. logDone("run - test --cap-drop=mknod cannot mknod lowercase")
  730. }
  731. func TestRunCapDropALLCannotMknod(t *testing.T) {
  732. cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  733. out, _, err := runCommandWithOutput(cmd)
  734. if err == nil {
  735. t.Fatal(err, out)
  736. }
  737. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  738. t.Fatalf("expected output not ok received %s", actual)
  739. }
  740. deleteAllContainers()
  741. logDone("run - test --cap-drop=ALL cannot mknod")
  742. }
  743. func TestRunCapDropALLAddMknodCannotMknod(t *testing.T) {
  744. cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "--cap-add=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  745. out, _, err := runCommandWithOutput(cmd)
  746. if err != nil {
  747. t.Fatal(err, out)
  748. }
  749. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  750. t.Fatalf("expected output ok received %s", actual)
  751. }
  752. deleteAllContainers()
  753. logDone("run - test --cap-drop=ALL --cap-add=MKNOD can mknod")
  754. }
  755. func TestRunCapAddInvalid(t *testing.T) {
  756. cmd := exec.Command(dockerBinary, "run", "--cap-add=CHPASS", "busybox", "ls")
  757. out, _, err := runCommandWithOutput(cmd)
  758. if err == nil {
  759. t.Fatal(err, out)
  760. }
  761. logDone("run - test --cap-add=CHPASS invalid")
  762. }
  763. func TestRunCapAddCanDownInterface(t *testing.T) {
  764. cmd := exec.Command(dockerBinary, "run", "--cap-add=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  765. out, _, err := runCommandWithOutput(cmd)
  766. if err != nil {
  767. t.Fatal(err, out)
  768. }
  769. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  770. t.Fatalf("expected output ok received %s", actual)
  771. }
  772. deleteAllContainers()
  773. logDone("run - test --cap-add=NET_ADMIN can set eth0 down")
  774. }
  775. func TestRunCapAddALLCanDownInterface(t *testing.T) {
  776. cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  777. out, _, err := runCommandWithOutput(cmd)
  778. if err != nil {
  779. t.Fatal(err, out)
  780. }
  781. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  782. t.Fatalf("expected output ok received %s", actual)
  783. }
  784. deleteAllContainers()
  785. logDone("run - test --cap-add=ALL can set eth0 down")
  786. }
  787. func TestRunCapAddALLDropNetAdminCanDownInterface(t *testing.T) {
  788. cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "--cap-drop=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  789. out, _, err := runCommandWithOutput(cmd)
  790. if err == nil {
  791. t.Fatal(err, out)
  792. }
  793. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  794. t.Fatalf("expected output not ok received %s", actual)
  795. }
  796. deleteAllContainers()
  797. logDone("run - test --cap-add=ALL --cap-drop=NET_ADMIN cannot set eth0 down")
  798. }
  799. func TestRunPrivilegedCanMount(t *testing.T) {
  800. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  801. out, _, err := runCommandWithOutput(cmd)
  802. if err != nil {
  803. t.Fatal(err)
  804. }
  805. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  806. t.Fatalf("expected output ok received %s", actual)
  807. }
  808. deleteAllContainers()
  809. logDone("run - test privileged can mount")
  810. }
  811. func TestRunUnPrivilegedCannotMount(t *testing.T) {
  812. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  813. out, _, err := runCommandWithOutput(cmd)
  814. if err == nil {
  815. t.Fatal(err, out)
  816. }
  817. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  818. t.Fatalf("expected output not ok received %s", actual)
  819. }
  820. deleteAllContainers()
  821. logDone("run - test un-privileged cannot mount")
  822. }
  823. func TestRunSysNotWritableInNonPrivilegedContainers(t *testing.T) {
  824. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/sys/kernel/profiling")
  825. if code, err := runCommand(cmd); err == nil || code == 0 {
  826. t.Fatal("sys should not be writable in a non privileged container")
  827. }
  828. deleteAllContainers()
  829. logDone("run - sys not writable in non privileged container")
  830. }
  831. func TestRunSysWritableInPrivilegedContainers(t *testing.T) {
  832. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/sys/kernel/profiling")
  833. if code, err := runCommand(cmd); err != nil || code != 0 {
  834. t.Fatalf("sys should be writable in privileged container")
  835. }
  836. deleteAllContainers()
  837. logDone("run - sys writable in privileged container")
  838. }
  839. func TestRunProcNotWritableInNonPrivilegedContainers(t *testing.T) {
  840. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/proc/sysrq-trigger")
  841. if code, err := runCommand(cmd); err == nil || code == 0 {
  842. t.Fatal("proc should not be writable in a non privileged container")
  843. }
  844. deleteAllContainers()
  845. logDone("run - proc not writable in non privileged container")
  846. }
  847. func TestRunProcWritableInPrivilegedContainers(t *testing.T) {
  848. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/proc/sysrq-trigger")
  849. if code, err := runCommand(cmd); err != nil || code != 0 {
  850. t.Fatalf("proc should be writable in privileged container")
  851. }
  852. deleteAllContainers()
  853. logDone("run - proc writable in privileged container")
  854. }
  855. func TestRunWithCpuset(t *testing.T) {
  856. cmd := exec.Command(dockerBinary, "run", "--cpuset", "0", "busybox", "true")
  857. if code, err := runCommand(cmd); err != nil || code != 0 {
  858. t.Fatalf("container should run successfuly with cpuset of 0: %s", err)
  859. }
  860. deleteAllContainers()
  861. logDone("run - cpuset 0")
  862. }
  863. func TestRunDeviceNumbers(t *testing.T) {
  864. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "ls -l /dev/null")
  865. out, _, err := runCommandWithOutput(cmd)
  866. if err != nil {
  867. t.Fatal(err, out)
  868. }
  869. deviceLineFields := strings.Fields(out)
  870. deviceLineFields[6] = ""
  871. deviceLineFields[7] = ""
  872. deviceLineFields[8] = ""
  873. expected := []string{"crw-rw-rw-", "1", "root", "root", "1,", "3", "", "", "", "/dev/null"}
  874. if !(reflect.DeepEqual(deviceLineFields, expected)) {
  875. t.Fatalf("expected output\ncrw-rw-rw- 1 root root 1, 3 May 24 13:29 /dev/null\n received\n %s\n", out)
  876. }
  877. deleteAllContainers()
  878. logDone("run - test device numbers")
  879. }
  880. func TestRunThatCharacterDevicesActLikeCharacterDevices(t *testing.T) {
  881. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "dd if=/dev/zero of=/zero bs=1k count=5 2> /dev/null ; du -h /zero")
  882. out, _, err := runCommandWithOutput(cmd)
  883. if err != nil {
  884. t.Fatal(err, out)
  885. }
  886. if actual := strings.Trim(out, "\r\n"); actual[0] == '0' {
  887. t.Fatalf("expected a new file called /zero to be create that is greater than 0 bytes long, but du says: %s", actual)
  888. }
  889. deleteAllContainers()
  890. logDone("run - test that character devices work.")
  891. }
  892. func TestRunUnprivilegedWithChroot(t *testing.T) {
  893. cmd := exec.Command(dockerBinary, "run", "busybox", "chroot", "/", "true")
  894. if _, err := runCommand(cmd); err != nil {
  895. t.Fatal(err)
  896. }
  897. deleteAllContainers()
  898. logDone("run - unprivileged with chroot")
  899. }
  900. func TestRunAddingOptionalDevices(t *testing.T) {
  901. cmd := exec.Command(dockerBinary, "run", "--device", "/dev/zero:/dev/nulo", "busybox", "sh", "-c", "ls /dev/nulo")
  902. out, _, err := runCommandWithOutput(cmd)
  903. if err != nil {
  904. t.Fatal(err, out)
  905. }
  906. if actual := strings.Trim(out, "\r\n"); actual != "/dev/nulo" {
  907. t.Fatalf("expected output /dev/nulo, received %s", actual)
  908. }
  909. deleteAllContainers()
  910. logDone("run - test --device argument")
  911. }
  912. func TestRunModeHostname(t *testing.T) {
  913. cmd := exec.Command(dockerBinary, "run", "-h=testhostname", "busybox", "cat", "/etc/hostname")
  914. out, _, err := runCommandWithOutput(cmd)
  915. if err != nil {
  916. t.Fatal(err, out)
  917. }
  918. if actual := strings.Trim(out, "\r\n"); actual != "testhostname" {
  919. t.Fatalf("expected 'testhostname', but says: '%s'", actual)
  920. }
  921. cmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hostname")
  922. out, _, err = runCommandWithOutput(cmd)
  923. if err != nil {
  924. t.Fatal(err, out)
  925. }
  926. hostname, err := os.Hostname()
  927. if err != nil {
  928. t.Fatal(err)
  929. }
  930. if actual := strings.Trim(out, "\r\n"); actual != hostname {
  931. t.Fatalf("expected '%s', but says: '%s'", hostname, actual)
  932. }
  933. deleteAllContainers()
  934. logDone("run - hostname and several network modes")
  935. }
  936. func TestRunRootWorkdir(t *testing.T) {
  937. s, _, err := cmd(t, "run", "--workdir", "/", "busybox", "pwd")
  938. if err != nil {
  939. t.Fatal(s, err)
  940. }
  941. if s != "/\n" {
  942. t.Fatalf("pwd returned '%s' (expected /\\n)", s)
  943. }
  944. deleteAllContainers()
  945. logDone("run - workdir /")
  946. }
  947. func TestRunAllowBindMountingRoot(t *testing.T) {
  948. s, _, err := cmd(t, "run", "-v", "/:/host", "busybox", "ls", "/host")
  949. if err != nil {
  950. t.Fatal(s, err)
  951. }
  952. deleteAllContainers()
  953. logDone("run - bind mount / as volume")
  954. }
  955. func TestRunDisallowBindMountingRootToRoot(t *testing.T) {
  956. cmd := exec.Command(dockerBinary, "run", "-v", "/:/", "busybox", "ls", "/host")
  957. out, _, err := runCommandWithOutput(cmd)
  958. if err == nil {
  959. t.Fatal(out, err)
  960. }
  961. deleteAllContainers()
  962. logDone("run - bind mount /:/ as volume should fail")
  963. }
  964. // Test recursive bind mount works by default
  965. func TestRunWithVolumesIsRecursive(t *testing.T) {
  966. tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
  967. if err != nil {
  968. t.Fatal(err)
  969. }
  970. defer os.RemoveAll(tmpDir)
  971. // Create a temporary tmpfs mount.
  972. tmpfsDir := filepath.Join(tmpDir, "tmpfs")
  973. if err := os.MkdirAll(tmpfsDir, 0777); err != nil {
  974. t.Fatalf("failed to mkdir at %s - %s", tmpfsDir, err)
  975. }
  976. if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
  977. t.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
  978. }
  979. f, err := ioutil.TempFile(tmpfsDir, "touch-me")
  980. if err != nil {
  981. t.Fatal(err)
  982. }
  983. defer f.Close()
  984. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
  985. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  986. if err != nil && exitCode != 0 {
  987. t.Fatal(out, stderr, err)
  988. }
  989. if !strings.Contains(out, filepath.Base(f.Name())) {
  990. t.Fatal("Recursive bind mount test failed. Expected file not found")
  991. }
  992. deleteAllContainers()
  993. logDone("run - volumes are bind mounted recursively")
  994. }
  995. func TestRunDnsDefaultOptions(t *testing.T) {
  996. cmd := exec.Command(dockerBinary, "run", "busybox", "cat", "/etc/resolv.conf")
  997. actual, _, err := runCommandWithOutput(cmd)
  998. if err != nil {
  999. t.Fatal(err, actual)
  1000. }
  1001. resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
  1002. if os.IsNotExist(err) {
  1003. t.Fatalf("/etc/resolv.conf does not exist")
  1004. }
  1005. if actual != string(resolvConf) {
  1006. t.Fatalf("expected resolv.conf is not the same of actual")
  1007. }
  1008. deleteAllContainers()
  1009. logDone("run - dns default options")
  1010. }
  1011. func TestRunDnsOptions(t *testing.T) {
  1012. cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
  1013. out, _, err := runCommandWithOutput(cmd)
  1014. if err != nil {
  1015. t.Fatal(err, out)
  1016. }
  1017. actual := strings.Replace(strings.Trim(out, "\r\n"), "\n", " ", -1)
  1018. if actual != "nameserver 127.0.0.1 search mydomain" {
  1019. t.Fatalf("expected 'nameserver 127.0.0.1 search mydomain', but says: '%s'", actual)
  1020. }
  1021. cmd = exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=.", "busybox", "cat", "/etc/resolv.conf")
  1022. out, _, err = runCommandWithOutput(cmd)
  1023. if err != nil {
  1024. t.Fatal(err, out)
  1025. }
  1026. actual = strings.Replace(strings.Trim(strings.Trim(out, "\r\n"), " "), "\n", " ", -1)
  1027. if actual != "nameserver 127.0.0.1" {
  1028. t.Fatalf("expected 'nameserver 127.0.0.1', but says: '%s'", actual)
  1029. }
  1030. logDone("run - dns options")
  1031. }
  1032. func TestRunDnsOptionsBasedOnHostResolvConf(t *testing.T) {
  1033. resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
  1034. if os.IsNotExist(err) {
  1035. t.Fatalf("/etc/resolv.conf does not exist")
  1036. }
  1037. hostNamservers := resolvconf.GetNameservers(resolvConf)
  1038. hostSearch := resolvconf.GetSearchDomains(resolvConf)
  1039. cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "busybox", "cat", "/etc/resolv.conf")
  1040. out, _, err := runCommandWithOutput(cmd)
  1041. if err != nil {
  1042. t.Fatal(err, out)
  1043. }
  1044. if actualNameservers := resolvconf.GetNameservers([]byte(out)); string(actualNameservers[0]) != "127.0.0.1" {
  1045. t.Fatalf("expected '127.0.0.1', but says: '%s'", string(actualNameservers[0]))
  1046. }
  1047. actualSearch := resolvconf.GetSearchDomains([]byte(out))
  1048. if len(actualSearch) != len(hostSearch) {
  1049. t.Fatalf("expected '%s' search domain(s), but it has: '%s'", len(hostSearch), len(actualSearch))
  1050. }
  1051. for i := range actualSearch {
  1052. if actualSearch[i] != hostSearch[i] {
  1053. t.Fatalf("expected '%s' domain, but says: '%s'", actualSearch[i], hostSearch[i])
  1054. }
  1055. }
  1056. cmd = exec.Command(dockerBinary, "run", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
  1057. out, _, err = runCommandWithOutput(cmd)
  1058. if err != nil {
  1059. t.Fatal(err, out)
  1060. }
  1061. actualNameservers := resolvconf.GetNameservers([]byte(out))
  1062. if len(actualNameservers) != len(hostNamservers) {
  1063. t.Fatalf("expected '%s' nameserver(s), but it has: '%s'", len(hostNamservers), len(actualNameservers))
  1064. }
  1065. for i := range actualNameservers {
  1066. if actualNameservers[i] != hostNamservers[i] {
  1067. t.Fatalf("expected '%s' nameserver, but says: '%s'", actualNameservers[i], hostNamservers[i])
  1068. }
  1069. }
  1070. if actualSearch = resolvconf.GetSearchDomains([]byte(out)); string(actualSearch[0]) != "mydomain" {
  1071. t.Fatalf("expected 'mydomain', but says: '%s'", string(actualSearch[0]))
  1072. }
  1073. deleteAllContainers()
  1074. logDone("run - dns options based on host resolv.conf")
  1075. }
  1076. func TestRunAddHost(t *testing.T) {
  1077. defer deleteAllContainers()
  1078. cmd := exec.Command(dockerBinary, "run", "--add-host=extra:86.75.30.9", "busybox", "grep", "extra", "/etc/hosts")
  1079. out, _, err := runCommandWithOutput(cmd)
  1080. if err != nil {
  1081. t.Fatal(err, out)
  1082. }
  1083. actual := strings.Trim(out, "\r\n")
  1084. if actual != "86.75.30.9\textra" {
  1085. t.Fatalf("expected '86.75.30.9\textra', but says: '%s'", actual)
  1086. }
  1087. logDone("run - add-host option")
  1088. }
  1089. // Regression test for #6983
  1090. func TestRunAttachStdErrOnlyTTYMode(t *testing.T) {
  1091. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stderr", "busybox", "true")
  1092. exitCode, err := runCommand(cmd)
  1093. if err != nil {
  1094. t.Fatal(err)
  1095. } else if exitCode != 0 {
  1096. t.Fatalf("Container should have exited with error code 0")
  1097. }
  1098. deleteAllContainers()
  1099. logDone("run - Attach stderr only with -t")
  1100. }
  1101. // Regression test for #6983
  1102. func TestRunAttachStdOutOnlyTTYMode(t *testing.T) {
  1103. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "busybox", "true")
  1104. exitCode, err := runCommand(cmd)
  1105. if err != nil {
  1106. t.Fatal(err)
  1107. } else if exitCode != 0 {
  1108. t.Fatalf("Container should have exited with error code 0")
  1109. }
  1110. deleteAllContainers()
  1111. logDone("run - Attach stdout only with -t")
  1112. }
  1113. // Regression test for #6983
  1114. func TestRunAttachStdOutAndErrTTYMode(t *testing.T) {
  1115. cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "-a", "stderr", "busybox", "true")
  1116. exitCode, err := runCommand(cmd)
  1117. if err != nil {
  1118. t.Fatal(err)
  1119. } else if exitCode != 0 {
  1120. t.Fatalf("Container should have exited with error code 0")
  1121. }
  1122. deleteAllContainers()
  1123. logDone("run - Attach stderr and stdout with -t")
  1124. }
  1125. func TestRunState(t *testing.T) {
  1126. defer deleteAllContainers()
  1127. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  1128. out, _, err := runCommandWithOutput(cmd)
  1129. if err != nil {
  1130. t.Fatal(err, out)
  1131. }
  1132. id := strings.TrimSpace(out)
  1133. state, err := inspectField(id, "State.Running")
  1134. if err != nil {
  1135. t.Fatal(err)
  1136. }
  1137. if state != "true" {
  1138. t.Fatal("Container state is 'not running'")
  1139. }
  1140. pid1, err := inspectField(id, "State.Pid")
  1141. if err != nil {
  1142. t.Fatal(err)
  1143. }
  1144. if pid1 == "0" {
  1145. t.Fatal("Container state Pid 0")
  1146. }
  1147. cmd = exec.Command(dockerBinary, "stop", id)
  1148. out, _, err = runCommandWithOutput(cmd)
  1149. if err != nil {
  1150. t.Fatal(err, out)
  1151. }
  1152. state, err = inspectField(id, "State.Running")
  1153. if err != nil {
  1154. t.Fatal(err)
  1155. }
  1156. if state != "false" {
  1157. t.Fatal("Container state is 'running'")
  1158. }
  1159. pid2, err := inspectField(id, "State.Pid")
  1160. if err != nil {
  1161. t.Fatal(err)
  1162. }
  1163. if pid2 == pid1 {
  1164. t.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
  1165. }
  1166. cmd = exec.Command(dockerBinary, "start", id)
  1167. out, _, err = runCommandWithOutput(cmd)
  1168. if err != nil {
  1169. t.Fatal(err, out)
  1170. }
  1171. state, err = inspectField(id, "State.Running")
  1172. if err != nil {
  1173. t.Fatal(err)
  1174. }
  1175. if state != "true" {
  1176. t.Fatal("Container state is 'not running'")
  1177. }
  1178. pid3, err := inspectField(id, "State.Pid")
  1179. if err != nil {
  1180. t.Fatal(err)
  1181. }
  1182. if pid3 == pid1 {
  1183. t.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
  1184. }
  1185. logDone("run - test container state.")
  1186. }
  1187. // Test for #1737
  1188. func TestRunCopyVolumeUidGid(t *testing.T) {
  1189. name := "testrunvolumesuidgid"
  1190. defer deleteImages(name)
  1191. defer deleteAllContainers()
  1192. _, err := buildImage(name,
  1193. `FROM busybox
  1194. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  1195. RUN echo 'dockerio:x:1001:' >> /etc/group
  1196. RUN mkdir -p /hello && touch /hello/test && chown dockerio.dockerio /hello`,
  1197. true)
  1198. if err != nil {
  1199. t.Fatal(err)
  1200. }
  1201. // Test that the uid and gid is copied from the image to the volume
  1202. cmd := exec.Command(dockerBinary, "run", "--rm", "-v", "/hello", name, "sh", "-c", "ls -l / | grep hello | awk '{print $3\":\"$4}'")
  1203. out, _, err := runCommandWithOutput(cmd)
  1204. if err != nil {
  1205. t.Fatal(err, out)
  1206. }
  1207. out = strings.TrimSpace(out)
  1208. if out != "dockerio:dockerio" {
  1209. t.Fatalf("Wrong /hello ownership: %s, expected dockerio:dockerio", out)
  1210. }
  1211. logDone("run - copy uid/gid for volume")
  1212. }
  1213. // Test for #1582
  1214. func TestRunCopyVolumeContent(t *testing.T) {
  1215. name := "testruncopyvolumecontent"
  1216. defer deleteImages(name)
  1217. defer deleteAllContainers()
  1218. _, err := buildImage(name,
  1219. `FROM busybox
  1220. RUN mkdir -p /hello/local && echo hello > /hello/local/world`,
  1221. true)
  1222. if err != nil {
  1223. t.Fatal(err)
  1224. }
  1225. // Test that the content is copied from the image to the volume
  1226. cmd := exec.Command(dockerBinary, "run", "--rm", "-v", "/hello", name, "sh", "-c", "find", "/hello")
  1227. out, _, err := runCommandWithOutput(cmd)
  1228. if err != nil {
  1229. t.Fatal(err, out)
  1230. }
  1231. if !(strings.Contains(out, "/hello/local/world") && strings.Contains(out, "/hello/local")) {
  1232. t.Fatal("Container failed to transfer content to volume")
  1233. }
  1234. logDone("run - copy volume content")
  1235. }
  1236. func TestRunCleanupCmdOnEntrypoint(t *testing.T) {
  1237. name := "testrunmdcleanuponentrypoint"
  1238. defer deleteImages(name)
  1239. defer deleteAllContainers()
  1240. if _, err := buildImage(name,
  1241. `FROM busybox
  1242. ENTRYPOINT ["echo"]
  1243. CMD ["testingpoint"]`,
  1244. true); err != nil {
  1245. t.Fatal(err)
  1246. }
  1247. runCmd := exec.Command(dockerBinary, "run", "--entrypoint", "whoami", name)
  1248. out, exit, err := runCommandWithOutput(runCmd)
  1249. if err != nil {
  1250. t.Fatalf("Error: %v, out: %q", err, out)
  1251. }
  1252. if exit != 0 {
  1253. t.Fatalf("expected exit code 0 received %d, out: %q", exit, out)
  1254. }
  1255. out = strings.TrimSpace(out)
  1256. if out != "root" {
  1257. t.Fatalf("Expected output root, got %q", out)
  1258. }
  1259. logDone("run - cleanup cmd on --entrypoint")
  1260. }
  1261. // TestRunWorkdirExistsAndIsFile checks that if 'docker run -w' with existing file can be detected
  1262. func TestRunWorkdirExistsAndIsFile(t *testing.T) {
  1263. defer deleteAllContainers()
  1264. runCmd := exec.Command(dockerBinary, "run", "-w", "/bin/cat", "busybox")
  1265. out, exit, err := runCommandWithOutput(runCmd)
  1266. if !(err != nil && exit == 1 && strings.Contains(out, "Cannot mkdir: /bin/cat is not a directory")) {
  1267. t.Fatalf("Docker must complains about making dir, but we got out: %s, exit: %d, err: %s", out, exit, err)
  1268. }
  1269. logDone("run - error on existing file for workdir")
  1270. }
  1271. func TestRunExitOnStdinClose(t *testing.T) {
  1272. name := "testrunexitonstdinclose"
  1273. defer deleteAllContainers()
  1274. runCmd := exec.Command(dockerBinary, "run", "--name", name, "-i", "busybox", "/bin/cat")
  1275. stdin, err := runCmd.StdinPipe()
  1276. if err != nil {
  1277. t.Fatal(err)
  1278. }
  1279. stdout, err := runCmd.StdoutPipe()
  1280. if err != nil {
  1281. t.Fatal(err)
  1282. }
  1283. if err := runCmd.Start(); err != nil {
  1284. t.Fatal(err)
  1285. }
  1286. if _, err := stdin.Write([]byte("hello\n")); err != nil {
  1287. t.Fatal(err)
  1288. }
  1289. r := bufio.NewReader(stdout)
  1290. line, err := r.ReadString('\n')
  1291. if err != nil {
  1292. t.Fatal(err)
  1293. }
  1294. line = strings.TrimSpace(line)
  1295. if line != "hello" {
  1296. t.Fatalf("Output should be 'hello', got '%q'", line)
  1297. }
  1298. if err := stdin.Close(); err != nil {
  1299. t.Fatal(err)
  1300. }
  1301. finish := make(chan struct{})
  1302. go func() {
  1303. if err := runCmd.Wait(); err != nil {
  1304. t.Fatal(err)
  1305. }
  1306. close(finish)
  1307. }()
  1308. select {
  1309. case <-finish:
  1310. case <-time.After(1 * time.Second):
  1311. t.Fatal("docker run failed to exit on stdin close")
  1312. }
  1313. state, err := inspectField(name, "State.Running")
  1314. if err != nil {
  1315. t.Fatal(err)
  1316. }
  1317. if state != "false" {
  1318. t.Fatal("Container must be stopped after stdin closing")
  1319. }
  1320. logDone("run - exit on stdin closing")
  1321. }
  1322. // Test for #2267
  1323. func TestRunWriteHostsFileAndNotCommit(t *testing.T) {
  1324. name := "writehosts"
  1325. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/hosts && cat /etc/hosts")
  1326. out, _, err := runCommandWithOutput(cmd)
  1327. if err != nil {
  1328. t.Fatal(err, out)
  1329. }
  1330. if !strings.Contains(out, "test2267") {
  1331. t.Fatal("/etc/hosts should contain 'test2267'")
  1332. }
  1333. cmd = exec.Command(dockerBinary, "diff", name)
  1334. if err != nil {
  1335. t.Fatal(err, out)
  1336. }
  1337. out, _, err = runCommandWithOutput(cmd)
  1338. if err != nil {
  1339. t.Fatal(err, out)
  1340. }
  1341. if len(strings.Trim(out, "\r\n")) != 0 {
  1342. t.Fatal("diff should be empty")
  1343. }
  1344. logDone("run - write to /etc/hosts and not commited")
  1345. }
  1346. // Test for #2267
  1347. func TestRunWriteHostnameFileAndNotCommit(t *testing.T) {
  1348. name := "writehostname"
  1349. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/hostname && cat /etc/hostname")
  1350. out, _, err := runCommandWithOutput(cmd)
  1351. if err != nil {
  1352. t.Fatal(err, out)
  1353. }
  1354. if !strings.Contains(out, "test2267") {
  1355. t.Fatal("/etc/hostname should contain 'test2267'")
  1356. }
  1357. cmd = exec.Command(dockerBinary, "diff", name)
  1358. if err != nil {
  1359. t.Fatal(err, out)
  1360. }
  1361. out, _, err = runCommandWithOutput(cmd)
  1362. if err != nil {
  1363. t.Fatal(err, out)
  1364. }
  1365. if len(strings.Trim(out, "\r\n")) != 0 {
  1366. t.Fatal("diff should be empty")
  1367. }
  1368. logDone("run - write to /etc/hostname and not commited")
  1369. }
  1370. // Test for #2267
  1371. func TestRunWriteResolvFileAndNotCommit(t *testing.T) {
  1372. name := "writeresolv"
  1373. cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/resolv.conf && cat /etc/resolv.conf")
  1374. out, _, err := runCommandWithOutput(cmd)
  1375. if err != nil {
  1376. t.Fatal(err, out)
  1377. }
  1378. if !strings.Contains(out, "test2267") {
  1379. t.Fatal("/etc/resolv.conf should contain 'test2267'")
  1380. }
  1381. cmd = exec.Command(dockerBinary, "diff", name)
  1382. if err != nil {
  1383. t.Fatal(err, out)
  1384. }
  1385. out, _, err = runCommandWithOutput(cmd)
  1386. if err != nil {
  1387. t.Fatal(err, out)
  1388. }
  1389. if len(strings.Trim(out, "\r\n")) != 0 {
  1390. t.Fatal("diff should be empty")
  1391. }
  1392. logDone("run - write to /etc/resolv.conf and not commited")
  1393. }
  1394. func TestRunWithBadDevice(t *testing.T) {
  1395. name := "baddevice"
  1396. cmd := exec.Command(dockerBinary, "run", "--name", name, "--device", "/etc", "busybox", "true")
  1397. out, _, err := runCommandWithOutput(cmd)
  1398. if err == nil {
  1399. t.Fatal("Run should fail with bad device")
  1400. }
  1401. expected := `"/etc": not a device node`
  1402. if !strings.Contains(out, expected) {
  1403. t.Fatalf("Output should contain %q, actual out: %q", expected, out)
  1404. }
  1405. logDone("run - error with bad device")
  1406. }
  1407. func TestRunEntrypoint(t *testing.T) {
  1408. name := "entrypoint"
  1409. cmd := exec.Command(dockerBinary, "run", "--name", name, "--entrypoint", "/bin/echo", "busybox", "-n", "foobar")
  1410. out, _, err := runCommandWithOutput(cmd)
  1411. if err != nil {
  1412. t.Fatal(err, out)
  1413. }
  1414. expected := "foobar"
  1415. if out != expected {
  1416. t.Fatalf("Output should be %q, actual out: %q", expected, out)
  1417. }
  1418. logDone("run - entrypoint")
  1419. }
  1420. func TestRunBindMounts(t *testing.T) {
  1421. tmpDir, err := ioutil.TempDir("", "docker-test-container")
  1422. if err != nil {
  1423. t.Fatal(err)
  1424. }
  1425. defer os.RemoveAll(tmpDir)
  1426. writeFile(path.Join(tmpDir, "touch-me"), "", t)
  1427. // Test reading from a read-only bind mount
  1428. cmd := exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox", "ls", "/tmp")
  1429. out, _, err := runCommandWithOutput(cmd)
  1430. if err != nil {
  1431. t.Fatal(err, out)
  1432. }
  1433. if !strings.Contains(out, "touch-me") {
  1434. t.Fatal("Container failed to read from bind mount")
  1435. }
  1436. // test writing to bind mount
  1437. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "busybox", "touch", "/tmp/holla")
  1438. out, _, err = runCommandWithOutput(cmd)
  1439. if err != nil {
  1440. t.Fatal(err, out)
  1441. }
  1442. readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1443. // test mounting to an illegal destination directory
  1444. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:.", tmpDir), "busybox", "ls", ".")
  1445. _, err = runCommand(cmd)
  1446. if err == nil {
  1447. t.Fatal("Container bind mounted illegal directory")
  1448. }
  1449. // test mount a file
  1450. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "busybox", "sh", "-c", "echo -n 'yotta' > /tmp/holla")
  1451. _, err = runCommand(cmd)
  1452. if err != nil {
  1453. t.Fatal(err, out)
  1454. }
  1455. content := readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1456. expected := "yotta"
  1457. if content != expected {
  1458. t.Fatalf("Output should be %q, actual out: %q", expected, content)
  1459. }
  1460. logDone("run - bind mounts")
  1461. }
  1462. func TestRunMutableNetworkFiles(t *testing.T) {
  1463. defer deleteAllContainers()
  1464. for _, fn := range []string{"resolv.conf", "hosts"} {
  1465. deleteAllContainers()
  1466. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s; while true; do sleep 1; done", fn)))
  1467. if err != nil {
  1468. t.Fatal(err, out)
  1469. }
  1470. time.Sleep(1 * time.Second)
  1471. contID := strings.TrimSpace(out)
  1472. f, err := os.Open(filepath.Join("/var/lib/docker/containers", contID, fn))
  1473. if err != nil {
  1474. t.Fatal(err)
  1475. }
  1476. content, err := ioutil.ReadAll(f)
  1477. f.Close()
  1478. if strings.TrimSpace(string(content)) != "success" {
  1479. t.Fatal("Content was not what was modified in the container", string(content))
  1480. }
  1481. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "c2", "busybox", "sh", "-c", fmt.Sprintf("while true; do cat /etc/%s; sleep 1; done", fn)))
  1482. if err != nil {
  1483. t.Fatal(err)
  1484. }
  1485. contID = strings.TrimSpace(out)
  1486. resolvConfPath := filepath.Join("/var/lib/docker/containers", contID, fn)
  1487. f, err = os.OpenFile(resolvConfPath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644)
  1488. if err != nil {
  1489. t.Fatal(err)
  1490. }
  1491. if _, err := f.Seek(0, 0); err != nil {
  1492. f.Close()
  1493. t.Fatal(err)
  1494. }
  1495. if err := f.Truncate(0); err != nil {
  1496. f.Close()
  1497. t.Fatal(err)
  1498. }
  1499. if _, err := f.Write([]byte("success2\n")); err != nil {
  1500. f.Close()
  1501. t.Fatal(err)
  1502. }
  1503. f.Close()
  1504. time.Sleep(2 * time.Second) // don't race sleep
  1505. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "logs", "c2"))
  1506. if err != nil {
  1507. t.Fatal(err)
  1508. }
  1509. lines := strings.Split(out, "\n")
  1510. if strings.TrimSpace(lines[len(lines)-2]) != "success2" {
  1511. t.Fatalf("Did not find the correct output in /etc/%s: %s %#v", fn, out, lines)
  1512. }
  1513. }
  1514. }
  1515. func TestRunStableIPAndPort(t *testing.T) {
  1516. const nContainers = 2
  1517. var ids, ips, macs, ports [nContainers]string
  1518. // Setup: Create a couple of containers and collect their IPs and public ports.
  1519. for i := 0; i < nContainers; i++ {
  1520. runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "1234", "busybox", "top")
  1521. out, _, err := runCommandWithOutput(runCmd)
  1522. if err != nil {
  1523. t.Fatal(err)
  1524. }
  1525. ids[i] = strings.TrimSpace(out)
  1526. ips[i], err = inspectField(ids[i], "NetworkSettings.IPAddress")
  1527. errorOut(err, t, out)
  1528. if ips[i] == "" {
  1529. t.Fatal("IP allocation failed")
  1530. }
  1531. macs[i], err = inspectField(ids[i], "NetworkSettings.MacAddress")
  1532. errorOut(err, t, out)
  1533. portCmd := exec.Command(dockerBinary, "port", ids[i], "1234")
  1534. ports[i], _, err = runCommandWithOutput(portCmd)
  1535. errorOut(err, t, out)
  1536. if ports[i] == "" {
  1537. t.Fatal("Port allocation failed")
  1538. }
  1539. }
  1540. // Stop them all.
  1541. for _, id := range ids {
  1542. cmd := exec.Command(dockerBinary, "stop", id)
  1543. out, _, err := runCommandWithOutput(cmd)
  1544. if err != nil {
  1545. t.Fatal(err, out)
  1546. }
  1547. }
  1548. // Create a new container and ensure it's not getting the IP or port of some stopped container.
  1549. {
  1550. runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "1234", "busybox", "top")
  1551. out, _, err := runCommandWithOutput(runCmd)
  1552. errorOut(err, t, out)
  1553. id := strings.TrimSpace(out)
  1554. ip, err := inspectField(id, "NetworkSettings.IPAddress")
  1555. errorOut(err, t, out)
  1556. portCmd := exec.Command(dockerBinary, "port", id, "1234")
  1557. port, _, err := runCommandWithOutput(portCmd)
  1558. errorOut(err, t, out)
  1559. for i := range ids {
  1560. if ip == ips[i] {
  1561. t.Fatalf("Conflicting IP: %s", ip)
  1562. }
  1563. if port == ports[i] {
  1564. t.Fatalf("Conflicting port: %s", port)
  1565. }
  1566. }
  1567. }
  1568. // Start the containers back, and ensure they are getting the same IPs, MACs and ports.
  1569. for i, id := range ids {
  1570. runCmd := exec.Command(dockerBinary, "start", id)
  1571. out, _, err := runCommandWithOutput(runCmd)
  1572. errorOut(err, t, out)
  1573. ip, err := inspectField(id, "NetworkSettings.IPAddress")
  1574. errorOut(err, t, out)
  1575. mac, err := inspectField(id, "NetworkSettings.MacAddress")
  1576. errorOut(err, t, out)
  1577. portCmd := exec.Command(dockerBinary, "port", ids[i], "1234")
  1578. port, _, err := runCommandWithOutput(portCmd)
  1579. errorOut(err, t, out)
  1580. if ips[i] != ip {
  1581. t.Fatalf("Container started with a different IP: %s != %s", ip, ips[i])
  1582. }
  1583. if macs[i] != mac {
  1584. t.Fatalf("Container started with a different MAC: %s != %s", mac, macs[i])
  1585. }
  1586. if ports[i] != port {
  1587. t.Fatalf("Container started with a different port: %s != %s", port, ports[i])
  1588. }
  1589. }
  1590. deleteAllContainers()
  1591. logDone("run - ips and ports must not change")
  1592. }
  1593. // Ensure that CIDFile gets deleted if it's empty
  1594. // Perform this test by making `docker run` fail
  1595. func TestRunCidFileCleanupIfEmpty(t *testing.T) {
  1596. tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
  1597. if err != nil {
  1598. t.Fatal(err)
  1599. }
  1600. defer os.RemoveAll(tmpDir)
  1601. tmpCidFile := path.Join(tmpDir, "cid")
  1602. cmd := exec.Command(dockerBinary, "run", "--cidfile", tmpCidFile, "scratch")
  1603. out, _, err := runCommandWithOutput(cmd)
  1604. t.Log(out)
  1605. if err == nil {
  1606. t.Fatal("Run without command must fail")
  1607. }
  1608. if _, err := os.Stat(tmpCidFile); err == nil {
  1609. t.Fatalf("empty CIDFile '%s' should've been deleted", tmpCidFile)
  1610. }
  1611. deleteAllContainers()
  1612. logDone("run - cleanup empty cidfile on fail")
  1613. }
  1614. // #2098 - Docker cidFiles only contain short version of the containerId
  1615. //sudo docker run --cidfile /tmp/docker_test.cid ubuntu echo "test"
  1616. // TestRunCidFile tests that run --cidfile returns the longid
  1617. func TestRunCidFileCheckIDLength(t *testing.T) {
  1618. tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
  1619. if err != nil {
  1620. t.Fatal(err)
  1621. }
  1622. tmpCidFile := path.Join(tmpDir, "cid")
  1623. defer os.RemoveAll(tmpDir)
  1624. cmd := exec.Command(dockerBinary, "run", "-d", "--cidfile", tmpCidFile, "busybox", "true")
  1625. out, _, err := runCommandWithOutput(cmd)
  1626. if err != nil {
  1627. t.Fatal(err)
  1628. }
  1629. id := strings.TrimSpace(out)
  1630. buffer, err := ioutil.ReadFile(tmpCidFile)
  1631. if err != nil {
  1632. t.Fatal(err)
  1633. }
  1634. cid := string(buffer)
  1635. if len(cid) != 64 {
  1636. t.Fatalf("--cidfile should be a long id, not '%s'", id)
  1637. }
  1638. if cid != id {
  1639. t.Fatalf("cid must be equal to %s, got %s", id, cid)
  1640. }
  1641. deleteAllContainers()
  1642. logDone("run - cidfile contains long id")
  1643. }
  1644. func TestRunNetworkNotInitializedNoneMode(t *testing.T) {
  1645. cmd := exec.Command(dockerBinary, "run", "-d", "--net=none", "busybox", "top")
  1646. out, _, err := runCommandWithOutput(cmd)
  1647. if err != nil {
  1648. t.Fatal(err)
  1649. }
  1650. id := strings.TrimSpace(out)
  1651. res, err := inspectField(id, "NetworkSettings.IPAddress")
  1652. if err != nil {
  1653. t.Fatal(err)
  1654. }
  1655. if res != "" {
  1656. t.Fatalf("For 'none' mode network must not be initialized, but container got IP: %s", res)
  1657. }
  1658. deleteAllContainers()
  1659. logDone("run - network must not be initialized in 'none' mode")
  1660. }
  1661. func TestRunDeallocatePortOnMissingIptablesRule(t *testing.T) {
  1662. cmd := exec.Command(dockerBinary, "run", "-d", "-p", "23:23", "busybox", "top")
  1663. out, _, err := runCommandWithOutput(cmd)
  1664. if err != nil {
  1665. t.Fatal(err)
  1666. }
  1667. id := strings.TrimSpace(out)
  1668. ip, err := inspectField(id, "NetworkSettings.IPAddress")
  1669. if err != nil {
  1670. t.Fatal(err)
  1671. }
  1672. iptCmd := exec.Command("iptables", "-D", "FORWARD", "-d", fmt.Sprintf("%s/32", ip),
  1673. "!", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-m", "tcp", "--dport", "23", "-j", "ACCEPT")
  1674. out, _, err = runCommandWithOutput(iptCmd)
  1675. if err != nil {
  1676. t.Fatal(err, out)
  1677. }
  1678. if err := deleteContainer(id); err != nil {
  1679. t.Fatal(err)
  1680. }
  1681. cmd = exec.Command(dockerBinary, "run", "-d", "-p", "23:23", "busybox", "top")
  1682. out, _, err = runCommandWithOutput(cmd)
  1683. if err != nil {
  1684. t.Fatal(err, out)
  1685. }
  1686. deleteAllContainers()
  1687. logDone("run - port should be deallocated even on iptables error")
  1688. }
  1689. func TestRunPortInUse(t *testing.T) {
  1690. port := "1234"
  1691. l, err := net.Listen("tcp", ":"+port)
  1692. if err != nil {
  1693. t.Fatal(err)
  1694. }
  1695. defer l.Close()
  1696. cmd := exec.Command(dockerBinary, "run", "-p", port+":80", "busybox", "true")
  1697. out, _, err := runCommandWithOutput(cmd)
  1698. if err == nil {
  1699. t.Fatalf("Binding on used port must fail")
  1700. }
  1701. if !strings.Contains(out, "address already in use") {
  1702. t.Fatalf("Out must be about \"address already in use\", got %s", out)
  1703. }
  1704. deleteAllContainers()
  1705. logDone("run - fail if port already in use")
  1706. }
  1707. // Regression test for #7792
  1708. func TestRunMountOrdering(t *testing.T) {
  1709. tmpDir, err := ioutil.TempDir("", "docker_nested_mount_test")
  1710. if err != nil {
  1711. t.Fatal(err)
  1712. }
  1713. defer os.RemoveAll(tmpDir)
  1714. tmpDir2, err := ioutil.TempDir("", "docker_nested_mount_test2")
  1715. if err != nil {
  1716. t.Fatal(err)
  1717. }
  1718. defer os.RemoveAll(tmpDir2)
  1719. // Create a temporary tmpfs mount.
  1720. fooDir := filepath.Join(tmpDir, "foo")
  1721. if err := os.MkdirAll(filepath.Join(tmpDir, "foo"), 0755); err != nil {
  1722. t.Fatalf("failed to mkdir at %s - %s", fooDir, err)
  1723. }
  1724. if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", fooDir), []byte{}, 0644); err != nil {
  1725. t.Fatal(err)
  1726. }
  1727. if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", tmpDir), []byte{}, 0644); err != nil {
  1728. t.Fatal(err)
  1729. }
  1730. if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", tmpDir2), []byte{}, 0644); err != nil {
  1731. t.Fatal(err)
  1732. }
  1733. cmd := exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp", tmpDir), "-v", fmt.Sprintf("%s:/tmp/foo", fooDir), "-v", fmt.Sprintf("%s:/tmp/tmp2", tmpDir2), "-v", fmt.Sprintf("%s:/tmp/tmp2/foo", fooDir), "busybox:latest", "sh", "-c", "ls /tmp/touch-me && ls /tmp/foo/touch-me && ls /tmp/tmp2/touch-me && ls /tmp/tmp2/foo/touch-me")
  1734. out, _, err := runCommandWithOutput(cmd)
  1735. if err != nil {
  1736. t.Fatal(out, err)
  1737. }
  1738. deleteAllContainers()
  1739. logDone("run - volumes are mounted in the correct order")
  1740. }
  1741. func TestRunExecDir(t *testing.T) {
  1742. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  1743. out, _, err := runCommandWithOutput(cmd)
  1744. if err != nil {
  1745. t.Fatal(err, out)
  1746. }
  1747. id := strings.TrimSpace(out)
  1748. execDir := filepath.Join(execDriverPath, id)
  1749. stateFile := filepath.Join(execDir, "state.json")
  1750. contFile := filepath.Join(execDir, "container.json")
  1751. {
  1752. fi, err := os.Stat(execDir)
  1753. if err != nil {
  1754. t.Fatal(err)
  1755. }
  1756. if !fi.IsDir() {
  1757. t.Fatalf("%q must be a directory", execDir)
  1758. }
  1759. fi, err = os.Stat(stateFile)
  1760. if err != nil {
  1761. t.Fatal(err)
  1762. }
  1763. fi, err = os.Stat(contFile)
  1764. if err != nil {
  1765. t.Fatal(err)
  1766. }
  1767. }
  1768. stopCmd := exec.Command(dockerBinary, "stop", id)
  1769. out, _, err = runCommandWithOutput(stopCmd)
  1770. if err != nil {
  1771. t.Fatal(err, out)
  1772. }
  1773. {
  1774. fi, err := os.Stat(execDir)
  1775. if err != nil {
  1776. t.Fatal(err)
  1777. }
  1778. if !fi.IsDir() {
  1779. t.Fatalf("%q must be a directory", execDir)
  1780. }
  1781. fi, err = os.Stat(stateFile)
  1782. if err == nil {
  1783. t.Fatalf("Statefile %q is exists for stopped container!", stateFile)
  1784. }
  1785. if !os.IsNotExist(err) {
  1786. t.Fatalf("Error should be about non-existing, got %s", err)
  1787. }
  1788. fi, err = os.Stat(contFile)
  1789. if err == nil {
  1790. t.Fatalf("Container file %q is exists for stopped container!", contFile)
  1791. }
  1792. if !os.IsNotExist(err) {
  1793. t.Fatalf("Error should be about non-existing, got %s", err)
  1794. }
  1795. }
  1796. startCmd := exec.Command(dockerBinary, "start", id)
  1797. out, _, err = runCommandWithOutput(startCmd)
  1798. if err != nil {
  1799. t.Fatal(err, out)
  1800. }
  1801. {
  1802. fi, err := os.Stat(execDir)
  1803. if err != nil {
  1804. t.Fatal(err)
  1805. }
  1806. if !fi.IsDir() {
  1807. t.Fatalf("%q must be a directory", execDir)
  1808. }
  1809. fi, err = os.Stat(stateFile)
  1810. if err != nil {
  1811. t.Fatal(err)
  1812. }
  1813. fi, err = os.Stat(contFile)
  1814. if err != nil {
  1815. t.Fatal(err)
  1816. }
  1817. }
  1818. rmCmd := exec.Command(dockerBinary, "rm", "-f", id)
  1819. out, _, err = runCommandWithOutput(rmCmd)
  1820. if err != nil {
  1821. t.Fatal(err, out)
  1822. }
  1823. {
  1824. _, err := os.Stat(execDir)
  1825. if err == nil {
  1826. t.Fatal(err)
  1827. }
  1828. if err == nil {
  1829. t.Fatalf("Exec directory %q is exists for removed container!", execDir)
  1830. }
  1831. if !os.IsNotExist(err) {
  1832. t.Fatalf("Error should be about non-existing, got %s", err)
  1833. }
  1834. }
  1835. logDone("run - check execdriver dir behavior")
  1836. }
  1837. // #6509
  1838. func TestRunRedirectStdout(t *testing.T) {
  1839. defer deleteAllContainers()
  1840. checkRedirect := func(command string) {
  1841. _, tty, err := pty.Open()
  1842. if err != nil {
  1843. t.Fatalf("Could not open pty: %v", err)
  1844. }
  1845. cmd := exec.Command("sh", "-c", command)
  1846. cmd.Stdin = tty
  1847. cmd.Stdout = tty
  1848. cmd.Stderr = tty
  1849. ch := make(chan struct{})
  1850. if err := cmd.Start(); err != nil {
  1851. t.Fatalf("start err: %v", err)
  1852. }
  1853. go func() {
  1854. if err := cmd.Wait(); err != nil {
  1855. t.Fatalf("wait err=%v", err)
  1856. }
  1857. close(ch)
  1858. }()
  1859. select {
  1860. case <-time.After(time.Second):
  1861. t.Fatal("command timeout")
  1862. case <-ch:
  1863. }
  1864. }
  1865. checkRedirect(dockerBinary + " run -i busybox cat /etc/passwd | grep -q root")
  1866. checkRedirect(dockerBinary + " run busybox cat /etc/passwd | grep -q root")
  1867. logDone("run - redirect stdout")
  1868. }
  1869. // Regression test for https://github.com/docker/docker/issues/8259
  1870. func TestRunReuseBindVolumeThatIsSymlink(t *testing.T) {
  1871. tmpDir, err := ioutil.TempDir(os.TempDir(), "testlink")
  1872. if err != nil {
  1873. t.Fatal(err)
  1874. }
  1875. defer os.RemoveAll(tmpDir)
  1876. linkPath := os.TempDir() + "/testlink2"
  1877. if err := os.Symlink(tmpDir, linkPath); err != nil {
  1878. t.Fatal(err)
  1879. }
  1880. defer os.RemoveAll(linkPath)
  1881. // Create first container
  1882. cmd := exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp/test", linkPath), "busybox", "ls", "-lh", "/tmp/test")
  1883. if _, err := runCommand(cmd); err != nil {
  1884. t.Fatal(err)
  1885. }
  1886. // Create second container with same symlinked path
  1887. // This will fail if the referenced issue is hit with a "Volume exists" error
  1888. cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp/test", linkPath), "busybox", "ls", "-lh", "/tmp/test")
  1889. if out, _, err := runCommandWithOutput(cmd); err != nil {
  1890. t.Fatal(err, out)
  1891. }
  1892. deleteAllContainers()
  1893. logDone("run - can remount old bindmount volume")
  1894. }
  1895. func TestVolumesNoCopyData(t *testing.T) {
  1896. defer deleteImages("dataimage")
  1897. defer deleteAllContainers()
  1898. if _, err := buildImage("dataimage",
  1899. `FROM busybox
  1900. RUN mkdir -p /foo
  1901. RUN touch /foo/bar`,
  1902. true); err != nil {
  1903. t.Fatal(err)
  1904. }
  1905. cmd := exec.Command(dockerBinary, "run", "--name", "test", "-v", "/foo", "busybox")
  1906. if _, err := runCommand(cmd); err != nil {
  1907. t.Fatal(err)
  1908. }
  1909. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "test", "dataimage", "ls", "-lh", "/foo/bar")
  1910. if out, _, err := runCommandWithOutput(cmd); err == nil || !strings.Contains(out, "No such file or directory") {
  1911. t.Fatalf("Data was copied on volumes-from but shouldn't be:\n%q", out)
  1912. }
  1913. tmpDir, err := ioutil.TempDir("", "docker_test_bind_mount_copy_data")
  1914. if err != nil {
  1915. t.Fatal(err)
  1916. }
  1917. defer os.RemoveAll(tmpDir)
  1918. cmd = exec.Command(dockerBinary, "run", "-v", tmpDir+":/foo", "dataimage", "ls", "-lh", "/foo/bar")
  1919. if out, _, err := runCommandWithOutput(cmd); err == nil || !strings.Contains(out, "No such file or directory") {
  1920. t.Fatalf("Data was copied on bind-mount but shouldn't be:\n%q", out)
  1921. }
  1922. logDone("run - volumes do not copy data for volumes-from and bindmounts")
  1923. }
  1924. func TestRunVolumesNotRecreatedOnStart(t *testing.T) {
  1925. // Clear out any remnants from other tests
  1926. deleteAllContainers()
  1927. info, err := ioutil.ReadDir(volumesConfigPath)
  1928. if err != nil {
  1929. t.Fatal(err)
  1930. }
  1931. if len(info) > 0 {
  1932. for _, f := range info {
  1933. if err := os.RemoveAll(volumesConfigPath + "/" + f.Name()); err != nil {
  1934. t.Fatal(err)
  1935. }
  1936. }
  1937. }
  1938. defer deleteAllContainers()
  1939. cmd := exec.Command(dockerBinary, "run", "-v", "/foo", "--name", "lone_starr", "busybox")
  1940. if _, err := runCommand(cmd); err != nil {
  1941. t.Fatal(err)
  1942. }
  1943. cmd = exec.Command(dockerBinary, "start", "lone_starr")
  1944. if _, err := runCommand(cmd); err != nil {
  1945. t.Fatal(err)
  1946. }
  1947. info, err = ioutil.ReadDir(volumesConfigPath)
  1948. if err != nil {
  1949. t.Fatal(err)
  1950. }
  1951. if len(info) != 1 {
  1952. t.Fatalf("Expected only 1 volume have %v", len(info))
  1953. }
  1954. logDone("run - volumes not recreated on start")
  1955. }