6ed06357f9
build / build (push) Successful in 1m27s
原来 worker 先 health 再 capture,两次拨号之间 socket 可能被换成另一个实例的 执行器:错的 agent 已经把写入者停了、快照也出了,事后 postcheck 只能拒绝登记, 副作用无法撤销。 现在 capture 请求自带期望身份(治理侧配置生成:project 必填,source_app / archive_root 配了就绑定)与覆盖三项的稳定指纹;接收方在操作互斥内、执行任何源 操作之前比对,缺字段、指纹与字段不一致、project/source_app 不符都直接拒绝, **错的执行器一次 capture 都不跑**。封存结果回显该指纹,worker 事后复核按它对照, postcheck 只作为证据防线。恢复路径不要求期望(它只把写入者恢复回来)。 负例是确定性的:两个真实 agent 各占一个 socket,把调用方指向错的那个,断言它的 capture 调用为 0 且仍能回答 health;同一份期望打到对的 socket 则只跑一次。
110 lines
3.5 KiB
Go
110 lines
3.5 KiB
Go
package libraryagent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func serveAgent(t *testing.T, agent *Agent, socket string) {
|
|
t.Helper()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
t.Cleanup(cancel)
|
|
go func() { _ = agent.Serve(ctx) }()
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for {
|
|
if _, err := os.Stat(socket); err == nil {
|
|
return
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("agent socket never appeared")
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
// 健康应答必须不排队等正在跑的封存:一次封存最长 15 分钟,如果 health 也去抢
|
|
// 操作锁,页面就会把"正在封存"看成"执行器不可达",进而误导运维。
|
|
func TestHealthAnswersWhileACaptureHoldsTheOperationLock(t *testing.T) {
|
|
agent, socket, calls := testAgent(t)
|
|
serveAgent(t, agent, socket)
|
|
// 占住操作锁并置 busy,模拟一次正在进行的封存。
|
|
agent.mu.Lock()
|
|
defer agent.mu.Unlock()
|
|
agent.setBusy(true)
|
|
defer agent.setBusy(false)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
result, err := Health(ctx, ClientConfig{Socket: socket, Timeout: 2 * time.Second})
|
|
if err != nil {
|
|
t.Fatalf("health while busy: %v", err)
|
|
}
|
|
var payload struct {
|
|
Project string `json:"project"`
|
|
Busy bool `json:"busy"`
|
|
}
|
|
if err = json.Unmarshal(result.Payload, &payload); err != nil {
|
|
t.Fatalf("health payload: %v", err)
|
|
}
|
|
if payload.Project != "weknora071full" || !payload.Busy {
|
|
t.Fatalf("health payload = %+v", payload)
|
|
}
|
|
if len(captureCalls(calls())) != 0 {
|
|
t.Fatalf("health must not run the capture binary, calls = %v", calls())
|
|
}
|
|
}
|
|
|
|
// 降级状态必须能被调用方看见:否则页面只能看到"可发起",而真发起会被拒。
|
|
func TestHealthReportsDegradedState(t *testing.T) {
|
|
agent, socket, _ := testAgent(t)
|
|
serveAgent(t, agent, socket)
|
|
// 启动恢复成功会清空降级;这里在它之后模拟"恢复失败"留下的降级状态。
|
|
agent.setDegraded("写入者恢复失败")
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
result, err := Health(ctx, ClientConfig{Socket: socket, Timeout: 2 * time.Second})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var payload struct {
|
|
Degraded string `json:"degraded"`
|
|
SourceApp string `json:"source_app"`
|
|
Writers string `json:"writers"`
|
|
}
|
|
if err = json.Unmarshal(result.Payload, &payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if payload.Degraded != "写入者恢复失败" || payload.SourceApp != "WKNFull-app" || payload.Writers != "app" {
|
|
t.Fatalf("health payload = %+v", payload)
|
|
}
|
|
}
|
|
|
|
// 封存结果必须自带"这次是谁执行的":调用方据此在事后复核,防止心跳与执行之间
|
|
// socket 被换成另一个实例。
|
|
func TestCaptureResultCarriesTheAgentIdentity(t *testing.T) {
|
|
agent, socket, _ := testAgent(t)
|
|
serveAgent(t, agent, socket)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
result, err := Capture(ctx, ClientConfig{Socket: socket, Timeout: 5 * time.Second}, "internal",
|
|
"lib-internal-identity", NewExpectation("weknora071full", "WKNFull-app", ""))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var payload struct {
|
|
SnapshotID string `json:"snapshot_id"`
|
|
Identity struct {
|
|
Project string `json:"project"`
|
|
} `json:"agent_identity"`
|
|
}
|
|
if err = json.Unmarshal(result.Payload, &payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if payload.SnapshotID != "abc" || payload.Identity.Project != "weknora071full" {
|
|
t.Fatalf("payload = %s", result.Payload)
|
|
}
|
|
}
|