docker_cli_run_unix_test.go 14 KB

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