ipcmode_linux_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "bufio"
  4. "context"
  5. "io/ioutil"
  6. "os"
  7. "regexp"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api/types"
  11. containertypes "github.com/docker/docker/api/types/container"
  12. "github.com/docker/docker/api/types/versions"
  13. "github.com/docker/docker/client"
  14. "github.com/docker/docker/integration/internal/container"
  15. "github.com/docker/docker/internal/test/daemon"
  16. "github.com/docker/docker/internal/test/request"
  17. "gotest.tools/assert"
  18. is "gotest.tools/assert/cmp"
  19. "gotest.tools/fs"
  20. "gotest.tools/skip"
  21. )
  22. // testIpcCheckDevExists checks whether a given mount (identified by its
  23. // major:minor pair from /proc/self/mountinfo) exists on the host system.
  24. //
  25. // The format of /proc/self/mountinfo is like:
  26. //
  27. // 29 23 0:24 / /dev/shm rw,nosuid,nodev shared:4 - tmpfs tmpfs rw
  28. // ^^^^\
  29. // - this is the minor:major we look for
  30. func testIpcCheckDevExists(mm string) (bool, error) {
  31. f, err := os.Open("/proc/self/mountinfo")
  32. if err != nil {
  33. return false, err
  34. }
  35. defer f.Close()
  36. s := bufio.NewScanner(f)
  37. for s.Scan() {
  38. fields := strings.Fields(s.Text())
  39. if len(fields) < 7 {
  40. continue
  41. }
  42. if fields[2] == mm {
  43. return true, nil
  44. }
  45. }
  46. return false, s.Err()
  47. }
  48. // testIpcNonePrivateShareable is a helper function to test "none",
  49. // "private" and "shareable" modes.
  50. func testIpcNonePrivateShareable(t *testing.T, mode string, mustBeMounted bool, mustBeShared bool) {
  51. defer setupTest(t)()
  52. cfg := containertypes.Config{
  53. Image: "busybox",
  54. Cmd: []string{"top"},
  55. }
  56. hostCfg := containertypes.HostConfig{
  57. IpcMode: containertypes.IpcMode(mode),
  58. }
  59. client := testEnv.APIClient()
  60. ctx := context.Background()
  61. resp, err := client.ContainerCreate(ctx, &cfg, &hostCfg, nil, "")
  62. assert.NilError(t, err)
  63. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  64. err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
  65. assert.NilError(t, err)
  66. // get major:minor pair for /dev/shm from container's /proc/self/mountinfo
  67. cmd := "awk '($5 == \"/dev/shm\") {printf $3}' /proc/self/mountinfo"
  68. result, err := container.Exec(ctx, client, resp.ID, []string{"sh", "-c", cmd})
  69. assert.NilError(t, err)
  70. mm := result.Combined()
  71. if !mustBeMounted {
  72. assert.Check(t, is.Equal(mm, ""))
  73. // no more checks to perform
  74. return
  75. }
  76. assert.Check(t, is.Equal(true, regexp.MustCompile("^[0-9]+:[0-9]+$").MatchString(mm)))
  77. shared, err := testIpcCheckDevExists(mm)
  78. assert.NilError(t, err)
  79. t.Logf("[testIpcPrivateShareable] ipcmode: %v, ipcdev: %v, shared: %v, mustBeShared: %v\n", mode, mm, shared, mustBeShared)
  80. assert.Check(t, is.Equal(shared, mustBeShared))
  81. }
  82. // TestIpcModeNone checks the container "none" IPC mode
  83. // (--ipc none) works as expected. It makes sure there is no
  84. // /dev/shm mount inside the container.
  85. func TestIpcModeNone(t *testing.T) {
  86. skip.If(t, testEnv.IsRemoteDaemon)
  87. testIpcNonePrivateShareable(t, "none", false, false)
  88. }
  89. // TestAPIIpcModePrivate checks the container private IPC mode
  90. // (--ipc private) works as expected. It gets the minor:major pair
  91. // of /dev/shm mount from the container, and makes sure there is no
  92. // such pair on the host.
  93. func TestIpcModePrivate(t *testing.T) {
  94. skip.If(t, testEnv.IsRemoteDaemon)
  95. testIpcNonePrivateShareable(t, "private", true, false)
  96. }
  97. // TestAPIIpcModeShareable checks the container shareable IPC mode
  98. // (--ipc shareable) works as expected. It gets the minor:major pair
  99. // of /dev/shm mount from the container, and makes sure such pair
  100. // also exists on the host.
  101. func TestIpcModeShareable(t *testing.T) {
  102. skip.If(t, testEnv.IsRemoteDaemon)
  103. testIpcNonePrivateShareable(t, "shareable", true, true)
  104. }
  105. // testIpcContainer is a helper function to test --ipc container:NNN mode in various scenarios
  106. func testIpcContainer(t *testing.T, donorMode string, mustWork bool) {
  107. t.Helper()
  108. defer setupTest(t)()
  109. cfg := containertypes.Config{
  110. Image: "busybox",
  111. Cmd: []string{"top"},
  112. }
  113. hostCfg := containertypes.HostConfig{
  114. IpcMode: containertypes.IpcMode(donorMode),
  115. }
  116. ctx := context.Background()
  117. client := testEnv.APIClient()
  118. // create and start the "donor" container
  119. resp, err := client.ContainerCreate(ctx, &cfg, &hostCfg, nil, "")
  120. assert.NilError(t, err)
  121. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  122. name1 := resp.ID
  123. err = client.ContainerStart(ctx, name1, types.ContainerStartOptions{})
  124. assert.NilError(t, err)
  125. // create and start the second container
  126. hostCfg.IpcMode = containertypes.IpcMode("container:" + name1)
  127. resp, err = client.ContainerCreate(ctx, &cfg, &hostCfg, nil, "")
  128. assert.NilError(t, err)
  129. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  130. name2 := resp.ID
  131. err = client.ContainerStart(ctx, name2, types.ContainerStartOptions{})
  132. if !mustWork {
  133. // start should fail with a specific error
  134. assert.Check(t, is.ErrorContains(err, "non-shareable IPC"))
  135. // no more checks to perform here
  136. return
  137. }
  138. // start should succeed
  139. assert.NilError(t, err)
  140. // check that IPC is shared
  141. // 1. create a file in the first container
  142. _, err = container.Exec(ctx, client, name1, []string{"sh", "-c", "printf covfefe > /dev/shm/bar"})
  143. assert.NilError(t, err)
  144. // 2. check it's the same file in the second one
  145. result, err := container.Exec(ctx, client, name2, []string{"cat", "/dev/shm/bar"})
  146. assert.NilError(t, err)
  147. out := result.Combined()
  148. assert.Check(t, is.Equal(true, regexp.MustCompile("^covfefe$").MatchString(out)))
  149. }
  150. // TestAPIIpcModeShareableAndPrivate checks that
  151. // 1) a container created with --ipc container:ID can use IPC of another shareable container.
  152. // 2) a container created with --ipc container:ID can NOT use IPC of another private container.
  153. func TestAPIIpcModeShareableAndContainer(t *testing.T) {
  154. skip.If(t, testEnv.IsRemoteDaemon)
  155. testIpcContainer(t, "shareable", true)
  156. testIpcContainer(t, "private", false)
  157. }
  158. /* TestAPIIpcModeHost checks that a container created with --ipc host
  159. * can use IPC of the host system.
  160. */
  161. func TestAPIIpcModeHost(t *testing.T) {
  162. skip.If(t, testEnv.IsRemoteDaemon)
  163. skip.If(t, testEnv.IsUserNamespace)
  164. cfg := containertypes.Config{
  165. Image: "busybox",
  166. Cmd: []string{"top"},
  167. }
  168. hostCfg := containertypes.HostConfig{
  169. IpcMode: containertypes.IpcMode("host"),
  170. }
  171. ctx := context.Background()
  172. client := testEnv.APIClient()
  173. resp, err := client.ContainerCreate(ctx, &cfg, &hostCfg, nil, "")
  174. assert.NilError(t, err)
  175. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  176. name := resp.ID
  177. err = client.ContainerStart(ctx, name, types.ContainerStartOptions{})
  178. assert.NilError(t, err)
  179. // check that IPC is shared
  180. // 1. create a file inside container
  181. _, err = container.Exec(ctx, client, name, []string{"sh", "-c", "printf covfefe > /dev/shm/." + name})
  182. assert.NilError(t, err)
  183. // 2. check it's the same on the host
  184. bytes, err := ioutil.ReadFile("/dev/shm/." + name)
  185. assert.NilError(t, err)
  186. assert.Check(t, is.Equal("covfefe", string(bytes)))
  187. // 3. clean up
  188. _, err = container.Exec(ctx, client, name, []string{"rm", "-f", "/dev/shm/." + name})
  189. assert.NilError(t, err)
  190. }
  191. // testDaemonIpcPrivateShareable is a helper function to test "private" and "shareable" daemon default ipc modes.
  192. func testDaemonIpcPrivateShareable(t *testing.T, mustBeShared bool, arg ...string) {
  193. defer setupTest(t)()
  194. d := daemon.New(t)
  195. d.StartWithBusybox(t, arg...)
  196. defer d.Stop(t)
  197. c := d.NewClientT(t)
  198. cfg := containertypes.Config{
  199. Image: "busybox",
  200. Cmd: []string{"top"},
  201. }
  202. ctx := context.Background()
  203. resp, err := c.ContainerCreate(ctx, &cfg, &containertypes.HostConfig{}, nil, "")
  204. assert.NilError(t, err)
  205. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  206. err = c.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
  207. assert.NilError(t, err)
  208. // get major:minor pair for /dev/shm from container's /proc/self/mountinfo
  209. cmd := "awk '($5 == \"/dev/shm\") {printf $3}' /proc/self/mountinfo"
  210. result, err := container.Exec(ctx, c, resp.ID, []string{"sh", "-c", cmd})
  211. assert.NilError(t, err)
  212. mm := result.Combined()
  213. assert.Check(t, is.Equal(true, regexp.MustCompile("^[0-9]+:[0-9]+$").MatchString(mm)))
  214. shared, err := testIpcCheckDevExists(mm)
  215. assert.NilError(t, err)
  216. t.Logf("[testDaemonIpcPrivateShareable] ipcdev: %v, shared: %v, mustBeShared: %v\n", mm, shared, mustBeShared)
  217. assert.Check(t, is.Equal(shared, mustBeShared))
  218. }
  219. // TestDaemonIpcModeShareable checks that --default-ipc-mode shareable works as intended.
  220. func TestDaemonIpcModeShareable(t *testing.T) {
  221. skip.If(t, testEnv.IsRemoteDaemon)
  222. testDaemonIpcPrivateShareable(t, true, "--default-ipc-mode", "shareable")
  223. }
  224. // TestDaemonIpcModePrivate checks that --default-ipc-mode private works as intended.
  225. func TestDaemonIpcModePrivate(t *testing.T) {
  226. skip.If(t, testEnv.IsRemoteDaemon)
  227. testDaemonIpcPrivateShareable(t, false, "--default-ipc-mode", "private")
  228. }
  229. // used to check if an IpcMode given in config works as intended
  230. func testDaemonIpcFromConfig(t *testing.T, mode string, mustExist bool) {
  231. config := `{"default-ipc-mode": "` + mode + `"}`
  232. file := fs.NewFile(t, "test-daemon-ipc-config", fs.WithContent(config))
  233. defer file.Remove()
  234. testDaemonIpcPrivateShareable(t, mustExist, "--config-file", file.Path())
  235. }
  236. // TestDaemonIpcModePrivateFromConfig checks that "default-ipc-mode: private" config works as intended.
  237. func TestDaemonIpcModePrivateFromConfig(t *testing.T) {
  238. skip.If(t, testEnv.IsRemoteDaemon)
  239. testDaemonIpcFromConfig(t, "private", false)
  240. }
  241. // TestDaemonIpcModeShareableFromConfig checks that "default-ipc-mode: shareable" config works as intended.
  242. func TestDaemonIpcModeShareableFromConfig(t *testing.T) {
  243. skip.If(t, testEnv.IsRemoteDaemon)
  244. testDaemonIpcFromConfig(t, "shareable", true)
  245. }
  246. // TestIpcModeOlderClient checks that older client gets shareable IPC mode
  247. // by default, even when the daemon default is private.
  248. func TestIpcModeOlderClient(t *testing.T) {
  249. skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.40"), "requires a daemon with DefaultIpcMode: private")
  250. c := testEnv.APIClient()
  251. skip.If(t, versions.LessThan(c.ClientVersion(), "1.40"), "requires client API >= 1.40")
  252. t.Parallel()
  253. ctx := context.Background()
  254. // pre-check: default ipc mode in daemon is private
  255. cID := container.Create(t, ctx, c, container.WithAutoRemove)
  256. inspect, err := c.ContainerInspect(ctx, cID)
  257. assert.NilError(t, err)
  258. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "private"))
  259. // main check: using older client creates "shareable" container
  260. c = request.NewAPIClient(t, client.WithVersion("1.39"))
  261. cID = container.Create(t, ctx, c, container.WithAutoRemove)
  262. inspect, err = c.ContainerInspect(ctx, cID)
  263. assert.NilError(t, err)
  264. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "shareable"))
  265. }