package util import "testing" func TestParseMinFreeSpace(t *testing.T) { tests := []struct { in string ok bool value *MinFreeSpace }{ {in: "42", ok: true, value: &MinFreeSpace{Type: AsPercent, Percent: 42, Raw: "42"}}, {in: "-1", ok: false, value: nil}, {in: "101", ok: false, value: nil}, {in: "100B", ok: false, value: nil}, {in: "100Ki", ok: true, value: &MinFreeSpace{Type: AsBytes, Bytes: 100 * 1024, Raw: "100Ki"}}, {in: "100GiB", ok: true, value: &MinFreeSpace{Type: AsBytes, Bytes: 100 * 1024 * 1024 * 1024, Raw: "100GiB"}}, {in: "42M", ok: true, value: &MinFreeSpace{Type: AsBytes, Bytes: 42 * 1000 * 1000, Raw: "42M"}}, {in: " 42 ", ok: true, value: &MinFreeSpace{Type: AsPercent, Percent: 42, Raw: "42"}}, {in: " 42M ", ok: true, value: &MinFreeSpace{Type: AsBytes, Bytes: 42 * 1000 * 1000, Raw: "42M"}}, } for _, p := range tests { got, err := ParseMinFreeSpace(p.in) if p.ok != (err == nil) { t.Errorf("failed to test %v", p.in) } if p.ok && err == nil && *got != *p.value { t.Errorf("failed to test %v", p.in) } } } // A location that went low must not flip back on every reading that touches // the limit; it is released only once free space is the margin above it. func TestMinFreeSpaceIsLowFromPercent(t *testing.T) { const total = uint64(1000) << 30 s := MinFreeSpace{Type: AsPercent, Percent: 7} free := func(percent float64) (uint64, uint64, float32) { return uint64(float64(total) * percent / 100), total, float32(percent) } // readings, as free percent, and the verdict wanted after each check. readings := []struct { percent float64 wantLow bool }{ {7.50, false}, // above the limit {7.00, false}, // exactly at the limit is not low {6.99, true}, // crosses the limit {7.01, true}, // a delete gives a little back: still low {6.98, true}, {7.40, true}, {7.99, true}, // not yet the margin above the limit {8.00, false}, {7.60, false}, // released, and stays so until it crosses the limit again {6.99, true}, } low := false for i, r := range readings { freeBytes, totalBytes, pct := free(r.percent) var desc string low, desc = s.IsLowFrom(low, freeBytes, totalBytes, pct) if low != r.wantLow { t.Fatalf("reading %d (%.2f%% free): low = %v, want %v (%s)", i, r.percent, low, r.wantLow, desc) } } } func TestMinFreeSpaceIsLowFromBytes(t *testing.T) { const total = uint64(10) << 40 // 10 TiB: the margin is 1% of it const limit = uint64(100) << 30 margin := total / 100 s := MinFreeSpace{Type: AsBytes, Bytes: limit, Raw: "100GiB"} steps := []struct { free uint64 wasLow bool wantLow bool }{ {limit + 1, false, false}, {limit, false, false}, {limit - 1, false, true}, {limit + 1, true, true}, {limit + margin - 1, true, true}, {limit + margin, true, false}, {limit + margin + 1, true, false}, // Not low before: the margin plays no part in going low. {limit + margin - 1, false, false}, } for i, st := range steps { got, desc := s.IsLowFrom(st.wasLow, st.free, total, float32(float64(st.free)/float64(total)*100)) if got != st.wantLow { t.Errorf("step %d (free %d, wasLow %v): low = %v, want %v (%s)", i, st.free, st.wasLow, got, st.wantLow, desc) } } } // Without a previous low verdict IsLowFrom is IsLow. func TestMinFreeSpaceIsLowFromMatchesIsLowWhenNotLow(t *testing.T) { for _, s := range []MinFreeSpace{ {Type: AsPercent, Percent: 7}, {Type: AsBytes, Bytes: 10 << 30}, {}, } { for _, free := range []uint64{0, 5 << 30, 10 << 30, 70 << 30, 71 << 30, 500 << 30} { const total = uint64(1000) << 30 pct := float32(float64(free) / float64(total) * 100) want, _ := s.IsLow(free, pct) got, _ := s.IsLowFrom(false, free, total, pct) if got != want { t.Errorf("%v free %d: IsLowFrom(false) = %v, IsLow = %v", s, free, got, want) } } } } // A byte limit larger than the disk's headroom to MaxUint64 must not wrap the // release level around to a small value. func TestMinFreeSpaceIsLowFromBytesOverflow(t *testing.T) { s := MinFreeSpace{Type: AsBytes, Bytes: ^uint64(0) - 10} if low, _ := s.IsLowFrom(true, ^uint64(0)-5, ^uint64(0), 100); !low { t.Fatal("a wrapped release level released a low location") } }