mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
139 lines
6.4 KiB
Go
139 lines
6.4 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweed-block/core/rpc/control"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func TestWorkspaceForceMoveJSONGoldenAnswers(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
dry, admitted bool
|
|
want string
|
|
exit int
|
|
}{
|
|
{"preview-refused", true, false, `{"admitted":false,"until":"2026-11-21T01:05:00Z","code":"FailedPrecondition"}` + "\n", 0},
|
|
{"preview-admitted", true, true, `{"admitted":true,"until":"","code":""}` + "\n", 0},
|
|
{"move-refused", false, false, `{"admitted":false,"until":"2026-11-21T01:05:00Z","code":"FailedPrecondition"}` + "\n", 1},
|
|
{"move-issued", false, true, `{"admitted":true,"until":"","code":"","move_id":"req-1","new_epoch":4,"new_host":"node-b","new_replica":"r1","kept_gen":9,"kept_lsn":42,"lost_range":{"proven":true,"after_lsn":42,"until":"2026-11-21T01:05:00Z"}}` + "\n", 0},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
preview := &control.WorkspaceMovePreview{Admitted: tc.admitted, MasterTimeNs: wsLostUntil.Add(-time.Minute).UnixNano(), LostUntilNs: wsLostUntil.UnixNano(), Reason: "this reason is not a machine-readable verdict"}
|
|
if !tc.admitted {
|
|
preview.Code = codes.FailedPrecondition.String()
|
|
}
|
|
f := &fakeWorkspace{status: wsHolder(), preview: preview, move: func(q *control.WorkspaceMove) (*control.WorkspaceGrant, error) {
|
|
if !tc.admitted {
|
|
return nil, status.Error(codes.FailedPrecondition, "forced move admitted (misleading prose)")
|
|
}
|
|
return wsGrant(q), nil
|
|
}}
|
|
addr, _ := startFakeWorkspace(t, f)
|
|
extra := []string{"--confirm-forced", "vol@3", "--json"}
|
|
if tc.dry {
|
|
extra = append(extra, "--dry-run")
|
|
}
|
|
code, out, errs := runCLI(forceMoveArgs(addr, extra...))
|
|
if code != tc.exit || out != tc.want {
|
|
t.Fatalf("exit=%d stdout=%q stderr=%q; want exit=%d stdout=%q", code, out, errs, tc.exit, tc.want)
|
|
}
|
|
if tc.dry && f.n("MoveWorkspace") != 0 {
|
|
t.Fatal("preview sent a move")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestWorkspaceForceMoveJSONDoesNotGuessAdmission(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
rpcCode codes.Code
|
|
dry, legacy bool
|
|
want string
|
|
exit int
|
|
}{
|
|
{"transport-unknown", codes.Unavailable, false, false, `{"admitted":false,"until":"","code":"Unavailable"}` + "\n", 3},
|
|
{"refusal-code", codes.NotFound, false, false, `{"admitted":false,"until":"","code":"NotFound"}` + "\n", 1},
|
|
{"legacy-preview", codes.OK, true, true, `{"admitted":false,"until":"2026-11-21T01:05:00.123456789Z","code":"Unknown"}` + "\n", 0},
|
|
{"preview-unavailable", codes.Unavailable, true, false, `{"admitted":false,"until":"","code":"Unavailable"}` + "\n", 3},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
f := &fakeWorkspace{status: wsHolder(), move: func(*control.WorkspaceMove) (*control.WorkspaceGrant, error) {
|
|
return nil, status.Error(tc.rpcCode, "admitted: true (untrusted prose)")
|
|
}}
|
|
if tc.legacy {
|
|
f.preview = &control.WorkspaceMovePreview{Reason: "admitted: true (legacy prose)", LostUntilNs: wsLostUntil.Add(123456789).UnixNano(), MasterTimeNs: wsLostUntil.Add(-time.Minute).UnixNano()}
|
|
}
|
|
addr, _ := startFakeWorkspace(t, f)
|
|
extra := []string{"--confirm-forced", "vol@3", "--json"}
|
|
if tc.dry {
|
|
extra = append(extra, "--dry-run")
|
|
}
|
|
code, out, errs := runCLI(forceMoveArgs(addr, extra...))
|
|
if code != tc.exit || out != tc.want {
|
|
t.Fatalf("exit=%d out=%q stderr=%q; want %d %q", code, out, errs, tc.exit, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestWorkspaceForceMoveJSONKeepsLocalConfirmationAndIssuedReadoutExits(t *testing.T) {
|
|
f := &fakeWorkspace{status: wsHolder(), move: func(q *control.WorkspaceMove) (*control.WorkspaceGrant, error) {
|
|
g := wsGrant(q)
|
|
g.LostRange = nil
|
|
return g, nil
|
|
}}
|
|
addr, dials := startFakeWorkspace(t, f)
|
|
code, out, _ := runCLI(forceMoveArgs(addr, "--json"))
|
|
if code != 2 || out != `{"admitted":false,"until":"","code":"InvalidArgument"}`+"\n" || *dials != 0 || f.total() != 0 {
|
|
t.Fatalf("local confirmation: %d %q dials=%d calls=%d", code, out, *dials, f.total())
|
|
}
|
|
code, out, _ = runCLI(forceMoveArgs(addr, "--json", "--confirm-forced", "vol@3"))
|
|
if code != 4 || out != `{"admitted":true,"until":"","code":"DataLoss","move_id":"req-1","new_epoch":4,"new_host":"node-b","new_replica":"r1","kept_gen":9,"kept_lsn":42,"lost_range":{"proven":false}}`+"\n" {
|
|
t.Fatalf("issued but unproven readout: %d %q", code, out)
|
|
}
|
|
}
|
|
|
|
func TestWorkspaceForceMoveJSONBindsLossEvidenceToTheValidatedGrant(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
gen, lsn uint64
|
|
badRange, badGrant bool
|
|
exit int
|
|
want string
|
|
}{
|
|
{"zero-frontier", 17, 0, false, false, 0, `{"admitted":true,"until":"","code":"","move_id":"req-1","new_epoch":4,"new_host":"node-b","new_replica":"r1","kept_gen":17,"kept_lsn":0,"lost_range":{"proven":true,"after_lsn":0,"until":"2026-11-21T01:05:00Z"}}` + "\n"},
|
|
{"large-frontier", 19, 9007199254740993, false, false, 0, `{"admitted":true,"until":"","code":"","move_id":"req-1","new_epoch":4,"new_host":"node-b","new_replica":"r1","kept_gen":19,"kept_lsn":9007199254740993,"lost_range":{"proven":true,"after_lsn":9007199254740993,"until":"2026-11-21T01:05:00Z"}}` + "\n"},
|
|
{"mismatched-range", 23, 71, true, false, 4, `{"admitted":true,"until":"","code":"DataLoss","move_id":"req-1","new_epoch":4,"new_host":"node-b","new_replica":"r1","kept_gen":23,"kept_lsn":71,"lost_range":{"proven":false}}` + "\n"},
|
|
{"mismatched-grant", 23, 71, false, true, 3, `{"admitted":false,"until":"","code":"Unknown"}` + "\n"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
f := &fakeWorkspace{status: wsHolder(), move: func(q *control.WorkspaceMove) (*control.WorkspaceGrant, error) {
|
|
g := wsGrant(q)
|
|
g.HeadGen, g.RemoteLsn = tc.gen, tc.lsn
|
|
g.LostRange.HeadGen, g.LostRange.LostAfterLsn = tc.gen, tc.lsn
|
|
if tc.badRange {
|
|
g.LostRange.RequestId = "another-move"
|
|
g.LostRange.LostAfterLsn = 999
|
|
}
|
|
if tc.badGrant {
|
|
g.VolumeId = "another-volume"
|
|
}
|
|
return g, nil
|
|
}}
|
|
addr, _ := startFakeWorkspace(t, f)
|
|
code, out, errs := runCLI(forceMoveArgs(addr, "--json", "--confirm-forced", "vol@3"))
|
|
if code != tc.exit || out != tc.want {
|
|
t.Fatalf("exit=%d out=%q stderr=%q; want %d %q", code, out, errs, tc.exit, tc.want)
|
|
}
|
|
if f.n("PreviewWorkspaceMove") != 0 {
|
|
t.Fatal("issued evidence came from a fresh preview")
|
|
}
|
|
})
|
|
}
|
|
}
|