package dirusage import ( "math" "testing" "time" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/util" ) func TestRootsOfAPath(t *testing.T) { for _, buckets := range []string{"/buckets", "/buckets/", ""} { roots := NewRoots(buckets) for path, want := range map[util.FullPath]util.FullPath{ "/buckets/b": "/buckets/b", "/buckets/b/x/y": "/buckets/b", "/home/u/f": "/home", "/topics/.system": "/topics", "/topics/.system/log/2026-12-03": "", "/etc/seaweedfs/dir_usage/1/top/x": "", "/buckets": "", "/": "", } { root, ok := roots.RootOf(path) if !ok { root = "" } if root != want { t.Errorf("buckets folder %q: %s counts toward %q, want %q", buckets, path, root, want) } } } nested := NewRoots("/tenant/buckets/") if root, ok := nested.RootOf("/tenant/buckets/b/f"); !ok || root != "/tenant/buckets/b" { t.Errorf("/tenant/buckets/b/f counts toward %q, %v", root, ok) } if _, ok := nested.RootOf("/tenant/other"); ok { t.Errorf("what holds the buckets folder counts") } } func TestExpiresByTheStepOfItsTTL(t *testing.T) { expiresAt := time.Date(2026, 12, 4, 10, 20, 0, 0, time.UTC) file := func(ttl time.Duration) *filer_pb.Entry { return &filer_pb.Entry{Name: "f", Attributes: &filer_pb.FuseAttributes{ FileSize: 1, Crtime: expiresAt.Add(-ttl).Unix(), TtlSec: int32(ttl / time.Second), }} } for _, c := range []struct{ ttl, step time.Duration }{ {30 * time.Minute, time.Hour}, {7 * 24 * time.Hour, time.Hour}, {200 * time.Hour, 2 * time.Hour}, {30 * 24 * time.Hour, 4 * time.Hour}, {365 * 24 * time.Hour, 64 * time.Hour}, } { step := int64(c.step / time.Second) want := (expiresAt.Unix() + step - 1) / step * step if got := Expires(file(c.ttl)); got != want { t.Errorf("TTL %v: comes off at %d, want %d, the end of its %v step", c.ttl, got, want, c.step) } } if got := Expires(&filer_pb.Entry{Name: "f", Attributes: &filer_pb.FuseAttributes{TtlSec: 60}}); got != 3600 { t.Errorf("an entry that expired long ago comes off at %d", got) } for _, entry := range []*filer_pb.Entry{nil, {Name: "f", Attributes: &filer_pb.FuseAttributes{FileSize: 1}}, {Name: "d", IsDirectory: true, Attributes: &filer_pb.FuseAttributes{TtlSec: 60}}} { if got := Expires(entry); got != 0 { t.Errorf("%v comes off at %d", entry, got) } } } func TestExpiryStepsAreFewAndShort(t *testing.T) { hour := int64(time.Hour / time.Second) for ttl := int64(1); ttl <= math.MaxInt32; ttl = ttl*9/8 + 1 { step := expiryStep(ttl) if step%hour != 0 || step/hour&(step/hour-1) != 0 { t.Fatalf("TTL %ds: step %ds is not a power of two hours", ttl, step) } if step > max(hour, ttl/100) { t.Errorf("TTL %ds: step %ds counts an expired entry for longer than an hour or 1%% of it", ttl, step) } if ttl/step >= 200 { t.Errorf("TTL %ds: %d steps of %ds", ttl, ttl/step, step) } } if !TakenOff(10, 10*int64(time.Second)) || TakenOff(10, 10*int64(time.Second)-1) || TakenOff(0, math.MaxInt64) { t.Errorf("TakenOff") } if TakenOff(math.MaxInt64/2, math.MaxInt64) { t.Errorf("a step beyond the last position is taken off") } }