docker_cli_run_test.go 39 KB

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