docker_cli_run_unix_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // +build !windows
  2. package main
  3. import (
  4. "bufio"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "path"
  10. "path/filepath"
  11. "strings"
  12. "time"
  13. "github.com/docker/docker/pkg/mount"
  14. "github.com/go-check/check"
  15. "github.com/kr/pty"
  16. )
  17. // #6509
  18. func (s *DockerSuite) TestRunRedirectStdout(c *check.C) {
  19. checkRedirect := func(command string) {
  20. _, tty, err := pty.Open()
  21. if err != nil {
  22. c.Fatalf("Could not open pty: %v", err)
  23. }
  24. cmd := exec.Command("sh", "-c", command)
  25. cmd.Stdin = tty
  26. cmd.Stdout = tty
  27. cmd.Stderr = tty
  28. if err := cmd.Start(); err != nil {
  29. c.Fatalf("start err: %v", err)
  30. }
  31. ch := make(chan error)
  32. go func() {
  33. ch <- cmd.Wait()
  34. close(ch)
  35. }()
  36. select {
  37. case <-time.After(10 * time.Second):
  38. c.Fatal("command timeout")
  39. case err := <-ch:
  40. if err != nil {
  41. c.Fatalf("wait err=%v", err)
  42. }
  43. }
  44. }
  45. checkRedirect(dockerBinary + " run -i busybox cat /etc/passwd | grep -q root")
  46. checkRedirect(dockerBinary + " run busybox cat /etc/passwd | grep -q root")
  47. }
  48. // Test recursive bind mount works by default
  49. func (s *DockerSuite) TestRunWithVolumesIsRecursive(c *check.C) {
  50. tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
  51. if err != nil {
  52. c.Fatal(err)
  53. }
  54. defer os.RemoveAll(tmpDir)
  55. // Create a temporary tmpfs mount.
  56. tmpfsDir := filepath.Join(tmpDir, "tmpfs")
  57. if err := os.MkdirAll(tmpfsDir, 0777); err != nil {
  58. c.Fatalf("failed to mkdir at %s - %s", tmpfsDir, err)
  59. }
  60. if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
  61. c.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
  62. }
  63. f, err := ioutil.TempFile(tmpfsDir, "touch-me")
  64. if err != nil {
  65. c.Fatal(err)
  66. }
  67. defer f.Close()
  68. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
  69. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  70. if err != nil && exitCode != 0 {
  71. c.Fatal(out, stderr, err)
  72. }
  73. if !strings.Contains(out, filepath.Base(f.Name())) {
  74. c.Fatal("Recursive bind mount test failed. Expected file not found")
  75. }
  76. }
  77. func (s *DockerSuite) TestRunWithUlimits(c *check.C) {
  78. testRequires(c, NativeExecDriver)
  79. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name=testulimits", "--ulimit", "nofile=42", "busybox", "/bin/sh", "-c", "ulimit -n"))
  80. if err != nil {
  81. c.Fatal(err, out)
  82. }
  83. ul := strings.TrimSpace(out)
  84. if ul != "42" {
  85. c.Fatalf("expected `ulimit -n` to be 42, got %s", ul)
  86. }
  87. }
  88. func (s *DockerSuite) TestRunContainerWithCgroupParent(c *check.C) {
  89. testRequires(c, NativeExecDriver)
  90. cgroupParent := "test"
  91. data, err := ioutil.ReadFile("/proc/self/cgroup")
  92. if err != nil {
  93. c.Fatalf("failed to read '/proc/self/cgroup - %v", err)
  94. }
  95. selfCgroupPaths := parseCgroupPaths(string(data))
  96. selfCpuCgroup, found := selfCgroupPaths["memory"]
  97. if !found {
  98. c.Fatalf("unable to find self cpu cgroup path. CgroupsPath: %v", selfCgroupPaths)
  99. }
  100. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
  101. if err != nil {
  102. c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
  103. }
  104. cgroupPaths := parseCgroupPaths(string(out))
  105. if len(cgroupPaths) == 0 {
  106. c.Fatalf("unexpected output - %q", string(out))
  107. }
  108. found = false
  109. expectedCgroupPrefix := path.Join(selfCpuCgroup, cgroupParent)
  110. for _, path := range cgroupPaths {
  111. if strings.HasPrefix(path, expectedCgroupPrefix) {
  112. found = true
  113. break
  114. }
  115. }
  116. if !found {
  117. c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have prefix %q. Cgroup Paths: %v", expectedCgroupPrefix, cgroupPaths)
  118. }
  119. }
  120. func (s *DockerSuite) TestRunContainerWithCgroupParentAbsPath(c *check.C) {
  121. testRequires(c, NativeExecDriver)
  122. cgroupParent := "/cgroup-parent/test"
  123. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
  124. if err != nil {
  125. c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
  126. }
  127. cgroupPaths := parseCgroupPaths(string(out))
  128. if len(cgroupPaths) == 0 {
  129. c.Fatalf("unexpected output - %q", string(out))
  130. }
  131. found := false
  132. for _, path := range cgroupPaths {
  133. if strings.HasPrefix(path, cgroupParent) {
  134. found = true
  135. break
  136. }
  137. }
  138. if !found {
  139. c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have prefix %q. Cgroup Paths: %v", cgroupParent, cgroupPaths)
  140. }
  141. }
  142. func (s *DockerSuite) TestRunDeviceDirectory(c *check.C) {
  143. testRequires(c, NativeExecDriver)
  144. cmd := exec.Command(dockerBinary, "run", "--device", "/dev/snd:/dev/snd", "busybox", "sh", "-c", "ls /dev/snd/")
  145. out, _, err := runCommandWithOutput(cmd)
  146. if err != nil {
  147. c.Fatal(err, out)
  148. }
  149. if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "timer") {
  150. c.Fatalf("expected output /dev/snd/timer, received %s", actual)
  151. }
  152. cmd = exec.Command(dockerBinary, "run", "--device", "/dev/snd:/dev/othersnd", "busybox", "sh", "-c", "ls /dev/othersnd/")
  153. out, _, err = runCommandWithOutput(cmd)
  154. if err != nil {
  155. c.Fatal(err, out)
  156. }
  157. if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "seq") {
  158. c.Fatalf("expected output /dev/othersnd/seq, received %s", actual)
  159. }
  160. }
  161. // TestRunDetach checks attaching and detaching with the escape sequence.
  162. func (s *DockerSuite) TestRunAttachDetach(c *check.C) {
  163. name := "attach-detach"
  164. cmd := exec.Command(dockerBinary, "run", "--name", name, "-it", "busybox", "cat")
  165. stdout, err := cmd.StdoutPipe()
  166. if err != nil {
  167. c.Fatal(err)
  168. }
  169. cpty, tty, err := pty.Open()
  170. if err != nil {
  171. c.Fatal(err)
  172. }
  173. defer cpty.Close()
  174. cmd.Stdin = tty
  175. if err := cmd.Start(); err != nil {
  176. c.Fatal(err)
  177. }
  178. if err := waitRun(name); err != nil {
  179. c.Fatal(err)
  180. }
  181. if _, err := cpty.Write([]byte("hello\n")); err != nil {
  182. c.Fatal(err)
  183. }
  184. out, err := bufio.NewReader(stdout).ReadString('\n')
  185. if err != nil {
  186. c.Fatal(err)
  187. }
  188. if strings.TrimSpace(out) != "hello" {
  189. c.Fatalf("expected 'hello', got %q", out)
  190. }
  191. // escape sequence
  192. if _, err := cpty.Write([]byte{16}); err != nil {
  193. c.Fatal(err)
  194. }
  195. time.Sleep(100 * time.Millisecond)
  196. if _, err := cpty.Write([]byte{17}); err != nil {
  197. c.Fatal(err)
  198. }
  199. ch := make(chan struct{})
  200. go func() {
  201. cmd.Wait()
  202. ch <- struct{}{}
  203. }()
  204. running, err := inspectField(name, "State.Running")
  205. if err != nil {
  206. c.Fatal(err)
  207. }
  208. if running != "true" {
  209. c.Fatal("expected container to still be running")
  210. }
  211. go func() {
  212. exec.Command(dockerBinary, "kill", name).Run()
  213. }()
  214. select {
  215. case <-ch:
  216. case <-time.After(10 * time.Millisecond):
  217. c.Fatal("timed out waiting for container to exit")
  218. }
  219. }
  220. // "test" should be printed
  221. func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) {
  222. testRequires(c, CpuCfsQuota)
  223. runCmd := exec.Command(dockerBinary, "run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test")
  224. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  225. if err != nil {
  226. c.Fatalf("failed to run container: %v, output: %q", err, out)
  227. }
  228. out = strings.TrimSpace(out)
  229. if out != "test" {
  230. c.Errorf("container should've printed 'test'")
  231. }
  232. out, err = inspectField("test", "HostConfig.CpuQuota")
  233. c.Assert(err, check.IsNil)
  234. if out != "8000" {
  235. c.Fatalf("setting the CPU CFS quota failed")
  236. }
  237. }
  238. func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) {
  239. testRequires(c, CpuCfsPeriod)
  240. runCmd := exec.Command(dockerBinary, "run", "--cpu-period", "50000", "--name", "test", "busybox", "true")
  241. if _, err := runCommand(runCmd); err != nil {
  242. c.Fatalf("failed to run container: %v", err)
  243. }
  244. out, err := inspectField("test", "HostConfig.CpuPeriod")
  245. c.Assert(err, check.IsNil)
  246. if out != "50000" {
  247. c.Fatalf("setting the CPU CFS period failed")
  248. }
  249. }
  250. func (s *DockerSuite) TestRunOOMExitCode(c *check.C) {
  251. testRequires(c, OomControl)
  252. errChan := make(chan error)
  253. go func() {
  254. defer close(errChan)
  255. runCmd := exec.Command(dockerBinary, "run", "-m", "4MB", "busybox", "sh", "-c", "x=a; while true; do x=$x$x$x$x; done")
  256. out, exitCode, _ := runCommandWithOutput(runCmd)
  257. if expected := 137; exitCode != expected {
  258. errChan <- fmt.Errorf("wrong exit code for OOM container: expected %d, got %d (output: %q)", expected, exitCode, out)
  259. }
  260. }()
  261. select {
  262. case err := <-errChan:
  263. c.Assert(err, check.IsNil)
  264. case <-time.After(30 * time.Second):
  265. c.Fatal("Timeout waiting for container to die on OOM")
  266. }
  267. }
  268. func (s *DockerSuite) TestContainerNetworkModeToSelf(c *check.C) {
  269. cmd := exec.Command(dockerBinary, "run", "--name=me", "--net=container:me", "busybox", "true")
  270. out, _, err := runCommandWithOutput(cmd)
  271. if err == nil || !strings.Contains(out, "cannot join own network") {
  272. c.Fatalf("using container net mode to self should result in an error")
  273. }
  274. }
  275. func (s *DockerSuite) TestRunContainerNetModeWithDnsMacHosts(c *check.C) {
  276. cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
  277. out, _, err := runCommandWithOutput(cmd)
  278. if err != nil {
  279. c.Fatalf("failed to run container: %v, output: %q", err, out)
  280. }
  281. cmd = exec.Command(dockerBinary, "run", "--dns", "1.2.3.4", "--net=container:parent", "busybox")
  282. out, _, err = runCommandWithOutput(cmd)
  283. if err == nil || !strings.Contains(out, "Conflicting options: --dns and the network mode") {
  284. c.Fatalf("run --net=container with --dns should error out")
  285. }
  286. cmd = exec.Command(dockerBinary, "run", "--mac-address", "92:d0:c6:0a:29:33", "--net=container:parent", "busybox")
  287. out, _, err = runCommandWithOutput(cmd)
  288. if err == nil || !strings.Contains(out, "--mac-address and the network mode") {
  289. c.Fatalf("run --net=container with --mac-address should error out")
  290. }
  291. cmd = exec.Command(dockerBinary, "run", "--add-host", "test:192.168.2.109", "--net=container:parent", "busybox")
  292. out, _, err = runCommandWithOutput(cmd)
  293. if err == nil || !strings.Contains(out, "--add-host and the network mode") {
  294. c.Fatalf("run --net=container with --add-host should error out")
  295. }
  296. }
  297. func (s *DockerSuite) TestRunContainerNetModeWithExposePort(c *check.C) {
  298. cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
  299. out, _, err := runCommandWithOutput(cmd)
  300. if err != nil {
  301. c.Fatalf("failed to run container: %v, output: %q", err, out)
  302. }
  303. cmd = exec.Command(dockerBinary, "run", "-p", "5000:5000", "--net=container:parent", "busybox")
  304. out, _, err = runCommandWithOutput(cmd)
  305. if err == nil || !strings.Contains(out, "Conflicting options: -p, -P, --publish-all, --publish and the network mode (--net)") {
  306. c.Fatalf("run --net=container with -p should error out")
  307. }
  308. cmd = exec.Command(dockerBinary, "run", "-P", "--net=container:parent", "busybox")
  309. out, _, err = runCommandWithOutput(cmd)
  310. if err == nil || !strings.Contains(out, "Conflicting options: -p, -P, --publish-all, --publish and the network mode (--net)") {
  311. c.Fatalf("run --net=container with -P should error out")
  312. }
  313. cmd = exec.Command(dockerBinary, "run", "--expose", "5000", "--net=container:parent", "busybox")
  314. out, _, err = runCommandWithOutput(cmd)
  315. if err == nil || !strings.Contains(out, "Conflicting options: --expose and the network mode (--expose)") {
  316. c.Fatalf("run --net=container with --expose should error out")
  317. }
  318. }
  319. func (s *DockerSuite) TestRunLinkToContainerNetMode(c *check.C) {
  320. cmd := exec.Command(dockerBinary, "run", "--name", "test", "-d", "busybox", "top")
  321. out, _, err := runCommandWithOutput(cmd)
  322. if err != nil {
  323. c.Fatalf("failed to run container: %v, output: %q", err, out)
  324. }
  325. cmd = exec.Command(dockerBinary, "run", "--name", "parent", "-d", "--net=container:test", "busybox", "top")
  326. out, _, err = runCommandWithOutput(cmd)
  327. if err != nil {
  328. c.Fatalf("failed to run container: %v, output: %q", err, out)
  329. }
  330. cmd = exec.Command(dockerBinary, "run", "-d", "--link=parent:parent", "busybox", "top")
  331. out, _, err = runCommandWithOutput(cmd)
  332. if err != nil {
  333. c.Fatalf("failed to run container: %v, output: %q", err, out)
  334. }
  335. cmd = exec.Command(dockerBinary, "run", "--name", "child", "-d", "--net=container:parent", "busybox", "top")
  336. out, _, err = runCommandWithOutput(cmd)
  337. if err != nil {
  338. c.Fatalf("failed to run container: %v, output: %q", err, out)
  339. }
  340. cmd = exec.Command(dockerBinary, "run", "-d", "--link=child:child", "busybox", "top")
  341. out, _, err = runCommandWithOutput(cmd)
  342. if err != nil {
  343. c.Fatalf("failed to run container: %v, output: %q", err, out)
  344. }
  345. }
  346. func (s *DockerSuite) TestRunLoopbackOnlyExistsWhenNetworkingDisabled(c *check.C) {
  347. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up")
  348. out, _, err := runCommandWithOutput(cmd)
  349. if err != nil {
  350. c.Fatal(err, out)
  351. }
  352. var (
  353. count = 0
  354. parts = strings.Split(out, "\n")
  355. )
  356. for _, l := range parts {
  357. if l != "" {
  358. count++
  359. }
  360. }
  361. if count != 1 {
  362. c.Fatalf("Wrong interface count in container %d", count)
  363. }
  364. if !strings.HasPrefix(out, "1: lo") {
  365. c.Fatalf("Wrong interface in test container: expected [1: lo], got %s", out)
  366. }
  367. }
  368. // Issue #4681
  369. func (s *DockerSuite) TestRunLoopbackWhenNetworkDisabled(c *check.C) {
  370. cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1")
  371. if _, err := runCommand(cmd); err != nil {
  372. c.Fatal(err)
  373. }
  374. }
  375. func (s *DockerSuite) TestRunModeNetContainerHostname(c *check.C) {
  376. testRequires(c, ExecSupport)
  377. cmd := exec.Command(dockerBinary, "run", "-i", "-d", "--name", "parent", "busybox", "top")
  378. out, _, err := runCommandWithOutput(cmd)
  379. if err != nil {
  380. c.Fatalf("failed to run container: %v, output: %q", err, out)
  381. }
  382. cmd = exec.Command(dockerBinary, "exec", "parent", "cat", "/etc/hostname")
  383. out, _, err = runCommandWithOutput(cmd)
  384. if err != nil {
  385. c.Fatalf("failed to exec command: %v, output: %q", err, out)
  386. }
  387. cmd = exec.Command(dockerBinary, "run", "--net=container:parent", "busybox", "cat", "/etc/hostname")
  388. out1, _, err := runCommandWithOutput(cmd)
  389. if err != nil {
  390. c.Fatalf("failed to run container: %v, output: %q", err, out1)
  391. }
  392. if out1 != out {
  393. c.Fatal("containers with shared net namespace should have same hostname")
  394. }
  395. }
  396. func (s *DockerSuite) TestRunNetworkNotInitializedNoneMode(c *check.C) {
  397. cmd := exec.Command(dockerBinary, "run", "-d", "--net=none", "busybox", "top")
  398. out, _, err := runCommandWithOutput(cmd)
  399. if err != nil {
  400. c.Fatal(err)
  401. }
  402. id := strings.TrimSpace(out)
  403. res, err := inspectField(id, "NetworkSettings.IPAddress")
  404. c.Assert(err, check.IsNil)
  405. if res != "" {
  406. c.Fatalf("For 'none' mode network must not be initialized, but container got IP: %s", res)
  407. }
  408. }
  409. func (s *DockerSuite) TestTwoContainersInNetHost(c *check.C) {
  410. dockerCmd(c, "run", "-d", "--net=host", "--name=first", "busybox", "top")
  411. dockerCmd(c, "run", "-d", "--net=host", "--name=second", "busybox", "top")
  412. dockerCmd(c, "stop", "first")
  413. dockerCmd(c, "stop", "second")
  414. }