docker_cli_run_unix_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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, _ := dockerCmd(c, "run", "--name=testulimits", "--ulimit", "nofile=42", "busybox", "/bin/sh", "-c", "ulimit -n")
  80. ul := strings.TrimSpace(out)
  81. if ul != "42" {
  82. c.Fatalf("expected `ulimit -n` to be 42, got %s", ul)
  83. }
  84. }
  85. func (s *DockerSuite) TestRunContainerWithCgroupParent(c *check.C) {
  86. testRequires(c, NativeExecDriver)
  87. cgroupParent := "test"
  88. name := "cgroup-test"
  89. out, _, err := dockerCmdWithError("run", "--cgroup-parent", cgroupParent, "--name", name, "busybox", "cat", "/proc/self/cgroup")
  90. if err != nil {
  91. c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
  92. }
  93. cgroupPaths := parseCgroupPaths(string(out))
  94. if len(cgroupPaths) == 0 {
  95. c.Fatalf("unexpected output - %q", string(out))
  96. }
  97. id, err := getIDByName(name)
  98. c.Assert(err, check.IsNil)
  99. expectedCgroup := path.Join(cgroupParent, id)
  100. found := false
  101. for _, path := range cgroupPaths {
  102. if strings.HasSuffix(path, expectedCgroup) {
  103. found = true
  104. break
  105. }
  106. }
  107. if !found {
  108. c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have suffix %q. Cgroup Paths: %v", expectedCgroup, cgroupPaths)
  109. }
  110. }
  111. func (s *DockerSuite) TestRunContainerWithCgroupParentAbsPath(c *check.C) {
  112. testRequires(c, NativeExecDriver)
  113. cgroupParent := "/cgroup-parent/test"
  114. name := "cgroup-test"
  115. out, _, err := dockerCmdWithError("run", "--cgroup-parent", cgroupParent, "--name", name, "busybox", "cat", "/proc/self/cgroup")
  116. if err != nil {
  117. c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
  118. }
  119. cgroupPaths := parseCgroupPaths(string(out))
  120. if len(cgroupPaths) == 0 {
  121. c.Fatalf("unexpected output - %q", string(out))
  122. }
  123. id, err := getIDByName(name)
  124. c.Assert(err, check.IsNil)
  125. expectedCgroup := path.Join(cgroupParent, id)
  126. found := false
  127. for _, path := range cgroupPaths {
  128. if strings.HasSuffix(path, expectedCgroup) {
  129. found = true
  130. break
  131. }
  132. }
  133. if !found {
  134. c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have suffix %q. Cgroup Paths: %v", expectedCgroup, cgroupPaths)
  135. }
  136. }
  137. func (s *DockerSuite) TestRunContainerWithCgroupMountRO(c *check.C) {
  138. testRequires(c, NativeExecDriver)
  139. filename := "/sys/fs/cgroup/devices/test123"
  140. out, _, err := dockerCmdWithError("run", "busybox", "touch", filename)
  141. if err == nil {
  142. c.Fatal("expected cgroup mount point to be read-only, touch file should fail")
  143. }
  144. expected := "Read-only file system"
  145. if !strings.Contains(out, expected) {
  146. c.Fatalf("expected output from failure to contain %s but contains %s", expected, out)
  147. }
  148. }
  149. func (s *DockerSuite) TestRunDeviceDirectory(c *check.C) {
  150. testRequires(c, NativeExecDriver)
  151. out, _ := dockerCmd(c, "run", "--device", "/dev/snd:/dev/snd", "busybox", "sh", "-c", "ls /dev/snd/")
  152. if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "timer") {
  153. c.Fatalf("expected output /dev/snd/timer, received %s", actual)
  154. }
  155. out, _ = dockerCmd(c, "run", "--device", "/dev/snd:/dev/othersnd", "busybox", "sh", "-c", "ls /dev/othersnd/")
  156. if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "seq") {
  157. c.Fatalf("expected output /dev/othersnd/seq, received %s", actual)
  158. }
  159. }
  160. // TestRunDetach checks attaching and detaching with the escape sequence.
  161. func (s *DockerSuite) TestRunAttachDetach(c *check.C) {
  162. name := "attach-detach"
  163. cmd := exec.Command(dockerBinary, "run", "--name", name, "-it", "busybox", "cat")
  164. stdout, err := cmd.StdoutPipe()
  165. if err != nil {
  166. c.Fatal(err)
  167. }
  168. cpty, tty, err := pty.Open()
  169. if err != nil {
  170. c.Fatal(err)
  171. }
  172. defer cpty.Close()
  173. cmd.Stdin = tty
  174. if err := cmd.Start(); err != nil {
  175. c.Fatal(err)
  176. }
  177. c.Assert(waitRun(name), check.IsNil)
  178. if _, err := cpty.Write([]byte("hello\n")); err != nil {
  179. c.Fatal(err)
  180. }
  181. out, err := bufio.NewReader(stdout).ReadString('\n')
  182. if err != nil {
  183. c.Fatal(err)
  184. }
  185. if strings.TrimSpace(out) != "hello" {
  186. c.Fatalf("expected 'hello', got %q", out)
  187. }
  188. // escape sequence
  189. if _, err := cpty.Write([]byte{16}); err != nil {
  190. c.Fatal(err)
  191. }
  192. time.Sleep(100 * time.Millisecond)
  193. if _, err := cpty.Write([]byte{17}); err != nil {
  194. c.Fatal(err)
  195. }
  196. ch := make(chan struct{})
  197. go func() {
  198. cmd.Wait()
  199. ch <- struct{}{}
  200. }()
  201. running, err := inspectField(name, "State.Running")
  202. if err != nil {
  203. c.Fatal(err)
  204. }
  205. if running != "true" {
  206. c.Fatal("expected container to still be running")
  207. }
  208. go func() {
  209. exec.Command(dockerBinary, "kill", name).Run()
  210. }()
  211. select {
  212. case <-ch:
  213. case <-time.After(10 * time.Millisecond):
  214. c.Fatal("timed out waiting for container to exit")
  215. }
  216. }
  217. // "test" should be printed
  218. func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) {
  219. testRequires(c, cpuCfsQuota)
  220. out, _, err := dockerCmdWithError("run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test")
  221. if err != nil {
  222. c.Fatalf("failed to run container: %v, output: %q", err, out)
  223. }
  224. out = strings.TrimSpace(out)
  225. if out != "test" {
  226. c.Errorf("container should've printed 'test'")
  227. }
  228. out, err = inspectField("test", "HostConfig.CpuQuota")
  229. c.Assert(err, check.IsNil)
  230. if out != "8000" {
  231. c.Fatalf("setting the CPU CFS quota failed")
  232. }
  233. }
  234. func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) {
  235. testRequires(c, cpuCfsPeriod)
  236. if _, _, err := dockerCmdWithError("run", "--cpu-period", "50000", "--name", "test", "busybox", "true"); err != nil {
  237. c.Fatalf("failed to run container: %v", err)
  238. }
  239. out, err := inspectField("test", "HostConfig.CpuPeriod")
  240. c.Assert(err, check.IsNil)
  241. if out != "50000" {
  242. c.Fatalf("setting the CPU CFS period failed")
  243. }
  244. }
  245. func (s *DockerSuite) TestRunWithKernelMemory(c *check.C) {
  246. testRequires(c, kernelMemorySupport)
  247. dockerCmd(c, "run", "--kernel-memory", "50M", "--name", "test", "busybox", "true")
  248. out, err := inspectField("test", "HostConfig.KernelMemory")
  249. c.Assert(err, check.IsNil)
  250. if out != "52428800" {
  251. c.Fatalf("setting the kernel memory limit failed")
  252. }
  253. }
  254. // "test" should be printed
  255. func (s *DockerSuite) TestRunEchoStdoutWitCPUShares(c *check.C) {
  256. testRequires(c, cpuShare)
  257. out, _ := dockerCmd(c, "run", "-c", "1000", "busybox", "echo", "test")
  258. if out != "test\n" {
  259. c.Errorf("container should've printed 'test'")
  260. }
  261. }
  262. // "test" should be printed
  263. func (s *DockerSuite) TestRunEchoStdoutWithCPUSharesAndMemoryLimit(c *check.C) {
  264. testRequires(c, cpuShare)
  265. testRequires(c, memoryLimitSupport)
  266. out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-c", "1000", "-m", "16m", "busybox", "echo", "test")
  267. if out != "test\n" {
  268. c.Errorf("container should've printed 'test', got %q instead", out)
  269. }
  270. }
  271. func (s *DockerSuite) TestRunWithCpuset(c *check.C) {
  272. testRequires(c, cgroupCpuset)
  273. if _, code := dockerCmd(c, "run", "--cpuset", "0", "busybox", "true"); code != 0 {
  274. c.Fatalf("container should run successfully with cpuset of 0")
  275. }
  276. }
  277. func (s *DockerSuite) TestRunWithCpusetCpus(c *check.C) {
  278. testRequires(c, cgroupCpuset)
  279. if _, code := dockerCmd(c, "run", "--cpuset-cpus", "0", "busybox", "true"); code != 0 {
  280. c.Fatalf("container should run successfully with cpuset-cpus of 0")
  281. }
  282. }
  283. func (s *DockerSuite) TestRunWithCpusetMems(c *check.C) {
  284. testRequires(c, cgroupCpuset)
  285. if _, code := dockerCmd(c, "run", "--cpuset-mems", "0", "busybox", "true"); code != 0 {
  286. c.Fatalf("container should run successfully with cpuset-mems of 0")
  287. }
  288. }
  289. func (s *DockerSuite) TestRunWithBlkioWeight(c *check.C) {
  290. testRequires(c, blkioWeight)
  291. if _, code := dockerCmd(c, "run", "--blkio-weight", "300", "busybox", "true"); code != 0 {
  292. c.Fatalf("container should run successfully with blkio-weight of 300")
  293. }
  294. }
  295. func (s *DockerSuite) TestRunWithBlkioInvalidWeight(c *check.C) {
  296. testRequires(c, blkioWeight)
  297. if _, _, err := dockerCmdWithError("run", "--blkio-weight", "5", "busybox", "true"); err == nil {
  298. c.Fatalf("run with invalid blkio-weight should failed")
  299. }
  300. }
  301. func (s *DockerSuite) TestRunOOMExitCode(c *check.C) {
  302. testRequires(c, oomControl)
  303. errChan := make(chan error)
  304. go func() {
  305. defer close(errChan)
  306. out, exitCode, _ := dockerCmdWithError("run", "-m", "4MB", "busybox", "sh", "-c", "x=a; while true; do x=$x$x$x$x; done")
  307. if expected := 137; exitCode != expected {
  308. errChan <- fmt.Errorf("wrong exit code for OOM container: expected %d, got %d (output: %q)", expected, exitCode, out)
  309. }
  310. }()
  311. select {
  312. case err := <-errChan:
  313. c.Assert(err, check.IsNil)
  314. case <-time.After(30 * time.Second):
  315. c.Fatal("Timeout waiting for container to die on OOM")
  316. }
  317. }
  318. func (s *DockerSuite) TestContainerNetworkModeToSelf(c *check.C) {
  319. out, _, err := dockerCmdWithError("run", "--name=me", "--net=container:me", "busybox", "true")
  320. if err == nil || !strings.Contains(out, "cannot join own network") {
  321. c.Fatalf("using container net mode to self should result in an error")
  322. }
  323. }
  324. func (s *DockerSuite) TestRunContainerNetModeWithDnsMacHosts(c *check.C) {
  325. out, _, err := dockerCmdWithError("run", "-d", "--name", "parent", "busybox", "top")
  326. if err != nil {
  327. c.Fatalf("failed to run container: %v, output: %q", err, out)
  328. }
  329. out, _, err = dockerCmdWithError("run", "--dns", "1.2.3.4", "--net=container:parent", "busybox")
  330. if err == nil || !strings.Contains(out, "Conflicting options: --dns and the network mode") {
  331. c.Fatalf("run --net=container with --dns should error out")
  332. }
  333. out, _, err = dockerCmdWithError("run", "--mac-address", "92:d0:c6:0a:29:33", "--net=container:parent", "busybox")
  334. if err == nil || !strings.Contains(out, "--mac-address and the network mode") {
  335. c.Fatalf("run --net=container with --mac-address should error out")
  336. }
  337. out, _, err = dockerCmdWithError("run", "--add-host", "test:192.168.2.109", "--net=container:parent", "busybox")
  338. if err == nil || !strings.Contains(out, "--add-host and the network mode") {
  339. c.Fatalf("run --net=container with --add-host should error out")
  340. }
  341. }
  342. func (s *DockerSuite) TestRunContainerNetModeWithExposePort(c *check.C) {
  343. dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
  344. out, _, err := dockerCmdWithError("run", "-p", "5000:5000", "--net=container:parent", "busybox")
  345. if err == nil || !strings.Contains(out, "Conflicting options: -p, -P, --publish-all, --publish and the network mode (--net)") {
  346. c.Fatalf("run --net=container with -p should error out")
  347. }
  348. out, _, err = dockerCmdWithError("run", "-P", "--net=container:parent", "busybox")
  349. if err == nil || !strings.Contains(out, "Conflicting options: -p, -P, --publish-all, --publish and the network mode (--net)") {
  350. c.Fatalf("run --net=container with -P should error out")
  351. }
  352. out, _, err = dockerCmdWithError("run", "--expose", "5000", "--net=container:parent", "busybox")
  353. if err == nil || !strings.Contains(out, "Conflicting options: --expose and the network mode (--expose)") {
  354. c.Fatalf("run --net=container with --expose should error out")
  355. }
  356. }
  357. func (s *DockerSuite) TestRunLinkToContainerNetMode(c *check.C) {
  358. dockerCmd(c, "run", "--name", "test", "-d", "busybox", "top")
  359. dockerCmd(c, "run", "--name", "parent", "-d", "--net=container:test", "busybox", "top")
  360. dockerCmd(c, "run", "-d", "--link=parent:parent", "busybox", "top")
  361. dockerCmd(c, "run", "--name", "child", "-d", "--net=container:parent", "busybox", "top")
  362. dockerCmd(c, "run", "-d", "--link=child:child", "busybox", "top")
  363. }
  364. func (s *DockerSuite) TestRunLoopbackOnlyExistsWhenNetworkingDisabled(c *check.C) {
  365. out, _ := dockerCmd(c, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up")
  366. var (
  367. count = 0
  368. parts = strings.Split(out, "\n")
  369. )
  370. for _, l := range parts {
  371. if l != "" {
  372. count++
  373. }
  374. }
  375. if count != 1 {
  376. c.Fatalf("Wrong interface count in container %d", count)
  377. }
  378. if !strings.HasPrefix(out, "1: lo") {
  379. c.Fatalf("Wrong interface in test container: expected [1: lo], got %s", out)
  380. }
  381. }
  382. // Issue #4681
  383. func (s *DockerSuite) TestRunLoopbackWhenNetworkDisabled(c *check.C) {
  384. dockerCmd(c, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1")
  385. }
  386. func (s *DockerSuite) TestRunModeNetContainerHostname(c *check.C) {
  387. testRequires(c, ExecSupport)
  388. dockerCmd(c, "run", "-i", "-d", "--name", "parent", "busybox", "top")
  389. out, _ := dockerCmd(c, "exec", "parent", "cat", "/etc/hostname")
  390. out1, _ := dockerCmd(c, "run", "--net=container:parent", "busybox", "cat", "/etc/hostname")
  391. if out1 != out {
  392. c.Fatal("containers with shared net namespace should have same hostname")
  393. }
  394. }
  395. func (s *DockerSuite) TestRunNetworkNotInitializedNoneMode(c *check.C) {
  396. out, _, err := dockerCmdWithError("run", "-d", "--net=none", "busybox", "top")
  397. id := strings.TrimSpace(out)
  398. res, err := inspectField(id, "NetworkSettings.IPAddress")
  399. c.Assert(err, check.IsNil)
  400. if res != "" {
  401. c.Fatalf("For 'none' mode network must not be initialized, but container got IP: %s", res)
  402. }
  403. }
  404. func (s *DockerSuite) TestTwoContainersInNetHost(c *check.C) {
  405. dockerCmd(c, "run", "-d", "--net=host", "--name=first", "busybox", "top")
  406. dockerCmd(c, "run", "-d", "--net=host", "--name=second", "busybox", "top")
  407. dockerCmd(c, "stop", "first")
  408. dockerCmd(c, "stop", "second")
  409. }
  410. // "test" should be printed
  411. func (s *DockerSuite) TestRunEchoStdoutWithMemoryLimit(c *check.C) {
  412. testRequires(c, memoryLimitSupport)
  413. out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-m", "16m", "busybox", "echo", "test")
  414. out = strings.Trim(out, "\r\n")
  415. if expected := "test"; out != expected {
  416. c.Fatalf("container should've printed %q but printed %q", expected, out)
  417. }
  418. }
  419. // should run without memory swap
  420. func (s *DockerSuite) TestRunWithoutMemoryswapLimit(c *check.C) {
  421. testRequires(c, NativeExecDriver)
  422. testRequires(c, memoryLimitSupport)
  423. testRequires(c, swapMemorySupport)
  424. dockerCmd(c, "run", "-m", "16m", "--memory-swap", "-1", "busybox", "true")
  425. }
  426. func (s *DockerSuite) TestRunWithSwappiness(c *check.C) {
  427. testRequires(c, memorySwappinessSupport)
  428. dockerCmd(c, "run", "--memory-swappiness", "0", "busybox", "true")
  429. }
  430. func (s *DockerSuite) TestRunWithSwappinessInvalid(c *check.C) {
  431. testRequires(c, memorySwappinessSupport)
  432. out, _, err := dockerCmdWithError("run", "--memory-swappiness", "101", "busybox", "true")
  433. if err == nil {
  434. c.Fatalf("failed. test was able to set invalid value, output: %q", out)
  435. }
  436. }