97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
package nas
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestUnescapeMountField(t *testing.T) {
|
|
cases := map[string]string{
|
|
`/mnt/plain`: "/mnt/plain",
|
|
`/mnt/my\040share`: "/mnt/my share",
|
|
`/mnt/tab\011here`: "/mnt/tab\there",
|
|
`/mnt/back\134slash`: `/mnt/back\slash`,
|
|
`//nas/dci`: "//nas/dci",
|
|
`/mnt/not\9octal`: `/mnt/not\9octal`,
|
|
`/mnt/trailing\`: `/mnt/trailing\`,
|
|
}
|
|
|
|
for in, want := range cases {
|
|
if got := unescapeMountField(in); got != want {
|
|
t.Errorf("unescapeMountField(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNormalizeMountSource(t *testing.T) {
|
|
cases := map[string]string{
|
|
"//NAS/DCI": "//nas/dci",
|
|
"//nas/dci/": "//nas/dci",
|
|
`\\nas\dci`: "//nas/dci",
|
|
" //nas/dci ": "//nas/dci",
|
|
"/": "/",
|
|
}
|
|
|
|
for in, want := range cases {
|
|
if got := normalizeMountSource(in); got != want {
|
|
t.Errorf("normalizeMountSource(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestReadMountTableSkipsMalformedLines(t *testing.T) {
|
|
path := writeMountTable(t,
|
|
"source-only",
|
|
"source /point-but-no-type",
|
|
"",
|
|
"//nas/dci /mnt/dci cifs rw 0 0",
|
|
)
|
|
|
|
entries, err := readMountTable(path)
|
|
if err != nil {
|
|
t.Fatalf("readMountTable failed: %v", err)
|
|
}
|
|
if len(entries) != 1 {
|
|
t.Fatalf("expected 1 usable entry, got %d: %v", len(entries), entries)
|
|
}
|
|
if entries[0].Source != "//nas/dci" || entries[0].Point != "/mnt/dci" || entries[0].FSType != "cifs" {
|
|
t.Errorf("unexpected entry: %+v", entries[0])
|
|
}
|
|
}
|
|
|
|
func TestReadMountTableMissingFile(t *testing.T) {
|
|
if _, err := readMountTable("/definitely/not/a/mount/table"); err == nil {
|
|
t.Error("expected an error for a missing mount table")
|
|
}
|
|
}
|
|
|
|
// TestReadRealMountTable closes the loop between the fixtures above and the
|
|
// kernel's actual output: the parser must cope with whatever this host has
|
|
// mounted, not just hand-written examples.
|
|
func TestReadRealMountTable(t *testing.T) {
|
|
if _, err := os.Stat(procMountsPath); err != nil {
|
|
t.Skipf("%s is not available on this platform", procMountsPath)
|
|
}
|
|
|
|
entries, err := readMountTable(procMountsPath)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse the host's %s: %v", procMountsPath, err)
|
|
}
|
|
if len(entries) == 0 {
|
|
t.Fatalf("%s parsed to zero entries, which cannot be right", procMountsPath)
|
|
}
|
|
|
|
foundRoot := false
|
|
for _, e := range entries {
|
|
if e.Point == "" || e.FSType == "" {
|
|
t.Errorf("entry parsed with empty fields: %+v", e)
|
|
}
|
|
if e.Point == "/" {
|
|
foundRoot = true
|
|
}
|
|
}
|
|
if !foundRoot {
|
|
t.Error("expected the root filesystem in the host's mount table")
|
|
}
|
|
}
|