docker_cli_run_test.go 67 KB

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