Files
knowledge-governance/cmd/worker/library_verify_test.go
T
姚章浩 5e1482e349
build / build (push) Successful in 1m40s
feat(library): 恢复验证成为可页面发起的真实作业
根因:生产可恢复性只有 PRODUCTION_BACKUP_RESTORE_VERIFIED 这个手填 env 开关,
页面没有任何入口去真跑一次恢复;CLI(librarydrill)又不是 worker 能复用的实现,
所以"已验证"既证明不了跑过、也证明不了跑的是当前目标。

改动:
- 共享实现 internal/libraryverify(维护隔离→捕获→基线校验→canary→恢复→复核→关窗口),
  cmd/librarydrill 改为薄封装,CLI 与 worker 只允许一份执行体。
- verify 成为独立业务作业:kind + VerifyStages(capture/verify/canary/restore/finalize),
  与 sync/restore/baseline 共用同一把写互斥;迁移 79 加 kind 约束、重建部分唯一索引、
  新建 library_restore_verifications(按 target_id 绑定,换库/索引/桶旧证据自动失效,
  同 target+scope+job 幂等)。
- worker:admitLibraryJob + runOwnedLibraryAttempt 之后调用共享实现,阶段实时写
  library_job_stages,结束后写真实终态;通过写 passed 证据,失败写 failed+原因(不伪成功);
  中断接管按同一作业 id 收掉维护窗口。
- API:POST/GET /api/v1/library/verifications(管理员;闸口关闭/维护中拒绝;返回完整
  整库目标、最近证据、失败原因),OpenAPI 同步。
- 闸口:同步/恢复/续作的证据判断改为读当前目标证据;历史 env 仅在证据缺失时兜底,
  证据存在时以证据为准。
- 页面:生产保护与恢复面板加「恢复验证」块(完整 KB/索引/桶、状态、最近证据、失败原因、
  管理员按钮、5s 轮询),作业类型补 verify 文案。

测试:make verify 16/16 全绿(go 1387 例 + web 454 例 + 契约/迁移/密钥/依赖锁定);
新增用例覆盖证据四条件、闸口证据优先、页面无证据/失败/通过三种呈现与管理员可见性。
2026-11-03 10:14:08 +08:00

54 lines
2.5 KiB
Go

package main
import (
"context"
"strings"
"testing"
"time"
"knowledge-governance/internal/appconfig"
"knowledge-governance/internal/governance"
)
// 同步闸口的"恢复可验证"判断必须以当前目标的证据为准:
// 没有证据 ⇒ 拒绝并说清怎么补;有 passed 证据 ⇒ 放行;只有失败证据 ⇒ 拒绝并带失败原因。
// 历史 env 覆盖只在证据缺失时兜底(页面不再写它)。
type verifyEvidenceStore struct {
evidence governance.LibraryRestoreVerification
found bool
}
func (s verifyEvidenceStore) LatestLibraryRestoreVerification(context.Context, string) (governance.LibraryRestoreVerification, bool, error) {
return s.evidence, s.found, nil
}
func TestLibraryRestoreEvidenceSatisfied(t *testing.T) {
now := time.Now().UTC()
passed := verifyEvidenceStore{found: true, evidence: governance.LibraryRestoreVerification{
Scope: "internal", Status: governance.LibraryVerificationPassed, SnapshotID: "snap-1", FinishedAt: now}}
if ok, reason := libraryRestoreEvidenceSatisfied(context.Background(), passed, appconfig.Config{}, "internal"); !ok {
t.Fatalf("passed evidence must satisfy gate: %q", reason)
}
failed := verifyEvidenceStore{found: true, evidence: governance.LibraryRestoreVerification{
Scope: "internal", Status: governance.LibraryVerificationFailed, SnapshotID: "snap-1",
Failure: "恢复后内容仍与预备份不一致", FinishedAt: now}}
if ok, reason := libraryRestoreEvidenceSatisfied(context.Background(), failed, appconfig.Config{}, "internal"); ok ||
!strings.Contains(reason, "恢复后内容仍与预备份不一致") {
t.Fatalf("failed evidence must block with its own failure: ok=%v reason=%q", ok, reason)
}
missing := verifyEvidenceStore{}
if ok, reason := libraryRestoreEvidenceSatisfied(context.Background(), missing, appconfig.Config{}, "internal"); ok ||
!strings.Contains(reason, "恢复验证") {
t.Fatalf("missing evidence must block and point at the remedy: ok=%v reason=%q", ok, reason)
}
// 历史部署的 env 覆盖:证据缺失时才生效,证据存在时不再起作用。
if ok, reason := libraryRestoreEvidenceSatisfied(context.Background(), missing,
appconfig.Config{ProductionBackupRestoreVerified: true}, "internal"); !ok {
t.Fatalf("legacy env override must still allow when no evidence: %q", reason)
}
if ok, _ := libraryRestoreEvidenceSatisfied(context.Background(), failed,
appconfig.Config{ProductionBackupRestoreVerified: true}, "internal"); ok {
t.Fatal("evidence must win over the legacy env override")
}
}