docker_cli_run_test.go 46 KB

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