package command import ( "context" "strings" "testing" "time" "github.com/seaweedfs/seaweedfs/weed/filer" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" ) func TestMetadataLogGap(t *testing.T) { logStart := time.Date(2026, 11, 30, 3, 7, 0, 0, time.UTC) logs := map[string][]*filer_pb.Entry{ filer.SystemLogDir: { {Name: "2026-12-01", IsDirectory: true}, {Name: "2026-11-30", IsDirectory: true}, {Name: "not-a-day", IsDirectory: true}, }, filer.SystemLogDir + "/2026-11-30": { {Name: "05-00.0000000a"}, {Name: "03-07.0000000b"}, }, } child := func(name string, commitTsNs int64, mtime time.Time) *filer_pb.Entry { return &filer_pb.Entry{Name: name, IsDirectory: true, CommitTsNs: commitTsNs, Attributes: &filer_pb.FuseAttributes{Mtime: mtime.Unix()}} } before, after := logStart.Add(-time.Hour), logStart.Add(time.Hour) tests := []struct { name string dirs map[string][]*filer_pb.Entry sourcePath string exclude []string offsetTsNs int64 want string }{ {name: "no metadata log", dirs: map[string][]*filer_pb.Entry{"/buckets": {child("old", before.UnixNano(), before)}}, sourcePath: "/buckets"}, {name: "position inside the log", offsetTsNs: after.UnixNano()}, {name: "position at the oldest file", offsetTsNs: logStart.UnixNano()}, {name: "position before the log", offsetTsNs: before.UnixNano(), want: "after the saved sync position 2026-11-30T02:07:00Z"}, {name: "fresh sync, every entry in the log", dirs: map[string][]*filer_pb.Entry{"/buckets": {child("recent", after.UnixNano(), before)}}, sourcePath: "/buckets"}, {name: "fresh sync, entry changed before the log", dirs: map[string][]*filer_pb.Entry{"/buckets": {child("recent", after.UnixNano(), after), child("historical", before.UnixNano(), before)}}, sourcePath: "/buckets", want: "/buckets/historical was last changed at 2026-11-30T02:07:00Z"}, {name: "fresh sync, entry without commit time", dirs: map[string][]*filer_pb.Entry{"/buckets": {child("historical", 0, before)}}, sourcePath: "/buckets", want: "/buckets/historical"}, {name: "fresh sync of the root skips the log directory", dirs: map[string][]*filer_pb.Entry{"/": {child("topics", before.UnixNano(), before)}}, sourcePath: "/"}, {name: "fresh sync skips excluded entries", dirs: map[string][]*filer_pb.Entry{"/buckets": {child("archive", before.UnixNano(), before), child("recent", after.UnixNano(), after)}}, sourcePath: "/buckets", exclude: []string{"/buckets/archive"}}, {name: "fresh sync skips the sync cursors", dirs: map[string][]*filer_pb.Entry{"/etc/seaweedfs": {child("filer_sync", before.UnixNano(), before)}}, sourcePath: "/etc/seaweedfs"}, {name: "fresh sync skips multipart uploads", dirs: map[string][]*filer_pb.Entry{"/buckets/b": {child(".uploads", before.UnixNano(), before)}}, sourcePath: "/buckets/b"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { dirs := map[string][]*filer_pb.Entry{} if tt.name != "no metadata log" { for k, v := range logs { dirs[k] = v } } for k, v := range tt.dirs { dirs[k] = v } got, err := metadataLogGap(context.Background(), &verifyTestInnerClient{entriesByDir: dirs}, tt.sourcePath, tt.exclude, tt.offsetTsNs) if err != nil { t.Fatalf("metadataLogGap: %v", err) } if tt.want == "" { if got != "" { t.Fatalf("got gap %q, want none", got) } return } if !strings.Contains(got, tt.want) { t.Fatalf("got gap %q, want it to mention %q", got, tt.want) } }) } }