diff --git a/cmd/erasure-healing-common_test.go b/cmd/erasure-healing-common_test.go index 317ca1cee..b37efe74a 100644 --- a/cmd/erasure-healing-common_test.go +++ b/cmd/erasure-healing-common_test.go @@ -23,6 +23,7 @@ import ( "fmt" "os" "path/filepath" + "reflect" "runtime" "testing" "time" @@ -797,27 +798,29 @@ func TestCheckObjectWithAllPartsMissingResult(t *testing.T) { t.Run(fmt.Sprintf("scanMode=%d", scanMode), func(t *testing.T) { meta := nativeAuditMetadata() full := &CheckPartsResp{Results: []int{checkPartSuccess, checkPartSuccess}} + mixed := &CheckPartsResp{Results: []int{checkPartSuccess, checkPartFileNotFound}} disks := []StorageAPI{ &checkPartsResultDisk{resp: nil}, // (nil, nil) &checkPartsResultDisk{resp: &CheckPartsResp{Results: []int{checkPartSuccess}}}, // short result - &checkPartsResultDisk{resp: full}, + &checkPartsResultDisk{resp: mixed}, &checkPartsResultDisk{resp: full}, } errs := make([]error, len(disks)) - dataErrsByDisk, _ := checkObjectWithAllParts(t.Context(), disks, meta, errs, meta[0], + byDisk, byPart := checkObjectWithAllParts(t.Context(), disks, meta, errs, meta[0], false, "bucket", "object", scanMode) - for i := range disks { - for p, got := range dataErrsByDisk[i] { - want := checkPartSuccess - if i < 2 { - want = checkPartDiskNotFound - } - if got != want { - t.Errorf("disk %d part %d: got %d, want %d", i, p, got, want) - } - } + unavailable := []int{checkPartDiskNotFound, checkPartDiskNotFound} + wantDisks := map[int][]int{0: unavailable, 1: unavailable, 2: mixed.Results, 3: full.Results} + if !reflect.DeepEqual(byDisk, wantDisks) { + t.Errorf("results by disk: got %v, want %v", byDisk, wantDisks) + } + wantParts := map[int][]int{ + 0: {checkPartDiskNotFound, checkPartDiskNotFound, checkPartSuccess, checkPartSuccess}, + 1: {checkPartDiskNotFound, checkPartDiskNotFound, checkPartFileNotFound, checkPartSuccess}, + } + if !reflect.DeepEqual(byPart, wantParts) { + t.Errorf("results by part: got %v, want %v", byPart, wantParts) } }) } diff --git a/cmd/storage-checkparts-stream_test.go b/cmd/storage-checkparts-stream_test.go new file mode 100644 index 000000000..51c7ad560 --- /dev/null +++ b/cmd/storage-checkparts-stream_test.go @@ -0,0 +1,102 @@ +// Copyright (c) 2026 Feng Ruohang +// +// This file is part of Silo Object Storage. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "context" + "errors" + "slices" + "testing" + "time" + + "github.com/minio/minio/internal/grid" +) + +func TestStorageRESTClientCheckPartsCompletion(t *testing.T) { + for _, tc := range []struct { + name string + reply []int + disconnect bool + remoteErr error + wantErr error + }{ + {name: "one-response", reply: []int{checkPartSuccess, checkPartFileNotFound}}, + {name: "zero-parts", reply: []int{}}, + {name: "empty-completion", wantErr: errDiskNotFound}, + {name: "disconnect", disconnect: true, wantErr: errDiskNotFound}, + {name: "storage-error", remoteErr: errFileCorrupt, wantErr: errFileCorrupt}, + } { + t.Run(tc.name, func(t *testing.T) { + g, err := grid.SetupTestGrid(2) + if err != nil { + t.Fatal(err) + } + t.Cleanup(g.Cleanup) + const route = "/disk" + handled := make(chan error, 1) + handle := func(_ context.Context, _ *CheckPartsHandlerParams, _ <-chan grid.NoPayload, out chan<- *CheckPartsResp) *grid.RemoteErr { + var closeErr error + if tc.reply != nil { + out <- &CheckPartsResp{Results: slices.Clone(tc.reply)} + } + if tc.disconnect { + closeErr = g.ClosePeerConnection(1, 0) + } + handled <- closeErr + return grid.NewRemoteErr(tc.remoteErr) + } + for _, mgr := range g.Managers { + if err := storageCheckPartsRPC.Register(mgr, handle, route); err != nil { + t.Fatal(err) + } + } + client := &storageRESTClient{gridConn: g.Managers[0].Connection(g.Hosts[1]).Subroute(route)} + client.SetDiskID("synthetic") + ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second) + defer cancel() + fi := FileInfo{Parts: []ObjectPartInfo{{Number: 1}, {Number: 2}}} + if tc.reply != nil && len(tc.reply) == 0 { + fi.Parts = nil + } + resp, err := client.CheckParts(ctx, "bucket", "object", fi) + select { + case closeErr := <-handled: + if closeErr != nil { + t.Fatalf("closing peer connection: %v", closeErr) + } + case <-ctx.Done(): + t.Fatalf("peer did not complete the handler: %v", ctx.Err()) + } + if ctx.Err() != nil { + t.Fatalf("caller context ended: %v", ctx.Err()) + } + if !errors.Is(err, tc.wantErr) { + t.Fatalf("CheckParts returned %v, want %v", err, tc.wantErr) + } + if tc.wantErr != nil { + if resp != nil { + t.Fatalf("unexpected response on error: %+v", resp) + } + return + } + if resp == nil || !slices.Equal(resp.Results, tc.reply) { + t.Fatalf("got %+v, want part results %v", resp, tc.reply) + } + }) + } +} diff --git a/internal/grid/debug.go b/internal/grid/debug.go index f80559f3f..2f41bde0e 100644 --- a/internal/grid/debug.go +++ b/internal/grid/debug.go @@ -128,6 +128,22 @@ func (t *TestGrid) Cleanup() { }) } +// ClosePeerConnection closes the live transport at one end of a test grid link. +// Both managers stay running, so callers observe a peer disconnect and may reconnect. +func (t *TestGrid) ClosePeerConnection(from, to int) error { + c := t.Managers[from].Connection(t.Hosts[to]) + c.connMu.Lock() + defer c.connMu.Unlock() + peer := c.debugInConn + if c.shouldConnect() { + peer = c.debugOutConn + } + if peer == nil { + return ErrDisconnected + } + return peer.Close() +} + // WaitAllConnect will wait for all connections to be established. func (t *TestGrid) WaitAllConnect(ctx context.Context) { for _, manager := range t.Managers {