docker_cli_run_test.go 52 KB

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