| 1 |
package mount |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/moby/sys/mountinfo" |
| 10 |
) |
| 11 |
|
| 12 |
func TestMount(t *testing.T) { |
| 13 |
if os.Getuid() != 0 { |
| 14 |
t.Skip("root required") |
| 15 |
} |
| 16 |
|
| 17 |
source := t.TempDir() |
| 18 |
|
| 19 |
if err := Mount("tmpfs", source, "tmpfs", "private"); err != nil { |
| 20 |
t.Fatal(err) |
| 21 |
} |
| 22 |
defer ensureUnmount(t, source) |
| 23 |
validateMount(t, source, "", "", "") |
| 24 |
if t.Failed() { |
| 25 |
t.FailNow() |
| 26 |
} |
| 27 |
|
| 28 |
target := t.TempDir() |
| 29 |
|
| 30 |
tests := []struct { |
| 31 |
source string |
| 32 |
ftype string |
| 33 |
options string |
| 34 |
expectedOpts string |
| 35 |
expectedOptional string |
| 36 |
expectedVFS string |
| 37 |
}{ |
| 38 |
|
| 39 |
{"tmpfs", "tmpfs", "", "", "", ""}, |
| 40 |
|
| 41 |
{"tmpfs", "tmpfs", "noexec", "noexec", "", ""}, |
| 42 |
|
| 43 |
{source, "", "bind", "", "", ""}, |
| 44 |
{source, "", "bind,private", "", "", ""}, |
| 45 |
{source, "", "bind,shared", "", "shared", ""}, |
| 46 |
{source, "", "bind,slave", "", "master", ""}, |
| 47 |
{source, "", "bind,unbindable", "", "unbindable", ""}, |
| 48 |
|
| 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 |
|
| 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 |
{"", "", "remount,size=128k", "rw", "", "rw,size=128k"}, |
| 62 |
{"", "", "remount,ro,size=128k", "ro", "", "ro,size=128k"}, |
| 63 |
} |
| 64 |
|
| 65 |
for _, tc := range tests { |
| 66 |
ftype, options := tc.ftype, tc.options |
| 67 |
if tc.ftype == "" { |
| 68 |
ftype = "none" |
| 69 |
} |
| 70 |
if tc.options == "" { |
| 71 |
options = "none" |
| 72 |
} |
| 73 |
|
| 74 |
t.Run(fmt.Sprintf("%v-%v", ftype, options), func(t *testing.T) { |
| 75 |
if strings.Contains(tc.options, "slave") { |
| 76 |
|
| 77 |
if err := MakeShared(source); err != nil { |
| 78 |
t.Fatal(err) |
| 79 |
} |
| 80 |
defer func() { |
| 81 |
if err := MakePrivate(source); err != nil { |
| 82 |
t.Fatal(err) |
| 83 |
} |
| 84 |
}() |
| 85 |
} |
| 86 |
if strings.Contains(tc.options, "remount") { |
| 87 |
|
| 88 |
if err := Mount("tmpfs", target, "tmpfs", ""); err != nil { |
| 89 |
t.Fatal(err) |
| 90 |
} |
| 91 |
} |
| 92 |
if err := Mount(tc.source, target, tc.ftype, tc.options); err != nil { |
| 93 |
t.Fatal(err) |
| 94 |
} |
| 95 |
defer ensureUnmount(t, target) |
| 96 |
validateMount(t, target, tc.expectedOpts, tc.expectedOptional, tc.expectedVFS) |
| 97 |
}) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
func ensureUnmount(t *testing.T, mnt string) { |
| 103 |
if err := Unmount(mnt); err != nil { |
| 104 |
t.Error(err) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func validateMount(t *testing.T, mnt string, opts, optional, vfs string) { |
| 110 |
info, err := mountinfo.GetMounts(nil) |
| 111 |
if err != nil { |
| 112 |
t.Fatal(err) |
| 113 |
} |
| 114 |
|
| 115 |
wantedOpts := make(map[string]struct{}) |
| 116 |
if opts != "" { |
| 117 |
for _, opt := range strings.Split(opts, ",") { |
| 118 |
wantedOpts[opt] = struct{}{} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
wantedOptional := make(map[string]struct{}) |
| 123 |
if optional != "" { |
| 124 |
for _, opt := range strings.Split(optional, ",") { |
| 125 |
wantedOptional[opt] = struct{}{} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
wantedVFS := make(map[string]struct{}) |
| 130 |
if vfs != "" { |
| 131 |
for _, opt := range strings.Split(vfs, ",") { |
| 132 |
wantedVFS[opt] = struct{}{} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
mnts := make(map[int]*mountinfo.Info, len(info)) |
| 137 |
for _, mi := range info { |
| 138 |
mnts[mi.ID] = mi |
| 139 |
} |
| 140 |
|
| 141 |
for _, mi := range info { |
| 142 |
if mi.Mountpoint != mnt { |
| 143 |
continue |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
p := mnts[mi.Parent] |
| 148 |
pOpts := make(map[string]struct{}) |
| 149 |
if p.Options != "" { |
| 150 |
for _, opt := range strings.Split(p.Options, ",") { |
| 151 |
pOpts[clean(opt)] = struct{}{} |
| 152 |
} |
| 153 |
} |
| 154 |
pOptional := make(map[string]struct{}) |
| 155 |
if p.Optional != "" { |
| 156 |
for _, field := range strings.Split(p.Optional, ",") { |
| 157 |
pOptional[clean(field)] = struct{}{} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
if mi.Options != "" { |
| 163 |
for _, opt := range strings.Split(mi.Options, ",") { |
| 164 |
opt = clean(opt) |
| 165 |
if !has(wantedOpts, opt) && !has(pOpts, opt) && opt != "relatime" { |
| 166 |
t.Errorf("unexpected mount option %q, expected %q", opt, opts) |
| 167 |
} |
| 168 |
delete(wantedOpts, opt) |
| 169 |
} |
| 170 |
} |
| 171 |
for opt := range wantedOpts { |
| 172 |
t.Errorf("missing mount option %q, found %q", opt, mi.Options) |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
if mi.Optional != "" { |
| 177 |
for _, field := range strings.Split(mi.Optional, ",") { |
| 178 |
field = clean(field) |
| 179 |
if !has(wantedOptional, field) && !has(pOptional, field) { |
| 180 |
t.Errorf("unexpected optional field %q, expected %q", field, optional) |
| 181 |
} |
| 182 |
delete(wantedOptional, field) |
| 183 |
} |
| 184 |
} |
| 185 |
for field := range wantedOptional { |
| 186 |
t.Errorf("missing optional field %q, found %q", field, mi.Optional) |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
if vfs != "" { |
| 191 |
if mi.VFSOptions != "" { |
| 192 |
for _, opt := range strings.Split(mi.VFSOptions, ",") { |
| 193 |
opt = clean(opt) |
| 194 |
if !has(wantedVFS, opt) && |
| 195 |
opt != "seclabel" && |
| 196 |
opt != "inode64" && opt != "inode32" { |
| 197 |
t.Errorf("unexpected vfs option %q, expected %q", opt, vfs) |
| 198 |
} |
| 199 |
delete(wantedVFS, opt) |
| 200 |
} |
| 201 |
} |
| 202 |
for opt := range wantedVFS { |
| 203 |
t.Errorf("missing vfs option %q, found %q", opt, mi.VFSOptions) |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
return |
| 208 |
} |
| 209 |
|
| 210 |
t.Errorf("failed to find mount %q", mnt) |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
func clean(v string) string { |
| 215 |
out, _, _ := strings.Cut(v, ":") |
| 216 |
return out |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
func has(m map[string]struct{}, key string) bool { |
| 221 |
_, ok := m[key] |
| 222 |
return ok |
| 223 |
} |