Files
seaweedfs/seaweed-block/cmd/sw-block/format_status_json_test.go
T

73 lines
2.7 KiB
Go

package main
import (
"bytes"
"context"
"encoding/json"
"net"
"testing"
control "github.com/seaweedfs/seaweed-block/core/rpc/control"
"google.golang.org/grpc"
)
// binaryTooOldStatusServer answers a cluster status whose volume carries the
// master's binary_too_old condition for a refused holder.
type binaryTooOldStatusServer struct{ inspectionControlServer }
func (s binaryTooOldStatusServer) GetClusterStatus(ctx context.Context, q *control.GetClusterStatusRequest) (*control.ClusterStatusResponse, error) {
r, err := s.inspectionControlServer.GetClusterStatus(ctx, q)
r.Volumes = append(r.Volumes, &control.VolumeEvidence{VolumeId: "v-healthy", Status: "ok"})
r.Volumes[0].Conditions = append(r.Volumes[0].Conditions, &control.ObservationCondition{
Type: "HolderReadsFormat", Status: "false", Reason: "binary_too_old", Severity: "error",
Message: "node host-b's process for this volume reads format level 0; the cluster's format level is 1: no grant (binary_too_old)",
})
return r, err
}
// A holder refused by the format gate sees only no assignment, so the
// operator's cluster status --json carries the master's binary_too_old
// condition, with the server and its report level.
func TestClusterStatusJSONShowsABinaryTooOldHolder(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
srv := grpc.NewServer()
control.RegisterEvidenceServiceServer(srv, cmdFakeEvidenceServer{})
control.RegisterClusterEvidenceServiceServer(srv, binaryTooOldStatusServer{})
go func() { _ = srv.Serve(ln) }()
t.Cleanup(func() { srv.Stop(); _ = ln.Close() })
var out, errout bytes.Buffer
if code := run([]string{"shell", "-master", ln.Addr().String(), "-c", "cluster.status -o json"}, &out, &errout); code != 0 {
t.Fatalf("exit=%d stderr=%s", code, &errout)
}
var body struct {
Volumes []struct {
VolumeID string `json:"volume_id"`
Conditions []struct {
Reason string `json:"reason"`
Message string `json:"message"`
} `json:"conditions"`
} `json:"volumes"`
}
if err := json.Unmarshal(out.Bytes(), &body); err != nil {
t.Fatal(err)
}
shown := map[string]bool{} // volume id -> has the binary_too_old condition
for _, v := range body.Volumes {
shown[v.VolumeID] = shown[v.VolumeID] || false
for _, c := range v.Conditions {
if c.Reason == "binary_too_old" && bytes.Contains([]byte(c.Message), []byte("host-b's process for this volume reads format level 0")) {
shown[v.VolumeID] = true
}
}
}
if !shown["v1"] {
t.Fatalf("cluster status --json: volume v1 has no binary_too_old condition: %s", &out)
}
if healthy, listed := shown["v-healthy"]; !listed || healthy {
t.Fatalf("cluster status --json: the healthy volume listed %v, with binary_too_old %v: %s", listed, healthy, &out)
}
}