| 1 |
|
| 2 |
|
| 3 |
package mount |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"os" |
| 8 |
"path" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/moby/sys/mountinfo" |
| 13 |
"golang.org/x/sys/unix" |
| 14 |
) |
| 15 |
|
| 16 |
func TestMountOptionsParsing(t *testing.T) { |
| 17 |
options := "noatime,ro,noexec,size=10k" |
| 18 |
|
| 19 |
flag, data := parseOptions(options) |
| 20 |
|
| 21 |
if data != "size=10k" { |
| 22 |
t.Fatalf("Expected size=10 got %s", data) |
| 23 |
} |
| 24 |
|
| 25 |
expected := NOATIME | RDONLY | NOEXEC |
| 26 |
|
| 27 |
if flag != expected { |
| 28 |
t.Fatalf("Expected %d got %d", expected, flag) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
func TestMounted(t *testing.T) { |
| 33 |
if os.Getuid() != 0 { |
| 34 |
t.Skip("root required") |
| 35 |
} |
| 36 |
|
| 37 |
var ( |
| 38 |
tmp = t.TempDir() |
| 39 |
sourceDir = path.Join(tmp, "source") |
| 40 |
targetDir = path.Join(tmp, "target") |
| 41 |
sourcePath = path.Join(sourceDir, "file.txt") |
| 42 |
targetPath = path.Join(targetDir, "file.txt") |
| 43 |
) |
| 44 |
|
| 45 |
if err := os.Mkdir(sourceDir, 0o777); err != nil { |
| 46 |
t.Fatal(err) |
| 47 |
} |
| 48 |
if err := os.Mkdir(targetDir, 0o777); err != nil { |
| 49 |
t.Fatal(err) |
| 50 |
} |
| 51 |
|
| 52 |
if err := os.WriteFile(sourcePath, []byte("hello"), 0o644); err != nil { |
| 53 |
t.Fatal(err) |
| 54 |
} |
| 55 |
|
| 56 |
if err := os.WriteFile(targetPath, nil, 0o644); err != nil { |
| 57 |
t.Fatal(err) |
| 58 |
} |
| 59 |
|
| 60 |
if err := Mount(sourceDir, targetDir, "none", "bind,rw"); err != nil { |
| 61 |
t.Fatal(err) |
| 62 |
} |
| 63 |
defer func() { |
| 64 |
if err := Unmount(targetDir); err != nil { |
| 65 |
t.Fatal(err) |
| 66 |
} |
| 67 |
}() |
| 68 |
|
| 69 |
mounted, err := mountinfo.Mounted(targetDir) |
| 70 |
if err != nil { |
| 71 |
t.Fatal(err) |
| 72 |
} |
| 73 |
if !mounted { |
| 74 |
t.Fatalf("Expected %s to be mounted", targetDir) |
| 75 |
} |
| 76 |
if _, err := os.Stat(targetDir); err != nil { |
| 77 |
t.Fatal(err) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func TestMountTmpfsOptions(t *testing.T) { |
| 82 |
if os.Getuid() != 0 { |
| 83 |
t.Skip("root required") |
| 84 |
} |
| 85 |
|
| 86 |
testCases := []struct { |
| 87 |
opts string |
| 88 |
expected string |
| 89 |
unexpected string |
| 90 |
}{ |
| 91 |
{ |
| 92 |
opts: "exec", |
| 93 |
unexpected: "noexec", |
| 94 |
}, |
| 95 |
{ |
| 96 |
opts: "noexec", |
| 97 |
expected: "noexec", |
| 98 |
unexpected: "exec", |
| 99 |
}, |
| 100 |
} |
| 101 |
|
| 102 |
target := t.TempDir() |
| 103 |
for _, tc := range testCases { |
| 104 |
t.Run(tc.opts, func(t *testing.T) { |
| 105 |
if err := Mount("tmpfs", target, "tmpfs", tc.opts); err != nil { |
| 106 |
t.Fatal(err) |
| 107 |
} |
| 108 |
defer ensureUnmount(t, target) |
| 109 |
|
| 110 |
mounts, err := mountinfo.GetMounts(mountinfo.SingleEntryFilter(target)) |
| 111 |
if err != nil { |
| 112 |
t.Fatal(err) |
| 113 |
} |
| 114 |
if len(mounts) != 1 { |
| 115 |
t.Fatal("Mount point ", target, " not found") |
| 116 |
} |
| 117 |
entry := mounts[0] |
| 118 |
opts := "," + entry.Options + "," |
| 119 |
if tc.expected != "" && !strings.Contains(opts, ","+tc.expected+",") { |
| 120 |
t.Fatal("Expected option ", tc.expected, " missing from ", entry.Options) |
| 121 |
} |
| 122 |
if tc.unexpected != "" && strings.Contains(opts, ","+tc.unexpected+",") { |
| 123 |
t.Fatal("Unexpected option ", tc.unexpected, " in ", entry.Options) |
| 124 |
} |
| 125 |
}) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestMountReadonly(t *testing.T) { |
| 130 |
if os.Getuid() != 0 { |
| 131 |
t.Skip("root required") |
| 132 |
} |
| 133 |
|
| 134 |
var ( |
| 135 |
tmp = t.TempDir() |
| 136 |
sourceDir = path.Join(tmp, "source") |
| 137 |
targetDir = path.Join(tmp, "target") |
| 138 |
sourcePath = path.Join(sourceDir, "file.txt") |
| 139 |
targetPath = path.Join(targetDir, "file.txt") |
| 140 |
) |
| 141 |
|
| 142 |
if err := os.Mkdir(sourceDir, 0o777); err != nil { |
| 143 |
t.Fatal(err) |
| 144 |
} |
| 145 |
if err := os.Mkdir(targetDir, 0o777); err != nil { |
| 146 |
t.Fatal(err) |
| 147 |
} |
| 148 |
|
| 149 |
if err := os.WriteFile(sourcePath, []byte("hello"), 0o644); err != nil { |
| 150 |
t.Fatal(err) |
| 151 |
} |
| 152 |
|
| 153 |
if err := os.WriteFile(targetPath, nil, 0o644); err != nil { |
| 154 |
t.Fatal(err) |
| 155 |
} |
| 156 |
|
| 157 |
if err := Mount(sourceDir, targetDir, "none", "bind,ro"); err != nil { |
| 158 |
t.Fatal(err) |
| 159 |
} |
| 160 |
defer func() { |
| 161 |
if err := Unmount(targetDir); err != nil { |
| 162 |
t.Fatal(err) |
| 163 |
} |
| 164 |
}() |
| 165 |
|
| 166 |
if err := os.WriteFile(targetPath, []byte("hello"), 0o644); err == nil { |
| 167 |
t.Fatal("Should not be able to open a ro file as rw") |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
func TestMergeTmpfsOptions(t *testing.T) { |
| 172 |
options := []string{"noatime", "ro", "size=10k", "defaults", "noexec", "atime", "defaults", "rw", "rprivate", "size=1024k", "slave", "exec"} |
| 173 |
expected := []string{"atime", "rw", "size=1024k", "slave", "exec"} |
| 174 |
merged, err := MergeTmpfsOptions(options) |
| 175 |
if err != nil { |
| 176 |
t.Fatal(err) |
| 177 |
} |
| 178 |
if len(expected) != len(merged) { |
| 179 |
t.Fatalf("Expected %s got %s", expected, merged) |
| 180 |
} |
| 181 |
for index := range merged { |
| 182 |
if merged[index] != expected[index] { |
| 183 |
t.Fatalf("Expected %s for the %dth option, got %s", expected, index, merged) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
options = []string{"noatime", "ro", "size=10k", "atime", "rw", "rprivate", "size=1024k", "slave", "size", "exec"} |
| 188 |
_, err = MergeTmpfsOptions(options) |
| 189 |
if err == nil { |
| 190 |
t.Fatal("Expected error got nil") |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
func TestRecursiveUnmountTooGreedy(t *testing.T) { |
| 195 |
if os.Getuid() != 0 { |
| 196 |
t.Skip("root required") |
| 197 |
} |
| 198 |
|
| 199 |
tmp := t.TempDir() |
| 200 |
|
| 201 |
|
| 202 |
dirs := []string{"dir-other", "dir/subdir1", "dir/subdir1/subsub", "dir/subdir2/subsub"} |
| 203 |
for _, d := range dirs { |
| 204 |
dir := path.Join(tmp, d) |
| 205 |
if err := os.MkdirAll(dir, 0o700); err != nil { |
| 206 |
t.Fatal(err) |
| 207 |
} |
| 208 |
if err := Mount("tmpfs", dir, "tmpfs", ""); err != nil { |
| 209 |
t.Fatal(err) |
| 210 |
} |
| 211 |
|
| 212 |
defer Unmount(dir) |
| 213 |
} |
| 214 |
|
| 215 |
mounted, err := mountinfo.Mounted(path.Join(tmp, "dir-other")) |
| 216 |
if err != nil { |
| 217 |
t.Fatalf("[pre-check] error from mountinfo.mounted: %v", err) |
| 218 |
} |
| 219 |
if !mounted { |
| 220 |
t.Fatal("[pre-check] expected dir-other to be mounted, but it's not") |
| 221 |
} |
| 222 |
|
| 223 |
if err := RecursiveUnmount(path.Join(tmp, "dir")); err != nil { |
| 224 |
t.Fatal(err) |
| 225 |
} |
| 226 |
mounted, err = mountinfo.Mounted(path.Join(tmp, "dir-other")) |
| 227 |
if err != nil { |
| 228 |
t.Fatalf("error from mountinfo.mounted: %v", err) |
| 229 |
} |
| 230 |
if !mounted { |
| 231 |
t.Fatal("expected dir-other to be mounted, but it's not") |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
func TestRecursiveUnmount_SubMountFailsToUnmount(t *testing.T) { |
| 236 |
if os.Getuid() != 0 { |
| 237 |
t.Skip("root required") |
| 238 |
} |
| 239 |
|
| 240 |
var ( |
| 241 |
tmp = t.TempDir() |
| 242 |
parent = tmp + "/sub1" |
| 243 |
child = tmp + "/sub1/sub2" |
| 244 |
grandChild = tmp + "/sub1/sub2/sub3" |
| 245 |
) |
| 246 |
|
| 247 |
err := os.MkdirAll(grandChild, 0o700) |
| 248 |
if err != nil { |
| 249 |
t.Fatal(err) |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
toMount := []string{grandChild, child, parent} |
| 257 |
for _, dir := range toMount { |
| 258 |
dir := dir |
| 259 |
if err := Mount("tmpfs", dir, "tmpfs", ""); err != nil { |
| 260 |
t.Fatal(err) |
| 261 |
} |
| 262 |
defer Unmount(dir) |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
shadowedMounts := []string{child, grandChild} |
| 267 |
for _, shadowedMount := range shadowedMounts { |
| 268 |
t.Run(shadowedMount, func(t *testing.T) { |
| 269 |
err := RecursiveUnmount(shadowedMount) |
| 270 |
if !errors.Is(err, unix.ENOENT) { |
| 271 |
t.Fatalf("expected submount(shadowed) %s to return unix.ENOENT, got %v", shadowedMount, err) |
| 272 |
} |
| 273 |
}) |
| 274 |
} |
| 275 |
} |