docker_cli_run_unix_test.go 15 KB

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