test(library): 隔离数据契约与 r11 身份自然键矩阵

真实本机 PG/ES/MinIO:v071 目录镜像 10 模式、结构引用/游标 15 模式、
8 表×2 外部范围身份矩阵、13 模式自然键(含软删除与合法同 parent 正例)、
备份期间三注入、收尾故障与授权撤销两个真实治理 PG 用例。

需隔离服务的用例在 test-scope.json 显式声明:普通 CI 跳过不算通过。
This commit is contained in:
姚章浩
2026-11-01 11:35:09 +08:00
parent 5eef13b615
commit 8febe12d7d
62 changed files with 7329 additions and 167 deletions
+16
View File
@@ -0,0 +1,16 @@
//go:build !kg_isolated_drill
package main
import (
"testing"
"time"
)
func TestProductionAttemptTimingIgnoresDrillEnvironment(t *testing.T) {
t.Setenv("KG_LIBRARY_TEST_ISOLATED", "1")
t.Setenv("WORKER_LEASE_DURATION", "1ms")
if libraryAttemptLease != 10*time.Minute {
t.Fatal("production lease shortened by drill injection")
}
}
@@ -0,0 +1,51 @@
package main
import (
"context"
"errors"
"testing"
"time"
"knowledge-governance/internal/libraryrelease"
)
type contractAdoptingTarget struct {
plainTarget
adopted int
planned int
reject error
}
func (t *contractAdoptingTarget) AdoptWriteSet(set libraryrelease.WriteSet) error {
t.adopted++
return t.reject
}
func (t *contractAdoptingTarget) PlanWriteSet(context.Context, libraryrelease.WriteSetPlan) (libraryrelease.WriteSet, error) {
t.planned++
return libraryrelease.WriteSet{}, errors.New("must not replan")
}
func TestContractWorkerWrapperReplaysPersistedPlan(t *testing.T) {
for _, reject := range []bool{false, true} {
inner := &contractAdoptingTarget{}
if reject {
inner.reject = errors.New("binding mismatch")
}
wrapped := &contentTrackedTarget{inner: inner, jobID: "job-1", onApply: func() {}}
set := sealedLedger(t, "")
now := time.Now().UTC()
request := libraryrelease.Request{JobID: "job-1", Target: "internal", SnapshotID: "target-snap", ParamsDigest: "digest", Now: now,
Grant: libraryrelease.Grant{Target: "internal", SnapshotID: "target-snap", ParamsDigest: "digest", ExpiresAt: now.Add(time.Hour), AuthorizedBy: "test"}}
backup := ledgerBackupStore{ref: libraryrelease.BackupRef{ID: "backup-1", SnapshotID: "backup-snap", Bytes: 4096}, write: &set}
result := libraryrelease.Run(context.Background(), request, wrapped, backup, nil)
if inner.adopted != 1 || inner.planned != 0 {
t.Fatalf("real wrapper lost adoption: adopted=%d planned=%d result=%+v", inner.adopted, inner.planned, result)
}
if reject && (inner.applied != 0 || result.State == "succeeded") {
t.Fatal("adoption failure must be zero-write")
}
if !reject && result.State != "succeeded" {
t.Fatalf("replay: %+v", result)
}
}
}
@@ -0,0 +1,197 @@
package main
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"time"
"knowledge-governance/internal/governance"
postgresstore "knowledge-governance/internal/platform/postgres"
)
// B28:内容已通过校验之后的两类收尾故障必须只补收尾、不重写目标,且绝不伪报成功。
//
// 这里用真实治理 PG(维护行、作业行、版本事件行都是真表),故障只注入在
// “释放 scope 维护占用”这一个步骤上;其余调用照旧打到真库。
type r11FinishFaultStore struct {
*postgresstore.Store
applies *int
failOnRelease bool
releaseFailure error
}
func (s *r11FinishFaultStore) ReleaseLibraryMaintenance(ctx context.Context, scope, ownerJobID string, now time.Time) error {
if s.failOnRelease {
return s.releaseFailure
}
return s.Store.ReleaseLibraryMaintenance(ctx, scope, ownerJobID, now)
}
type r11HealthyTarget struct{ err error }
func (h r11HealthyTarget) VerifyRunning(context.Context) error { return h.err }
func TestR11FinalizeFailureAfterTerminalHoldsThenRepairsWithoutRewrite(t *testing.T) {
store, _ := openLedgerStore(t)
ctx := context.Background()
now := time.Now().UTC()
const (
scope = "internal"
instance = "kgseal-r11-finalize"
jobID = "libsync-r11-finalize"
target = "target-snap-r11-finalize"
backup = "backup-snap-r11-finalize"
operation = "libop-r11-finalize"
)
if _, _, err := store.CreateLibraryJob(ctx, governance.LibraryJob{ID: jobID, Scope: scope,
Kind: governance.LibraryJobSync, IdempotencyKey: "library.sync:internal:r11-finalize",
RequestDigest: "digest-r11-finalize", Actor: "admin", TargetSnapshotID: target},
governance.SyncStages(), now); err != nil {
t.Fatalf("create library job: %v", err)
}
// 生效事件必须引用真实 operation:登记前先落这条执行意图(真表,不是夹具)。
if _, _, err := store.EnsureLibraryOperation(ctx, governance.LibraryOperation{ID: operation, Scope: scope,
Kind: governance.LibraryJobSync, TargetSnapshotID: target, RequestDigest: "digest-r11-finalize",
OperationKey: "r11-finalize", Actor: "admin", CreatedAt: now}); err != nil {
t.Fatalf("ensure library operation: %v", err)
}
// 这次作业确实持有两把维护锁:收尾失败后必须仍然持有(页面显示维护中)。
if _, err := store.AcquireLibraryMaintenance(ctx, postgresstore.LibraryMaintenance{Scope: scope,
OwnerJobID: jobID, TargetIdentity: "scope=" + scope, Level: "library", State: "acquired",
IngressState: "closed", Services: []string{"app"}}, now); err != nil {
t.Fatalf("acquire library maintenance: %v", err)
}
if _, err := store.AcquireInstanceMaintenance(ctx, postgresstore.LibraryInstanceMaintenance{Instance: instance,
OwnerJobID: jobID, OwnerScope: scope, TargetFingerprint: "fingerprint-r11",
Level: "instance", Services: []string{"app"}, IngressState: "closed", State: "acquired"}, now); err != nil {
t.Fatalf("acquire instance maintenance: %v", err)
}
// 完整锚点:真实字节数与写集合摘要。缺任何一项,收尾都该拒绝登记而不是补一份空台账。
if err := store.RecordLibraryVersion(ctx, governance.LibraryVersion{
ID: governance.LibraryVersionID(governance.LibraryVersionProductionPrebackup, scope, backup),
Scope: scope, Kind: governance.LibraryVersionProductionPrebackup, SnapshotID: backup,
SourceJobID: jobID, CreatedBy: "tester",
Evidence: map[string]any{"backup_snapshot": backup, "backup_bytes": float64(4096),
"writeset_digest": "writeset-digest-r11", "writeset_rows": float64(7)},
CreatedAt: now,
}); err != nil {
t.Fatalf("record prebackup version: %v", err)
}
// 目标内容落盘且计数:任何“再来一次”的重写都会让计数增长。
root := t.TempDir()
published := filepath.Join(root, "target-content.txt")
if err := os.WriteFile(published, []byte("published-v1"), 0o600); err != nil {
t.Fatal(err)
}
applies := 1 // 内容写入(含校验)已经发生过一次
writer := &libraryAttemptWriter{Store: store, attempt: governance.LibraryAttempt{
OperationID: operation, JobID: jobID, Owner: "queue-r11-finalize"}}
recorder := libraryStageRecorder{store: store, jobID: jobID, scope: scope, operationID: operation}
payload := libraryReleasePayload{LibraryJobID: jobID, Scope: scope, TargetSnapshotID: target, FinalizeOnly: true}
previous := governance.LibraryJob{ID: jobID, Scope: scope, Kind: governance.LibraryJobSync,
SnapshotID: backup, TargetSnapshotID: target}
grant := governance.LibraryReleaseAuthorization{AuthorizedBy: "admin", Scope: scope, SnapshotID: target}
// 1) 事件登记失败(这里用真实数据缺口:锚点里没有写集合摘要)必须拒绝登记,
// 不写生效版本、不进入终态、不重写目标。
if err := store.RecordLibraryVersion(ctx, governance.LibraryVersion{
ID: governance.LibraryVersionID(governance.LibraryVersionProductionPrebackup, scope, backup+"-noanchor"),
Scope: scope, Kind: governance.LibraryVersionProductionPrebackup, SnapshotID: backup + "-noanchor",
SourceJobID: jobID + "-noanchor", CreatedBy: "tester",
Evidence: map[string]any{"backup_snapshot": backup + "-noanchor"},
CreatedAt: now,
}); err != nil {
t.Fatalf("record anchorless prebackup: %v", err)
}
badBoundary := payload
badBoundary.TargetSnapshotID = target + "-noanchor"
badPrevious := previous
badPrevious.SnapshotID = backup + "-noanchor"
if _, err := rebuildSuccessBoundary(ctx, writer, recorder, badBoundary, badPrevious,
governance.LibraryJobSync, grant); err == nil {
t.Fatal("anchorless prebackup was accepted as a success boundary")
}
if _, found, err := store.LibraryVersionByRef(ctx, scope, governance.LibraryVersionProductionEffective,
jobID, target+"-noanchor"); err != nil || found {
t.Fatalf("refused registration still wrote an effective version: found=%v err=%v", found, err)
}
if job, err := store.LibraryJob(ctx, jobID); err != nil || job.State == governance.LibraryJobSucceeded {
t.Fatalf("refused registration reported success: state=%v err=%v", job.State, err)
}
// 2) journal 终态之后、释放维护占用这一步失败:不许报成功,锁必须继续持有。
failing := &r11FinishFaultStore{Store: store, applies: &applies, failOnRelease: true,
releaseFailure: errors.New("injected: scope maintenance release failed after terminal journal")}
boundary := successBoundary{Ready: true, Backup: backup, Target: target, FinishedAt: now}
if err := finishLibrarySuccess(ctx, failing, r11HealthyTarget{}, instance, scope, jobID, boundary); err == nil {
t.Fatal("finish reported success even though the maintenance release failed")
}
if job, err := store.LibraryJob(ctx, jobID); err != nil || job.State != governance.LibraryJobFailed {
t.Fatalf("job must be failed after finalize failure: state=%v err=%v", job.State, err)
}
held, err := store.LibraryMaintenanceOf(ctx, scope)
if err != nil || held == nil || held.OwnerJobID != jobID || held.ReleasedAt != nil {
t.Fatalf("scope maintenance must stay held by this job: %+v err=%v", held, err)
}
if held.State != "needs_restore" {
t.Fatalf("held maintenance state = %q, want needs_restore", held.State)
}
if applies != 1 {
t.Fatalf("finalize failure rewrote target content: applies=%d", applies)
}
content, err := os.ReadFile(published)
if err != nil || string(content) != "published-v1" {
t.Fatalf("target bytes changed during finalize failure: %q err=%v", content, err)
}
// 3) 修复只补收尾:同一个持久凭据再收尾一次即可成功,且仍然不重写。
repaired := &r11FinishFaultStore{Store: store, applies: &applies}
if err := finishLibrarySuccess(ctx, repaired, r11HealthyTarget{}, instance, scope, jobID, boundary); err != nil {
t.Fatalf("finalize-only repair failed: %v", err)
}
if job, err := store.LibraryJob(ctx, jobID); err != nil || job.State != governance.LibraryJobSucceeded {
t.Fatalf("job not succeeded after repair: state=%v err=%v", job.State, err)
}
released, err := store.LibraryMaintenanceOf(ctx, scope)
if err != nil || released == nil || released.ReleasedAt == nil {
t.Fatalf("scope maintenance not released after repair: %+v err=%v", released, err)
}
if applies != 1 {
t.Fatalf("finalize-only repair rewrote target content: applies=%d", applies)
}
content, err = os.ReadFile(published)
if err != nil || string(content) != "published-v1" {
t.Fatalf("target bytes changed during repair: %q err=%v", content, err)
}
// 4) current/event 与实际内容一致:补登记的生效版本恰好一条,重复收尾不再新增。
if _, err := rebuildSuccessBoundary(ctx, writer, recorder, payload, previous,
governance.LibraryJobSync, grant); err != nil {
t.Fatalf("rebuild boundary after repair: %v", err)
}
if _, err := rebuildSuccessBoundary(ctx, writer, recorder, payload, previous,
governance.LibraryJobSync, grant); err != nil {
t.Fatalf("idempotent rebuild: %v", err)
}
current, found, err := store.LibraryCurrentVersion(ctx, scope)
if err != nil || !found || current.SnapshotID != target || current.SourceJobID != jobID {
t.Fatalf("current does not point at the repaired snapshot: %+v found=%v err=%v", current, found, err)
}
versions, err := store.LibraryVersionsBySnapshot(ctx, scope, target)
if err != nil {
t.Fatalf("read effective versions: %v", err)
}
same := 0
for _, version := range versions {
if version.Kind == governance.LibraryVersionProductionEffective {
same++
}
}
if same != 1 {
t.Fatalf("effective version rows for %s = %d, want exactly 1", target, same)
}
}
@@ -0,0 +1,102 @@
package main
import (
"context"
"strings"
"testing"
"time"
"knowledge-governance/internal/governance"
"knowledge-governance/internal/libraryrelease"
)
// r11RevokingBackups 在真实预备份完成之后撤销授权:这正是"排空之后、首次写之前"
// 被撤销的那个窗口,不是凭空造一个失败。
type r11RevokingBackups struct {
inner libraryrelease.BackupStore
after func()
}
func (b r11RevokingBackups) Prepare(ctx context.Context, id string, current libraryrelease.CurrentState) (libraryrelease.BackupRef, error) {
ref, err := b.inner.Prepare(ctx, id, current)
if err == nil && b.after != nil {
b.after()
}
return ref, err
}
// B30:授权在预备份之后被撤销,必须一笔目标写都不发生;同一链路在没有撤销时
// 正常完成(正例对照,证明这条门不是靠"永远失败"通过的)。
func TestR11RevokedAuthorizationStopsBeforeFirstWrite(t *testing.T) {
store, _ := openLedgerStore(t)
ctx := context.Background()
now := time.Now().UTC()
const (
scope = "internal"
jobID = "libsync-r11-revoke"
snapshot = "control-snap-r11-revoke"
target = "target-snap-r11-revoke"
params = "params-r11-revoke"
operation = "libop-r11-revoke"
backup = "backup-snap-r11-revoke"
authID = "libauth-r11-revoke"
)
if _, _, err := store.CreateLibraryJob(ctx, governance.LibraryJob{ID: jobID, Scope: scope,
Kind: governance.LibraryJobSync, IdempotencyKey: "library.sync:internal:r11-revoke",
RequestDigest: params, Actor: "admin", TargetSnapshotID: target}, governance.SyncStages(), now); err != nil {
t.Fatalf("create library job: %v", err)
}
if _, _, err := store.EnsureLibraryOperation(ctx, governance.LibraryOperation{ID: operation, Scope: scope,
Kind: governance.LibraryJobSync, TargetSnapshotID: target, RequestDigest: params,
OperationKey: "r11-revoke", Actor: "admin", CreatedAt: now}); err != nil {
t.Fatalf("ensure library operation: %v", err)
}
if err := store.CreateLibraryAuthorization(ctx, governance.LibraryReleaseAuthorization{ID: authID, Scope: scope,
SnapshotID: target, ParamsDigest: params, AuthorizedBy: "admin", ExpiresAt: now.Add(time.Hour), CreatedAt: now}); err != nil {
t.Fatalf("create authorization: %v", err)
}
grant := libraryrelease.Grant{Target: scope, SnapshotID: target, ParamsDigest: params,
ExpiresAt: now.Add(time.Hour), AuthorizedBy: "admin"}
request := libraryrelease.Request{JobID: jobID, Target: scope, SnapshotID: target,
ParamsDigest: params, Now: now, Grant: grant}
backups := ledgerTestBackups{ref: libraryrelease.BackupRef{ID: "backup-" + backup, SnapshotID: backup,
Bytes: 4096, Universe: map[string]libraryrelease.TableFingerprint{"resources": {Count: 1, Digest: "universe"}}}}
// 正例对照:授权有效时同一条链路写完四个阶段。
control := &planningTarget{}
controlTarget := &contentTrackedTarget{inner: control, jobID: jobID, onApply: func() {},
revalidateGrant: revalidateLibraryGrant(store)}
if out := libraryrelease.Run(ctx, request, controlTarget, backups, nil); out.State != "succeeded" {
t.Fatalf("control run did not succeed with a live authorization: %+v", out)
}
if control.applied != 4 {
t.Fatalf("control run applied %d stages, want 4", control.applied)
}
// 撤销后:预备份成功、授权随即失效,写集合都不该被算出来(那会分配序列)。
guarded := &planningTarget{}
guardedTarget := &contentTrackedTarget{inner: guarded, jobID: jobID, onApply: func() {},
revalidateGrant: revalidateLibraryGrant(store)}
revoking := r11RevokingBackups{inner: backups, after: func() {
if err := store.RevokeLibraryAuthorization(ctx, authID, time.Now().UTC()); err != nil {
t.Fatalf("revoke authorization: %v", err)
}
}}
out := libraryrelease.Run(ctx, request, guardedTarget, revoking, nil)
if out.State != "failed" {
t.Fatalf("revoked authorization still produced state %q: %+v", out.State, out)
}
if !strings.Contains(out.Error, "持久授权") {
t.Fatalf("refusal reason does not name the persisted authorization: %q", out.Error)
}
if guarded.applied != 0 || guarded.planned != 0 {
t.Fatalf("revoked authorization wrote target: applied=%d planned=%d", guarded.applied, guarded.planned)
}
if out.Backup.SnapshotID != backup || !strings.Contains(out.Recovery, "预备份保留") {
t.Fatalf("prebackup must be preserved for a retry: backup=%+v recovery=%q", out.Backup, out.Recovery)
}
// 撤销是持久的:再读一次仍然查不到有效授权。
if _, ok, err := store.ActiveLibraryAuthorization(ctx, scope, target, params, time.Now().UTC()); err != nil || ok {
t.Fatalf("revocation did not persist: ok=%v err=%v", ok, err)
}
}
+61
View File
@@ -0,0 +1,61 @@
package main
import (
"context"
"errors"
"testing"
"time"
)
func TestContractLeaseLossWaitsForActualWriteExit(t *testing.T) {
lost := errors.New("lease lost")
entered := make(chan struct{})
cancelled := make(chan struct{})
release := make(chan struct{})
finished := make(chan error, 1)
go func() {
finished <- runWithLibraryLease(context.Background(), time.Millisecond, func(context.Context) error { <-entered; return lost }, func(ctx context.Context) error { close(entered); <-ctx.Done(); close(cancelled); <-release; return nil })
}()
select {
case <-cancelled:
case <-time.After(time.Second):
t.Fatal("loss did not cancel actual writer")
}
select {
case <-finished:
t.Fatal("returned while actual write still running")
default:
}
close(release)
select {
case err := <-finished:
if !errors.Is(err, lost) {
t.Fatalf("loss hidden: %v", err)
}
case <-time.After(time.Second):
t.Fatal("writer not joined")
}
}
func TestContractLeaseRenewsDuringLongWrite(t *testing.T) {
renewed := make(chan struct{}, 1)
err := runWithLibraryLease(context.Background(), time.Millisecond, func(context.Context) error {
select {
case renewed <- struct{}{}:
default:
}
return nil
}, func(ctx context.Context) error {
select {
case <-renewed:
return nil
case <-time.After(time.Second):
return errors.New("no renewal")
case <-ctx.Done():
return ctx.Err()
}
})
if err != nil {
t.Fatal(err)
}
}