mounter_linux_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // +build linux
  2. package mount
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "strings"
  8. "testing"
  9. )
  10. func TestMount(t *testing.T) {
  11. if os.Getuid() != 0 {
  12. t.Skip("not root tests would fail")
  13. }
  14. source, err := ioutil.TempDir("", "mount-test-source-")
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. defer os.RemoveAll(source)
  19. // Ensure we have a known start point by mounting tmpfs with given options
  20. if err := Mount("tmpfs", source, "tmpfs", "private"); err != nil {
  21. t.Fatal(err)
  22. }
  23. defer ensureUnmount(t, source)
  24. validateMount(t, source, "", "")
  25. if t.Failed() {
  26. t.FailNow()
  27. }
  28. target, err := ioutil.TempDir("", "mount-test-target-")
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. defer os.RemoveAll(target)
  33. tests := []struct {
  34. source string
  35. ftype string
  36. options string
  37. expectedOpts string
  38. expectedOptional string
  39. }{
  40. // No options
  41. {"tmpfs", "tmpfs", "", "", ""},
  42. // Default rw / ro test
  43. {source, "", "bind", "", ""},
  44. {source, "", "bind,private", "", ""},
  45. {source, "", "bind,shared", "", "shared"},
  46. {source, "", "bind,slave", "", "master"},
  47. {source, "", "bind,unbindable", "", "unbindable"},
  48. // Read Write tests
  49. {source, "", "bind,rw", "rw", ""},
  50. {source, "", "bind,rw,private", "rw", ""},
  51. {source, "", "bind,rw,shared", "rw", "shared"},
  52. {source, "", "bind,rw,slave", "rw", "master"},
  53. {source, "", "bind,rw,unbindable", "rw", "unbindable"},
  54. // Read Only tests
  55. {source, "", "bind,ro", "ro", ""},
  56. {source, "", "bind,ro,private", "ro", ""},
  57. {source, "", "bind,ro,shared", "ro", "shared"},
  58. {source, "", "bind,ro,slave", "ro", "master"},
  59. {source, "", "bind,ro,unbindable", "ro", "unbindable"},
  60. }
  61. for _, tc := range tests {
  62. ftype, options := tc.ftype, tc.options
  63. if tc.ftype == "" {
  64. ftype = "none"
  65. }
  66. if tc.options == "" {
  67. options = "none"
  68. }
  69. t.Run(fmt.Sprintf("%v-%v", ftype, options), func(t *testing.T) {
  70. if strings.Contains(tc.options, "slave") {
  71. // Slave requires a shared source
  72. if err := MakeShared(source); err != nil {
  73. t.Fatal(err)
  74. }
  75. defer func() {
  76. if err := MakePrivate(source); err != nil {
  77. t.Fatal(err)
  78. }
  79. }()
  80. }
  81. if err := Mount(tc.source, target, tc.ftype, tc.options); err != nil {
  82. t.Fatal(err)
  83. }
  84. defer ensureUnmount(t, target)
  85. validateMount(t, target, tc.expectedOpts, tc.expectedOptional)
  86. })
  87. }
  88. }
  89. // ensureUnmount umounts mnt checking for errors
  90. func ensureUnmount(t *testing.T, mnt string) {
  91. if err := Unmount(mnt); err != nil {
  92. t.Error(err)
  93. }
  94. }
  95. // validateMount checks that mnt has the given options
  96. func validateMount(t *testing.T, mnt string, opts, optional string) {
  97. info, err := GetMounts()
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. wantedOpts := make(map[string]struct{})
  102. if opts != "" {
  103. for _, opt := range strings.Split(opts, ",") {
  104. wantedOpts[opt] = struct{}{}
  105. }
  106. }
  107. wantedOptional := make(map[string]struct{})
  108. if optional != "" {
  109. for _, opt := range strings.Split(optional, ",") {
  110. wantedOptional[opt] = struct{}{}
  111. }
  112. }
  113. mnts := make(map[int]*Info, len(info))
  114. for _, mi := range info {
  115. mnts[mi.ID] = mi
  116. }
  117. for _, mi := range info {
  118. if mi.Mountpoint != mnt {
  119. continue
  120. }
  121. // Use parent info as the defaults
  122. p := mnts[mi.Parent]
  123. pOpts := make(map[string]struct{})
  124. if p.Opts != "" {
  125. for _, opt := range strings.Split(p.Opts, ",") {
  126. pOpts[clean(opt)] = struct{}{}
  127. }
  128. }
  129. pOptional := make(map[string]struct{})
  130. if p.Optional != "" {
  131. for _, field := range strings.Split(p.Optional, ",") {
  132. pOptional[clean(field)] = struct{}{}
  133. }
  134. }
  135. // Validate Opts
  136. if mi.Opts != "" {
  137. for _, opt := range strings.Split(mi.Opts, ",") {
  138. opt = clean(opt)
  139. if !has(wantedOpts, opt) && !has(pOpts, opt) {
  140. t.Errorf("unexpected mount option %q expected %q", opt, opts)
  141. }
  142. delete(wantedOpts, opt)
  143. }
  144. }
  145. for opt := range wantedOpts {
  146. t.Errorf("missing mount option %q found %q", opt, mi.Opts)
  147. }
  148. // Validate Optional
  149. if mi.Optional != "" {
  150. for _, field := range strings.Split(mi.Optional, ",") {
  151. field = clean(field)
  152. if !has(wantedOptional, field) && !has(pOptional, field) {
  153. t.Errorf("unexpected optional failed %q expected %q", field, optional)
  154. }
  155. delete(wantedOptional, field)
  156. }
  157. }
  158. for field := range wantedOptional {
  159. t.Errorf("missing optional field %q found %q", field, mi.Optional)
  160. }
  161. return
  162. }
  163. t.Errorf("failed to find mount %q", mnt)
  164. }
  165. // clean strips off any value param after the colon
  166. func clean(v string) string {
  167. return strings.SplitN(v, ":", 2)[0]
  168. }
  169. // has returns true if key is a member of m
  170. func has(m map[string]struct{}, key string) bool {
  171. _, ok := m[key]
  172. return ok
  173. }