[disk][linux]: refactor Partitions and add tests.

pull/1861/head
shirou 1 month ago
parent c4ec263816
commit 9896b27464

@ -285,14 +285,25 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
return nil, err return nil, err
} }
ret := make([]PartitionStat, 0, len(lines)) var ret []PartitionStat
if useMounts { // use mounts file
ret = parseFieldsOnMounts(lines, all, fs)
} else { // use mountinfo
ret, err = parseFieldsOnMountinfo(ctx, lines, all, fs, filename)
if err != nil {
return nil, fmt.Errorf("error parsing mountinfo file %s: %w", filename, err)
}
}
return ret, nil
}
func parseFieldsOnMounts(lines []string, all bool, fs []string) []PartitionStat {
ret := make([]PartitionStat, 0, len(lines))
for _, line := range lines { for _, line := range lines {
var d PartitionStat
if useMounts {
fields := strings.Fields(line) fields := strings.Fields(line)
d = PartitionStat{ d := PartitionStat{
Device: fields[0], Device: fields[0],
Mountpoint: unescapeFstab(fields[1]), Mountpoint: unescapeFstab(fields[1]),
Fstype: fields[2], Fstype: fields[2],
@ -304,7 +315,16 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
continue continue
} }
} }
} else { ret = append(ret, d)
}
return ret
}
func parseFieldsOnMountinfo(ctx context.Context, lines []string, all bool, fs []string, filename string) ([]PartitionStat, error) {
ret := make([]PartitionStat, 0, len(lines))
for _, line := range lines {
// a line of 1/mountinfo has the following structure: // a line of 1/mountinfo has the following structure:
// 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue // 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
// (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) // (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)
@ -328,7 +348,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
fstype := fields[0] fstype := fields[0]
device := fields[1] device := fields[1]
d = PartitionStat{ d := PartitionStat{
Device: device, Device: device,
Mountpoint: unescapeFstab(mountPoint), Mountpoint: unescapeFstab(mountPoint),
Fstype: fstype, Fstype: fstype,
@ -356,10 +376,8 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
d.Device = strings.Replace(d.Device, "root", filepath.Base(devpath), 1) d.Device = strings.Replace(d.Device, "root", filepath.Base(devpath), 1)
} }
} }
}
ret = append(ret, d) ret = append(ret, d)
} }
return ret, nil return ret, nil
} }

@ -0,0 +1,83 @@
// SPDX-License-Identifier: BSD-3-Clause
//go:build linux
package disk
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_parseFieldsOnMountinfo(t *testing.T) {
fs := []string{"sysfs", "tmpfs"}
lines := []string{
"111 80 0:22 / /sys rw,nosuid,nodev,noexec,noatime shared:15 - sysfs sysfs rw",
"114 80 0:61 / /run rw,nosuid,nodev shared:18 - tmpfs none rw,mode=755",
}
cases := map[string]struct {
all bool
expect []PartitionStat
}{
"all": {
all: true,
expect: []PartitionStat{
{Device: "sysfs", Mountpoint: "/sys", Fstype: "sysfs", Opts: []string{"rw", "nosuid", "nodev", "noexec", "noatime"}},
{Device: "none", Mountpoint: "/run", Fstype: "tmpfs", Opts: []string{"rw", "nosuid", "nodev"}},
},
},
"not all": {
all: false,
expect: []PartitionStat{
{Device: "sysfs", Mountpoint: "/sys", Fstype: "sysfs", Opts: []string{"rw", "nosuid", "nodev", "noexec", "noatime"}},
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
actual, err := parseFieldsOnMountinfo(context.Background(), lines, c.all, fs, "")
require.NoError(t, err)
assert.Equal(t, c.expect, actual)
})
}
}
func Test_parseFieldsOnMounts(t *testing.T) {
fs := []string{"sysfs", "tmpfs"}
lines := []string{
"sysfs /sys sysfs rw,nosuid,nodev,noexec,noatime 0 0",
"none /run tmpfs rw,nosuid,nodev,mode=755 0 0",
}
cases := map[string]struct {
all bool
expect []PartitionStat
}{
"all": {
all: true,
expect: []PartitionStat{
{Device: "sysfs", Mountpoint: "/sys", Fstype: "sysfs", Opts: []string{"rw", "nosuid", "nodev", "noexec", "noatime"}},
{Device: "none", Mountpoint: "/run", Fstype: "tmpfs", Opts: []string{"rw", "nosuid", "nodev", "mode=755"}},
},
},
"not all": {
all: false,
expect: []PartitionStat{
{Device: "sysfs", Mountpoint: "/sys", Fstype: "sysfs", Opts: []string{"rw", "nosuid", "nodev", "noexec", "noatime"}},
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
actual := parseFieldsOnMounts(lines, c.all, fs)
assert.Equal(t, c.expect, actual)
})
}
}
Loading…
Cancel
Save