docker_cli_run_unix_test.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. // +build !windows
  2. package main
  3. import (
  4. "bufio"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "regexp"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/docker/docker/pkg/homedir"
  16. "github.com/docker/docker/pkg/integration/checker"
  17. "github.com/docker/docker/pkg/mount"
  18. "github.com/docker/docker/pkg/parsers"
  19. "github.com/docker/docker/pkg/sysinfo"
  20. "github.com/go-check/check"
  21. "github.com/kr/pty"
  22. )
  23. // #6509
  24. func (s *DockerSuite) TestRunRedirectStdout(c *check.C) {
  25. checkRedirect := func(command string) {
  26. _, tty, err := pty.Open()
  27. c.Assert(err, checker.IsNil, check.Commentf("Could not open pty"))
  28. cmd := exec.Command("sh", "-c", command)
  29. cmd.Stdin = tty
  30. cmd.Stdout = tty
  31. cmd.Stderr = tty
  32. c.Assert(cmd.Start(), checker.IsNil)
  33. ch := make(chan error)
  34. go func() {
  35. ch <- cmd.Wait()
  36. close(ch)
  37. }()
  38. select {
  39. case <-time.After(10 * time.Second):
  40. c.Fatal("command timeout")
  41. case err := <-ch:
  42. c.Assert(err, checker.IsNil, check.Commentf("wait err"))
  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. // /tmp gets permission denied
  51. testRequires(c, NotUserNamespace, SameHostDaemon)
  52. tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
  53. c.Assert(err, checker.IsNil)
  54. defer os.RemoveAll(tmpDir)
  55. // Create a temporary tmpfs mount.
  56. tmpfsDir := filepath.Join(tmpDir, "tmpfs")
  57. c.Assert(os.MkdirAll(tmpfsDir, 0777), checker.IsNil, check.Commentf("failed to mkdir at %s", tmpfsDir))
  58. c.Assert(mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""), checker.IsNil, check.Commentf("failed to create a tmpfs mount at %s", tmpfsDir))
  59. f, err := ioutil.TempFile(tmpfsDir, "touch-me")
  60. c.Assert(err, checker.IsNil)
  61. defer f.Close()
  62. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
  63. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  64. c.Assert(err, checker.IsNil)
  65. c.Assert(out, checker.Contains, filepath.Base(f.Name()), check.Commentf("Recursive bind mount test failed. Expected file not found"))
  66. }
  67. func (s *DockerSuite) TestRunDeviceDirectory(c *check.C) {
  68. testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
  69. if _, err := os.Stat("/dev/snd"); err != nil {
  70. c.Skip("Host does not have /dev/snd")
  71. }
  72. out, _ := dockerCmd(c, "run", "--device", "/dev/snd:/dev/snd", "busybox", "sh", "-c", "ls /dev/snd/")
  73. c.Assert(strings.Trim(out, "\r\n"), checker.Contains, "timer", check.Commentf("expected output /dev/snd/timer"))
  74. out, _ = dockerCmd(c, "run", "--device", "/dev/snd:/dev/othersnd", "busybox", "sh", "-c", "ls /dev/othersnd/")
  75. c.Assert(strings.Trim(out, "\r\n"), checker.Contains, "seq", check.Commentf("expected output /dev/othersnd/seq"))
  76. }
  77. // TestRunDetach checks attaching and detaching with the default escape sequence.
  78. func (s *DockerSuite) TestRunAttachDetach(c *check.C) {
  79. name := "attach-detach"
  80. dockerCmd(c, "run", "--name", name, "-itd", "busybox", "cat")
  81. cmd := exec.Command(dockerBinary, "attach", name)
  82. stdout, err := cmd.StdoutPipe()
  83. c.Assert(err, checker.IsNil)
  84. cpty, tty, err := pty.Open()
  85. c.Assert(err, checker.IsNil)
  86. defer cpty.Close()
  87. cmd.Stdin = tty
  88. c.Assert(cmd.Start(), checker.IsNil)
  89. c.Assert(waitRun(name), check.IsNil)
  90. _, err = cpty.Write([]byte("hello\n"))
  91. c.Assert(err, checker.IsNil)
  92. out, err := bufio.NewReader(stdout).ReadString('\n')
  93. c.Assert(err, checker.IsNil)
  94. c.Assert(strings.TrimSpace(out), checker.Equals, "hello")
  95. // escape sequence
  96. _, err = cpty.Write([]byte{16})
  97. c.Assert(err, checker.IsNil)
  98. time.Sleep(100 * time.Millisecond)
  99. _, err = cpty.Write([]byte{17})
  100. c.Assert(err, checker.IsNil)
  101. ch := make(chan struct{})
  102. go func() {
  103. cmd.Wait()
  104. ch <- struct{}{}
  105. }()
  106. select {
  107. case <-ch:
  108. case <-time.After(10 * time.Second):
  109. c.Fatal("timed out waiting for container to exit")
  110. }
  111. running := inspectField(c, name, "State.Running")
  112. c.Assert(running, checker.Equals, "true", check.Commentf("expected container to still be running"))
  113. }
  114. // TestRunDetach checks attaching and detaching with the escape sequence specified via flags.
  115. func (s *DockerSuite) TestRunAttachDetachFromFlag(c *check.C) {
  116. name := "attach-detach"
  117. keyCtrlA := []byte{1}
  118. keyA := []byte{97}
  119. dockerCmd(c, "run", "--name", name, "-itd", "busybox", "cat")
  120. cmd := exec.Command(dockerBinary, "attach", "--detach-keys='ctrl-a,a'", name)
  121. stdout, err := cmd.StdoutPipe()
  122. if err != nil {
  123. c.Fatal(err)
  124. }
  125. cpty, tty, err := pty.Open()
  126. if err != nil {
  127. c.Fatal(err)
  128. }
  129. defer cpty.Close()
  130. cmd.Stdin = tty
  131. if err := cmd.Start(); err != nil {
  132. c.Fatal(err)
  133. }
  134. c.Assert(waitRun(name), check.IsNil)
  135. if _, err := cpty.Write([]byte("hello\n")); err != nil {
  136. c.Fatal(err)
  137. }
  138. out, err := bufio.NewReader(stdout).ReadString('\n')
  139. if err != nil {
  140. c.Fatal(err)
  141. }
  142. if strings.TrimSpace(out) != "hello" {
  143. c.Fatalf("expected 'hello', got %q", out)
  144. }
  145. // escape sequence
  146. if _, err := cpty.Write(keyCtrlA); err != nil {
  147. c.Fatal(err)
  148. }
  149. time.Sleep(100 * time.Millisecond)
  150. if _, err := cpty.Write(keyA); err != nil {
  151. c.Fatal(err)
  152. }
  153. ch := make(chan struct{})
  154. go func() {
  155. cmd.Wait()
  156. ch <- struct{}{}
  157. }()
  158. select {
  159. case <-ch:
  160. case <-time.After(10 * time.Second):
  161. c.Fatal("timed out waiting for container to exit")
  162. }
  163. running := inspectField(c, name, "State.Running")
  164. c.Assert(running, checker.Equals, "true", check.Commentf("expected container to still be running"))
  165. }
  166. // TestRunDetach checks attaching and detaching with the escape sequence specified via config file.
  167. func (s *DockerSuite) TestRunAttachDetachFromConfig(c *check.C) {
  168. keyCtrlA := []byte{1}
  169. keyA := []byte{97}
  170. // Setup config
  171. homeKey := homedir.Key()
  172. homeVal := homedir.Get()
  173. tmpDir, err := ioutil.TempDir("", "fake-home")
  174. c.Assert(err, checker.IsNil)
  175. defer os.RemoveAll(tmpDir)
  176. dotDocker := filepath.Join(tmpDir, ".docker")
  177. os.Mkdir(dotDocker, 0600)
  178. tmpCfg := filepath.Join(dotDocker, "config.json")
  179. defer func() { os.Setenv(homeKey, homeVal) }()
  180. os.Setenv(homeKey, tmpDir)
  181. data := `{
  182. "detachKeys": "ctrl-a,a"
  183. }`
  184. err = ioutil.WriteFile(tmpCfg, []byte(data), 0600)
  185. c.Assert(err, checker.IsNil)
  186. // Then do the work
  187. name := "attach-detach"
  188. dockerCmd(c, "run", "--name", name, "-itd", "busybox", "cat")
  189. cmd := exec.Command(dockerBinary, "attach", name)
  190. stdout, err := cmd.StdoutPipe()
  191. if err != nil {
  192. c.Fatal(err)
  193. }
  194. cpty, tty, err := pty.Open()
  195. if err != nil {
  196. c.Fatal(err)
  197. }
  198. defer cpty.Close()
  199. cmd.Stdin = tty
  200. if err := cmd.Start(); err != nil {
  201. c.Fatal(err)
  202. }
  203. c.Assert(waitRun(name), check.IsNil)
  204. if _, err := cpty.Write([]byte("hello\n")); err != nil {
  205. c.Fatal(err)
  206. }
  207. out, err := bufio.NewReader(stdout).ReadString('\n')
  208. if err != nil {
  209. c.Fatal(err)
  210. }
  211. if strings.TrimSpace(out) != "hello" {
  212. c.Fatalf("expected 'hello', got %q", out)
  213. }
  214. // escape sequence
  215. if _, err := cpty.Write(keyCtrlA); err != nil {
  216. c.Fatal(err)
  217. }
  218. time.Sleep(100 * time.Millisecond)
  219. if _, err := cpty.Write(keyA); err != nil {
  220. c.Fatal(err)
  221. }
  222. ch := make(chan struct{})
  223. go func() {
  224. cmd.Wait()
  225. ch <- struct{}{}
  226. }()
  227. select {
  228. case <-ch:
  229. case <-time.After(10 * time.Second):
  230. c.Fatal("timed out waiting for container to exit")
  231. }
  232. running := inspectField(c, name, "State.Running")
  233. c.Assert(running, checker.Equals, "true", check.Commentf("expected container to still be running"))
  234. }
  235. // TestRunDetach checks attaching and detaching with the detach flags, making sure it overrides config file
  236. func (s *DockerSuite) TestRunAttachDetachKeysOverrideConfig(c *check.C) {
  237. keyCtrlA := []byte{1}
  238. keyA := []byte{97}
  239. // Setup config
  240. homeKey := homedir.Key()
  241. homeVal := homedir.Get()
  242. tmpDir, err := ioutil.TempDir("", "fake-home")
  243. c.Assert(err, checker.IsNil)
  244. defer os.RemoveAll(tmpDir)
  245. dotDocker := filepath.Join(tmpDir, ".docker")
  246. os.Mkdir(dotDocker, 0600)
  247. tmpCfg := filepath.Join(dotDocker, "config.json")
  248. defer func() { os.Setenv(homeKey, homeVal) }()
  249. os.Setenv(homeKey, tmpDir)
  250. data := `{
  251. "detachKeys": "ctrl-e,e"
  252. }`
  253. err = ioutil.WriteFile(tmpCfg, []byte(data), 0600)
  254. c.Assert(err, checker.IsNil)
  255. // Then do the work
  256. name := "attach-detach"
  257. dockerCmd(c, "run", "--name", name, "-itd", "busybox", "cat")
  258. cmd := exec.Command(dockerBinary, "attach", "--detach-keys='ctrl-a,a'", name)
  259. stdout, err := cmd.StdoutPipe()
  260. if err != nil {
  261. c.Fatal(err)
  262. }
  263. cpty, tty, err := pty.Open()
  264. if err != nil {
  265. c.Fatal(err)
  266. }
  267. defer cpty.Close()
  268. cmd.Stdin = tty
  269. if err := cmd.Start(); err != nil {
  270. c.Fatal(err)
  271. }
  272. c.Assert(waitRun(name), check.IsNil)
  273. if _, err := cpty.Write([]byte("hello\n")); err != nil {
  274. c.Fatal(err)
  275. }
  276. out, err := bufio.NewReader(stdout).ReadString('\n')
  277. if err != nil {
  278. c.Fatal(err)
  279. }
  280. if strings.TrimSpace(out) != "hello" {
  281. c.Fatalf("expected 'hello', got %q", out)
  282. }
  283. // escape sequence
  284. if _, err := cpty.Write(keyCtrlA); err != nil {
  285. c.Fatal(err)
  286. }
  287. time.Sleep(100 * time.Millisecond)
  288. if _, err := cpty.Write(keyA); err != nil {
  289. c.Fatal(err)
  290. }
  291. ch := make(chan struct{})
  292. go func() {
  293. cmd.Wait()
  294. ch <- struct{}{}
  295. }()
  296. select {
  297. case <-ch:
  298. case <-time.After(10 * time.Second):
  299. c.Fatal("timed out waiting for container to exit")
  300. }
  301. running := inspectField(c, name, "State.Running")
  302. c.Assert(running, checker.Equals, "true", check.Commentf("expected container to still be running"))
  303. }
  304. // "test" should be printed
  305. func (s *DockerSuite) TestRunWithCPUQuota(c *check.C) {
  306. testRequires(c, cpuCfsQuota)
  307. file := "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
  308. out, _ := dockerCmd(c, "run", "--cpu-quota", "8000", "--name", "test", "busybox", "cat", file)
  309. c.Assert(strings.TrimSpace(out), checker.Equals, "8000")
  310. out = inspectField(c, "test", "HostConfig.CpuQuota")
  311. c.Assert(out, checker.Equals, "8000", check.Commentf("setting the CPU CFS quota failed"))
  312. }
  313. func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) {
  314. testRequires(c, cpuCfsPeriod)
  315. file := "/sys/fs/cgroup/cpu/cpu.cfs_period_us"
  316. out, _ := dockerCmd(c, "run", "--cpu-period", "50000", "--name", "test", "busybox", "cat", file)
  317. c.Assert(strings.TrimSpace(out), checker.Equals, "50000")
  318. out = inspectField(c, "test", "HostConfig.CpuPeriod")
  319. c.Assert(out, checker.Equals, "50000", check.Commentf("setting the CPU CFS period failed"))
  320. }
  321. func (s *DockerSuite) TestRunWithKernelMemory(c *check.C) {
  322. testRequires(c, kernelMemorySupport)
  323. file := "/sys/fs/cgroup/memory/memory.kmem.limit_in_bytes"
  324. stdout, _, _ := dockerCmdWithStdoutStderr(c, "run", "--kernel-memory", "50M", "--name", "test1", "busybox", "cat", file)
  325. c.Assert(strings.TrimSpace(stdout), checker.Equals, "52428800")
  326. out := inspectField(c, "test1", "HostConfig.KernelMemory")
  327. c.Assert(out, check.Equals, "52428800")
  328. }
  329. func (s *DockerSuite) TestRunWithInvalidKernelMemory(c *check.C) {
  330. testRequires(c, kernelMemorySupport)
  331. out, _, err := dockerCmdWithError("run", "--kernel-memory", "2M", "busybox", "true")
  332. c.Assert(err, check.NotNil)
  333. expected := "Minimum kernel memory limit allowed is 4MB"
  334. c.Assert(out, checker.Contains, expected)
  335. out, _, err = dockerCmdWithError("run", "--kernel-memory", "-16m", "--name", "test2", "busybox", "echo", "test")
  336. c.Assert(err, check.NotNil)
  337. expected = "invalid size"
  338. c.Assert(out, checker.Contains, expected)
  339. }
  340. func (s *DockerSuite) TestRunWithCPUShares(c *check.C) {
  341. testRequires(c, cpuShare)
  342. file := "/sys/fs/cgroup/cpu/cpu.shares"
  343. out, _ := dockerCmd(c, "run", "--cpu-shares", "1000", "--name", "test", "busybox", "cat", file)
  344. c.Assert(strings.TrimSpace(out), checker.Equals, "1000")
  345. out = inspectField(c, "test", "HostConfig.CPUShares")
  346. c.Assert(out, check.Equals, "1000")
  347. }
  348. // "test" should be printed
  349. func (s *DockerSuite) TestRunEchoStdoutWithCPUSharesAndMemoryLimit(c *check.C) {
  350. testRequires(c, cpuShare)
  351. testRequires(c, memoryLimitSupport)
  352. out, _, _ := dockerCmdWithStdoutStderr(c, "run", "--cpu-shares", "1000", "-m", "32m", "busybox", "echo", "test")
  353. c.Assert(out, checker.Equals, "test\n", check.Commentf("container should've printed 'test'"))
  354. }
  355. func (s *DockerSuite) TestRunWithCpusetCpus(c *check.C) {
  356. testRequires(c, cgroupCpuset)
  357. file := "/sys/fs/cgroup/cpuset/cpuset.cpus"
  358. out, _ := dockerCmd(c, "run", "--cpuset-cpus", "0", "--name", "test", "busybox", "cat", file)
  359. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  360. out = inspectField(c, "test", "HostConfig.CpusetCpus")
  361. c.Assert(out, check.Equals, "0")
  362. }
  363. func (s *DockerSuite) TestRunWithCpusetMems(c *check.C) {
  364. testRequires(c, cgroupCpuset)
  365. file := "/sys/fs/cgroup/cpuset/cpuset.mems"
  366. out, _ := dockerCmd(c, "run", "--cpuset-mems", "0", "--name", "test", "busybox", "cat", file)
  367. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  368. out = inspectField(c, "test", "HostConfig.CpusetMems")
  369. c.Assert(out, check.Equals, "0")
  370. }
  371. func (s *DockerSuite) TestRunWithBlkioWeight(c *check.C) {
  372. testRequires(c, blkioWeight)
  373. file := "/sys/fs/cgroup/blkio/blkio.weight"
  374. out, _ := dockerCmd(c, "run", "--blkio-weight", "300", "--name", "test", "busybox", "cat", file)
  375. c.Assert(strings.TrimSpace(out), checker.Equals, "300")
  376. out = inspectField(c, "test", "HostConfig.BlkioWeight")
  377. c.Assert(out, check.Equals, "300")
  378. }
  379. func (s *DockerSuite) TestRunWithInvalidBlkioWeight(c *check.C) {
  380. testRequires(c, blkioWeight)
  381. out, _, err := dockerCmdWithError("run", "--blkio-weight", "5", "busybox", "true")
  382. c.Assert(err, check.NotNil, check.Commentf(out))
  383. expected := "Range of blkio weight is from 10 to 1000"
  384. c.Assert(out, checker.Contains, expected)
  385. }
  386. func (s *DockerSuite) TestRunWithInvalidPathforBlkioWeightDevice(c *check.C) {
  387. testRequires(c, blkioWeight)
  388. out, _, err := dockerCmdWithError("run", "--blkio-weight-device", "/dev/sdX:100", "busybox", "true")
  389. c.Assert(err, check.NotNil, check.Commentf(out))
  390. }
  391. func (s *DockerSuite) TestRunWithInvalidPathforBlkioDeviceReadBps(c *check.C) {
  392. testRequires(c, blkioWeight)
  393. out, _, err := dockerCmdWithError("run", "--device-read-bps", "/dev/sdX:500", "busybox", "true")
  394. c.Assert(err, check.NotNil, check.Commentf(out))
  395. }
  396. func (s *DockerSuite) TestRunWithInvalidPathforBlkioDeviceWriteBps(c *check.C) {
  397. testRequires(c, blkioWeight)
  398. out, _, err := dockerCmdWithError("run", "--device-write-bps", "/dev/sdX:500", "busybox", "true")
  399. c.Assert(err, check.NotNil, check.Commentf(out))
  400. }
  401. func (s *DockerSuite) TestRunWithInvalidPathforBlkioDeviceReadIOps(c *check.C) {
  402. testRequires(c, blkioWeight)
  403. out, _, err := dockerCmdWithError("run", "--device-read-iops", "/dev/sdX:500", "busybox", "true")
  404. c.Assert(err, check.NotNil, check.Commentf(out))
  405. }
  406. func (s *DockerSuite) TestRunWithInvalidPathforBlkioDeviceWriteIOps(c *check.C) {
  407. testRequires(c, blkioWeight)
  408. out, _, err := dockerCmdWithError("run", "--device-write-iops", "/dev/sdX:500", "busybox", "true")
  409. c.Assert(err, check.NotNil, check.Commentf(out))
  410. }
  411. func (s *DockerSuite) TestRunOOMExitCode(c *check.C) {
  412. testRequires(c, oomControl)
  413. errChan := make(chan error)
  414. go func() {
  415. defer close(errChan)
  416. //changing memory to 40MB from 4MB due to an issue with GCCGO that test fails to start the container.
  417. out, exitCode, _ := dockerCmdWithError("run", "-m", "40MB", "busybox", "sh", "-c", "x=a; while true; do x=$x$x$x$x; done")
  418. if expected := 137; exitCode != expected {
  419. errChan <- fmt.Errorf("wrong exit code for OOM container: expected %d, got %d (output: %q)", expected, exitCode, out)
  420. }
  421. }()
  422. select {
  423. case err := <-errChan:
  424. c.Assert(err, check.IsNil)
  425. case <-time.After(600 * time.Second):
  426. c.Fatal("Timeout waiting for container to die on OOM")
  427. }
  428. }
  429. func (s *DockerSuite) TestRunWithMemoryLimit(c *check.C) {
  430. testRequires(c, memoryLimitSupport)
  431. file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
  432. stdout, _, _ := dockerCmdWithStdoutStderr(c, "run", "-m", "32M", "--name", "test", "busybox", "cat", file)
  433. c.Assert(strings.TrimSpace(stdout), checker.Equals, "33554432")
  434. out := inspectField(c, "test", "HostConfig.Memory")
  435. c.Assert(out, check.Equals, "33554432")
  436. }
  437. // TestRunWithoutMemoryswapLimit sets memory limit and disables swap
  438. // memory limit, this means the processes in the container can use
  439. // 16M memory and as much swap memory as they need (if the host
  440. // supports swap memory).
  441. func (s *DockerSuite) TestRunWithoutMemoryswapLimit(c *check.C) {
  442. testRequires(c, DaemonIsLinux)
  443. testRequires(c, memoryLimitSupport)
  444. testRequires(c, swapMemorySupport)
  445. dockerCmd(c, "run", "-m", "32m", "--memory-swap", "-1", "busybox", "true")
  446. }
  447. func (s *DockerSuite) TestRunWithSwappiness(c *check.C) {
  448. testRequires(c, memorySwappinessSupport)
  449. file := "/sys/fs/cgroup/memory/memory.swappiness"
  450. out, _ := dockerCmd(c, "run", "--memory-swappiness", "0", "--name", "test", "busybox", "cat", file)
  451. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  452. out = inspectField(c, "test", "HostConfig.MemorySwappiness")
  453. c.Assert(out, check.Equals, "0")
  454. }
  455. func (s *DockerSuite) TestRunWithSwappinessInvalid(c *check.C) {
  456. testRequires(c, memorySwappinessSupport)
  457. out, _, err := dockerCmdWithError("run", "--memory-swappiness", "101", "busybox", "true")
  458. c.Assert(err, check.NotNil)
  459. expected := "Valid memory swappiness range is 0-100"
  460. c.Assert(out, checker.Contains, expected, check.Commentf("Expected output to contain %q, not %q", out, expected))
  461. out, _, err = dockerCmdWithError("run", "--memory-swappiness", "-10", "busybox", "true")
  462. c.Assert(err, check.NotNil)
  463. c.Assert(out, checker.Contains, expected, check.Commentf("Expected output to contain %q, not %q", out, expected))
  464. }
  465. func (s *DockerSuite) TestRunWithMemoryReservation(c *check.C) {
  466. testRequires(c, memoryReservationSupport)
  467. file := "/sys/fs/cgroup/memory/memory.soft_limit_in_bytes"
  468. out, _ := dockerCmd(c, "run", "--memory-reservation", "200M", "--name", "test", "busybox", "cat", file)
  469. c.Assert(strings.TrimSpace(out), checker.Equals, "209715200")
  470. out = inspectField(c, "test", "HostConfig.MemoryReservation")
  471. c.Assert(out, check.Equals, "209715200")
  472. }
  473. func (s *DockerSuite) TestRunWithMemoryReservationInvalid(c *check.C) {
  474. testRequires(c, memoryLimitSupport)
  475. testRequires(c, memoryReservationSupport)
  476. out, _, err := dockerCmdWithError("run", "-m", "500M", "--memory-reservation", "800M", "busybox", "true")
  477. c.Assert(err, check.NotNil)
  478. expected := "Minimum memory limit should be larger than memory reservation limit"
  479. c.Assert(strings.TrimSpace(out), checker.Contains, expected, check.Commentf("run container should fail with invalid memory reservation"))
  480. }
  481. func (s *DockerSuite) TestStopContainerSignal(c *check.C) {
  482. 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`)
  483. containerID := strings.TrimSpace(out)
  484. c.Assert(waitRun(containerID), checker.IsNil)
  485. dockerCmd(c, "stop", containerID)
  486. out, _ = dockerCmd(c, "logs", containerID)
  487. c.Assert(out, checker.Contains, "exit trapped", check.Commentf("Expected `exit trapped` in the log"))
  488. }
  489. func (s *DockerSuite) TestRunSwapLessThanMemoryLimit(c *check.C) {
  490. testRequires(c, memoryLimitSupport)
  491. testRequires(c, swapMemorySupport)
  492. out, _, err := dockerCmdWithError("run", "-m", "16m", "--memory-swap", "15m", "busybox", "echo", "test")
  493. expected := "Minimum memoryswap limit should be larger than memory limit"
  494. c.Assert(err, check.NotNil)
  495. c.Assert(out, checker.Contains, expected)
  496. }
  497. func (s *DockerSuite) TestRunInvalidCpusetCpusFlagValue(c *check.C) {
  498. testRequires(c, cgroupCpuset, SameHostDaemon)
  499. sysInfo := sysinfo.New(true)
  500. cpus, err := parsers.ParseUintList(sysInfo.Cpus)
  501. c.Assert(err, check.IsNil)
  502. var invalid int
  503. for i := 0; i <= len(cpus)+1; i++ {
  504. if !cpus[i] {
  505. invalid = i
  506. break
  507. }
  508. }
  509. out, _, err := dockerCmdWithError("run", "--cpuset-cpus", strconv.Itoa(invalid), "busybox", "true")
  510. c.Assert(err, check.NotNil)
  511. expected := fmt.Sprintf("Error response from daemon: Requested CPUs are not available - requested %s, available: %s", strconv.Itoa(invalid), sysInfo.Cpus)
  512. c.Assert(out, checker.Contains, expected)
  513. }
  514. func (s *DockerSuite) TestRunInvalidCpusetMemsFlagValue(c *check.C) {
  515. testRequires(c, cgroupCpuset)
  516. sysInfo := sysinfo.New(true)
  517. mems, err := parsers.ParseUintList(sysInfo.Mems)
  518. c.Assert(err, check.IsNil)
  519. var invalid int
  520. for i := 0; i <= len(mems)+1; i++ {
  521. if !mems[i] {
  522. invalid = i
  523. break
  524. }
  525. }
  526. out, _, err := dockerCmdWithError("run", "--cpuset-mems", strconv.Itoa(invalid), "busybox", "true")
  527. c.Assert(err, check.NotNil)
  528. expected := fmt.Sprintf("Error response from daemon: Requested memory nodes are not available - requested %s, available: %s", strconv.Itoa(invalid), sysInfo.Mems)
  529. c.Assert(out, checker.Contains, expected)
  530. }
  531. func (s *DockerSuite) TestRunInvalidCPUShares(c *check.C) {
  532. testRequires(c, cpuShare, DaemonIsLinux)
  533. out, _, err := dockerCmdWithError("run", "--cpu-shares", "1", "busybox", "echo", "test")
  534. c.Assert(err, check.NotNil, check.Commentf(out))
  535. expected := "The minimum allowed cpu-shares is 2"
  536. c.Assert(out, checker.Contains, expected)
  537. out, _, err = dockerCmdWithError("run", "--cpu-shares", "-1", "busybox", "echo", "test")
  538. c.Assert(err, check.NotNil, check.Commentf(out))
  539. expected = "shares: invalid argument"
  540. c.Assert(out, checker.Contains, expected)
  541. out, _, err = dockerCmdWithError("run", "--cpu-shares", "99999999", "busybox", "echo", "test")
  542. c.Assert(err, check.NotNil, check.Commentf(out))
  543. expected = "The maximum allowed cpu-shares is"
  544. c.Assert(out, checker.Contains, expected)
  545. }
  546. func (s *DockerSuite) TestRunWithDefaultShmSize(c *check.C) {
  547. testRequires(c, DaemonIsLinux)
  548. name := "shm-default"
  549. out, _ := dockerCmd(c, "run", "--name", name, "busybox", "mount")
  550. shmRegex := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
  551. if !shmRegex.MatchString(out) {
  552. c.Fatalf("Expected shm of 64MB in mount command, got %v", out)
  553. }
  554. shmSize := inspectField(c, name, "HostConfig.ShmSize")
  555. c.Assert(shmSize, check.Equals, "67108864")
  556. }
  557. func (s *DockerSuite) TestRunWithShmSize(c *check.C) {
  558. testRequires(c, DaemonIsLinux)
  559. name := "shm"
  560. out, _ := dockerCmd(c, "run", "--name", name, "--shm-size=1G", "busybox", "mount")
  561. shmRegex := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=1048576k`)
  562. if !shmRegex.MatchString(out) {
  563. c.Fatalf("Expected shm of 1GB in mount command, got %v", out)
  564. }
  565. shmSize := inspectField(c, name, "HostConfig.ShmSize")
  566. c.Assert(shmSize, check.Equals, "1073741824")
  567. }
  568. func (s *DockerSuite) TestRunTmpfsMounts(c *check.C) {
  569. // TODO Windows (Post TP4): This test cannot run on a Windows daemon as
  570. // Windows does not support tmpfs mounts.
  571. testRequires(c, DaemonIsLinux)
  572. if out, _, err := dockerCmdWithError("run", "--tmpfs", "/run", "busybox", "touch", "/run/somefile"); err != nil {
  573. c.Fatalf("/run directory not mounted on tmpfs %q %s", err, out)
  574. }
  575. if out, _, err := dockerCmdWithError("run", "--tmpfs", "/run:noexec", "busybox", "touch", "/run/somefile"); err != nil {
  576. c.Fatalf("/run directory not mounted on tmpfs %q %s", err, out)
  577. }
  578. if out, _, err := dockerCmdWithError("run", "--tmpfs", "/run:noexec,nosuid,rw,size=5k,mode=700", "busybox", "touch", "/run/somefile"); err != nil {
  579. c.Fatalf("/run failed to mount on tmpfs with valid options %q %s", err, out)
  580. }
  581. if _, _, err := dockerCmdWithError("run", "--tmpfs", "/run:foobar", "busybox", "touch", "/run/somefile"); err == nil {
  582. c.Fatalf("/run mounted on tmpfs when it should have vailed within invalid mount option")
  583. }
  584. if _, _, err := dockerCmdWithError("run", "--tmpfs", "/run", "-v", "/run:/run", "busybox", "touch", "/run/somefile"); err == nil {
  585. c.Fatalf("Should have generated an error saying Duplicate mount points")
  586. }
  587. }
  588. // TestRunSeccompProfileDenyUnshare checks that 'docker run --security-opt seccomp:/tmp/profile.json debian:jessie unshare' exits with operation not permitted.
  589. func (s *DockerSuite) TestRunSeccompProfileDenyUnshare(c *check.C) {
  590. testRequires(c, SameHostDaemon, seccompEnabled, NotArm, Apparmor)
  591. jsonData := `{
  592. "defaultAction": "SCMP_ACT_ALLOW",
  593. "syscalls": [
  594. {
  595. "name": "unshare",
  596. "action": "SCMP_ACT_ERRNO"
  597. }
  598. ]
  599. }`
  600. tmpFile, err := ioutil.TempFile("", "profile.json")
  601. defer tmpFile.Close()
  602. if err != nil {
  603. c.Fatal(err)
  604. }
  605. if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
  606. c.Fatal(err)
  607. }
  608. runCmd := exec.Command(dockerBinary, "run", "--security-opt", "apparmor:unconfined", "--security-opt", "seccomp:"+tmpFile.Name(), "debian:jessie", "unshare", "-p", "-m", "-f", "-r", "mount", "-t", "proc", "none", "/proc")
  609. out, _, _ := runCommandWithOutput(runCmd)
  610. if !strings.Contains(out, "Operation not permitted") {
  611. c.Fatalf("expected unshare with seccomp profile denied to fail, got %s", out)
  612. }
  613. }
  614. // TestRunSeccompProfileDenyChmod checks that 'docker run --security-opt seccomp:/tmp/profile.json busybox chmod 400 /etc/hostname' exits with operation not permitted.
  615. func (s *DockerSuite) TestRunSeccompProfileDenyChmod(c *check.C) {
  616. testRequires(c, SameHostDaemon, seccompEnabled)
  617. jsonData := `{
  618. "defaultAction": "SCMP_ACT_ALLOW",
  619. "syscalls": [
  620. {
  621. "name": "chmod",
  622. "action": "SCMP_ACT_ERRNO"
  623. }
  624. ]
  625. }`
  626. tmpFile, err := ioutil.TempFile("", "profile.json")
  627. defer tmpFile.Close()
  628. if err != nil {
  629. c.Fatal(err)
  630. }
  631. if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
  632. c.Fatal(err)
  633. }
  634. runCmd := exec.Command(dockerBinary, "run", "--security-opt", "seccomp:"+tmpFile.Name(), "busybox", "chmod", "400", "/etc/hostname")
  635. out, _, _ := runCommandWithOutput(runCmd)
  636. if !strings.Contains(out, "Operation not permitted") {
  637. c.Fatalf("expected chmod with seccomp profile denied to fail, got %s", out)
  638. }
  639. }
  640. // TestRunSeccompProfileDenyUnshareUserns checks that 'docker run debian:jessie unshare --map-root-user --user sh -c whoami' with a specific profile to
  641. // deny unhare of a userns exits with operation not permitted.
  642. func (s *DockerSuite) TestRunSeccompProfileDenyUnshareUserns(c *check.C) {
  643. testRequires(c, SameHostDaemon, seccompEnabled, NotArm, Apparmor)
  644. // from sched.h
  645. jsonData := fmt.Sprintf(`{
  646. "defaultAction": "SCMP_ACT_ALLOW",
  647. "syscalls": [
  648. {
  649. "name": "unshare",
  650. "action": "SCMP_ACT_ERRNO",
  651. "args": [
  652. {
  653. "index": 0,
  654. "value": %d,
  655. "op": "SCMP_CMP_EQ"
  656. }
  657. ]
  658. }
  659. ]
  660. }`, uint64(0x10000000))
  661. tmpFile, err := ioutil.TempFile("", "profile.json")
  662. defer tmpFile.Close()
  663. if err != nil {
  664. c.Fatal(err)
  665. }
  666. if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
  667. c.Fatal(err)
  668. }
  669. runCmd := exec.Command(dockerBinary, "run", "--security-opt", "apparmor:unconfined", "--security-opt", "seccomp:"+tmpFile.Name(), "debian:jessie", "unshare", "--map-root-user", "--user", "sh", "-c", "whoami")
  670. out, _, _ := runCommandWithOutput(runCmd)
  671. if !strings.Contains(out, "Operation not permitted") {
  672. c.Fatalf("expected unshare userns with seccomp profile denied to fail, got %s", out)
  673. }
  674. }
  675. // TestRunSeccompProfileDenyCloneUserns checks that 'docker run syscall-test'
  676. // with a the default seccomp profile exits with operation not permitted.
  677. func (s *DockerSuite) TestRunSeccompProfileDenyCloneUserns(c *check.C) {
  678. testRequires(c, SameHostDaemon, seccompEnabled)
  679. runCmd := exec.Command(dockerBinary, "run", "syscall-test", "userns-test", "id")
  680. out, _, err := runCommandWithOutput(runCmd)
  681. if err == nil || !strings.Contains(out, "clone failed: Operation not permitted") {
  682. c.Fatalf("expected clone userns with default seccomp profile denied to fail, got %s: %v", out, err)
  683. }
  684. }
  685. // TestRunSeccompUnconfinedCloneUserns checks that
  686. // 'docker run --security-opt seccomp:unconfined syscall-test' allows creating a userns.
  687. func (s *DockerSuite) TestRunSeccompUnconfinedCloneUserns(c *check.C) {
  688. testRequires(c, SameHostDaemon, seccompEnabled, UserNamespaceInKernel, NotUserNamespace)
  689. // make sure running w privileged is ok
  690. runCmd := exec.Command(dockerBinary, "run", "--security-opt", "seccomp:unconfined", "syscall-test", "userns-test", "id")
  691. if out, _, err := runCommandWithOutput(runCmd); err != nil || !strings.Contains(out, "nobody") {
  692. c.Fatalf("expected clone userns with --security-opt seccomp:unconfined to succeed, got %s: %v", out, err)
  693. }
  694. }
  695. // TestRunSeccompAllowPrivCloneUserns checks that 'docker run --privileged syscall-test'
  696. // allows creating a userns.
  697. func (s *DockerSuite) TestRunSeccompAllowPrivCloneUserns(c *check.C) {
  698. testRequires(c, SameHostDaemon, seccompEnabled, UserNamespaceInKernel, NotUserNamespace)
  699. // make sure running w privileged is ok
  700. runCmd := exec.Command(dockerBinary, "run", "--privileged", "syscall-test", "userns-test", "id")
  701. if out, _, err := runCommandWithOutput(runCmd); err != nil || !strings.Contains(out, "nobody") {
  702. c.Fatalf("expected clone userns with --privileged to succeed, got %s: %v", out, err)
  703. }
  704. }
  705. // TestRunSeccompAllowSetrlimit checks that 'docker run debian:jessie ulimit -v 1048510' succeeds.
  706. func (s *DockerSuite) TestRunSeccompAllowSetrlimit(c *check.C) {
  707. testRequires(c, SameHostDaemon, seccompEnabled)
  708. // ulimit uses setrlimit, so we want to make sure we don't break it
  709. runCmd := exec.Command(dockerBinary, "run", "debian:jessie", "bash", "-c", "ulimit -v 1048510")
  710. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  711. c.Fatalf("expected ulimit with seccomp to succeed, got %s: %v", out, err)
  712. }
  713. }
  714. func (s *DockerSuite) TestRunSeccompDefaultProfile(c *check.C) {
  715. testRequires(c, SameHostDaemon, seccompEnabled, NotUserNamespace)
  716. var group sync.WaitGroup
  717. group.Add(4)
  718. errChan := make(chan error, 4)
  719. go func() {
  720. out, _, err := dockerCmdWithError("run", "--cap-add", "ALL", "syscall-test", "acct-test")
  721. if err == nil || !strings.Contains(out, "Operation not permitted") {
  722. errChan <- fmt.Errorf("expected Operation not permitted, got: %s", out)
  723. }
  724. group.Done()
  725. }()
  726. go func() {
  727. out, _, err := dockerCmdWithError("run", "--cap-add", "ALL", "syscall-test", "ns-test", "echo", "hello")
  728. if err == nil || !strings.Contains(out, "Operation not permitted") {
  729. errChan <- fmt.Errorf("expected Operation not permitted, got: %s", out)
  730. }
  731. group.Done()
  732. }()
  733. go func() {
  734. out, _, err := dockerCmdWithError("run", "--cap-add", "ALL", "--security-opt", "seccomp:unconfined", "syscall-test", "acct-test")
  735. if err == nil || !strings.Contains(out, "No such file or directory") {
  736. errChan <- fmt.Errorf("expected No such file or directory, got: %s", out)
  737. }
  738. group.Done()
  739. }()
  740. go func() {
  741. out, _, err := dockerCmdWithError("run", "--cap-add", "ALL", "--security-opt", "seccomp:unconfined", "syscall-test", "ns-test", "echo", "hello")
  742. if err != nil || !strings.Contains(out, "hello") {
  743. errChan <- fmt.Errorf("expected hello, got: %s, %v", out, err)
  744. }
  745. group.Done()
  746. }()
  747. group.Wait()
  748. close(errChan)
  749. for err := range errChan {
  750. c.Assert(err, checker.IsNil)
  751. }
  752. }
  753. // TestRunNoNewPrivSetuid checks that --security-opt=no-new-privileges prevents
  754. // effective uid transtions on executing setuid binaries.
  755. func (s *DockerSuite) TestRunNoNewPrivSetuid(c *check.C) {
  756. testRequires(c, DaemonIsLinux, NotUserNamespace, SameHostDaemon)
  757. // test that running a setuid binary results in no effective uid transition
  758. runCmd := exec.Command(dockerBinary, "run", "--security-opt", "no-new-privileges", "--user", "1000", "nnp-test", "/usr/bin/nnp-test")
  759. if out, _, err := runCommandWithOutput(runCmd); err != nil || !strings.Contains(out, "EUID=1000") {
  760. c.Fatalf("expected output to contain EUID=1000, got %s: %v", out, err)
  761. }
  762. }
  763. func (s *DockerSuite) TestRunApparmorProcDirectory(c *check.C) {
  764. testRequires(c, SameHostDaemon, Apparmor)
  765. // running w seccomp unconfined tests the apparmor profile
  766. runCmd := exec.Command(dockerBinary, "run", "--security-opt", "seccomp:unconfined", "debian:jessie", "chmod", "777", "/proc/1/cgroup")
  767. if out, _, err := runCommandWithOutput(runCmd); err == nil || !(strings.Contains(out, "Permission denied") || strings.Contains(out, "Operation not permitted")) {
  768. c.Fatalf("expected chmod 777 /proc/1/cgroup to fail, got %s: %v", out, err)
  769. }
  770. runCmd = exec.Command(dockerBinary, "run", "--security-opt", "seccomp:unconfined", "debian:jessie", "chmod", "777", "/proc/1/attr/current")
  771. if out, _, err := runCommandWithOutput(runCmd); err == nil || !(strings.Contains(out, "Permission denied") || strings.Contains(out, "Operation not permitted")) {
  772. c.Fatalf("expected chmod 777 /proc/1/attr/current to fail, got %s: %v", out, err)
  773. }
  774. }
  775. // make sure the default profile can be successfully parsed (using unshare as it is
  776. // something which we know is blocked in the default profile)
  777. func (s *DockerSuite) TestRunSeccompWithDefaultProfile(c *check.C) {
  778. testRequires(c, SameHostDaemon, seccompEnabled)
  779. out, _, err := dockerCmdWithError("run", "--security-opt", "seccomp:../profiles/seccomp/default.json", "debian:jessie", "unshare", "--map-root-user", "--user", "sh", "-c", "whoami")
  780. c.Assert(err, checker.NotNil, check.Commentf(out))
  781. c.Assert(strings.TrimSpace(out), checker.Equals, "unshare: unshare failed: Operation not permitted")
  782. }
  783. // TestRunDeviceSymlink checks run with device that follows symlink (#13840)
  784. func (s *DockerSuite) TestRunDeviceSymlink(c *check.C) {
  785. testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm, SameHostDaemon)
  786. if _, err := os.Stat("/dev/zero"); err != nil {
  787. c.Skip("Host does not have /dev/zero")
  788. }
  789. // Create a temporary directory to create symlink
  790. tmpDir, err := ioutil.TempDir("", "docker_device_follow_symlink_tests")
  791. c.Assert(err, checker.IsNil)
  792. defer os.RemoveAll(tmpDir)
  793. // Create a symbolic link to /dev/zero
  794. symZero := filepath.Join(tmpDir, "zero")
  795. err = os.Symlink("/dev/zero", symZero)
  796. c.Assert(err, checker.IsNil)
  797. // Create a temporary file "temp" inside tmpDir, write some data to "tmpDir/temp",
  798. // then create a symlink "tmpDir/file" to the temporary file "tmpDir/temp".
  799. tmpFile := filepath.Join(tmpDir, "temp")
  800. err = ioutil.WriteFile(tmpFile, []byte("temp"), 0666)
  801. c.Assert(err, checker.IsNil)
  802. symFile := filepath.Join(tmpDir, "file")
  803. err = os.Symlink(tmpFile, symFile)
  804. c.Assert(err, checker.IsNil)
  805. // md5sum of 'dd if=/dev/zero bs=4K count=8' is bb7df04e1b0a2570657527a7e108ae23
  806. out, _ := dockerCmd(c, "run", "--device", symZero+":/dev/symzero", "busybox", "sh", "-c", "dd if=/dev/symzero bs=4K count=8 | md5sum")
  807. c.Assert(strings.Trim(out, "\r\n"), checker.Contains, "bb7df04e1b0a2570657527a7e108ae23", check.Commentf("expected output bb7df04e1b0a2570657527a7e108ae23"))
  808. // symlink "tmpDir/file" to a file "tmpDir/temp" will result in an error as it is not a device.
  809. out, _, err = dockerCmdWithError("run", "--device", symFile+":/dev/symzero", "busybox", "sh", "-c", "dd if=/dev/symzero bs=4K count=8 | md5sum")
  810. c.Assert(err, check.NotNil)
  811. c.Assert(strings.Trim(out, "\r\n"), checker.Contains, "not a device node", check.Commentf("expected output 'not a device node'"))
  812. }
  813. // TestRunPidsLimit makes sure the pids cgroup is set with --pids-limit
  814. func (s *DockerSuite) TestRunPidsLimit(c *check.C) {
  815. testRequires(c, pidsLimit)
  816. file := "/sys/fs/cgroup/pids/pids.max"
  817. out, _ := dockerCmd(c, "run", "--name", "skittles", "--pids-limit", "2", "busybox", "cat", file)
  818. c.Assert(strings.TrimSpace(out), checker.Equals, "2")
  819. out = inspectField(c, "skittles", "HostConfig.PidsLimit")
  820. c.Assert(out, checker.Equals, "2", check.Commentf("setting the pids limit failed"))
  821. }