docker_cli_run_test.go 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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 TestLoopbackOnlyExistsWhenNetworkingDisabled(t *testing.T) {
  567. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up")
  568. out, _, err := runCommandWithOutput(cmd)
  569. if err != nil {
  570. t.Fatal(err, out)
  571. }
  572. var (
  573. count = 0
  574. parts = strings.Split(out, "\n")
  575. )
  576. for _, l := range parts {
  577. if l != "" {
  578. count++
  579. }
  580. }
  581. if count != 1 {
  582. t.Fatalf("Wrong interface count in container %d", count)
  583. }
  584. if !strings.HasPrefix(out, "1: lo") {
  585. t.Fatalf("Wrong interface in test container: expected [1: lo], got %s", out)
  586. }
  587. deleteAllContainers()
  588. logDone("run - test loopback only exists when networking disabled")
  589. }
  590. func TestPrivilegedCanMknod(t *testing.T) {
  591. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  592. out, _, err := runCommandWithOutput(cmd)
  593. if err != nil {
  594. t.Fatal(err)
  595. }
  596. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  597. t.Fatalf("expected output ok received %s", actual)
  598. }
  599. deleteAllContainers()
  600. logDone("run - test privileged can mknod")
  601. }
  602. func TestUnPrivilegedCanMknod(t *testing.T) {
  603. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  604. out, _, err := runCommandWithOutput(cmd)
  605. if err != nil {
  606. t.Fatal(err)
  607. }
  608. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  609. t.Fatalf("expected output ok received %s", actual)
  610. }
  611. deleteAllContainers()
  612. logDone("run - test un-privileged can mknod")
  613. }
  614. func TestCapDropInvalid(t *testing.T) {
  615. cmd := exec.Command(dockerBinary, "run", "--cap-drop=CHPASS", "busybox", "ls")
  616. out, _, err := runCommandWithOutput(cmd)
  617. if err == nil {
  618. t.Fatal(err, out)
  619. }
  620. logDone("run - test --cap-drop=CHPASS invalid")
  621. }
  622. func TestCapDropCannotMknod(t *testing.T) {
  623. cmd := exec.Command(dockerBinary, "run", "--cap-drop=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  624. out, _, err := runCommandWithOutput(cmd)
  625. if err == nil {
  626. t.Fatal(err, out)
  627. }
  628. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  629. t.Fatalf("expected output not ok received %s", actual)
  630. }
  631. deleteAllContainers()
  632. logDone("run - test --cap-drop=MKNOD cannot mknod")
  633. }
  634. func TestCapDropALLCannotMknod(t *testing.T) {
  635. cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  636. out, _, err := runCommandWithOutput(cmd)
  637. if err == nil {
  638. t.Fatal(err, out)
  639. }
  640. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  641. t.Fatalf("expected output not ok received %s", actual)
  642. }
  643. deleteAllContainers()
  644. logDone("run - test --cap-drop=ALL cannot mknod")
  645. }
  646. func TestCapDropALLAddMknodCannotMknod(t *testing.T) {
  647. cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "--cap-add=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  648. out, _, err := runCommandWithOutput(cmd)
  649. if err != nil {
  650. t.Fatal(err, out)
  651. }
  652. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  653. t.Fatalf("expected output ok received %s", actual)
  654. }
  655. deleteAllContainers()
  656. logDone("run - test --cap-drop=ALL --cap-add=MKNOD can mknod")
  657. }
  658. func TestCapAddInvalid(t *testing.T) {
  659. cmd := exec.Command(dockerBinary, "run", "--cap-add=CHPASS", "busybox", "ls")
  660. out, _, err := runCommandWithOutput(cmd)
  661. if err == nil {
  662. t.Fatal(err, out)
  663. }
  664. logDone("run - test --cap-add=CHPASS invalid")
  665. }
  666. func TestCapAddCanDownInterface(t *testing.T) {
  667. cmd := exec.Command(dockerBinary, "run", "--cap-add=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  668. out, _, err := runCommandWithOutput(cmd)
  669. if err != nil {
  670. t.Fatal(err, out)
  671. }
  672. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  673. t.Fatalf("expected output ok received %s", actual)
  674. }
  675. deleteAllContainers()
  676. logDone("run - test --cap-add=NET_ADMIN can set eth0 down")
  677. }
  678. func TestCapAddALLCanDownInterface(t *testing.T) {
  679. cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  680. out, _, err := runCommandWithOutput(cmd)
  681. if err != nil {
  682. t.Fatal(err, out)
  683. }
  684. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  685. t.Fatalf("expected output ok received %s", actual)
  686. }
  687. deleteAllContainers()
  688. logDone("run - test --cap-add=ALL can set eth0 down")
  689. }
  690. func TestCapAddALLDropNetAdminCanDownInterface(t *testing.T) {
  691. cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "--cap-drop=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
  692. out, _, err := runCommandWithOutput(cmd)
  693. if err == nil {
  694. t.Fatal(err, out)
  695. }
  696. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  697. t.Fatalf("expected output not ok received %s", actual)
  698. }
  699. deleteAllContainers()
  700. logDone("run - test --cap-add=ALL --cap-drop=NET_ADMIN cannot set eth0 down")
  701. }
  702. func TestPrivilegedCanMount(t *testing.T) {
  703. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  704. out, _, err := runCommandWithOutput(cmd)
  705. if err != nil {
  706. t.Fatal(err)
  707. }
  708. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  709. t.Fatalf("expected output ok received %s", actual)
  710. }
  711. deleteAllContainers()
  712. logDone("run - test privileged can mount")
  713. }
  714. func TestUnPrivilegedCannotMount(t *testing.T) {
  715. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  716. out, _, err := runCommandWithOutput(cmd)
  717. if err == nil {
  718. t.Fatal(err, out)
  719. }
  720. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  721. t.Fatalf("expected output not ok received %s", actual)
  722. }
  723. deleteAllContainers()
  724. logDone("run - test un-privileged cannot mount")
  725. }
  726. func TestSysNotWritableInNonPrivilegedContainers(t *testing.T) {
  727. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/sys/kernel/profiling")
  728. if code, err := runCommand(cmd); err == nil || code == 0 {
  729. t.Fatal("sys should not be writable in a non privileged container")
  730. }
  731. deleteAllContainers()
  732. logDone("run - sys not writable in non privileged container")
  733. }
  734. func TestSysWritableInPrivilegedContainers(t *testing.T) {
  735. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/sys/kernel/profiling")
  736. if code, err := runCommand(cmd); err != nil || code != 0 {
  737. t.Fatalf("sys should be writable in privileged container")
  738. }
  739. deleteAllContainers()
  740. logDone("run - sys writable in privileged container")
  741. }
  742. func TestProcNotWritableInNonPrivilegedContainers(t *testing.T) {
  743. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/proc/sysrq-trigger")
  744. if code, err := runCommand(cmd); err == nil || code == 0 {
  745. t.Fatal("proc should not be writable in a non privileged container")
  746. }
  747. deleteAllContainers()
  748. logDone("run - proc not writable in non privileged container")
  749. }
  750. func TestProcWritableInPrivilegedContainers(t *testing.T) {
  751. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/proc/sysrq-trigger")
  752. if code, err := runCommand(cmd); err != nil || code != 0 {
  753. t.Fatalf("proc should be writable in privileged container")
  754. }
  755. deleteAllContainers()
  756. logDone("run - proc writable in privileged container")
  757. }
  758. func TestRunWithCpuset(t *testing.T) {
  759. cmd := exec.Command(dockerBinary, "run", "--cpuset", "0", "busybox", "true")
  760. if code, err := runCommand(cmd); err != nil || code != 0 {
  761. t.Fatalf("container should run successfuly with cpuset of 0: %s", err)
  762. }
  763. deleteAllContainers()
  764. logDone("run - cpuset 0")
  765. }
  766. func TestDeviceNumbers(t *testing.T) {
  767. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "ls -l /dev/null")
  768. out, _, err := runCommandWithOutput(cmd)
  769. if err != nil {
  770. t.Fatal(err, out)
  771. }
  772. deviceLineFields := strings.Fields(out)
  773. deviceLineFields[6] = ""
  774. deviceLineFields[7] = ""
  775. deviceLineFields[8] = ""
  776. expected := []string{"crw-rw-rw-", "1", "root", "root", "1,", "3", "", "", "", "/dev/null"}
  777. if !(reflect.DeepEqual(deviceLineFields, expected)) {
  778. t.Fatalf("expected output\ncrw-rw-rw- 1 root root 1, 3 May 24 13:29 /dev/null\n received\n %s\n", out)
  779. }
  780. deleteAllContainers()
  781. logDone("run - test device numbers")
  782. }
  783. func TestThatCharacterDevicesActLikeCharacterDevices(t *testing.T) {
  784. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "dd if=/dev/zero of=/zero bs=1k count=5 2> /dev/null ; du -h /zero")
  785. out, _, err := runCommandWithOutput(cmd)
  786. if err != nil {
  787. t.Fatal(err, out)
  788. }
  789. if actual := strings.Trim(out, "\r\n"); actual[0] == '0' {
  790. t.Fatalf("expected a new file called /zero to be create that is greater than 0 bytes long, but du says: %s", actual)
  791. }
  792. deleteAllContainers()
  793. logDone("run - test that character devices work.")
  794. }
  795. func TestRunUnprivilegedWithChroot(t *testing.T) {
  796. cmd := exec.Command(dockerBinary, "run", "busybox", "chroot", "/", "true")
  797. if _, err := runCommand(cmd); err != nil {
  798. t.Fatal(err)
  799. }
  800. deleteAllContainers()
  801. logDone("run - unprivileged with chroot")
  802. }
  803. func TestAddingOptionalDevices(t *testing.T) {
  804. cmd := exec.Command(dockerBinary, "run", "--device", "/dev/zero:/dev/nulo", "busybox", "sh", "-c", "ls /dev/nulo")
  805. out, _, err := runCommandWithOutput(cmd)
  806. if err != nil {
  807. t.Fatal(err, out)
  808. }
  809. if actual := strings.Trim(out, "\r\n"); actual != "/dev/nulo" {
  810. t.Fatalf("expected output /dev/nulo, received %s", actual)
  811. }
  812. deleteAllContainers()
  813. logDone("run - test --device argument")
  814. }
  815. func TestModeHostname(t *testing.T) {
  816. cmd := exec.Command(dockerBinary, "run", "-h=testhostname", "busybox", "cat", "/etc/hostname")
  817. out, _, err := runCommandWithOutput(cmd)
  818. if err != nil {
  819. t.Fatal(err, out)
  820. }
  821. if actual := strings.Trim(out, "\r\n"); actual != "testhostname" {
  822. t.Fatalf("expected 'testhostname', but says: '%s'", actual)
  823. }
  824. cmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hostname")
  825. out, _, err = runCommandWithOutput(cmd)
  826. if err != nil {
  827. t.Fatal(err, out)
  828. }
  829. hostname, err := os.Hostname()
  830. if err != nil {
  831. t.Fatal(err)
  832. }
  833. if actual := strings.Trim(out, "\r\n"); actual != hostname {
  834. t.Fatalf("expected '%s', but says: '%s'", hostname, actual)
  835. }
  836. deleteAllContainers()
  837. logDone("run - hostname and several network modes")
  838. }
  839. func TestRootWorkdir(t *testing.T) {
  840. s, _, err := cmd(t, "run", "--workdir", "/", "busybox", "pwd")
  841. if err != nil {
  842. t.Fatal(s, err)
  843. }
  844. if s != "/\n" {
  845. t.Fatalf("pwd returned '%s' (expected /\\n)", s)
  846. }
  847. deleteAllContainers()
  848. logDone("run - workdir /")
  849. }
  850. func TestAllowBindMountingRoot(t *testing.T) {
  851. s, _, err := cmd(t, "run", "-v", "/:/host", "busybox", "ls", "/host")
  852. if err != nil {
  853. t.Fatal(s, err)
  854. }
  855. deleteAllContainers()
  856. logDone("run - bind mount / as volume")
  857. }
  858. func TestDisallowBindMountingRootToRoot(t *testing.T) {
  859. cmd := exec.Command(dockerBinary, "run", "-v", "/:/", "busybox", "ls", "/host")
  860. out, _, err := runCommandWithOutput(cmd)
  861. if err == nil {
  862. t.Fatal(out, err)
  863. }
  864. deleteAllContainers()
  865. logDone("run - bind mount /:/ as volume should fail")
  866. }
  867. func TestDnsDefaultOptions(t *testing.T) {
  868. cmd := exec.Command(dockerBinary, "run", "busybox", "cat", "/etc/resolv.conf")
  869. actual, _, err := runCommandWithOutput(cmd)
  870. if err != nil {
  871. t.Fatal(err, actual)
  872. }
  873. resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
  874. if os.IsNotExist(err) {
  875. t.Fatalf("/etc/resolv.conf does not exist")
  876. }
  877. if actual != string(resolvConf) {
  878. t.Fatalf("expected resolv.conf is not the same of actual")
  879. }
  880. deleteAllContainers()
  881. logDone("run - dns default options")
  882. }
  883. func TestDnsOptions(t *testing.T) {
  884. cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
  885. out, _, err := runCommandWithOutput(cmd)
  886. if err != nil {
  887. t.Fatal(err, out)
  888. }
  889. actual := strings.Replace(strings.Trim(out, "\r\n"), "\n", " ", -1)
  890. if actual != "nameserver 127.0.0.1 search mydomain" {
  891. t.Fatalf("expected 'nameserver 127.0.0.1 search mydomain', but says: '%s'", actual)
  892. }
  893. cmd = exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=.", "busybox", "cat", "/etc/resolv.conf")
  894. out, _, err = runCommandWithOutput(cmd)
  895. if err != nil {
  896. t.Fatal(err, out)
  897. }
  898. actual = strings.Replace(strings.Trim(strings.Trim(out, "\r\n"), " "), "\n", " ", -1)
  899. if actual != "nameserver 127.0.0.1" {
  900. t.Fatalf("expected 'nameserver 127.0.0.1', but says: '%s'", actual)
  901. }
  902. logDone("run - dns options")
  903. }
  904. func TestDnsOptionsBasedOnHostResolvConf(t *testing.T) {
  905. resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
  906. if os.IsNotExist(err) {
  907. t.Fatalf("/etc/resolv.conf does not exist")
  908. }
  909. hostNamservers := resolvconf.GetNameservers(resolvConf)
  910. hostSearch := resolvconf.GetSearchDomains(resolvConf)
  911. cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "busybox", "cat", "/etc/resolv.conf")
  912. out, _, err := runCommandWithOutput(cmd)
  913. if err != nil {
  914. t.Fatal(err, out)
  915. }
  916. if actualNameservers := resolvconf.GetNameservers([]byte(out)); string(actualNameservers[0]) != "127.0.0.1" {
  917. t.Fatalf("expected '127.0.0.1', but says: '%s'", string(actualNameservers[0]))
  918. }
  919. actualSearch := resolvconf.GetSearchDomains([]byte(out))
  920. if len(actualSearch) != len(hostSearch) {
  921. t.Fatalf("expected '%s' search domain(s), but it has: '%s'", len(hostSearch), len(actualSearch))
  922. }
  923. for i := range actualSearch {
  924. if actualSearch[i] != hostSearch[i] {
  925. t.Fatalf("expected '%s' domain, but says: '%s'", actualSearch[i], hostSearch[i])
  926. }
  927. }
  928. cmd = exec.Command(dockerBinary, "run", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
  929. out, _, err = runCommandWithOutput(cmd)
  930. if err != nil {
  931. t.Fatal(err, out)
  932. }
  933. actualNameservers := resolvconf.GetNameservers([]byte(out))
  934. if len(actualNameservers) != len(hostNamservers) {
  935. t.Fatalf("expected '%s' nameserver(s), but it has: '%s'", len(hostNamservers), len(actualNameservers))
  936. }
  937. for i := range actualNameservers {
  938. if actualNameservers[i] != hostNamservers[i] {
  939. t.Fatalf("expected '%s' nameserver, but says: '%s'", actualNameservers[i], hostNamservers[i])
  940. }
  941. }
  942. if actualSearch = resolvconf.GetSearchDomains([]byte(out)); string(actualSearch[0]) != "mydomain" {
  943. t.Fatalf("expected 'mydomain', but says: '%s'", string(actualSearch[0]))
  944. }
  945. deleteAllContainers()
  946. logDone("run - dns options based on host resolv.conf")
  947. }