package shell import ( "context" "encoding/json" "strconv" "strings" "testing" "time" "github.com/seaweedfs/seaweedfs/weed/filer/dirusage" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" ) func (s *fsDuTestFilerServer) GetFilerConfiguration(context.Context, *filer_pb.GetFilerConfigurationRequest) (*filer_pb.GetFilerConfigurationResponse, error) { return &filer_pb.GetFilerConfigurationResponse{Signature: 1, DirBuckets: "/buckets"}, nil } // addRecord adds the record the dir_usage worker job keeps of a root. func (s *fsDuTestFilerServer) addRecord(t *testing.T, kind string, record *dirusage.Record) { t.Helper() content, err := json.Marshal(record) if err != nil { t.Fatalf("encode record: %v", err) } dir := dirusage.Dir + "/1/" + kind s.addDir(dir) name := record.Root[strings.LastIndex(record.Root, "/")+1:] + ".json" s.addEntry(dir, &filer_pb.Entry{Name: name, Content: content, Attributes: &filer_pb.FuseAttributes{FileSize: uint64(len(content))}}) } func TestFsDuReportPrintsTheRecordsInsteadOfWalking(t *testing.T) { server := newFsDuTestFilerServer() server.addFile("/buckets/b/a/x/f", 1) at := time.Date(2026, 12, 3, 8, 0, 0, 0, time.UTC) server.addRecord(t, "buckets", &dirusage.Record{Version: dirusage.Version, Root: "/buckets/b", TsNs: at.UnixNano(), Dirs: map[string]dirusage.Usage{ "": {Bytes: 1000, Chunks: 10}, "a": {Bytes: 600, Chunks: 6}, "a/x": {Bytes: 300, Chunks: 3}, "c": {Bytes: 100, Chunks: 1}, }}) server.addRecord(t, "buckets", &dirusage.Record{Version: dirusage.Version, Root: "/buckets/b2", TsNs: at.Add(time.Hour).UnixNano(), Dirs: map[string]dirusage.Usage{ "": {Bytes: 5, Chunks: 1}, }}) server.addRecord(t, "top", &dirusage.Record{Version: dirusage.Version, Root: "/home", TsNs: at.UnixNano(), Dirs: map[string]dirusage.Usage{ "": {Bytes: 7, Chunks: 2}, }}) // the last replay reached past /home's record without changing it server.addDir(dirusage.Dir) server.addEntry(dirusage.Dir, &filer_pb.Entry{Name: "cursor-1.json", Content: []byte(`{"ts_ns":` + strconv.FormatInt(at.Add(30*time.Minute).UnixNano(), 10) + `}`)}) commandEnv := newFsDuTestCommandEnv(t, server) asOf := "as of 2026-12-03T08:30:00Z, " for _, c := range []struct { args []string lines []string }{ {[]string{"-report", "/buckets/b"}, []string{ "block: 10\tlogical size: 1000\t/buckets/b", }}, {[]string{"-report", "-depth=2", "/buckets/b/"}, []string{ "block: 3\tlogical size: 300\t/buckets/b/a/x", "block: 6\tlogical size: 600\t/buckets/b/a", "block: 1\tlogical size: 100\t/buckets/b/c", "block: 10\tlogical size: 1000\t/buckets/b", }}, {[]string{"-report", "-depth=1", "/buckets/b/a"}, []string{ "block: 3\tlogical size: 300\t/buckets/b/a/x", "block: 6\tlogical size: 600\t/buckets/b/a", }}, {[]string{"-report", "-depth=1", "/buckets"}, []string{ "block: 10\tlogical size: 1000\t/buckets/b", "block: 1\tlogical size: 5\t/buckets/b2", "block: 11\tlogical size: 1005\t/buckets", }}, // /etc holds the records here, and has none of its own {[]string{"-report", "-depth=1", "/"}, []string{ "block: 11\tlogical size: 1005\t/buckets", "block: 2\tlogical size: 7\t/home", "block: 13\tlogical size: 1012\t/", "not counted, no record yet: /etc", }}, {[]string{"-report", "/buckets/b/a/small"}, []string{ "the record of /buckets/b keeps no total of /buckets/b/a/small: what is there counts toward the directory above it", }}, } { server.resetCounts() out, err := runFsDu(t, commandEnv, c.args...) if err != nil { t.Errorf("fs.du %s: %v", strings.Join(c.args, " "), err) continue } lines := strings.Split(strings.TrimSuffix(out, "\n"), "\n") if len(lines) == 0 || !strings.HasPrefix(lines[len(lines)-1], asOf) { t.Errorf("fs.du %s: no %q line in\n%s", strings.Join(c.args, " "), asOf, out) continue } if got := strings.Join(lines[:len(lines)-1], "\n"); got != strings.Join(c.lines, "\n") { t.Errorf("fs.du %s printed\n%s\nwant\n%s", strings.Join(c.args, " "), got, strings.Join(c.lines, "\n")) } // the records, and the folders holding the buckets and the // top-level directories to tell which have none server.mu.Lock() for dir := range server.listed { if !strings.HasPrefix(dir, dirusage.Dir) && dir != "/" && dir != "/buckets" { t.Errorf("fs.du %s listed %s", strings.Join(c.args, " "), dir) } } server.mu.Unlock() } } func TestFsDuReportSaysWhenThereIsNoRecord(t *testing.T) { server := newFsDuTestFilerServer() server.addDir("/buckets/b") commandEnv := newFsDuTestCommandEnv(t, server) for _, args := range [][]string{ {"-report", "/buckets/b"}, {"-report", "/buckets"}, {"-report", "/etc/seaweedfs/dir_usage"}, } { if out, err := runFsDu(t, commandEnv, args...); err == nil { t.Errorf("fs.du %s: no error, printed\n%s", strings.Join(args, " "), out) } } } func TestFsDuReportSaysWhichBucketsItLeavesOut(t *testing.T) { server := newFsDuTestFilerServer() server.addFile("/buckets/b/f", 1000) // a bucket created since the last run, or whose record was deleted to // have it seeded again server.addDir("/buckets/new") server.addRecord(t, "buckets", &dirusage.Record{Version: dirusage.Version, Root: "/buckets/b", TsNs: time.Now().UnixNano(), Dirs: map[string]dirusage.Usage{ "": {Bytes: 1000, Chunks: 1}, }}) commandEnv := newFsDuTestCommandEnv(t, server) out, err := runFsDu(t, commandEnv, "-report", "-depth=1", "/buckets") if err != nil { t.Fatalf("fs.du -report /buckets: %v", err) } if !strings.Contains(out, "not counted, no record yet: /buckets/new\n") || !strings.Contains(out, "logical size: 1000\t/buckets\n") { t.Errorf("fs.du -report /buckets printed\n%s", out) } }