docker_cli_run_unix_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // +build !windows
  2. package main
  3. import (
  4. "bufio"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "github.com/docker/docker/pkg/mount"
  13. "github.com/go-check/check"
  14. "github.com/kr/pty"
  15. )
  16. // #6509
  17. func (s *DockerSuite) TestRunRedirectStdout(c *check.C) {
  18. checkRedirect := func(command string) {
  19. _, tty, err := pty.Open()
  20. if err != nil {
  21. c.Fatalf("Could not open pty: %v", err)
  22. }
  23. cmd := exec.Command("sh", "-c", command)
  24. cmd.Stdin = tty
  25. cmd.Stdout = tty
  26. cmd.Stderr = tty
  27. if err := cmd.Start(); err != nil {
  28. c.Fatalf("start err: %v", err)
  29. }
  30. ch := make(chan error)
  31. go func() {
  32. ch <- cmd.Wait()
  33. close(ch)
  34. }()
  35. select {
  36. case <-time.After(10 * time.Second):
  37. c.Fatal("command timeout")
  38. case err := <-ch:
  39. if err != nil {
  40. c.Fatalf("wait err=%v", err)
  41. }
  42. }
  43. }
  44. checkRedirect(dockerBinary + " run -i busybox cat /etc/passwd | grep -q root")
  45. checkRedirect(dockerBinary + " run busybox cat /etc/passwd | grep -q root")
  46. }
  47. // Test recursive bind mount works by default
  48. func (s *DockerSuite) TestRunWithVolumesIsRecursive(c *check.C) {
  49. tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
  50. if err != nil {
  51. c.Fatal(err)
  52. }
  53. defer os.RemoveAll(tmpDir)
  54. // Create a temporary tmpfs mount.
  55. tmpfsDir := filepath.Join(tmpDir, "tmpfs")
  56. if err := os.MkdirAll(tmpfsDir, 0777); err != nil {
  57. c.Fatalf("failed to mkdir at %s - %s", tmpfsDir, err)
  58. }
  59. if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
  60. c.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
  61. }
  62. f, err := ioutil.TempFile(tmpfsDir, "touch-me")
  63. if err != nil {
  64. c.Fatal(err)
  65. }
  66. defer f.Close()
  67. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
  68. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  69. if err != nil && exitCode != 0 {
  70. c.Fatal(out, stderr, err)
  71. }
  72. if !strings.Contains(out, filepath.Base(f.Name())) {
  73. c.Fatal("Recursive bind mount test failed. Expected file not found")
  74. }
  75. }
  76. func (s *DockerSuite) TestRunDeviceDirectory(c *check.C) {
  77. testRequires(c, NativeExecDriver)
  78. if _, err := os.Stat("/dev/snd"); err != nil {
  79. c.Skip("Host does not have /dev/snd")
  80. }
  81. out, _ := dockerCmd(c, "run", "--device", "/dev/snd:/dev/snd", "busybox", "sh", "-c", "ls /dev/snd/")
  82. if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "timer") {
  83. c.Fatalf("expected output /dev/snd/timer, received %s", actual)
  84. }
  85. out, _ = dockerCmd(c, "run", "--device", "/dev/snd:/dev/othersnd", "busybox", "sh", "-c", "ls /dev/othersnd/")
  86. if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "seq") {
  87. c.Fatalf("expected output /dev/othersnd/seq, received %s", actual)
  88. }
  89. }
  90. // TestRunDetach checks attaching and detaching with the escape sequence.
  91. func (s *DockerSuite) TestRunAttachDetach(c *check.C) {
  92. name := "attach-detach"
  93. cmd := exec.Command(dockerBinary, "run", "--name", name, "-it", "busybox", "cat")
  94. stdout, err := cmd.StdoutPipe()
  95. if err != nil {
  96. c.Fatal(err)
  97. }
  98. cpty, tty, err := pty.Open()
  99. if err != nil {
  100. c.Fatal(err)
  101. }
  102. defer cpty.Close()
  103. cmd.Stdin = tty
  104. if err := cmd.Start(); err != nil {
  105. c.Fatal(err)
  106. }
  107. c.Assert(waitRun(name), check.IsNil)
  108. if _, err := cpty.Write([]byte("hello\n")); err != nil {
  109. c.Fatal(err)
  110. }
  111. out, err := bufio.NewReader(stdout).ReadString('\n')
  112. if err != nil {
  113. c.Fatal(err)
  114. }
  115. if strings.TrimSpace(out) != "hello" {
  116. c.Fatalf("expected 'hello', got %q", out)
  117. }
  118. // escape sequence
  119. if _, err := cpty.Write([]byte{16}); err != nil {
  120. c.Fatal(err)
  121. }
  122. time.Sleep(100 * time.Millisecond)
  123. if _, err := cpty.Write([]byte{17}); err != nil {
  124. c.Fatal(err)
  125. }
  126. ch := make(chan struct{})
  127. go func() {
  128. cmd.Wait()
  129. ch <- struct{}{}
  130. }()
  131. running, err := inspectField(name, "State.Running")
  132. if err != nil {
  133. c.Fatal(err)
  134. }
  135. if running != "true" {
  136. c.Fatal("expected container to still be running")
  137. }
  138. go func() {
  139. exec.Command(dockerBinary, "kill", name).Run()
  140. }()
  141. select {
  142. case <-ch:
  143. case <-time.After(10 * time.Millisecond):
  144. c.Fatal("timed out waiting for container to exit")
  145. }
  146. }
  147. // "test" should be printed
  148. func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) {
  149. testRequires(c, cpuCfsQuota)
  150. out, _, err := dockerCmdWithError("run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test")
  151. if err != nil {
  152. c.Fatalf("failed to run container: %v, output: %q", err, out)
  153. }
  154. out = strings.TrimSpace(out)
  155. if out != "test" {
  156. c.Errorf("container should've printed 'test'")
  157. }
  158. out, err = inspectField("test", "HostConfig.CpuQuota")
  159. c.Assert(err, check.IsNil)
  160. if out != "8000" {
  161. c.Fatalf("setting the CPU CFS quota failed")
  162. }
  163. }
  164. func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) {
  165. testRequires(c, cpuCfsPeriod)
  166. if _, _, err := dockerCmdWithError("run", "--cpu-period", "50000", "--name", "test", "busybox", "true"); err != nil {
  167. c.Fatalf("failed to run container: %v", err)
  168. }
  169. out, err := inspectField("test", "HostConfig.CpuPeriod")
  170. c.Assert(err, check.IsNil)
  171. if out != "50000" {
  172. c.Fatalf("setting the CPU CFS period failed")
  173. }
  174. }
  175. func (s *DockerSuite) TestRunWithKernelMemory(c *check.C) {
  176. testRequires(c, kernelMemorySupport)
  177. dockerCmd(c, "run", "--kernel-memory", "50M", "--name", "test", "busybox", "true")
  178. out, err := inspectField("test", "HostConfig.KernelMemory")
  179. c.Assert(err, check.IsNil)
  180. if out != "52428800" {
  181. c.Fatalf("setting the kernel memory limit failed")
  182. }
  183. }
  184. // "test" should be printed
  185. func (s *DockerSuite) TestRunEchoStdoutWitCPUShares(c *check.C) {
  186. testRequires(c, cpuShare)
  187. out, _ := dockerCmd(c, "run", "--cpu-shares", "1000", "busybox", "echo", "test")
  188. if out != "test\n" {
  189. c.Errorf("container should've printed 'test', got %q instead", out)
  190. }
  191. }
  192. // "test" should be printed
  193. func (s *DockerSuite) TestRunEchoStdoutWithCPUSharesAndMemoryLimit(c *check.C) {
  194. testRequires(c, cpuShare)
  195. testRequires(c, memoryLimitSupport)
  196. out, _ := dockerCmd(c, "run", "--cpu-shares", "1000", "-m", "16m", "busybox", "echo", "test")
  197. if out != "test\n" {
  198. c.Errorf("container should've printed 'test', got %q instead", out)
  199. }
  200. }
  201. func (s *DockerSuite) TestRunWithCpuset(c *check.C) {
  202. testRequires(c, cgroupCpuset)
  203. if _, code := dockerCmd(c, "run", "--cpuset", "0", "busybox", "true"); code != 0 {
  204. c.Fatalf("container should run successfully with cpuset of 0")
  205. }
  206. }
  207. func (s *DockerSuite) TestRunWithCpusetCpus(c *check.C) {
  208. testRequires(c, cgroupCpuset)
  209. if _, code := dockerCmd(c, "run", "--cpuset-cpus", "0", "busybox", "true"); code != 0 {
  210. c.Fatalf("container should run successfully with cpuset-cpus of 0")
  211. }
  212. }
  213. func (s *DockerSuite) TestRunWithCpusetMems(c *check.C) {
  214. testRequires(c, cgroupCpuset)
  215. if _, code := dockerCmd(c, "run", "--cpuset-mems", "0", "busybox", "true"); code != 0 {
  216. c.Fatalf("container should run successfully with cpuset-mems of 0")
  217. }
  218. }
  219. func (s *DockerSuite) TestRunWithBlkioWeight(c *check.C) {
  220. testRequires(c, blkioWeight)
  221. if _, code := dockerCmd(c, "run", "--blkio-weight", "300", "busybox", "true"); code != 0 {
  222. c.Fatalf("container should run successfully with blkio-weight of 300")
  223. }
  224. }
  225. func (s *DockerSuite) TestRunWithBlkioInvalidWeight(c *check.C) {
  226. testRequires(c, blkioWeight)
  227. if _, _, err := dockerCmdWithError("run", "--blkio-weight", "5", "busybox", "true"); err == nil {
  228. c.Fatalf("run with invalid blkio-weight should failed")
  229. }
  230. }
  231. func (s *DockerSuite) TestRunOOMExitCode(c *check.C) {
  232. testRequires(c, oomControl)
  233. errChan := make(chan error)
  234. go func() {
  235. defer close(errChan)
  236. //changing memory to 40MB from 4MB due to an issue with GCCGO that test fails to start the container.
  237. out, exitCode, _ := dockerCmdWithError("run", "-m", "40MB", "busybox", "sh", "-c", "x=a; while true; do x=$x$x$x$x; done")
  238. if expected := 137; exitCode != expected {
  239. errChan <- fmt.Errorf("wrong exit code for OOM container: expected %d, got %d (output: %q)", expected, exitCode, out)
  240. }
  241. }()
  242. select {
  243. case err := <-errChan:
  244. c.Assert(err, check.IsNil)
  245. case <-time.After(30 * time.Second):
  246. c.Fatal("Timeout waiting for container to die on OOM")
  247. }
  248. }
  249. // "test" should be printed
  250. func (s *DockerSuite) TestRunEchoStdoutWithMemoryLimit(c *check.C) {
  251. testRequires(c, memoryLimitSupport)
  252. out, _ := dockerCmd(c, "run", "-m", "16m", "busybox", "echo", "test")
  253. out = strings.Trim(out, "\r\n")
  254. if expected := "test"; out != expected {
  255. c.Fatalf("container should've printed %q but printed %q", expected, out)
  256. }
  257. }
  258. // TestRunWithoutMemoryswapLimit sets memory limit and disables swap
  259. // memory limit, this means the processes in the container can use
  260. // 16M memory and as much swap memory as they need (if the host
  261. // supports swap memory).
  262. func (s *DockerSuite) TestRunWithoutMemoryswapLimit(c *check.C) {
  263. testRequires(c, NativeExecDriver)
  264. testRequires(c, memoryLimitSupport)
  265. testRequires(c, swapMemorySupport)
  266. dockerCmd(c, "run", "-m", "16m", "--memory-swap", "-1", "busybox", "true")
  267. }
  268. func (s *DockerSuite) TestRunWithSwappiness(c *check.C) {
  269. testRequires(c, memorySwappinessSupport)
  270. dockerCmd(c, "run", "--memory-swappiness", "0", "busybox", "true")
  271. }
  272. func (s *DockerSuite) TestRunWithSwappinessInvalid(c *check.C) {
  273. testRequires(c, memorySwappinessSupport)
  274. out, _, err := dockerCmdWithError("run", "--memory-swappiness", "101", "busybox", "true")
  275. if err == nil {
  276. c.Fatalf("failed. test was able to set invalid value, output: %q", out)
  277. }
  278. }
  279. func (s *DockerSuite) TestStopContainerSignal(c *check.C) {
  280. out, _ := dockerCmd(c, "run", "--stop-signal", "SIGUSR1", "-d", "busybox", "/bin/sh", "-c", `trap 'echo "exit trapped"; exit 0' USR1; while true; do sleep 1; done`)
  281. containerID := strings.TrimSpace(out)
  282. if err := waitRun(containerID); err != nil {
  283. c.Fatal(err)
  284. }
  285. dockerCmd(c, "stop", containerID)
  286. out, _ = dockerCmd(c, "logs", containerID)
  287. if !strings.Contains(out, "exit trapped") {
  288. c.Fatalf("Expected `exit trapped` in the log, got %v", out)
  289. }
  290. }