docker_api_ipcmode_test.go 6.7 KB

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