From 58be8bde5d7e5f31438db582fe34a676300a3ec3 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 14 Aug 2021 20:05:26 +0200 Subject: [PATCH 1/2] mountinfo: rewrite to use a test-table This allows us to more easily add more/other samples, as well as combine the TestParseFedoraMountinfo, TestParseUbuntuMountinfo, TestParseGentooMountinfo, and TestParseFedoraMountinfoFields tests. Signed-off-by: Sebastiaan van Stijn --- mountinfo/mountinfo_linux_test.go | 99 ++++++++++++++++--------------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/mountinfo/mountinfo_linux_test.go b/mountinfo/mountinfo_linux_test.go index afca2347..7bdfab2d 100644 --- a/mountinfo/mountinfo_linux_test.go +++ b/mountinfo/mountinfo_linux_test.go @@ -424,56 +424,57 @@ const ( 649 94 259:5 /tmp/newline\012tab\011space\040backslash\134quote1'quote2" /tmp/newline\012tab\011space\040backslash\134quote1'quote2" rw,relatime shared:47 - ext4 /dev/nvme0n1p5 rw,seclabel` ) -func TestParseFedoraMountinfo(t *testing.T) { - r := bytes.NewBuffer([]byte(fedoraMountinfo)) - _, err := GetMountsFromReader(r, nil) - if err != nil { - t.Fatal(err) - } -} - -func TestParseUbuntuMountinfo(t *testing.T) { - r := bytes.NewBuffer([]byte(ubuntuMountinfo)) - _, err := GetMountsFromReader(r, nil) - if err != nil { - t.Fatal(err) - } -} - -func TestParseGentooMountinfo(t *testing.T) { - r := bytes.NewBuffer([]byte(gentooMountinfo)) - _, err := GetMountsFromReader(r, nil) - if err != nil { - t.Fatal(err) - } -} - -func TestParseFedoraMountinfoFields(t *testing.T) { - r := bytes.NewBuffer([]byte(fedoraMountinfo)) - infos, err := GetMountsFromReader(r, nil) - if err != nil { - t.Fatal(err) - } - expectedLength := 58 - if len(infos) != expectedLength { - t.Fatalf("Expected %d entries, got %d", expectedLength, len(infos)) - } - mi := Info{ - ID: 15, - Parent: 35, - Major: 0, - Minor: 3, - Root: "/", - Mountpoint: "/proc", - Options: "rw,nosuid,nodev,noexec,relatime", - Optional: "shared:5", - FSType: "proc", - Source: "proc", - VFSOptions: "rw", +func TestParseMountInfo(t *testing.T) { + testCases := []struct { + name string + info string + expectedLength int + mi *Info + }{ + { + name: "fedora", + info: fedoraMountinfo, + expectedLength: 58, + mi: &Info{ + ID: 15, + Parent: 35, + Major: 0, + Minor: 3, + Root: "/", + Mountpoint: "/proc", + Options: "rw,nosuid,nodev,noexec,relatime", + Optional: "shared:5", + FSType: "proc", + Source: "proc", + VFSOptions: "rw", + }, + }, + { + name: "gentoo", + info: gentooMountinfo, + expectedLength: 222, + }, + { + name: "ubuntu", + info: ubuntuMountinfo, + expectedLength: 130, + }, } - - if *infos[0] != mi { - t.Fatalf("expected %#v, got %#v", mi, infos[0]) + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + r := bytes.NewBuffer([]byte(tc.info)) + infos, err := GetMountsFromReader(r, nil) + if err != nil { + t.Fatal(err) + } + if len(infos) != tc.expectedLength { + t.Errorf("Expected %d entries, got %d", tc.expectedLength, len(infos)) + } + if tc.mi != nil && *infos[0] != *tc.mi { + t.Fatalf("expected %#v, got %#v", tc.mi, infos[0]) + } + }) } } From 6089f58f8ae68a9da765461bd788adc875bc4783 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 14 Aug 2021 20:08:23 +0200 Subject: [PATCH 2/2] mountinfo: TestParseMountInfo also check gentoo and ubuntu result Signed-off-by: Sebastiaan van Stijn --- mountinfo/mountinfo_linux_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/mountinfo/mountinfo_linux_test.go b/mountinfo/mountinfo_linux_test.go index 7bdfab2d..b6c85bb9 100644 --- a/mountinfo/mountinfo_linux_test.go +++ b/mountinfo/mountinfo_linux_test.go @@ -453,11 +453,37 @@ func TestParseMountInfo(t *testing.T) { name: "gentoo", info: gentooMountinfo, expectedLength: 222, + mi: &Info{ + ID: 15, + Parent: 1, + Major: 8, + Minor: 6, + Root: "/", + Mountpoint: "/", + Options: "rw,noatime,nodiratime", + Optional: "", + FSType: "ext4", + Source: "/dev/sda6", + VFSOptions: "rw,data=ordered", + }, }, { name: "ubuntu", info: ubuntuMountinfo, expectedLength: 130, + mi: &Info{ + ID: 15, + Parent: 20, + Major: 0, + Minor: 14, + Root: "/", + Mountpoint: "/sys", + Options: "rw,nosuid,nodev,noexec,relatime", + Optional: "", + FSType: "sysfs", + Source: "sysfs", + VFSOptions: "rw", + }, }, } for _, tc := range testCases {