oci_linux_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "os"
  4. "testing"
  5. containertypes "github.com/docker/docker/api/types/container"
  6. "github.com/docker/docker/container"
  7. "github.com/docker/docker/daemon/config"
  8. "github.com/docker/docker/oci"
  9. "github.com/docker/docker/pkg/idtools"
  10. "gotest.tools/assert"
  11. is "gotest.tools/assert/cmp"
  12. )
  13. // TestTmpfsDevShmNoDupMount checks that a user-specified /dev/shm tmpfs
  14. // mount (as in "docker run --tmpfs /dev/shm:rw,size=NNN") does not result
  15. // in "Duplicate mount point" error from the engine.
  16. // https://github.com/moby/moby/issues/35455
  17. func TestTmpfsDevShmNoDupMount(t *testing.T) {
  18. d := Daemon{
  19. // some empty structs to avoid getting a panic
  20. // caused by a null pointer dereference
  21. idMapping: &idtools.IdentityMapping{},
  22. configStore: &config.Config{},
  23. }
  24. c := &container.Container{
  25. ShmPath: "foobar", // non-empty, for c.IpcMounts() to work
  26. HostConfig: &containertypes.HostConfig{
  27. IpcMode: containertypes.IpcMode("shareable"), // default mode
  28. // --tmpfs /dev/shm:rw,exec,size=NNN
  29. Tmpfs: map[string]string{
  30. "/dev/shm": "rw,exec,size=1g",
  31. },
  32. },
  33. }
  34. // Mimic the code flow of daemon.createSpec(), enough to reproduce the issue
  35. ms, err := d.setupMounts(c)
  36. assert.Check(t, err)
  37. ms = append(ms, c.IpcMounts()...)
  38. tmpfsMounts, err := c.TmpfsMounts()
  39. assert.Check(t, err)
  40. ms = append(ms, tmpfsMounts...)
  41. s := oci.DefaultSpec()
  42. err = setMounts(&d, &s, c, ms)
  43. assert.Check(t, err)
  44. }
  45. // TestIpcPrivateVsReadonly checks that in case of IpcMode: private
  46. // and ReadonlyRootfs: true (as in "docker run --ipc private --read-only")
  47. // the resulting /dev/shm mount is NOT made read-only.
  48. // https://github.com/moby/moby/issues/36503
  49. func TestIpcPrivateVsReadonly(t *testing.T) {
  50. d := Daemon{
  51. // some empty structs to avoid getting a panic
  52. // caused by a null pointer dereference
  53. idMapping: &idtools.IdentityMapping{},
  54. configStore: &config.Config{},
  55. }
  56. c := &container.Container{
  57. HostConfig: &containertypes.HostConfig{
  58. IpcMode: containertypes.IpcMode("private"),
  59. ReadonlyRootfs: true,
  60. },
  61. }
  62. // We can't call createSpec() so mimick the minimal part
  63. // of its code flow, just enough to reproduce the issue.
  64. ms, err := d.setupMounts(c)
  65. assert.Check(t, err)
  66. s := oci.DefaultSpec()
  67. s.Root.Readonly = c.HostConfig.ReadonlyRootfs
  68. err = setMounts(&d, &s, c, ms)
  69. assert.Check(t, err)
  70. // Find the /dev/shm mount in ms, check it does not have ro
  71. for _, m := range s.Mounts {
  72. if m.Destination != "/dev/shm" {
  73. continue
  74. }
  75. assert.Check(t, is.Equal(false, inSlice(m.Options, "ro")))
  76. }
  77. }
  78. func TestGetSourceMount(t *testing.T) {
  79. // must be able to find source mount for /
  80. mnt, _, err := getSourceMount("/")
  81. assert.NilError(t, err)
  82. assert.Equal(t, mnt, "/")
  83. // must be able to find source mount for current directory
  84. cwd, err := os.Getwd()
  85. assert.NilError(t, err)
  86. _, _, err = getSourceMount(cwd)
  87. assert.NilError(t, err)
  88. }