docker_cli_run_unix_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // +build !windows
  2. package main
  3. import (
  4. "bufio"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/docker/docker/pkg/integration/checker"
  14. "github.com/docker/docker/pkg/mount"
  15. "github.com/docker/docker/pkg/parsers"
  16. "github.com/docker/docker/pkg/sysinfo"
  17. "github.com/go-check/check"
  18. "github.com/kr/pty"
  19. )
  20. // #6509
  21. func (s *DockerSuite) TestRunRedirectStdout(c *check.C) {
  22. checkRedirect := func(command string) {
  23. _, tty, err := pty.Open()
  24. if err != nil {
  25. c.Fatalf("Could not open pty: %v", err)
  26. }
  27. cmd := exec.Command("sh", "-c", command)
  28. cmd.Stdin = tty
  29. cmd.Stdout = tty
  30. cmd.Stderr = tty
  31. if err := cmd.Start(); err != nil {
  32. c.Fatalf("start err: %v", err)
  33. }
  34. ch := make(chan error)
  35. go func() {
  36. ch <- cmd.Wait()
  37. close(ch)
  38. }()
  39. select {
  40. case <-time.After(10 * time.Second):
  41. c.Fatal("command timeout")
  42. case err := <-ch:
  43. if err != nil {
  44. c.Fatalf("wait err=%v", err)
  45. }
  46. }
  47. }
  48. checkRedirect(dockerBinary + " run -i busybox cat /etc/passwd | grep -q root")
  49. checkRedirect(dockerBinary + " run busybox cat /etc/passwd | grep -q root")
  50. }
  51. // Test recursive bind mount works by default
  52. func (s *DockerSuite) TestRunWithVolumesIsRecursive(c *check.C) {
  53. // /tmp gets permission denied
  54. testRequires(c, NotUserNamespace)
  55. tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
  56. if err != nil {
  57. c.Fatal(err)
  58. }
  59. defer os.RemoveAll(tmpDir)
  60. // Create a temporary tmpfs mount.
  61. tmpfsDir := filepath.Join(tmpDir, "tmpfs")
  62. if err := os.MkdirAll(tmpfsDir, 0777); err != nil {
  63. c.Fatalf("failed to mkdir at %s - %s", tmpfsDir, err)
  64. }
  65. if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
  66. c.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
  67. }
  68. f, err := ioutil.TempFile(tmpfsDir, "touch-me")
  69. if err != nil {
  70. c.Fatal(err)
  71. }
  72. defer f.Close()
  73. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
  74. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  75. if err != nil && exitCode != 0 {
  76. c.Fatal(out, stderr, err)
  77. }
  78. if !strings.Contains(out, filepath.Base(f.Name())) {
  79. c.Fatal("Recursive bind mount test failed. Expected file not found")
  80. }
  81. }
  82. func (s *DockerSuite) TestRunDeviceDirectory(c *check.C) {
  83. testRequires(c, DaemonIsLinux, NotUserNamespace)
  84. if _, err := os.Stat("/dev/snd"); err != nil {
  85. c.Skip("Host does not have /dev/snd")
  86. }
  87. out, _ := dockerCmd(c, "run", "--device", "/dev/snd:/dev/snd", "busybox", "sh", "-c", "ls /dev/snd/")
  88. if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "timer") {
  89. c.Fatalf("expected output /dev/snd/timer, received %s", actual)
  90. }
  91. out, _ = dockerCmd(c, "run", "--device", "/dev/snd:/dev/othersnd", "busybox", "sh", "-c", "ls /dev/othersnd/")
  92. if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "seq") {
  93. c.Fatalf("expected output /dev/othersnd/seq, received %s", actual)
  94. }
  95. }
  96. // TestRunDetach checks attaching and detaching with the escape sequence.
  97. func (s *DockerSuite) TestRunAttachDetach(c *check.C) {
  98. name := "attach-detach"
  99. cmd := exec.Command(dockerBinary, "run", "--name", name, "-it", "busybox", "cat")
  100. stdout, err := cmd.StdoutPipe()
  101. if err != nil {
  102. c.Fatal(err)
  103. }
  104. cpty, tty, err := pty.Open()
  105. if err != nil {
  106. c.Fatal(err)
  107. }
  108. defer cpty.Close()
  109. cmd.Stdin = tty
  110. if err := cmd.Start(); err != nil {
  111. c.Fatal(err)
  112. }
  113. c.Assert(waitRun(name), check.IsNil)
  114. if _, err := cpty.Write([]byte("hello\n")); err != nil {
  115. c.Fatal(err)
  116. }
  117. out, err := bufio.NewReader(stdout).ReadString('\n')
  118. if err != nil {
  119. c.Fatal(err)
  120. }
  121. if strings.TrimSpace(out) != "hello" {
  122. c.Fatalf("expected 'hello', got %q", out)
  123. }
  124. // escape sequence
  125. if _, err := cpty.Write([]byte{16}); err != nil {
  126. c.Fatal(err)
  127. }
  128. time.Sleep(100 * time.Millisecond)
  129. if _, err := cpty.Write([]byte{17}); err != nil {
  130. c.Fatal(err)
  131. }
  132. ch := make(chan struct{})
  133. go func() {
  134. cmd.Wait()
  135. ch <- struct{}{}
  136. }()
  137. running, err := inspectField(name, "State.Running")
  138. if err != nil {
  139. c.Fatal(err)
  140. }
  141. if running != "true" {
  142. c.Fatal("expected container to still be running")
  143. }
  144. go func() {
  145. exec.Command(dockerBinary, "kill", name).Run()
  146. }()
  147. select {
  148. case <-ch:
  149. case <-time.After(10 * time.Millisecond):
  150. c.Fatal("timed out waiting for container to exit")
  151. }
  152. }
  153. // "test" should be printed
  154. func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) {
  155. testRequires(c, cpuCfsQuota)
  156. out, _, err := dockerCmdWithError("run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test")
  157. if err != nil {
  158. c.Fatalf("failed to run container: %v, output: %q", err, out)
  159. }
  160. out = strings.TrimSpace(out)
  161. if out != "test" {
  162. c.Errorf("container should've printed 'test'")
  163. }
  164. out, err = inspectField("test", "HostConfig.CpuQuota")
  165. c.Assert(err, check.IsNil)
  166. if out != "8000" {
  167. c.Fatalf("setting the CPU CFS quota failed")
  168. }
  169. }
  170. func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) {
  171. testRequires(c, cpuCfsPeriod)
  172. if _, _, err := dockerCmdWithError("run", "--cpu-period", "50000", "--name", "test", "busybox", "true"); err != nil {
  173. c.Fatalf("failed to run container: %v", err)
  174. }
  175. out, err := inspectField("test", "HostConfig.CpuPeriod")
  176. c.Assert(err, check.IsNil)
  177. if out != "50000" {
  178. c.Fatalf("setting the CPU CFS period failed")
  179. }
  180. }
  181. func (s *DockerSuite) TestRunWithKernelMemory(c *check.C) {
  182. testRequires(c, kernelMemorySupport)
  183. dockerCmd(c, "run", "--kernel-memory", "50M", "--name", "test1", "busybox", "true")
  184. out, err := inspectField("test1", "HostConfig.KernelMemory")
  185. c.Assert(err, check.IsNil)
  186. c.Assert(out, check.Equals, "52428800")
  187. out, _, err = dockerCmdWithError("run", "--kernel-memory", "-16m", "--name", "test2", "busybox", "echo", "test")
  188. expected := "invalid size"
  189. c.Assert(err, check.NotNil)
  190. c.Assert(out, checker.Contains, expected)
  191. }
  192. // "test" should be printed
  193. func (s *DockerSuite) TestRunEchoStdoutWitCPUShares(c *check.C) {
  194. testRequires(c, cpuShare)
  195. out, _ := dockerCmd(c, "run", "--cpu-shares", "1000", "busybox", "echo", "test")
  196. if out != "test\n" {
  197. c.Errorf("container should've printed 'test', got %q instead", out)
  198. }
  199. }
  200. // "test" should be printed
  201. func (s *DockerSuite) TestRunEchoStdoutWithCPUSharesAndMemoryLimit(c *check.C) {
  202. testRequires(c, cpuShare)
  203. testRequires(c, memoryLimitSupport)
  204. out, _, _ := dockerCmdWithStdoutStderr(c, "run", "--cpu-shares", "1000", "-m", "32m", "busybox", "echo", "test")
  205. if out != "test\n" {
  206. c.Errorf("container should've printed 'test', got %q instead", out)
  207. }
  208. }
  209. func (s *DockerSuite) TestRunWithCpuset(c *check.C) {
  210. testRequires(c, cgroupCpuset)
  211. if _, code := dockerCmd(c, "run", "--cpuset", "0", "busybox", "true"); code != 0 {
  212. c.Fatalf("container should run successfully with cpuset of 0")
  213. }
  214. }
  215. func (s *DockerSuite) TestRunWithCpusetCpus(c *check.C) {
  216. testRequires(c, cgroupCpuset)
  217. if _, code := dockerCmd(c, "run", "--cpuset-cpus", "0", "busybox", "true"); code != 0 {
  218. c.Fatalf("container should run successfully with cpuset-cpus of 0")
  219. }
  220. }
  221. func (s *DockerSuite) TestRunWithCpusetMems(c *check.C) {
  222. testRequires(c, cgroupCpuset)
  223. if _, code := dockerCmd(c, "run", "--cpuset-mems", "0", "busybox", "true"); code != 0 {
  224. c.Fatalf("container should run successfully with cpuset-mems of 0")
  225. }
  226. }
  227. func (s *DockerSuite) TestRunWithBlkioWeight(c *check.C) {
  228. testRequires(c, blkioWeight)
  229. if _, code := dockerCmd(c, "run", "--blkio-weight", "300", "busybox", "true"); code != 0 {
  230. c.Fatalf("container should run successfully with blkio-weight of 300")
  231. }
  232. }
  233. func (s *DockerSuite) TestRunWithBlkioInvalidWeight(c *check.C) {
  234. testRequires(c, blkioWeight)
  235. out, _, err := dockerCmdWithError("run", "--blkio-weight", "5", "busybox", "true")
  236. c.Assert(err, check.NotNil, check.Commentf(out))
  237. expected := "Range of blkio weight is from 10 to 1000"
  238. c.Assert(out, checker.Contains, expected)
  239. }
  240. func (s *DockerSuite) TestRunOOMExitCode(c *check.C) {
  241. testRequires(c, oomControl)
  242. errChan := make(chan error)
  243. go func() {
  244. defer close(errChan)
  245. //changing memory to 40MB from 4MB due to an issue with GCCGO that test fails to start the container.
  246. out, exitCode, _ := dockerCmdWithError("run", "-m", "40MB", "busybox", "sh", "-c", "x=a; while true; do x=$x$x$x$x; done")
  247. if expected := 137; exitCode != expected {
  248. errChan <- fmt.Errorf("wrong exit code for OOM container: expected %d, got %d (output: %q)", expected, exitCode, out)
  249. }
  250. }()
  251. select {
  252. case err := <-errChan:
  253. c.Assert(err, check.IsNil)
  254. case <-time.After(30 * time.Second):
  255. c.Fatal("Timeout waiting for container to die on OOM")
  256. }
  257. }
  258. // "test" should be printed
  259. func (s *DockerSuite) TestRunEchoStdoutWithMemoryLimit(c *check.C) {
  260. testRequires(c, memoryLimitSupport)
  261. out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-m", "32m", "busybox", "echo", "test")
  262. out = strings.Trim(out, "\r\n")
  263. if expected := "test"; out != expected {
  264. c.Fatalf("container should've printed %q but printed %q", expected, out)
  265. }
  266. }
  267. // TestRunWithoutMemoryswapLimit sets memory limit and disables swap
  268. // memory limit, this means the processes in the container can use
  269. // 16M memory and as much swap memory as they need (if the host
  270. // supports swap memory).
  271. func (s *DockerSuite) TestRunWithoutMemoryswapLimit(c *check.C) {
  272. testRequires(c, DaemonIsLinux)
  273. testRequires(c, memoryLimitSupport)
  274. testRequires(c, swapMemorySupport)
  275. dockerCmd(c, "run", "-m", "16m", "--memory-swap", "-1", "busybox", "true")
  276. }
  277. func (s *DockerSuite) TestRunWithSwappiness(c *check.C) {
  278. testRequires(c, memorySwappinessSupport)
  279. dockerCmd(c, "run", "--memory-swappiness", "0", "busybox", "true")
  280. }
  281. func (s *DockerSuite) TestRunWithSwappinessInvalid(c *check.C) {
  282. testRequires(c, memorySwappinessSupport)
  283. out, _, err := dockerCmdWithError("run", "--memory-swappiness", "101", "busybox", "true")
  284. c.Assert(err, check.NotNil)
  285. expected := "Valid memory swappiness range is 0-100"
  286. c.Assert(out, checker.Contains, expected, check.Commentf("Expected output to contain %q, not %q", out, expected))
  287. out, _, err = dockerCmdWithError("run", "--memory-swappiness", "-10", "busybox", "true")
  288. c.Assert(err, check.NotNil)
  289. c.Assert(out, checker.Contains, expected, check.Commentf("Expected output to contain %q, not %q", out, expected))
  290. }
  291. func (s *DockerSuite) TestRunWithMemoryReservation(c *check.C) {
  292. testRequires(c, memoryReservationSupport)
  293. dockerCmd(c, "run", "--memory-reservation", "200M", "busybox", "true")
  294. }
  295. func (s *DockerSuite) TestRunWithMemoryReservationInvalid(c *check.C) {
  296. testRequires(c, memoryLimitSupport)
  297. testRequires(c, memoryReservationSupport)
  298. out, _, err := dockerCmdWithError("run", "-m", "500M", "--memory-reservation", "800M", "busybox", "true")
  299. c.Assert(err, check.NotNil)
  300. expected := "Minimum memory limit should be larger than memory reservation limit"
  301. if !strings.Contains(strings.TrimSpace(out), expected) {
  302. c.Fatalf("run container should fail with invalid memory reservation, output: %q", out)
  303. }
  304. }
  305. func (s *DockerSuite) TestStopContainerSignal(c *check.C) {
  306. 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`)
  307. containerID := strings.TrimSpace(out)
  308. if err := waitRun(containerID); err != nil {
  309. c.Fatal(err)
  310. }
  311. dockerCmd(c, "stop", containerID)
  312. out, _ = dockerCmd(c, "logs", containerID)
  313. if !strings.Contains(out, "exit trapped") {
  314. c.Fatalf("Expected `exit trapped` in the log, got %v", out)
  315. }
  316. }
  317. func (s *DockerSuite) TestRunSwapLessThanMemoryLimit(c *check.C) {
  318. testRequires(c, memoryLimitSupport)
  319. testRequires(c, swapMemorySupport)
  320. out, _, err := dockerCmdWithError("run", "-m", "16m", "--memory-swap", "15m", "busybox", "echo", "test")
  321. expected := "Minimum memoryswap limit should be larger than memory limit"
  322. c.Assert(err, check.NotNil)
  323. if !strings.Contains(out, expected) {
  324. c.Fatalf("Expected output to contain %q, not %q", out, expected)
  325. }
  326. }
  327. func (s *DockerSuite) TestRunInvalidCpusetCpusFlagValue(c *check.C) {
  328. testRequires(c, cgroupCpuset)
  329. sysInfo := sysinfo.New(true)
  330. cpus, err := parsers.ParseUintList(sysInfo.Cpus)
  331. c.Assert(err, check.IsNil)
  332. var invalid int
  333. for i := 0; i <= len(cpus)+1; i++ {
  334. if !cpus[i] {
  335. invalid = i
  336. break
  337. }
  338. }
  339. out, _, err := dockerCmdWithError("run", "--cpuset-cpus", strconv.Itoa(invalid), "busybox", "true")
  340. c.Assert(err, check.NotNil)
  341. expected := fmt.Sprintf("Error response from daemon: Requested CPUs are not available - requested %s, available: %s.\n", strconv.Itoa(invalid), sysInfo.Cpus)
  342. c.Assert(out, check.Equals, expected, check.Commentf("Expected output to contain %q, got %q", expected, out))
  343. }
  344. func (s *DockerSuite) TestRunInvalidCpusetMemsFlagValue(c *check.C) {
  345. testRequires(c, cgroupCpuset)
  346. sysInfo := sysinfo.New(true)
  347. mems, err := parsers.ParseUintList(sysInfo.Mems)
  348. c.Assert(err, check.IsNil)
  349. var invalid int
  350. for i := 0; i <= len(mems)+1; i++ {
  351. if !mems[i] {
  352. invalid = i
  353. break
  354. }
  355. }
  356. out, _, err := dockerCmdWithError("run", "--cpuset-mems", strconv.Itoa(invalid), "busybox", "true")
  357. c.Assert(err, check.NotNil)
  358. expected := fmt.Sprintf("Error response from daemon: Requested memory nodes are not available - requested %s, available: %s.\n", strconv.Itoa(invalid), sysInfo.Mems)
  359. c.Assert(out, check.Equals, expected, check.Commentf("Expected output to contain %q, got %q", expected, out))
  360. }
  361. func (s *DockerSuite) TestRunInvalidCPUShares(c *check.C) {
  362. testRequires(c, cpuShare, DaemonIsLinux)
  363. out, _, err := dockerCmdWithError("run", "--cpu-shares", "1", "busybox", "echo", "test")
  364. c.Assert(err, check.NotNil, check.Commentf(out))
  365. expected := "The minimum allowed cpu-shares is 2"
  366. c.Assert(out, checker.Contains, expected)
  367. out, _, err = dockerCmdWithError("run", "--cpu-shares", "-1", "busybox", "echo", "test")
  368. c.Assert(err, check.NotNil, check.Commentf(out))
  369. expected = "shares: invalid argument"
  370. c.Assert(out, checker.Contains, expected)
  371. out, _, err = dockerCmdWithError("run", "--cpu-shares", "99999999", "busybox", "echo", "test")
  372. c.Assert(err, check.NotNil, check.Commentf(out))
  373. expected = "The maximum allowed cpu-shares is"
  374. c.Assert(out, checker.Contains, expected)
  375. }