docker_cli_run_test.go 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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. }