test: refuse unbounded clock skew in shell diagnostics

This commit is contained in:
pingqiu committed 2026-11-29 00:40:31 -08:00
1 parent 2aec1d503b
commit b4102766bc
2 files changed
+44 -3

No files matched your search

+7 -3
View File
@@ -133,6 +133,7 @@ func (s inspectionShell) check(volume, format string, stdout, stderr io.Writer)
f = checkElsewhere("master did not answer; prerequisite unavailable")
}
r := inspectionResult{ID: c.ID, Title: c.Title, Reach: c.Reach, Status: f.Status, Finding: f.Finding}
r.Finding = strings.Join(strings.Fields(r.Finding), " ")
if f.Status == "FAIL" {
code = 1
r.Cure = c.Cure
@@ -405,9 +406,12 @@ func (o *inspectionObservation) clock() checkFinding {
if diff < 0 {
diff = -diff
}
bound := diff + rtt/2
finding := fmt.Sprintf("local clock difference plus RTT uncertainty %s; configured max skew %s", bound, o.shell.maxClockSkew)
if bound >= o.shell.maxClockSkew/2 {
if diff < 0 {
return checkFail("clock difference exceeds the duration range")
}
finding := fmt.Sprintf("local clock difference %s plus RTT uncertainty %s; configured max skew %s", diff, rtt/2, o.shell.maxClockSkew)
limit := o.shell.maxClockSkew / 2
if diff >= limit || rtt/2 >= limit-diff {
return checkFail(finding)
}
return checkOK(finding)
+37
View File
@@ -2,7 +2,9 @@ package main
import (
"bytes"
"context"
"encoding/json"
"net"
"net/http"
"net/http/httptest"
"os"
@@ -10,6 +12,11 @@ import (
"strings"
"sync/atomic"
"testing"
"time"
"github.com/seaweedfs/seaweed-block/core/rpc/control"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/timestamppb"
)
func TestShellClusterStatusReadsTheMasterWithoutKubernetes(t *testing.T) {
@@ -29,6 +36,36 @@ func TestShellClusterStatusReadsTheMasterWithoutKubernetes(t *testing.T) {
}
}
type futureInspectionMaster struct{ cmdFakeClusterEvidenceServer }
func (futureInspectionMaster) GetClusterStatus(ctx context.Context, q *control.GetClusterStatusRequest) (*control.ClusterStatusResponse, error) {
r, err := (cmdFakeClusterEvidenceServer{}).GetClusterStatus(ctx, q)
r.CapturedAt = timestamppb.New(time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC))
return r, err
}
func TestShellClockCannotCallAnOverflowingDifferenceOK(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
srv := grpc.NewServer()
control.RegisterClusterEvidenceServiceServer(srv, futureInspectionMaster{})
go func() { _ = srv.Serve(ln) }()
defer srv.Stop()
defer ln.Close()
var out, errout bytes.Buffer
run([]string{"shell", "-master", ln.Addr().String(), "-server-id", "m02", "-c", "cluster.check -o json"}, &out, &errout)
var rows []inspectionResult
if err := json.Unmarshal(out.Bytes(), &rows); err != nil {
t.Fatal(err)
}
for _, r := range rows {
if r.ID == "clock.skew" && r.Status != "FAIL" {
t.Fatalf("impossible clock accepted: %+v", r)
}
}
}
func TestShellCheckNeverCallsOffHostChecksOK(t *testing.T) {
addr, closeMaster := startCmdFakeMaster(t)
defer closeMaster()