Files
knowledge-governance/cmd/worker/library_seal_source_failure_test.go
T
姚章浩 4421dd38ce
build / build (push) Successful in 1m35s
fix(worker): 封存来源校验失败写入失败终态
verifyCapturedSource 失败时原实现只 return err:队列作业已是 failed,业务行
library_jobs 却停在 running/export、error 为空,页面永远显示"导出中"(实测
libseal-lib-internal-20261102T234053)。同函数其余四条失败分支都同时写 stage 与
job,这一条漏了。

现在 ensureCapturedSource 在校验失败时按既有惯例写 stage failed + job failed,
原错原样进取值、stage detail 与 job.error,恢复文案指向"修配置后重新封存"(快照
字节完好,不需要重采)。成功路径不变。

验证:
- RED:新增用例在修复前失败于 "stage writes = [], want exactly one failed stage"
- GREEN:TestCapturedSourceMismatchPersistsFailedStageAndJob /
  TestCapturedSourceMatchStaysSuccessful 通过
- make verify:16 项必需检查全绿(tests-go 含新用例)
2026-11-03 07:59:40 +08:00

101 lines
4.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"context"
"os"
"strings"
"testing"
"time"
"knowledge-governance/internal/governance"
)
// fakeStageStore 记录封存失败分支对业务作业行的两次落库调用。
type fakeStageStore struct {
stageCalls []string
jobCalls []string
}
func (f *fakeStageStore) UpdateLibraryStage(_ context.Context, jobID, stage, state string, _, _, _, _ *int64, detail string, _ time.Time) error {
f.stageCalls = append(f.stageCalls, jobID+"|"+stage+"|"+state+"|"+detail)
return nil
}
func (f *fakeStageStore) AdvanceLibraryJob(_ context.Context, jobID string, state governance.LibraryJobState, stage, message, recovery, _, _ string, _ time.Time) error {
f.jobCalls = append(f.jobCalls, jobID+"|"+string(state)+"|"+stage+"|"+message+"|"+recovery)
return nil
}
func (f *fakeStageStore) LibraryVersionByRef(context.Context, string, governance.LibraryVersionKind, string, string) (governance.LibraryVersion, bool, error) {
return governance.LibraryVersion{}, false, nil
}
func (f *fakeStageStore) RecordLibraryVersion(context.Context, governance.LibraryVersion) error {
return nil
}
// 归档根必须是非符号链接的 0700 目录;t.TempDir 的权限受 umask 影响。
func privateDir(t *testing.T) string {
t.Helper()
root := t.TempDir()
if err := os.Chmod(root, 0700); err != nil {
t.Fatal(err)
}
return root
}
func sourceFor(scope, kb string) governance.LibrarySourceIdentity {
return governance.LibrarySourceIdentity{Project: "kgmaint-weknora", App: "kgmaint-app", Scope: scope, TenantID: 1, KnowledgeBaseID: kb}
}
// RED 用例:快照来源与配置不一致时,队列已 failed、页面却永远"导出中"(真实事故:
// libseal-lib-internal-20261102T234053 running/export/error 空)。失败必须同时写进
// stage 与 job,且原错原样出现在两处与返回值里 —— 不能被"已记录失败"遮住。
func TestCapturedSourceMismatchPersistsFailedStageAndJob(t *testing.T) {
root := privateDir(t)
snapshotID := sealTinyBaseline(t, root, "internal", time.Date(2026, 11, 2, 23, 40, 53, 0, time.UTC))
// 清单里是 kb-baseline;worker 配置漂移后拿的是另一个 KB(256 验收遗留正是这样)。
source := sourceFor("internal", "0c84a098-d3ed-4b82-a165-e6de9691d6a8")
store := &fakeStageStore{}
err := ensureCapturedSource(context.Background(), store, "libseal-lib-internal-20261102T234053",
root, snapshotID, source, time.Now().UTC())
if err == nil || !strings.Contains(err.Error(), "captured manifest differs from configured source") {
t.Fatalf("mismatch must keep the original error, got %v", err)
}
if len(store.stageCalls) != 1 {
t.Fatalf("stage writes = %v, want exactly one failed stage", store.stageCalls)
}
if !strings.HasPrefix(store.stageCalls[0], "libseal-lib-internal-20261102T234053|export|failed|") ||
!strings.Contains(store.stageCalls[0], "captured manifest differs from configured source") {
t.Fatalf("stage write = %q, want failed+original error", store.stageCalls[0])
}
if len(store.jobCalls) != 1 {
t.Fatalf("job writes = %v, want exactly one failed job", store.jobCalls)
}
if !strings.HasPrefix(store.jobCalls[0], "libseal-lib-internal-20261102T234053|failed|export|") ||
!strings.Contains(store.jobCalls[0], "captured manifest differs from configured source") {
t.Fatalf("job write = %q, want failed+original error", store.jobCalls[0])
}
// 恢复说明必须指向真实下一步,而不是暗示数据损坏。
if !strings.Contains(store.jobCalls[0], "重新封存") {
t.Fatalf("job recovery = %q, want a re-seal instruction", store.jobCalls[0])
}
}
// 正例对照:身份一致时不得写任何失败,也不得把成功路径变成失败。
func TestCapturedSourceMatchStaysSuccessful(t *testing.T) {
root := privateDir(t)
snapshotID := sealTinyBaseline(t, root, "internal", time.Date(2026, 11, 2, 23, 40, 53, 0, time.UTC))
store := &fakeStageStore{}
if err := ensureCapturedSource(context.Background(), store, "libseal-ok",
root, snapshotID, sourceFor("internal", "kb-baseline"), time.Now().UTC()); err != nil {
t.Fatalf("matching source rejected: %v", err)
}
if len(store.stageCalls) != 0 || len(store.jobCalls) != 0 {
t.Fatalf("success path wrote failures: stage=%v job=%v", store.stageCalls, store.jobCalls)
}
}