docker_cli_run_unix_test.go 35 KB

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