package discovery import ( "os" "path/filepath" "testing" ) func TestTwoDirectoriesOnOneDiskAreOnePoolOfBytes(t *testing.T) { // The reason this matters: free space belongs to the disk, not to the path. // Counting two directories separately lets the control plane add 24 GB to // 24 GB and place a 40 GB model on a node that has 24. base := t.TempDir() a := filepath.Join(base, "models-a") b := filepath.Join(base, "models-b") for _, d := range []string{a, b} { if err := os.Mkdir(d, 0o755); err != nil { t.Fatal(err) } } got := ScanStorage([]string{a, b}) if len(got) != 1 { t.Fatalf("two directories on one filesystem reported as %d pools: %+v", len(got), got) } if got[0].Path != a { t.Errorf("kept %q rather than the first directory configured", got[0].Path) } } func TestStorageReportsSomethingPlausible(t *testing.T) { got := ScanStorage([]string{t.TempDir()}) if len(got) != 1 { t.Fatalf("a real directory reported %d filesystems", len(got)) } s := got[0] if s.TotalBytes <= 0 { t.Error("a filesystem with no size at all") } if s.FreeBytes < 0 || s.FreeBytes > s.TotalBytes { t.Errorf("free %d of total %d", s.FreeBytes, s.TotalBytes) } } func TestAMissingDirectoryIsNotAFullDisk(t *testing.T) { // A mount that has not come back yet looks exactly like a missing // directory. Reporting it as zero free bytes would take the node out of // placement for a reason that is not true; reporting nothing says only // that nothing is known, which is the honest answer. got := ScanStorage([]string{filepath.Join(t.TempDir(), "never-mounted")}) if len(got) != 0 { t.Errorf("a missing directory was reported as a filesystem: %+v", got) } } func TestNoDirectoriesConstrainNothing(t *testing.T) { if got := ScanStorage(nil); len(got) != 0 { t.Errorf("scanning nothing found %+v", got) } if got := ScanStorage([]string{""}); len(got) != 0 { t.Errorf("an empty path found %+v", got) } }