docker_cli_run_test.go 69 KB

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