diff --git a/weed/server/filer_server_handlers_copy_manifest_test.go b/weed/server/filer_server_handlers_copy_manifest_test.go index ea50565de4..338cff462a 100644 --- a/weed/server/filer_server_handlers_copy_manifest_test.go +++ b/weed/server/filer_server_handlers_copy_manifest_test.go @@ -59,19 +59,13 @@ func (m *chunkCopyMaster) LookupVolume(_ context.Context, req *master_pb.LookupV return resp, nil } -func TestCopyChunkManifestPreservesOverwrites(t *testing.T) { +// startChunkCopyFixture serves chunks from a fake volume server, keeps what is +// written to it, and returns a filer server whose store holds nothing yet. +func startChunkCopyFixture(t *testing.T, ctx context.Context, chunks map[string][]byte) (*FilerServer, *renameTestStore, *wdclient.MasterClient) { + t.Helper() operation.InvalidateVolumeIdLocationCache("1") t.Cleanup(func() { operation.InvalidateVolumeIdLocationCache("1") }) - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) - defer cancel() - old := &filer_pb.FileChunk{FileId: "1,0100000001", Size: 7, ModifiedTsNs: 1} - filer_pb.BeforeEntrySerialization([]*filer_pb.FileChunk{old}) - manifest, err := proto.Marshal(&filer_pb.FileChunkManifest{Chunks: []*filer_pb.FileChunk{old}}) - if err != nil { - t.Fatal(err) - } var mu sync.Mutex - chunks := map[string][]byte{"/1,0100000001": []byte("OLD old"), "/1,0200000001": []byte("NEW"), "/1,0300000001": manifest} volume := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { mu.Lock() defer mu.Unlock() @@ -103,8 +97,38 @@ func TestCopyChunkManifestPreservesOverwrites(t *testing.T) { if ctx.Err() != nil { t.Fatal(ctx.Err()) } - store := newRenameTestStore() + fs := &FilerServer{filer: newRenameTestFiler(t, store), option: &FilerOption{}, grpcDialOption: dialOption, + entryLockTable: util.NewLockTable[util.FullPath]()} + fs.filer.MasterClient = mc + return fs, store, mc +} + +func copyEntryThroughHandler(t *testing.T, ctx context.Context, fs *FilerServer, src, dst string) *filer.Entry { + t.Helper() + rec := httptest.NewRecorder() + fs.PostHandler(rec, httptest.NewRequest(http.MethodPost, dst+"?cp.from="+src, http.NoBody).WithContext(ctx), 0) + if rec.Code != http.StatusNoContent { + t.Fatalf("copy: status %d, body %s", rec.Code, rec.Body.String()) + } + entry, err := fs.filer.FindEntry(ctx, util.FullPath(dst)) + if err != nil { + t.Fatal(err) + } + return entry +} + +func TestCopyChunkManifestPreservesOverwrites(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + old := &filer_pb.FileChunk{FileId: "1,0100000001", Size: 7, ModifiedTsNs: 1} + filer_pb.BeforeEntrySerialization([]*filer_pb.FileChunk{old}) + manifest, err := proto.Marshal(&filer_pb.FileChunkManifest{Chunks: []*filer_pb.FileChunk{old}}) + if err != nil { + t.Fatal(err) + } + fs, store, mc := startChunkCopyFixture(t, ctx, map[string][]byte{"/1,0100000001": []byte("OLD old"), "/1,0200000001": []byte("NEW"), "/1,0300000001": manifest}) + src := newFileEntry("/src", 1) src.Chunks = []*filer_pb.FileChunk{ {FileId: "1,0300000001", Size: old.Size, ModifiedTsNs: 1, IsChunkManifest: true}, @@ -113,9 +137,6 @@ func TestCopyChunkManifestPreservesOverwrites(t *testing.T) { filer_pb.BeforeEntrySerialization(src.Chunks) filer_pb.AfterEntryDeserialization(src.Chunks) store.entries["/src"] = src - fs := &FilerServer{filer: newRenameTestFiler(t, store), option: &FilerOption{}, grpcDialOption: dialOption, - entryLockTable: util.NewLockTable[util.FullPath]()} - fs.filer.MasterClient = mc read := func(entry *filer.Entry) string { t.Helper() var buf bytes.Buffer @@ -127,16 +148,7 @@ func TestCopyChunkManifestPreservesOverwrites(t *testing.T) { if got := read(src); got != "NEW old" { t.Fatalf("source reads %q, want NEW old", got) } - rec := httptest.NewRecorder() - fs.PostHandler(rec, httptest.NewRequest(http.MethodPost, "/dst?cp.from=/src", http.NoBody).WithContext(ctx), 0) - if rec.Code != http.StatusNoContent { - t.Fatalf("copy: status %d, body %s", rec.Code, rec.Body.String()) - } - dst, err := fs.filer.FindEntry(ctx, "/dst") - if err != nil { - t.Fatal(err) - } - if got := read(dst); got != "NEW old" { + if got := read(copyEntryThroughHandler(t, ctx, fs, "/src", "/dst")); got != "NEW old" { t.Fatalf("copied file reads %q, want NEW old", got) } }