docker_api_ipcmode_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // build +linux
  2. package main
  3. import (
  4. "bufio"
  5. "context"
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "strings"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/api/types/container"
  12. "github.com/docker/docker/integration-cli/checker"
  13. "github.com/docker/docker/integration-cli/cli"
  14. "github.com/go-check/check"
  15. )
  16. /* testIpcCheckDevExists checks whether a given mount (identified by its
  17. * major:minor pair from /proc/self/mountinfo) exists on the host system.
  18. *
  19. * The format of /proc/self/mountinfo is like:
  20. *
  21. * 29 23 0:24 / /dev/shm rw,nosuid,nodev shared:4 - tmpfs tmpfs rw
  22. * ^^^^\
  23. * - this is the minor:major we look for
  24. */
  25. func testIpcCheckDevExists(mm string) (bool, error) {
  26. f, err := os.Open("/proc/self/mountinfo")
  27. if err != nil {
  28. return false, err
  29. }
  30. defer f.Close()
  31. s := bufio.NewScanner(f)
  32. for s.Scan() {
  33. fields := strings.Fields(s.Text())
  34. if len(fields) < 7 {
  35. continue
  36. }
  37. if fields[2] == mm {
  38. return true, nil
  39. }
  40. }
  41. return false, s.Err()
  42. }
  43. // testIpcNonePrivateShareable is a helper function to test "none",
  44. // "private" and "shareable" modes.
  45. func testIpcNonePrivateShareable(c *check.C, mode string, mustBeMounted bool, mustBeShared bool) {
  46. cfg := container.Config{
  47. Image: "busybox",
  48. Cmd: []string{"top"},
  49. }
  50. hostCfg := container.HostConfig{
  51. IpcMode: container.IpcMode(mode),
  52. }
  53. ctx := context.Background()
  54. client := testEnv.APIClient()
  55. resp, err := client.ContainerCreate(ctx, &cfg, &hostCfg, nil, "")
  56. c.Assert(err, checker.IsNil)
  57. c.Assert(len(resp.Warnings), checker.Equals, 0)
  58. err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
  59. c.Assert(err, checker.IsNil)
  60. // get major:minor pair for /dev/shm from container's /proc/self/mountinfo
  61. cmd := "awk '($5 == \"/dev/shm\") {printf $3}' /proc/self/mountinfo"
  62. mm := cli.DockerCmd(c, "exec", "-i", resp.ID, "sh", "-c", cmd).Combined()
  63. if !mustBeMounted {
  64. c.Assert(mm, checker.Equals, "")
  65. // no more checks to perform
  66. return
  67. }
  68. c.Assert(mm, checker.Matches, "^[0-9]+:[0-9]+$")
  69. shared, err := testIpcCheckDevExists(mm)
  70. c.Assert(err, checker.IsNil)
  71. c.Logf("[testIpcPrivateShareable] ipcmode: %v, ipcdev: %v, shared: %v, mustBeShared: %v\n", mode, mm, shared, mustBeShared)
  72. c.Assert(shared, checker.Equals, mustBeShared)
  73. }
  74. /* TestAPIIpcModeNone checks the container "none" IPC mode
  75. * (--ipc none) works as expected. It makes sure there is no
  76. * /dev/shm mount inside the container.
  77. */
  78. func (s *DockerSuite) TestAPIIpcModeNone(c *check.C) {
  79. testRequires(c, DaemonIsLinux, MinimumAPIVersion("1.32"))
  80. testIpcNonePrivateShareable(c, "none", false, false)
  81. }
  82. /* TestAPIIpcModePrivate checks the container private IPC mode
  83. * (--ipc private) works as expected. It gets the minor:major pair
  84. * of /dev/shm mount from the container, and makes sure there is no
  85. * such pair on the host.
  86. */
  87. func (s *DockerSuite) TestAPIIpcModePrivate(c *check.C) {
  88. testRequires(c, DaemonIsLinux, SameHostDaemon)
  89. testIpcNonePrivateShareable(c, "private", true, false)
  90. }
  91. /* TestAPIIpcModeShareable checks the container shareable IPC mode
  92. * (--ipc shareable) works as expected. It gets the minor:major pair
  93. * of /dev/shm mount from the container, and makes sure such pair
  94. * also exists on the host.
  95. */
  96. func (s *DockerSuite) TestAPIIpcModeShareable(c *check.C) {
  97. testRequires(c, DaemonIsLinux, SameHostDaemon)
  98. testIpcNonePrivateShareable(c, "shareable", true, true)
  99. }
  100. // testIpcContainer is a helper function to test --ipc container:NNN mode in various scenarios
  101. func testIpcContainer(s *DockerSuite, c *check.C, donorMode string, mustWork bool) {
  102. cfg := container.Config{
  103. Image: "busybox",
  104. Cmd: []string{"top"},
  105. }
  106. hostCfg := container.HostConfig{
  107. IpcMode: container.IpcMode(donorMode),
  108. }
  109. ctx := context.Background()
  110. client := testEnv.APIClient()
  111. // create and start the "donor" container
  112. resp, err := client.ContainerCreate(ctx, &cfg, &hostCfg, nil, "")
  113. c.Assert(err, checker.IsNil)
  114. c.Assert(len(resp.Warnings), checker.Equals, 0)
  115. name1 := resp.ID
  116. err = client.ContainerStart(ctx, name1, types.ContainerStartOptions{})
  117. c.Assert(err, checker.IsNil)
  118. // create and start the second container
  119. hostCfg.IpcMode = container.IpcMode("container:" + name1)
  120. resp, err = client.ContainerCreate(ctx, &cfg, &hostCfg, nil, "")
  121. c.Assert(err, checker.IsNil)
  122. c.Assert(len(resp.Warnings), checker.Equals, 0)
  123. name2 := resp.ID
  124. err = client.ContainerStart(ctx, name2, types.ContainerStartOptions{})
  125. if !mustWork {
  126. // start should fail with a specific error
  127. c.Assert(err, checker.NotNil)
  128. c.Assert(fmt.Sprintf("%v", err), checker.Contains, "non-shareable IPC")
  129. // no more checks to perform here
  130. return
  131. }
  132. // start should succeed
  133. c.Assert(err, checker.IsNil)
  134. // check that IPC is shared
  135. // 1. create a file in the first container
  136. cli.DockerCmd(c, "exec", name1, "sh", "-c", "printf covfefe > /dev/shm/bar")
  137. // 2. check it's the same file in the second one
  138. out := cli.DockerCmd(c, "exec", "-i", name2, "cat", "/dev/shm/bar").Combined()
  139. c.Assert(out, checker.Matches, "^covfefe$")
  140. }
  141. /* TestAPIIpcModeShareableAndContainer checks that a container created with
  142. * --ipc container:ID can use IPC of another shareable container.
  143. */
  144. func (s *DockerSuite) TestAPIIpcModeShareableAndContainer(c *check.C) {
  145. testRequires(c, DaemonIsLinux)
  146. testIpcContainer(s, c, "shareable", true)
  147. }
  148. /* TestAPIIpcModePrivateAndContainer checks that a container created with
  149. * --ipc container:ID can NOT use IPC of another private container.
  150. */
  151. func (s *DockerSuite) TestAPIIpcModePrivateAndContainer(c *check.C) {
  152. testRequires(c, DaemonIsLinux, MinimumAPIVersion("1.32"))
  153. testIpcContainer(s, c, "private", false)
  154. }
  155. /* TestAPIIpcModeHost checks that a container created with --ipc host
  156. * can use IPC of the host system.
  157. */
  158. func (s *DockerSuite) TestAPIIpcModeHost(c *check.C) {
  159. testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace)
  160. cfg := container.Config{
  161. Image: "busybox",
  162. Cmd: []string{"top"},
  163. }
  164. hostCfg := container.HostConfig{
  165. IpcMode: container.IpcMode("host"),
  166. }
  167. ctx := context.Background()
  168. client := testEnv.APIClient()
  169. resp, err := client.ContainerCreate(ctx, &cfg, &hostCfg, nil, "")
  170. c.Assert(err, checker.IsNil)
  171. c.Assert(len(resp.Warnings), checker.Equals, 0)
  172. name := resp.ID
  173. err = client.ContainerStart(ctx, name, types.ContainerStartOptions{})
  174. c.Assert(err, checker.IsNil)
  175. // check that IPC is shared
  176. // 1. create a file inside container
  177. cli.DockerCmd(c, "exec", name, "sh", "-c", "printf covfefe > /dev/shm/."+name)
  178. // 2. check it's the same on the host
  179. bytes, err := ioutil.ReadFile("/dev/shm/." + name)
  180. c.Assert(err, checker.IsNil)
  181. c.Assert(string(bytes), checker.Matches, "^covfefe$")
  182. // 3. clean up
  183. cli.DockerCmd(c, "exec", name, "rm", "-f", "/dev/shm/."+name)
  184. }