Skip to content

Commit 03cca99

Browse files
committed
mountinfo: unify mountedBy checks
Instead of copy/pasting the code to check individual mountedBy* implementations, add a list and a loop to check them. The only special thing is, we have to check if the name of the function being tested is "mountedByStat", to skip erroring out on bind mounts. Otherwise the code is the same. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 03b9f8d commit 03cca99

1 file changed

Lines changed: 29 additions & 36 deletions

File tree

mountinfo/mounted_linux_test.go

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"net"
66
"os"
77
"path/filepath"
8+
"reflect"
9+
"runtime"
10+
"strings"
811
"testing"
912

1013
"golang.org/x/sys/unix"
@@ -276,8 +279,14 @@ func tryOpenat2() error {
276279
}
277280

278281
func TestMountedBy(t *testing.T) {
279-
openat2Supported := tryOpenat2() == nil
280282
checked := false
283+
284+
// List of individual implementations to check.
285+
toCheck := []func(string) (bool, error){mountedByMountinfo, mountedByStat}
286+
if tryOpenat2() == nil {
287+
toCheck = append(toCheck, mountedByOpenat2)
288+
}
289+
281290
for _, tc := range testMounts {
282291
tc := tc
283292
t.Run(tc.desc, func(t *testing.T) {
@@ -316,45 +325,29 @@ func TestMountedBy(t *testing.T) {
316325
t.Fatalf("normalizePath: %v", err)
317326
}
318327

319-
mounted, err = mountedByMountinfo(m)
320-
if err != nil {
321-
t.Errorf("mountedByMountinfo error: %v", err)
322-
// Check false is returned in error case.
323-
if mounted != false {
324-
t.Errorf("MountedByMountinfo: expected false on error, got %v", mounted)
325-
}
326-
} else if mounted != exp {
327-
t.Errorf("mountedByMountinfo: expected %v, got %v", exp, mounted)
328-
}
329-
checked = true
330-
331-
mounted, err = mountedByStat(m)
332-
if err != nil {
333-
t.Errorf("mountedByStat error: %v", err)
334-
// Check false is returned in error case.
335-
if mounted != false {
336-
t.Errorf("MountedByStat: expected false on error, got %v", mounted)
337-
}
338-
} else if mounted != exp && !tc.isBind { // mountedByStat can not detect bind mounts
339-
t.Errorf("mountedByStat: expected %v, got %v", exp, mounted)
340-
}
341-
342-
if !openat2Supported {
343-
return
344-
}
345-
mounted, err = mountedByOpenat2(m)
346-
if err != nil {
347-
t.Errorf("mountedByOpenat2 error: %v", err)
348-
// Check false is returned in error case.
349-
if mounted != false {
350-
t.Errorf("MountedByOpenat2: expected false on error, got %v", mounted)
328+
for _, fn := range toCheck {
329+
// Figure out function name.
330+
name := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
331+
332+
mounted, err = fn(m)
333+
if err != nil {
334+
t.Errorf("%s: %v", name, err)
335+
// Check false is returned in error case.
336+
if mounted != false {
337+
t.Errorf("%s: expected false on error, got %v", name, mounted)
338+
}
339+
} else if mounted != exp {
340+
if tc.isBind && strings.HasSuffix(name, "mountedByStat") {
341+
// mountedByStat can not detect bind mounts.
342+
} else {
343+
t.Errorf("%s: expected %v, got %v", name, exp, mounted)
344+
}
351345
}
352-
} else if mounted != exp {
353-
t.Errorf("mountedByOpenat2: expected %v, got %v", exp, mounted)
346+
checked = true
354347
}
355348
})
356-
357349
}
350+
358351
if !checked {
359352
t.Skip("no mounts to check")
360353
}

0 commit comments

Comments
 (0)