docker_api_ipcmode_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // build +linux
  2. package main
  3. import (
  4. "bufio"
  5. "context"
  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/go-check/check"
  14. )
  15. /* testIpcCheckDevExists checks whether a given mount (identified by its
  16. * major:minor pair from /proc/self/mountinfo) exists on the host system.
  17. *
  18. * The format of /proc/self/mountinfo is like:
  19. *
  20. * 29 23 0:24 / /dev/shm rw,nosuid,nodev shared:4 - tmpfs tmpfs rw
  21. * ^^^^\
  22. * - this is the minor:major we look for
  23. */
  24. func testIpcCheckDevExists(mm string) (bool, error) {
  25. f, err := os.Open("/proc/self/mountinfo")
  26. if err != nil {
  27. return false, err
  28. }
  29. defer f.Close()
  30. s := bufio.NewScanner(f)
  31. for s.Scan() {
  32. fields := strings.Fields(s.Text())
  33. if len(fields) < 7 {
  34. continue
  35. }
  36. if fields[2] == mm {
  37. return true, nil
  38. }
  39. }
  40. return false, s.Err()
  41. }
  42. /* TestAPIIpcModeHost checks that a container created with --ipc host
  43. * can use IPC of the host system.
  44. */
  45. func (s *DockerSuite) TestAPIIpcModeHost(c *check.C) {
  46. testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace)
  47. cfg := container.Config{
  48. Image: "busybox",
  49. Cmd: []string{"top"},
  50. }
  51. hostCfg := container.HostConfig{
  52. IpcMode: container.IpcMode("host"),
  53. }
  54. ctx := context.Background()
  55. client := testEnv.APIClient()
  56. resp, err := client.ContainerCreate(ctx, &cfg, &hostCfg, nil, "")
  57. c.Assert(err, checker.IsNil)
  58. c.Assert(len(resp.Warnings), checker.Equals, 0)
  59. name := resp.ID
  60. err = client.ContainerStart(ctx, name, types.ContainerStartOptions{})
  61. c.Assert(err, checker.IsNil)
  62. // check that IPC is shared
  63. // 1. create a file inside container
  64. cli.DockerCmd(c, "exec", name, "sh", "-c", "printf covfefe > /dev/shm/."+name)
  65. // 2. check it's the same on the host
  66. bytes, err := ioutil.ReadFile("/dev/shm/." + name)
  67. c.Assert(err, checker.IsNil)
  68. c.Assert(string(bytes), checker.Matches, "^covfefe$")
  69. // 3. clean up
  70. cli.DockerCmd(c, "exec", name, "rm", "-f", "/dev/shm/."+name)
  71. }