docker_cli_run_test.go 54 KB

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