mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
265 lines
12 KiB
Go
265 lines
12 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/seaweedfs/seaweed-block/core/rpc/answer"
|
|
"reflect"
|
|
"sort"
|
|
"testing"
|
|
|
|
control "github.com/seaweedfs/seaweed-block/core/rpc/control"
|
|
"github.com/seaweedfs/seaweed-block/core/rpc/lifecycleclient"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
// finalize-format's answer is typed: exit 0 for finalized or already, 2 for
|
|
// a refusal, 75 for a transient failure (the master unreachable), 1 for any
|
|
// other failure (a failed durable write included); --json prints the same
|
|
// keys for every outcome, and a refusal lists each refused host from the
|
|
// master's typed metadata, never from its message text. The hosts the master
|
|
// could not confirm are listed, not blocking, on a refusal (metadata) and on
|
|
// a finalize (the response) alike.
|
|
func TestClusterFinalizeFormatTypedAnswer(t *testing.T) {
|
|
refused := lifecycleclient.WithReasonMetadata(status.New(codes.FailedPrecondition, "message text a script must not parse"),
|
|
lifecycleclient.ReasonFormatHostsTooOld, map[string]string{
|
|
lifecycleclient.HostMetadataPrefix + "node-c": "volume v2 reads level 0",
|
|
lifecycleclient.HostMetadataPrefix + "node-b": "volume v1 reads level 0",
|
|
lifecycleclient.UnconfirmedMetadataPrefix + "node-d": "no report",
|
|
}).Err()
|
|
unconfirmed := []*control.FormatHostNote{{ServerId: "node-d", Detail: "no report"}, {ServerId: "node-e", Detail: "volume v3: no report"}}
|
|
for _, tc := range []struct {
|
|
name string
|
|
resp *control.FinalizeFormatResponse
|
|
err error
|
|
dialErr error
|
|
exit int
|
|
result string
|
|
reason string
|
|
hosts []finalizeHost
|
|
listed []finalizeHost
|
|
levelOut uint32
|
|
}{
|
|
{name: "finalized", resp: &control.FinalizeFormatResponse{FormatLevel: 1}, exit: 0, result: "finalized", levelOut: 1},
|
|
{name: "finalized-listing", resp: &control.FinalizeFormatResponse{FormatLevel: 1, Unconfirmed: unconfirmed}, exit: 0, result: "finalized", levelOut: 1,
|
|
listed: []finalizeHost{{"node-d", "no report"}, {"node-e", "volume v3: no report"}}},
|
|
{name: "already", resp: &control.FinalizeFormatResponse{FormatLevel: 1, Already: true}, exit: 0, result: "already", levelOut: 1},
|
|
{name: "refused", err: refused, exit: 2, result: "refused", reason: lifecycleclient.ReasonFormatHostsTooOld,
|
|
hosts: []finalizeHost{{"node-b", "volume v1 reads level 0"}, {"node-c", "volume v2 reads level 0"}},
|
|
listed: []finalizeHost{{"node-d", "no report"}}},
|
|
{name: "unreachable", err: status.Error(codes.Unavailable, "connection refused"), exit: 75, result: "unavailable", reason: "Unavailable"},
|
|
{name: "deadline", err: status.Error(codes.DeadlineExceeded, "context deadline exceeded"), exit: 75, result: "unavailable", reason: "DeadlineExceeded"},
|
|
{name: "dial", dialErr: answer.LocalConfiguration(errors.New("bad address")), exit: 78, result: "failed", reason: "bad_request"},
|
|
{name: "write-failed", err: status.Error(codes.Internal, "format finalize: attestation write failed"), exit: 3, result: "unknown", reason: "Internal"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
f := &fakeClusterMaster{finalize: func(*control.FinalizeFormatRequest) (*control.FinalizeFormatResponse, error) {
|
|
return tc.resp, answer.Record(answer.Unsent, tc.err)
|
|
}}
|
|
withClusterMaster(t, f)
|
|
if tc.dialErr != nil {
|
|
clusterLifecycleDial = func(string) (control.LifecycleServiceClient, func() error, error) { return nil, nil, tc.dialErr }
|
|
}
|
|
var out, errOut bytes.Buffer
|
|
code := run([]string{"cluster", "finalize-format", "--master", "m:1", "--json"}, &out, &errOut)
|
|
if code != tc.exit {
|
|
t.Fatalf("exit %d, want %d (stderr %q)", code, tc.exit, errOut.String())
|
|
}
|
|
var keys map[string]json.RawMessage
|
|
if err := json.Unmarshal(out.Bytes(), &keys); err != nil {
|
|
t.Fatalf("--json output %q: %v", out.String(), err)
|
|
}
|
|
var names []string
|
|
for k := range keys {
|
|
names = append(names, k)
|
|
}
|
|
sort.Strings(names)
|
|
if want := []string{"format_level", "hosts", "listed", "message", "reason", "result"}; !reflect.DeepEqual(names, want) {
|
|
t.Fatalf("--json keys %v, want %v for every outcome", names, want)
|
|
}
|
|
var a finalizeAnswer
|
|
if err := json.Unmarshal(out.Bytes(), &a); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a.Result != tc.result || a.Reason != tc.reason || a.FormatLevel != tc.levelOut {
|
|
t.Fatalf("answer %+v, want result %q reason %q level %d", a, tc.result, tc.reason, tc.levelOut)
|
|
}
|
|
if want := tc.hosts; want == nil && len(a.Hosts) != 0 || want != nil && !reflect.DeepEqual(a.Hosts, want) {
|
|
t.Fatalf("hosts %+v, want %+v", a.Hosts, want)
|
|
}
|
|
if want := tc.listed; a.Listed == nil || want == nil && len(a.Listed) != 0 || want != nil && !reflect.DeepEqual(a.Listed, want) {
|
|
t.Fatalf("listed %+v, want %+v (an empty list, never null)", a.Listed, want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A usage error is "any other failure": exit 1, with the same --json shape.
|
|
func TestClusterFinalizeFormatUsageIsAFailure(t *testing.T) {
|
|
var out, errOut bytes.Buffer
|
|
if code := run([]string{"cluster", "finalize-format", "--json"}, &out, &errOut); code != 1 {
|
|
t.Fatalf("finalize without --master: exit %d, want 1", code)
|
|
}
|
|
var a finalizeAnswer
|
|
if err := json.Unmarshal(out.Bytes(), &a); err != nil || a.Result != "failed" || a.Reason != "usage" || a.Hosts == nil {
|
|
t.Fatalf("usage answer %q: %+v %v", out.String(), a, err)
|
|
}
|
|
}
|
|
|
|
// A malformed or unknown option answers in JSON whichever side of --json it
|
|
// is on, and sends nothing to the master.
|
|
func TestClusterFinalizeFormatBadOptionAnswersJSONWithoutAnRPC(t *testing.T) {
|
|
calls := 0
|
|
f := &fakeClusterMaster{finalize: func(*control.FinalizeFormatRequest) (*control.FinalizeFormatResponse, error) {
|
|
calls++
|
|
return &control.FinalizeFormatResponse{FormatLevel: 1}, nil
|
|
}}
|
|
withClusterMaster(t, f)
|
|
for _, args := range [][]string{
|
|
{"cluster", "finalize-format", "--bogus", "--json", "--master", "m:1"},
|
|
{"cluster", "finalize-format", "--json", "--master", "m:1", "--bogus"},
|
|
{"cluster", "finalize-format", "--timeout", "soon", "-json=true", "--master", "m:1"},
|
|
} {
|
|
var out, errOut bytes.Buffer
|
|
code := run(args, &out, &errOut)
|
|
var a finalizeAnswer
|
|
if code != 1 || json.Unmarshal(out.Bytes(), &a) != nil || a.Result != "failed" || a.Reason != "usage" || a.Hosts == nil {
|
|
t.Fatalf("%v: exit %d, stdout %q: %+v", args, code, out.String(), a)
|
|
}
|
|
}
|
|
if calls != 0 {
|
|
t.Fatalf("a malformed command sent %d finalize RPCs", calls)
|
|
}
|
|
// The mode is the arguments' last --json, whatever a partial parse saw.
|
|
for _, tc := range []struct {
|
|
args []string
|
|
json bool
|
|
}{
|
|
{[]string{"cluster", "finalize-format", "--bogus", "--json", "--json=false"}, false},
|
|
{[]string{"cluster", "finalize-format", "--json", "--bogus", "--json=false"}, false},
|
|
{[]string{"cluster", "finalize-format", "--json=false", "--bogus", "--json"}, true},
|
|
{[]string{"cluster", "finalize-format", "--json=false", "--json", "--bogus"}, true},
|
|
} {
|
|
var out, errOut bytes.Buffer
|
|
code := run(tc.args, &out, &errOut)
|
|
var a finalizeAnswer
|
|
gotJSON := json.Unmarshal(out.Bytes(), &a) == nil && a.Result == "failed"
|
|
if code != 1 || gotJSON != tc.json || (!tc.json && out.Len() != 0) {
|
|
t.Fatalf("%v: exit %d, stdout %q; want JSON %v", tc.args, code, out.String(), tc.json)
|
|
}
|
|
}
|
|
if calls != 0 {
|
|
t.Fatalf("a malformed command sent %d finalize RPCs", calls)
|
|
}
|
|
}
|
|
|
|
// retire-host's answer is typed like finalize-format's: exit 0 retired, 2
|
|
// refused (the held volumes from the master's typed metadata, never its
|
|
// message text), 75 the master could not be reached, 1 any other failure or
|
|
// a usage error; --json prints the same keys for every outcome.
|
|
func TestClusterRetireHostTypedAnswer(t *testing.T) {
|
|
holds := lifecycleclient.WithReasonMetadata(status.New(codes.FailedPrecondition, "message text a script must not parse"),
|
|
lifecycleclient.ReasonHostHoldsWork, map[string]string{
|
|
lifecycleclient.VolumeMetadataPrefix + "v2": "busy",
|
|
lifecycleclient.VolumeMetadataPrefix + "v1": "busy",
|
|
}).Err()
|
|
for _, tc := range []struct {
|
|
name string
|
|
err error
|
|
dialErr error
|
|
args []string
|
|
exit int
|
|
result string
|
|
reason string
|
|
volumes []string
|
|
}{
|
|
{name: "retired", exit: 0, result: "retired"},
|
|
{name: "holds-work", err: holds, exit: 2, result: "refused", reason: lifecycleclient.ReasonHostHoldsWork, volumes: []string{"v1", "v2"}},
|
|
{name: "not-registered", err: status.Error(codes.FailedPrecondition, "retire refused: host is not registered"), exit: 2, result: "refused", reason: "FailedPrecondition"},
|
|
{name: "unreachable", err: status.Error(codes.Unavailable, "connection refused"), exit: 75, result: "unavailable", reason: "Unavailable"},
|
|
{name: "deadline", err: status.Error(codes.DeadlineExceeded, "context deadline exceeded"), exit: 75, result: "unavailable", reason: "DeadlineExceeded"},
|
|
{name: "dial", dialErr: answer.LocalConfiguration(errors.New("bad address")), exit: 78, result: "failed", reason: "bad_request"},
|
|
{name: "internal", err: status.Error(codes.Internal, "node inventory write failed"), exit: 3, result: "unknown", reason: "Internal"},
|
|
{name: "usage", args: []string{"cluster", "retire-host", "--json", "--bogus", "--master", "m:1"}, exit: 1, result: "failed", reason: "usage"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
calls := 0
|
|
f := &fakeClusterMaster{retire: func(r *control.RetireHostRequest) (*control.RetireHostResponse, error) {
|
|
calls++
|
|
if tc.err != nil {
|
|
return nil, answer.Record(answer.Unsent, tc.err)
|
|
}
|
|
return &control.RetireHostResponse{ServerId: r.GetServerId()}, nil
|
|
}}
|
|
withClusterMaster(t, f)
|
|
if tc.dialErr != nil {
|
|
clusterLifecycleDial = func(string) (control.LifecycleServiceClient, func() error, error) { return nil, nil, tc.dialErr }
|
|
}
|
|
args := tc.args
|
|
if args == nil {
|
|
args = []string{"cluster", "retire-host", "--master", "m:1", "--server", "busy", "--confirm", "busy", "--json"}
|
|
}
|
|
var out, errOut bytes.Buffer
|
|
code := run(args, &out, &errOut)
|
|
if code != tc.exit {
|
|
t.Fatalf("exit %d, want %d (stderr %q)", code, tc.exit, errOut.String())
|
|
}
|
|
var keys map[string]json.RawMessage
|
|
if err := json.Unmarshal(out.Bytes(), &keys); err != nil {
|
|
t.Fatalf("--json output %q: %v", out.String(), err)
|
|
}
|
|
var names []string
|
|
for k := range keys {
|
|
names = append(names, k)
|
|
}
|
|
sort.Strings(names)
|
|
if want := []string{"message", "reason", "result", "server", "volumes"}; !reflect.DeepEqual(names, want) {
|
|
t.Fatalf("--json keys %v, want %v for every outcome", names, want)
|
|
}
|
|
var a retireAnswer
|
|
if err := json.Unmarshal(out.Bytes(), &a); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a.Result != tc.result || a.Reason != tc.reason {
|
|
t.Fatalf("answer %+v, want result %q reason %q", a, tc.result, tc.reason)
|
|
}
|
|
if want := tc.volumes; want == nil && len(a.Volumes) != 0 || want != nil && !reflect.DeepEqual(a.Volumes, want) {
|
|
t.Fatalf("volumes %v, want %v", a.Volumes, want)
|
|
}
|
|
if tc.name == "usage" && calls != 0 {
|
|
t.Fatalf("a usage error sent %d retire RPCs", calls)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// failingWriter refuses every write: a stdout the JSON answer cannot reach.
|
|
type failingWriter struct{}
|
|
|
|
func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("stdout closed") }
|
|
|
|
// A --json answer that cannot be written fails the command: a script that
|
|
// reads it must not see a successful exit without an answer.
|
|
func TestClusterJSONOutputFailureFailsTheCommand(t *testing.T) {
|
|
f := &fakeClusterMaster{
|
|
finalize: func(*control.FinalizeFormatRequest) (*control.FinalizeFormatResponse, error) {
|
|
return &control.FinalizeFormatResponse{FormatLevel: 1}, nil
|
|
},
|
|
retire: func(r *control.RetireHostRequest) (*control.RetireHostResponse, error) {
|
|
return &control.RetireHostResponse{ServerId: r.GetServerId()}, nil
|
|
},
|
|
}
|
|
withClusterMaster(t, f)
|
|
for _, args := range [][]string{
|
|
{"cluster", "finalize-format", "--master", "m:1", "--json"},
|
|
{"cluster", "retire-host", "--master", "m:1", "--server", "node-c", "--confirm", "node-c", "--json"},
|
|
} {
|
|
var errOut bytes.Buffer
|
|
if code := run(args, failingWriter{}, &errOut); code != 1 {
|
|
t.Fatalf("%v with an unwritable stdout: exit %d, want 1 (stderr %q)", args, code, errOut.String())
|
|
}
|
|
}
|
|
}
|