| 1 |
package mountinfo |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/go-quicktest/qt" |
| 8 |
) |
| 9 |
|
| 10 |
func TestParseEntriesPreservesRoot(t *testing.T) { |
| 11 |
|
| 12 |
|
| 13 |
const mountinfo = ` |
| 14 |
39 29 0:12 / /sys/kernel/tracing rw - tracefs tracefs rw |
| 15 |
40 29 0:12 /events /weird/tracing-events rw - tracefs tracefs rw |
| 16 |
` |
| 17 |
|
| 18 |
entries, err := parseEntries(strings.NewReader(mountinfo)) |
| 19 |
qt.Assert(t, qt.IsNil(err)) |
| 20 |
qt.Assert(t, qt.HasLen(entries, 2)) |
| 21 |
qt.Assert(t, qt.Equals(entries[0].Root, "/")) |
| 22 |
qt.Assert(t, qt.Equals(entries[1].Root, "/events")) |
| 23 |
} |
| 24 |
|
| 25 |
func TestParseEntriesLongLine(t *testing.T) { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
var lowers []string |
| 30 |
for range 1000 { |
| 31 |
lowers = append(lowers, "/var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/12345/fs") |
| 32 |
} |
| 33 |
long := "8 23 0:23 / / rw,relatime - overlay overlay rw,lowerdir=" + strings.Join(lowers, ":") |
| 34 |
if len(long) <= 64*1024 { |
| 35 |
t.Fatalf("test fixture not long enough: %d bytes", len(long)) |
| 36 |
} |
| 37 |
mountinfo := long + "\n39 29 0:12 / /sys/kernel/tracing rw - tracefs tracefs rw\n" |
| 38 |
|
| 39 |
entries, err := parseEntries(strings.NewReader(mountinfo)) |
| 40 |
qt.Assert(t, qt.IsNil(err)) |
| 41 |
qt.Assert(t, qt.HasLen(entries, 2)) |
| 42 |
qt.Assert(t, qt.Equals(entries[0].FSType, "overlay")) |
| 43 |
qt.Assert(t, qt.Equals(entries[1].MountPoint, "/sys/kernel/tracing")) |
| 44 |
qt.Assert(t, qt.Equals(entries[1].FSType, "tracefs")) |
| 45 |
} |
| 46 |
|
| 47 |
func TestParseEntries(t *testing.T) { |
| 48 |
const mountinfo = ` |
| 49 |
8 23 0:23 / / rw,relatime - overlay overlay rw,lowerdir=/overlay:/host,upperdir=/upper,workdir=/work,uuid=on |
| 50 |
29 28 0:27 / /sys rw,nosuid,nodev,noexec,relatime - sysfs sys rw |
| 51 |
30 28 0:28 / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw |
| 52 |
35 29 0:30 / /sys/fs/bpf rw,nosuid,nodev,noexec,relatime - bpf bpf rw,mode=700 |
| 53 |
36 29 0:30 / /sys/fs/foo\040bar\040baz rw,nosuid,nodev,noexec,relatime - bpf bpf rw,mode=700 |
| 54 |
37 29 0:30 / /sys/fs/功能\011\012\134bpf rw,nosuid,nodev,noexec,relatime - bpf bpf rw,mode=700 |
| 55 |
38 29 0:8 / /sys/kernel/debug rw,nosuid,nodev,noexec,relatime - debugfs debugfs rw |
| 56 |
39 29 0:12 / /sys/kernel/tracing rw,nosuid,nodev,noexec,relatime - tracefs tracefs rw |
| 57 |
` |
| 58 |
|
| 59 |
entries, err := parseEntries(strings.NewReader(mountinfo)) |
| 60 |
qt.Assert(t, qt.IsNil(err)) |
| 61 |
qt.Assert(t, qt.HasLen(entries, 8)) |
| 62 |
|
| 63 |
qt.Assert(t, qt.Equals(entries[0].MountPoint, "/")) |
| 64 |
qt.Assert(t, qt.Equals(entries[0].FSType, "overlay")) |
| 65 |
|
| 66 |
qt.Assert(t, qt.Equals(entries[3].MountPoint, "/sys/fs/bpf")) |
| 67 |
qt.Assert(t, qt.Equals(entries[3].FSType, "bpf")) |
| 68 |
|
| 69 |
|
| 70 |
qt.Assert(t, qt.Equals(entries[4].MountPoint, "/sys/fs/foo bar baz")) |
| 71 |
qt.Assert(t, qt.Equals(entries[5].MountPoint, "/sys/fs/功能\t\n\\bpf")) |
| 72 |
|
| 73 |
qt.Assert(t, qt.Equals(entries[6].MountPoint, "/sys/kernel/debug")) |
| 74 |
qt.Assert(t, qt.Equals(entries[6].FSType, "debugfs")) |
| 75 |
|
| 76 |
qt.Assert(t, qt.Equals(entries[7].MountPoint, "/sys/kernel/tracing")) |
| 77 |
qt.Assert(t, qt.Equals(entries[7].FSType, "tracefs")) |
| 78 |
} |
| 79 |
|
| 80 |
func TestParseEntriesInvalid(t *testing.T) { |
| 81 |
t.Run("missing dash separator", func(t *testing.T) { |
| 82 |
const mountinfo = `48 46 0:30 / /sys/fs/bpf rw,nosuid,nodev,noexec,relatime` |
| 83 |
_, err := parseEntries(strings.NewReader(mountinfo)) |
| 84 |
qt.Assert(t, qt.IsNotNil(err)) |
| 85 |
}) |
| 86 |
|
| 87 |
t.Run("too few fields before dash", func(t *testing.T) { |
| 88 |
const mountinfo = `48 46 0:30 / /sys/fs/bpf - bpf bpf rw` |
| 89 |
_, err := parseEntries(strings.NewReader(mountinfo)) |
| 90 |
qt.Assert(t, qt.IsNotNil(err)) |
| 91 |
}) |
| 92 |
|
| 93 |
t.Run("missing fstype after dash", func(t *testing.T) { |
| 94 |
const mountinfo = `48 46 0:30 / /sys/fs/bpf rw,nosuid - ` |
| 95 |
_, err := parseEntries(strings.NewReader(mountinfo)) |
| 96 |
qt.Assert(t, qt.IsNotNil(err)) |
| 97 |
}) |
| 98 |
} |
| 99 |
|
| 100 |
func TestFindByFSType(t *testing.T) { |
| 101 |
const mountinfo = ` |
| 102 |
35 29 0:30 / /sys/fs/bpf rw,nosuid,nodev,noexec,relatime - bpf bpf rw,mode=700 |
| 103 |
36 29 0:30 / /sys/fs/bpf rw,relatime - bpf none rw,delegate_cmds=prog_load |
| 104 |
37 29 0:30 / /run/tw/bpf rw,relatime - bpf none rw,delegate_cmds=prog_load |
| 105 |
38 29 0:8 / /sys/kernel/debug rw,nosuid,nodev,noexec,relatime - debugfs debugfs rw |
| 106 |
39 29 0:12 / /sys/kernel/tracing rw,nosuid,nodev,noexec,relatime - tracefs tracefs rw |
| 107 |
40 29 0:13 / /custom/tracing rw - tracefs tracefs rw |
| 108 |
` |
| 109 |
|
| 110 |
entries, err := parseEntries(strings.NewReader(mountinfo)) |
| 111 |
qt.Assert(t, qt.IsNil(err)) |
| 112 |
|
| 113 |
t.Run("filters and dedupes by mount point", func(t *testing.T) { |
| 114 |
got := filterByFSType(entries, "bpf") |
| 115 |
qt.Assert(t, qt.DeepEquals(got, []string{"/sys/fs/bpf", "/run/tw/bpf"})) |
| 116 |
}) |
| 117 |
|
| 118 |
t.Run("returns all matching mount points in order", func(t *testing.T) { |
| 119 |
got := filterByFSType(entries, "tracefs") |
| 120 |
qt.Assert(t, qt.DeepEquals(got, []string{"/sys/kernel/tracing", "/custom/tracing"})) |
| 121 |
}) |
| 122 |
|
| 123 |
t.Run("single match", func(t *testing.T) { |
| 124 |
got := filterByFSType(entries, "debugfs") |
| 125 |
qt.Assert(t, qt.DeepEquals(got, []string{"/sys/kernel/debug"})) |
| 126 |
}) |
| 127 |
|
| 128 |
t.Run("no match returns empty", func(t *testing.T) { |
| 129 |
got := filterByFSType(entries, "nfs") |
| 130 |
qt.Assert(t, qt.HasLen(got, 0)) |
| 131 |
}) |
| 132 |
} |