8a68ed1819
父审查确认四项是原 finding 未闭环,本包按真实调用链修完(先 RED 再修)。 R4a 正常流程必现阻断:API 先入 handoff 队列作业并关联,交接却只接受"未关联", 每次正常发布都被自己的所有权检查挡住。新增 governance.LibraryHandoffQueueJobID (jobs.id 为 varchar(36),原实现拼后缀超长,插入即失败);EnqueueAndAttachLibraryJob 增加"声明的前驱队列作业号",只允许当前无人持有或正是该 handoff 时替换,任意替换拒绝。 测试从 API 真实前驱(已关联)开始,覆盖正常交接、错误前驱拒绝、交接后重放、 以及 release 与 handoff 两条队列行都存在。 R4b 迟到 handoff 污染已交接作业:失败出口(含 Push 错误)改为所有权 CAS (FailLibraryHandoffIfOwner),只在业务作业仍由本次 handoff 持有时写 state/stage/recovery; 已交接/已终态零修改。所有权判定改为只看 library_jobs.queue_job_id(阶段行是否存在 与所有权无关,原实现会误判)。阶段写入也带所有权。用测试屏障确定性地复现"失败出口 到来时所有权已切给 release",验证 release running 与 succeeded 两种状态下零修改。 R2 回传游标每轮重置:改为跨轮保留(单轮仍 20 页预算),读到不足一页才回到起点; 新增 pullCursor 与跨轮覆盖测试(2100 条引用、每轮 5 页仍全部覆盖,不重复不漏取)。 R3 原产地误判:封存成功后会再登记 production_effective(同一快照),原实现把 sealed+target 判成冲突,正常再发布/历史恢复被误拒。现在原产地只由捕获事件决定 (test_sealed=源 / production_prebackup=目标),effective/recovery_verified 只是引用; 二者冲突或都没有捕获事件仍拒绝。测试覆盖正常成功链、真冲突、无捕获事件三种。 验证:隔离 fixture PostgreSQL 实跑 cmd/worker 全包通过(含上述四条真实调用链); libraryhandoff/librarysnapshot/httpapi/governance/platform-postgres/libraryctl 全绿; 完整 ci_checks 见提交时日志。未部署、未推送、未构建镜像;真实跨主机 SSH 链路仍未验证。
180 lines
7.0 KiB
Go
180 lines
7.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"knowledge-governance/internal/appconfig"
|
|
"knowledge-governance/internal/governance"
|
|
postgresstore "knowledge-governance/internal/platform/postgres"
|
|
)
|
|
|
|
func fixturePool(t *testing.T) (*pgxpool.Pool, *postgresstore.Store) {
|
|
t.Helper()
|
|
dsn := os.Getenv("KG_TEST_DSN")
|
|
if dsn == "" {
|
|
t.Skip("KG_TEST_DSN 未设置:跳过隔离 fixture 库测试")
|
|
}
|
|
ctx := context.Background()
|
|
pool, err := pgxpool.New(ctx, dsn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(pool.Close)
|
|
if err := postgresstore.ApplyMigrations(ctx, pool); err != nil {
|
|
t.Fatalf("migrate fixture db: %v", err)
|
|
}
|
|
return pool, postgresstore.NewStore(pool)
|
|
}
|
|
|
|
// appconfigFixture 让**源绑定与目标绑定不同**:封存快照的身份是源 KB,发布目标是专用验收 KB。
|
|
// 这正是云测试方案 A 的真实形状(源 WKNFull,目标 a8e9c8ad/2e980686 专用 KB)。
|
|
func appconfigFixture(f handoffFixture) appconfig.Config {
|
|
return appconfig.Config{
|
|
LibrarySourceTenantID: f.tenant, LibrarySourceInternalKB: f.kb, LibrarySourceExternalKB: f.kb,
|
|
LibraryProductionTenantID: f.tenant, LibraryProductionInternalKB: "kb-target-acceptance",
|
|
LibraryProductionExternalKB: "kb-target-acceptance",
|
|
}
|
|
}
|
|
|
|
// recordSealedVersion 登记"这份快照是一次封存的产物",搬运据此判定它应按源绑定核对。
|
|
func recordSealedVersion(t *testing.T, store *postgresstore.Store, snapshot, scope string) {
|
|
t.Helper()
|
|
if err := store.RecordLibraryVersion(context.Background(), governance.LibraryVersion{
|
|
ID: governance.LibraryVersionID(governance.LibraryVersionTestSealed, scope, snapshot),
|
|
Scope: scope, Kind: governance.LibraryVersionTestSealed, SnapshotID: snapshot,
|
|
SourceJobID: "fixture-capture", CreatedAt: time.Now().UTC(),
|
|
}); err != nil {
|
|
t.Fatalf("record sealed version: %v", err)
|
|
}
|
|
}
|
|
|
|
// resetFixtureJobs 收掉上一个用例留下的在飞 fixture 作业:隔离库会被反复使用,
|
|
// (scope,kind) 的唯一索引会把新建当成冲突。只动 handoff fixture 的行。
|
|
func resetFixtureJobs(t *testing.T, pool *pgxpool.Pool) {
|
|
t.Helper()
|
|
if _, err := pool.Exec(context.Background(),
|
|
`UPDATE library_jobs SET state='failed', finished_at=now(), updated_at=now()
|
|
WHERE id LIKE 'libsync-handoff-%' AND state IN ('pending','running')`); err != nil {
|
|
t.Fatalf("reset fixture jobs: %v", err)
|
|
}
|
|
}
|
|
|
|
func digestOf(label string) string {
|
|
sum := sha256.Sum256([]byte(label))
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
// 回传枚举必须覆盖"第 51 条以后"和"失败作业留下的预备份",不能用固定 LIMIT 50 截断。
|
|
func TestSnapshotCandidatesPaginateBeyondFiftyAndIncludeRecoveryBackups(t *testing.T) {
|
|
pool, store := fixturePool(t)
|
|
ctx := context.Background()
|
|
scope := "internal"
|
|
if _, err := pool.Exec(ctx, `DELETE FROM library_jobs WHERE id LIKE 'pagination-%'`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
base := time.Now().UTC().Add(-time.Hour)
|
|
want := map[string]bool{}
|
|
for index := 0; index < 55; index++ {
|
|
id := "pagination-ok-" + digestOf(string(rune('a'+index%26)) + time.Duration(index).String())[:10] + string(rune('a'+index%26)) + string(rune('a'+index/26))
|
|
snapshot := digestOf("snapshot-" + id)
|
|
want[snapshot] = true
|
|
if _, err := pool.Exec(ctx, `
|
|
INSERT INTO library_jobs (id, scope, kind, state, snapshot_id, created_at, updated_at, request_digest, operation_id, attempt_epoch, idempotency_key)
|
|
VALUES ($1,$2,'sync','succeeded',$3,$4,$4,'d','op',1,$1)
|
|
ON CONFLICT (id) DO UPDATE SET snapshot_id=EXCLUDED.snapshot_id, state='succeeded'`,
|
|
id, scope, snapshot, base.Add(time.Duration(index)*time.Second)); err != nil {
|
|
t.Fatalf("insert fixture job %d: %v", index, err)
|
|
}
|
|
// 最新的 50 条之外还有恢复基线:一个失败作业留下的预备份。
|
|
}
|
|
failedSnapshot := digestOf("failed-prebackup")
|
|
want[failedSnapshot] = true
|
|
if _, err := pool.Exec(ctx, `
|
|
INSERT INTO library_jobs (id, scope, kind, state, snapshot_id, recovery, created_at, updated_at, request_digest, operation_id, attempt_epoch, idempotency_key)
|
|
VALUES ('pagination-failed',$1,'sync','failed',$2,'按预备份恢复',$3,$3,'d','op',1,'pagination-failed')
|
|
ON CONFLICT (id) DO UPDATE SET snapshot_id=EXCLUDED.snapshot_id, state='failed'`,
|
|
scope, failedSnapshot, base.Add(2*time.Hour)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
seen := map[string]bool{}
|
|
var afterCreated time.Time
|
|
afterID := ""
|
|
for page := 0; page < 20; page++ {
|
|
items, err := store.LibrarySnapshotCandidates(ctx, scope, afterCreated, afterID, 7)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(items) == 0 {
|
|
break
|
|
}
|
|
for _, item := range items {
|
|
if seen[item.SnapshotID] {
|
|
t.Fatalf("pagination repeated snapshot %s", item.SnapshotID)
|
|
}
|
|
seen[item.SnapshotID] = true
|
|
}
|
|
last := items[len(items)-1]
|
|
afterCreated, afterID = last.CreatedAt, last.SnapshotID
|
|
if len(items) < 7 {
|
|
break
|
|
}
|
|
}
|
|
for snapshot := range want {
|
|
if !seen[snapshot] {
|
|
t.Fatalf("pagination missed snapshot %s(第51条之后或失败作业的预备份被截断)", snapshot)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 重放:已成功的作业不允许被一次迟到的搬运失败改写成 failed;重复搬运不重复投递。
|
|
func TestHandoffReplayDoesNotOverwriteTerminalStateOrDuplicateReleaseJob(t *testing.T) {
|
|
pool, store := fixturePool(t)
|
|
ctx := context.Background()
|
|
resetFixtureJobs(t, pool)
|
|
fixture := newHandoffFixture(t)
|
|
config := appconfigFixture(fixture)
|
|
snapshot := fixture.seal(t)
|
|
recordSealedVersion(t, store, snapshot, "internal")
|
|
job := createFixtureJob(t, store, "internal", snapshot)
|
|
handoffQueueID := bindHandoffQueue(t, nil, store, job, "internal", snapshot)
|
|
payload := libraryHandoffPayload{LibraryJobID: job.ID, Scope: "internal",
|
|
TargetSnapshotID: snapshot, NextType: librarySyncJobType}
|
|
if err := runLibraryHandoffForQueue(ctx, store, config, fixture.handoff, payload, handoffQueueID); err != nil {
|
|
t.Fatalf("first handoff: %v", err)
|
|
}
|
|
// 幂等重放:不得产生第二个队列作业,也不得改状态。
|
|
if err := runLibraryHandoffForQueue(ctx, store, config, fixture.handoff, payload, handoffQueueID); err != nil {
|
|
t.Fatalf("replayed handoff: %v", err)
|
|
}
|
|
var queued int
|
|
if err := pool.QueryRow(ctx, `SELECT count(*) FROM jobs WHERE id=$1`,
|
|
governance.QueueJobIDFor(job.ID)).Scan(&queued); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if queued != 1 {
|
|
t.Fatalf("replay produced %d release queue jobs", queued)
|
|
}
|
|
// 迟到的失败重放:作业已成功,不允许被改写。
|
|
if _, err := pool.Exec(ctx, `UPDATE library_jobs SET state='succeeded', error='', recovery='' WHERE id=$1`, job.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bad := libraryHandoffPayload{LibraryJobID: job.ID, Scope: "internal",
|
|
TargetSnapshotID: fixture.unknownSnapshotID(), NextType: librarySyncJobType}
|
|
_ = runLibraryHandoffForQueue(ctx, store, config, fixture.handoff, bad, handoffQueueID)
|
|
after, err := store.LibraryJob(ctx, job.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if after.State != governance.LibraryJobSucceeded {
|
|
t.Fatalf("a late failure replay overwrote state to %s", after.State)
|
|
}
|
|
}
|