Skip to content

Commit 303f101

Browse files
manugupt1kolyshkin
authored andcommitted
mountinfo: add tests for MountedFast
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent 9742587 commit 303f101

1 file changed

Lines changed: 60 additions & 2 deletions

File tree

mountinfo/mounted_linux_test.go

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func tMount(t *testing.T, src, dst, fstype string, flags uintptr, options string
3434
return nil
3535
}
3636

37-
var testMounts = []struct {
37+
type testMount struct {
3838
desc string
3939
isNotExist bool
4040
isMount bool
@@ -48,7 +48,9 @@ var testMounts = []struct {
4848
// simplicity (no need to check for errors or call t.Cleanup()), and
4949
// it may call t.Fatal, but practically we don't expect it.
5050
prepare func(t *testing.T) (string, error)
51-
}{
51+
}
52+
53+
var testMounts = []testMount{
5254
{
5355
desc: "non-existent path",
5456
isNotExist: true,
@@ -278,12 +280,65 @@ func tryOpenat2() error {
278280
return err
279281
}
280282

283+
func testMountedFast(t *testing.T, path string, tc *testMount, openat2Supported bool) {
284+
mounted, sure, err := MountedFast(path)
285+
if err != nil {
286+
// Got an error; is it expected?
287+
if !(tc.isNotExist && errors.Is(err, os.ErrNotExist)) {
288+
t.Errorf("MountedFast: unexpected error: %v", err)
289+
}
290+
291+
// In case of an error, sure and mounted must be false.
292+
if sure {
293+
t.Error("MountedFast: expected sure to be false on error")
294+
}
295+
if mounted {
296+
t.Error("MountedFast: expected mounted to be false on error")
297+
}
298+
299+
// No more checks.
300+
return
301+
}
302+
303+
if openat2Supported {
304+
if mounted != tc.isMount {
305+
t.Errorf("MountedFast: expected mounted to be %v, got %v", tc.isMount, mounted)
306+
}
307+
308+
// No more checks.
309+
return
310+
}
311+
312+
if tc.isBind {
313+
// For bind mounts, in case openat2 is not supported,
314+
// sure and mounted must be false.
315+
if sure {
316+
t.Error("MountedFast: expected sure to be false for a bind mount")
317+
}
318+
if mounted {
319+
t.Error("MountedFast: expected mounted to be false for a bind mount")
320+
}
321+
} else {
322+
if mounted != tc.isMount {
323+
t.Errorf("MountFast: expected mounted to be %v, got %v", tc.isMount, mounted)
324+
}
325+
if tc.isMount && !sure {
326+
t.Error("MountFast: expected sure to be true for normal mount")
327+
}
328+
if !tc.isMount && sure {
329+
t.Error("MountFast: expected sure to be false for non-mount")
330+
}
331+
}
332+
}
333+
281334
func TestMountedBy(t *testing.T) {
282335
checked := false
336+
openat2Supported := false
283337

284338
// List of individual implementations to check.
285339
toCheck := []func(string) (bool, error){mountedByMountinfo, mountedByStat}
286340
if tryOpenat2() == nil {
341+
openat2Supported = true
287342
toCheck = append(toCheck, mountedByOpenat2)
288343
}
289344

@@ -312,6 +367,9 @@ func TestMountedBy(t *testing.T) {
312367
}
313368
}
314369

370+
// Check the public MountedFast() function as a whole.
371+
testMountedFast(t, m, &tc, openat2Supported)
372+
315373
// Check individual mountedBy* implementations.
316374

317375
// All mountedBy* functions should be called with normalized paths.

0 commit comments

Comments
 (0)