docker_cli_run_unix_test.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. out, _, err = dockerCmdWithError("run", "--memory-reservation", "1k", "busybox", "true")
  482. c.Assert(err, check.NotNil)
  483. expected = "Minimum memory reservation allowed is 4MB"
  484. c.Assert(strings.TrimSpace(out), checker.Contains, expected, check.Commentf("run container should fail with invalid memory reservation"))
  485. }
  486. func (s *DockerSuite) TestStopContainerSignal(c *check.C) {
  487. 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`)
  488. containerID := strings.TrimSpace(out)
  489. c.Assert(waitRun(containerID), checker.IsNil)
  490. dockerCmd(c, "stop", containerID)
  491. out, _ = dockerCmd(c, "logs", containerID)
  492. c.Assert(out, checker.Contains, "exit trapped", check.Commentf("Expected `exit trapped` in the log"))
  493. }
  494. func (s *DockerSuite) TestRunSwapLessThanMemoryLimit(c *check.C) {
  495. testRequires(c, memoryLimitSupport)
  496. testRequires(c, swapMemorySupport)
  497. out, _, err := dockerCmdWithError("run", "-m", "16m", "--memory-swap", "15m", "busybox", "echo", "test")
  498. expected := "Minimum memoryswap limit should be larger than memory limit"
  499. c.Assert(err, check.NotNil)
  500. c.Assert(out, checker.Contains, expected)
  501. }
  502. func (s *DockerSuite) TestRunInvalidCpusetCpusFlagValue(c *check.C) {
  503. testRequires(c, cgroupCpuset, SameHostDaemon)
  504. sysInfo := sysinfo.New(true)
  505. cpus, err := parsers.ParseUintList(sysInfo.Cpus)
  506. c.Assert(err, check.IsNil)
  507. var invalid int
  508. for i := 0; i <= len(cpus)+1; i++ {
  509. if !cpus[i] {
  510. invalid = i
  511. break
  512. }
  513. }
  514. out, _, err := dockerCmdWithError("run", "--cpuset-cpus", strconv.Itoa(invalid), "busybox", "true")
  515. c.Assert(err, check.NotNil)
  516. expected := fmt.Sprintf("Error response from daemon: Requested CPUs are not available - requested %s, available: %s", strconv.Itoa(invalid), sysInfo.Cpus)
  517. c.Assert(out, checker.Contains, expected)
  518. }
  519. func (s *DockerSuite) TestRunInvalidCpusetMemsFlagValue(c *check.C) {
  520. testRequires(c, cgroupCpuset)
  521. sysInfo := sysinfo.New(true)
  522. mems, err := parsers.ParseUintList(sysInfo.Mems)
  523. c.Assert(err, check.IsNil)
  524. var invalid int
  525. for i := 0; i <= len(mems)+1; i++ {
  526. if !mems[i] {
  527. invalid = i
  528. break
  529. }
  530. }
  531. out, _, err := dockerCmdWithError("run", "--cpuset-mems", strconv.Itoa(invalid), "busybox", "true")
  532. c.Assert(err, check.NotNil)
  533. expected := fmt.Sprintf("Error response from daemon: Requested memory nodes are not available - requested %s, available: %s", strconv.Itoa(invalid), sysInfo.Mems)
  534. c.Assert(out, checker.Contains, expected)
  535. }
  536. func (s *DockerSuite) TestRunInvalidCPUShares(c *check.C) {
  537. testRequires(c, cpuShare, DaemonIsLinux)
  538. out, _, err := dockerCmdWithError("run", "--cpu-shares", "1", "busybox", "echo", "test")
  539. c.Assert(err, check.NotNil, check.Commentf(out))
  540. expected := "The minimum allowed cpu-shares is 2"
  541. c.Assert(out, checker.Contains, expected)
  542. out, _, err = dockerCmdWithError("run", "--cpu-shares", "-1", "busybox", "echo", "test")
  543. c.Assert(err, check.NotNil, check.Commentf(out))
  544. expected = "shares: invalid argument"
  545. c.Assert(out, checker.Contains, expected)
  546. out, _, err = dockerCmdWithError("run", "--cpu-shares", "99999999", "busybox", "echo", "test")
  547. c.Assert(err, check.NotNil, check.Commentf(out))
  548. expected = "The maximum allowed cpu-shares is"
  549. c.Assert(out, checker.Contains, expected)
  550. }
  551. func (s *DockerSuite) TestRunWithDefaultShmSize(c *check.C) {
  552. testRequires(c, DaemonIsLinux)
  553. name := "shm-default"
  554. out, _ := dockerCmd(c, "run", "--name", name, "busybox", "mount")
  555. shmRegex := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
  556. if !shmRegex.MatchString(out) {
  557. c.Fatalf("Expected shm of 64MB in mount command, got %v", out)
  558. }
  559. shmSize := inspectField(c, name, "HostConfig.ShmSize")
  560. c.Assert(shmSize, check.Equals, "67108864")
  561. }
  562. func (s *DockerSuite) TestRunWithShmSize(c *check.C) {
  563. testRequires(c, DaemonIsLinux)
  564. name := "shm"
  565. out, _ := dockerCmd(c, "run", "--name", name, "--shm-size=1G", "busybox", "mount")
  566. shmRegex := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=1048576k`)
  567. if !shmRegex.MatchString(out) {
  568. c.Fatalf("Expected shm of 1GB in mount command, got %v", out)
  569. }
  570. shmSize := inspectField(c, name, "HostConfig.ShmSize")
  571. c.Assert(shmSize, check.Equals, "1073741824")
  572. }
  573. func (s *DockerSuite) TestRunTmpfsMounts(c *check.C) {
  574. // TODO Windows (Post TP4): This test cannot run on a Windows daemon as
  575. // Windows does not support tmpfs mounts.
  576. testRequires(c, DaemonIsLinux)
  577. if out, _, err := dockerCmdWithError("run", "--tmpfs", "/run", "busybox", "touch", "/run/somefile"); err != nil {
  578. c.Fatalf("/run directory not mounted on tmpfs %q %s", err, out)
  579. }
  580. if out, _, err := dockerCmdWithError("run", "--tmpfs", "/run:noexec", "busybox", "touch", "/run/somefile"); err != nil {
  581. c.Fatalf("/run directory not mounted on tmpfs %q %s", err, out)
  582. }
  583. if out, _, err := dockerCmdWithError("run", "--tmpfs", "/run:noexec,nosuid,rw,size=5k,mode=700", "busybox", "touch", "/run/somefile"); err != nil {
  584. c.Fatalf("/run failed to mount on tmpfs with valid options %q %s", err, out)
  585. }
  586. if _, _, err := dockerCmdWithError("run", "--tmpfs", "/run:foobar", "busybox", "touch", "/run/somefile"); err == nil {
  587. c.Fatalf("/run mounted on tmpfs when it should have vailed within invalid mount option")
  588. }
  589. if _, _, err := dockerCmdWithError("run", "--tmpfs", "/run", "-v", "/run:/run", "busybox", "touch", "/run/somefile"); err == nil {
  590. c.Fatalf("Should have generated an error saying Duplicate mount points")
  591. }
  592. }
  593. // TestRunSeccompProfileDenyUnshare checks that 'docker run --security-opt seccomp=/tmp/profile.json debian:jessie unshare' exits with operation not permitted.
  594. func (s *DockerSuite) TestRunSeccompProfileDenyUnshare(c *check.C) {
  595. testRequires(c, SameHostDaemon, seccompEnabled, NotArm, Apparmor)
  596. jsonData := `{
  597. "defaultAction": "SCMP_ACT_ALLOW",
  598. "syscalls": [
  599. {
  600. "name": "unshare",
  601. "action": "SCMP_ACT_ERRNO"
  602. }
  603. ]
  604. }`
  605. tmpFile, err := ioutil.TempFile("", "profile.json")
  606. defer tmpFile.Close()
  607. if err != nil {
  608. c.Fatal(err)
  609. }
  610. if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
  611. c.Fatal(err)
  612. }
  613. 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")
  614. out, _, _ := runCommandWithOutput(runCmd)
  615. if !strings.Contains(out, "Operation not permitted") {
  616. c.Fatalf("expected unshare with seccomp profile denied to fail, got %s", out)
  617. }
  618. }
  619. // TestRunSeccompProfileDenyChmod checks that 'docker run --security-opt seccomp=/tmp/profile.json busybox chmod 400 /etc/hostname' exits with operation not permitted.
  620. func (s *DockerSuite) TestRunSeccompProfileDenyChmod(c *check.C) {
  621. testRequires(c, SameHostDaemon, seccompEnabled)
  622. jsonData := `{
  623. "defaultAction": "SCMP_ACT_ALLOW",
  624. "syscalls": [
  625. {
  626. "name": "chmod",
  627. "action": "SCMP_ACT_ERRNO"
  628. }
  629. ]
  630. }`
  631. tmpFile, err := ioutil.TempFile("", "profile.json")
  632. defer tmpFile.Close()
  633. if err != nil {
  634. c.Fatal(err)
  635. }
  636. if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
  637. c.Fatal(err)
  638. }
  639. runCmd := exec.Command(dockerBinary, "run", "--security-opt", "seccomp="+tmpFile.Name(), "busybox", "chmod", "400", "/etc/hostname")
  640. out, _, _ := runCommandWithOutput(runCmd)
  641. if !strings.Contains(out, "Operation not permitted") {
  642. c.Fatalf("expected chmod with seccomp profile denied to fail, got %s", out)
  643. }
  644. }
  645. // TestRunSeccompProfileDenyUnshareUserns checks that 'docker run debian:jessie unshare --map-root-user --user sh -c whoami' with a specific profile to
  646. // deny unhare of a userns exits with operation not permitted.
  647. func (s *DockerSuite) TestRunSeccompProfileDenyUnshareUserns(c *check.C) {
  648. testRequires(c, SameHostDaemon, seccompEnabled, NotArm, Apparmor)
  649. // from sched.h
  650. jsonData := fmt.Sprintf(`{
  651. "defaultAction": "SCMP_ACT_ALLOW",
  652. "syscalls": [
  653. {
  654. "name": "unshare",
  655. "action": "SCMP_ACT_ERRNO",
  656. "args": [
  657. {
  658. "index": 0,
  659. "value": %d,
  660. "op": "SCMP_CMP_EQ"
  661. }
  662. ]
  663. }
  664. ]
  665. }`, uint64(0x10000000))
  666. tmpFile, err := ioutil.TempFile("", "profile.json")
  667. defer tmpFile.Close()
  668. if err != nil {
  669. c.Fatal(err)
  670. }
  671. if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
  672. c.Fatal(err)
  673. }
  674. runCmd := exec.Command(dockerBinary, "run", "--security-opt", "apparmor=unconfined", "--security-opt", "seccomp="+tmpFile.Name(), "debian:jessie", "unshare", "--map-root-user", "--user", "sh", "-c", "whoami")
  675. out, _, _ := runCommandWithOutput(runCmd)
  676. if !strings.Contains(out, "Operation not permitted") {
  677. c.Fatalf("expected unshare userns with seccomp profile denied to fail, got %s", out)
  678. }
  679. }
  680. // TestRunSeccompProfileDenyCloneUserns checks that 'docker run syscall-test'
  681. // with a the default seccomp profile exits with operation not permitted.
  682. func (s *DockerSuite) TestRunSeccompProfileDenyCloneUserns(c *check.C) {
  683. testRequires(c, SameHostDaemon, seccompEnabled)
  684. runCmd := exec.Command(dockerBinary, "run", "syscall-test", "userns-test", "id")
  685. out, _, err := runCommandWithOutput(runCmd)
  686. if err == nil || !strings.Contains(out, "clone failed: Operation not permitted") {
  687. c.Fatalf("expected clone userns with default seccomp profile denied to fail, got %s: %v", out, err)
  688. }
  689. }
  690. // TestRunSeccompUnconfinedCloneUserns checks that
  691. // 'docker run --security-opt seccomp=unconfined syscall-test' allows creating a userns.
  692. func (s *DockerSuite) TestRunSeccompUnconfinedCloneUserns(c *check.C) {
  693. testRequires(c, SameHostDaemon, seccompEnabled, UserNamespaceInKernel, NotUserNamespace)
  694. // make sure running w privileged is ok
  695. runCmd := exec.Command(dockerBinary, "run", "--security-opt", "seccomp=unconfined", "syscall-test", "userns-test", "id")
  696. if out, _, err := runCommandWithOutput(runCmd); err != nil || !strings.Contains(out, "nobody") {
  697. c.Fatalf("expected clone userns with --security-opt seccomp=unconfined to succeed, got %s: %v", out, err)
  698. }
  699. }
  700. // TestRunSeccompAllowPrivCloneUserns checks that 'docker run --privileged syscall-test'
  701. // allows creating a userns.
  702. func (s *DockerSuite) TestRunSeccompAllowPrivCloneUserns(c *check.C) {
  703. testRequires(c, SameHostDaemon, seccompEnabled, UserNamespaceInKernel, NotUserNamespace)
  704. // make sure running w privileged is ok
  705. runCmd := exec.Command(dockerBinary, "run", "--privileged", "syscall-test", "userns-test", "id")
  706. if out, _, err := runCommandWithOutput(runCmd); err != nil || !strings.Contains(out, "nobody") {
  707. c.Fatalf("expected clone userns with --privileged to succeed, got %s: %v", out, err)
  708. }
  709. }
  710. // TestRunSeccompAllowSetrlimit checks that 'docker run debian:jessie ulimit -v 1048510' succeeds.
  711. func (s *DockerSuite) TestRunSeccompAllowSetrlimit(c *check.C) {
  712. testRequires(c, SameHostDaemon, seccompEnabled)
  713. // ulimit uses setrlimit, so we want to make sure we don't break it
  714. runCmd := exec.Command(dockerBinary, "run", "debian:jessie", "bash", "-c", "ulimit -v 1048510")
  715. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  716. c.Fatalf("expected ulimit with seccomp to succeed, got %s: %v", out, err)
  717. }
  718. }
  719. func (s *DockerSuite) TestRunSeccompDefaultProfile(c *check.C) {
  720. testRequires(c, SameHostDaemon, seccompEnabled, NotUserNamespace)
  721. var group sync.WaitGroup
  722. group.Add(4)
  723. errChan := make(chan error, 4)
  724. go func() {
  725. out, _, err := dockerCmdWithError("run", "--cap-add", "ALL", "syscall-test", "acct-test")
  726. if err == nil || !strings.Contains(out, "Operation not permitted") {
  727. errChan <- fmt.Errorf("expected Operation not permitted, got: %s", out)
  728. }
  729. group.Done()
  730. }()
  731. go func() {
  732. out, _, err := dockerCmdWithError("run", "--cap-add", "ALL", "syscall-test", "ns-test", "echo", "hello")
  733. if err == nil || !strings.Contains(out, "Operation not permitted") {
  734. errChan <- fmt.Errorf("expected Operation not permitted, got: %s", out)
  735. }
  736. group.Done()
  737. }()
  738. go func() {
  739. out, _, err := dockerCmdWithError("run", "--cap-add", "ALL", "--security-opt", "seccomp=unconfined", "syscall-test", "acct-test")
  740. if err == nil || !strings.Contains(out, "No such file or directory") {
  741. errChan <- fmt.Errorf("expected No such file or directory, got: %s", out)
  742. }
  743. group.Done()
  744. }()
  745. go func() {
  746. out, _, err := dockerCmdWithError("run", "--cap-add", "ALL", "--security-opt", "seccomp=unconfined", "syscall-test", "ns-test", "echo", "hello")
  747. if err != nil || !strings.Contains(out, "hello") {
  748. errChan <- fmt.Errorf("expected hello, got: %s, %v", out, err)
  749. }
  750. group.Done()
  751. }()
  752. group.Wait()
  753. close(errChan)
  754. for err := range errChan {
  755. c.Assert(err, checker.IsNil)
  756. }
  757. }
  758. // TestRunNoNewPrivSetuid checks that --security-opt=no-new-privileges prevents
  759. // effective uid transtions on executing setuid binaries.
  760. func (s *DockerSuite) TestRunNoNewPrivSetuid(c *check.C) {
  761. testRequires(c, DaemonIsLinux, NotUserNamespace, SameHostDaemon)
  762. // test that running a setuid binary results in no effective uid transition
  763. runCmd := exec.Command(dockerBinary, "run", "--security-opt", "no-new-privileges", "--user", "1000", "nnp-test", "/usr/bin/nnp-test")
  764. if out, _, err := runCommandWithOutput(runCmd); err != nil || !strings.Contains(out, "EUID=1000") {
  765. c.Fatalf("expected output to contain EUID=1000, got %s: %v", out, err)
  766. }
  767. }
  768. func (s *DockerSuite) TestRunApparmorProcDirectory(c *check.C) {
  769. testRequires(c, SameHostDaemon, Apparmor)
  770. // running w seccomp unconfined tests the apparmor profile
  771. runCmd := exec.Command(dockerBinary, "run", "--security-opt", "seccomp=unconfined", "busybox", "chmod", "777", "/proc/1/cgroup")
  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/cgroup to fail, got %s: %v", out, err)
  774. }
  775. runCmd = exec.Command(dockerBinary, "run", "--security-opt", "seccomp=unconfined", "busybox", "chmod", "777", "/proc/1/attr/current")
  776. if out, _, err := runCommandWithOutput(runCmd); err == nil || !(strings.Contains(out, "Permission denied") || strings.Contains(out, "Operation not permitted")) {
  777. c.Fatalf("expected chmod 777 /proc/1/attr/current to fail, got %s: %v", out, err)
  778. }
  779. }
  780. // make sure the default profile can be successfully parsed (using unshare as it is
  781. // something which we know is blocked in the default profile)
  782. func (s *DockerSuite) TestRunSeccompWithDefaultProfile(c *check.C) {
  783. testRequires(c, SameHostDaemon, seccompEnabled)
  784. out, _, err := dockerCmdWithError("run", "--security-opt", "seccomp=../profiles/seccomp/default.json", "debian:jessie", "unshare", "--map-root-user", "--user", "sh", "-c", "whoami")
  785. c.Assert(err, checker.NotNil, check.Commentf(out))
  786. c.Assert(strings.TrimSpace(out), checker.Equals, "unshare: unshare failed: Operation not permitted")
  787. }
  788. // TestRunDeviceSymlink checks run with device that follows symlink (#13840)
  789. func (s *DockerSuite) TestRunDeviceSymlink(c *check.C) {
  790. testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm, SameHostDaemon)
  791. if _, err := os.Stat("/dev/zero"); err != nil {
  792. c.Skip("Host does not have /dev/zero")
  793. }
  794. // Create a temporary directory to create symlink
  795. tmpDir, err := ioutil.TempDir("", "docker_device_follow_symlink_tests")
  796. c.Assert(err, checker.IsNil)
  797. defer os.RemoveAll(tmpDir)
  798. // Create a symbolic link to /dev/zero
  799. symZero := filepath.Join(tmpDir, "zero")
  800. err = os.Symlink("/dev/zero", symZero)
  801. c.Assert(err, checker.IsNil)
  802. // Create a temporary file "temp" inside tmpDir, write some data to "tmpDir/temp",
  803. // then create a symlink "tmpDir/file" to the temporary file "tmpDir/temp".
  804. tmpFile := filepath.Join(tmpDir, "temp")
  805. err = ioutil.WriteFile(tmpFile, []byte("temp"), 0666)
  806. c.Assert(err, checker.IsNil)
  807. symFile := filepath.Join(tmpDir, "file")
  808. err = os.Symlink(tmpFile, symFile)
  809. c.Assert(err, checker.IsNil)
  810. // md5sum of 'dd if=/dev/zero bs=4K count=8' is bb7df04e1b0a2570657527a7e108ae23
  811. out, _ := dockerCmd(c, "run", "--device", symZero+":/dev/symzero", "busybox", "sh", "-c", "dd if=/dev/symzero bs=4K count=8 | md5sum")
  812. c.Assert(strings.Trim(out, "\r\n"), checker.Contains, "bb7df04e1b0a2570657527a7e108ae23", check.Commentf("expected output bb7df04e1b0a2570657527a7e108ae23"))
  813. // symlink "tmpDir/file" to a file "tmpDir/temp" will result in an error as it is not a device.
  814. out, _, err = dockerCmdWithError("run", "--device", symFile+":/dev/symzero", "busybox", "sh", "-c", "dd if=/dev/symzero bs=4K count=8 | md5sum")
  815. c.Assert(err, check.NotNil)
  816. c.Assert(strings.Trim(out, "\r\n"), checker.Contains, "not a device node", check.Commentf("expected output 'not a device node'"))
  817. }
  818. // TestRunPidsLimit makes sure the pids cgroup is set with --pids-limit
  819. func (s *DockerSuite) TestRunPidsLimit(c *check.C) {
  820. testRequires(c, pidsLimit)
  821. file := "/sys/fs/cgroup/pids/pids.max"
  822. out, _ := dockerCmd(c, "run", "--name", "skittles", "--pids-limit", "2", "busybox", "cat", file)
  823. c.Assert(strings.TrimSpace(out), checker.Equals, "2")
  824. out = inspectField(c, "skittles", "HostConfig.PidsLimit")
  825. c.Assert(out, checker.Equals, "2", check.Commentf("setting the pids limit failed"))
  826. }
  827. func (s *DockerSuite) TestRunPrivilegedAllowedDevices(c *check.C) {
  828. testRequires(c, DaemonIsLinux, NotUserNamespace)
  829. file := "/sys/fs/cgroup/devices/devices.list"
  830. out, _ := dockerCmd(c, "run", "--privileged", "busybox", "cat", file)
  831. c.Logf("out: %q", out)
  832. c.Assert(strings.TrimSpace(out), checker.Equals, "a *:* rwm")
  833. }
  834. func (s *DockerSuite) TestRunUserDeviceAllowed(c *check.C) {
  835. testRequires(c, DaemonIsLinux)
  836. fi, err := os.Stat("/dev/snd/timer")
  837. if err != nil {
  838. c.Skip("Host does not have /dev/snd/timer")
  839. }
  840. stat, ok := fi.Sys().(*syscall.Stat_t)
  841. if !ok {
  842. c.Skip("Could not stat /dev/snd/timer")
  843. }
  844. file := "/sys/fs/cgroup/devices/devices.list"
  845. out, _ := dockerCmd(c, "run", "--device", "/dev/snd/timer:w", "busybox", "cat", file)
  846. c.Assert(out, checker.Contains, fmt.Sprintf("c %d:%d w", stat.Rdev/256, stat.Rdev%256))
  847. }