2eff9447e1
build / build (push) Failing after 1m20s
ReconcileLibraryJobs 不再把 completed capture queue 当成不存在,保留待核对 pending 封存作业;failed/cancelled/missing 仍按真实队列状态处置。管理员核对 入口只允许精确的启动误判形态,并要求 completed queue、export succeeded、 唯一 test_sealed 版本、来源身份与完整归档复核。新增 library_job_repairs 不可变审计表,在同一事务内 CAS 保存原 state/stage/error/recovery/finished_at 及本次 actor/原因/证据后修复;真实失败/取消不开放。 验证:真实隔离 PostgreSQL 启动收敛 RED/GREEN、启动→handler 纵向 pending 与 系统误判 failed、审计保留/幂等、负例;真实 Go 全量与 make verify 17 项通过。
362 lines
13 KiB
Go
362 lines
13 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/governance"
|
|
"knowledge-governance/internal/jobs"
|
|
"knowledge-governance/internal/librarysnapshot"
|
|
)
|
|
|
|
func (f *fakeLibraryStore) QueueJob(_ context.Context, id string) (jobs.Job, bool, error) {
|
|
if f.queue == nil || f.queue.ID != id {
|
|
return jobs.Job{}, false, nil
|
|
}
|
|
return *f.queue, true, nil
|
|
}
|
|
|
|
func (f *fakeLibraryStore) LibraryCaptureExportEvidence(_ context.Context, _ string) (string, string, error) {
|
|
return f.stageState, "", f.stageErr
|
|
}
|
|
|
|
func (f *fakeLibraryStore) LibraryJobStage(_ context.Context, _, stage string) (string, string, error) {
|
|
if f.stageErr != nil {
|
|
return "", "", f.stageErr
|
|
}
|
|
if stage != "export" {
|
|
return "", "", errors.New("unexpected stage")
|
|
}
|
|
return f.stageState, "", nil
|
|
}
|
|
|
|
func (f *fakeLibraryStore) LibraryTestSealedVersionForJob(_ context.Context, scope, jobID string) (governance.LibraryVersion, bool, error) {
|
|
if f.versionErr != nil {
|
|
return governance.LibraryVersion{}, false, f.versionErr
|
|
}
|
|
if f.sealedVersion == nil || f.sealedVersion.Scope != scope || f.sealedVersion.SourceJobID != jobID {
|
|
return governance.LibraryVersion{}, false, nil
|
|
}
|
|
return *f.sealedVersion, true, nil
|
|
}
|
|
|
|
func reconcileFixture(t *testing.T) (*fakeLibraryStore, Config, governance.LibraryJob, []byte) {
|
|
t.Helper()
|
|
root := libraryFixtureRoot(t)
|
|
snapshotID := sealSharedVersions(t, root, 1, 4)[0]
|
|
archive, err := librarysnapshot.Open(root, 256*1024*1024)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manifest, err := archive.Load(snapshotID)
|
|
archive.Close()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
config := Config{LibraryArchiveRoot: root,
|
|
LibrarySourceProject: "fixture-project", LibrarySourceApp: "fixture-app",
|
|
LibrarySourceTenantID: manifest.TenantID, LibrarySourceInternalKB: manifest.KnowledgeBaseID}
|
|
source := configuredLibrarySource(config, "internal")
|
|
sourceID, err := source.ID()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
job := governance.LibraryJob{ID: "libseal-cap-1", Scope: "internal", Kind: governance.LibraryJobSeal,
|
|
State: governance.LibraryJobPending, QueueJobID: "libcap-1"}
|
|
payload, _ := json.Marshal(map[string]string{"scope": "internal", "capture_job": "cap-1"})
|
|
store := &fakeLibraryStore{
|
|
jobs: []governance.LibraryJob{job},
|
|
stageState: "succeeded",
|
|
queue: &jobs.Job{ID: "libcap-1", Type: libraryCaptureJobType, State: jobs.StateCompleted,
|
|
Payload: payload, IdempotencyKey: "library.capture:cap-1"},
|
|
sealedVersion: &governance.LibraryVersion{ID: "libver-1", Scope: "internal",
|
|
Kind: governance.LibraryVersionTestSealed, SnapshotID: snapshotID, SourceJobID: job.ID,
|
|
Evidence: map[string]any{"source_id": sourceID, "capture_job": "cap-1"}},
|
|
}
|
|
config.Store = store
|
|
return store, config, job, payload
|
|
}
|
|
|
|
func (f *fakeLibraryStore) ReconcileLibraryCaptureTerminal(_ context.Context, jobID, scope, queueJobID, snapshotID string, _ time.Time) (bool, error) {
|
|
if f.reconcileDenied {
|
|
return false, nil
|
|
}
|
|
for index := range f.jobs {
|
|
if f.jobs[index].ID != jobID || f.jobs[index].Scope != scope || f.jobs[index].Kind != governance.LibraryJobSeal ||
|
|
f.jobs[index].QueueJobID != queueJobID || (f.jobs[index].State != governance.LibraryJobPending && f.jobs[index].State != governance.LibraryJobRunning) ||
|
|
(f.jobs[index].SnapshotID != "" && f.jobs[index].SnapshotID != snapshotID) {
|
|
return false, nil
|
|
}
|
|
f.jobs[index].State = governance.LibraryJobSucceeded
|
|
f.jobs[index].SnapshotID = snapshotID
|
|
f.lastState = governance.LibraryJobSucceeded
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func (f *fakeLibraryStore) ReconcileMisclassifiedLibraryCapture(_ context.Context, jobID, scope, queueJobID, snapshotID, expectedStage, expectedError, expectedRecovery string, expectedFinished *time.Time, _ string, _ string, _ string, _ map[string]any, _ time.Time) (bool, error) {
|
|
if f.reconcileDenied {
|
|
return false, nil
|
|
}
|
|
for index := range f.jobs {
|
|
j := &f.jobs[index]
|
|
if j.ID != jobID || j.Scope != scope || j.Kind != governance.LibraryJobSeal || j.QueueJobID != queueJobID ||
|
|
j.State != governance.LibraryJobFailed || j.Stage != expectedStage || j.Error != expectedError ||
|
|
j.Recovery != expectedRecovery || j.SnapshotID != "" || (j.FinishedAt == nil) != (expectedFinished == nil) ||
|
|
(j.FinishedAt != nil && expectedFinished != nil && !j.FinishedAt.Equal(*expectedFinished)) {
|
|
return false, nil
|
|
}
|
|
j.State = governance.LibraryJobSucceeded
|
|
j.SnapshotID = snapshotID
|
|
j.Stage = "export"
|
|
f.lastState = governance.LibraryJobSucceeded
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
var _ libraryCaptureReconciler = (*fakeLibraryStore)(nil)
|
|
|
|
func runReconcile(t *testing.T, config Config, jobID string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/library/jobs/"+jobID+"/reconcile-capture", nil)
|
|
req.SetPathValue("id", jobID)
|
|
rec := httptest.NewRecorder()
|
|
libraryCaptureReconcile(rec, req, config)
|
|
return rec
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRepairsExactCompletedCapture(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("code=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
var body map[string]any
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if body["repaired"] != true || body["snapshot_id"] == "" || body["audit_operation_id"] == "" {
|
|
t.Fatalf("body=%v", body)
|
|
}
|
|
if store.lastState != governance.LibraryJobSucceeded {
|
|
t.Fatalf("advanced state=%s", store.lastState)
|
|
}
|
|
if len(store.operations) != 1 {
|
|
t.Fatalf("audit operations=%v", store.operations)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileAlreadySucceededIsReadOnly(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
job.State = governance.LibraryJobSucceeded
|
|
job.SnapshotID = store.sealedVersion.SnapshotID
|
|
store.jobs[0] = job
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("code=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
var body map[string]any
|
|
_ = json.Unmarshal(rec.Body.Bytes(), &body)
|
|
if body["already_succeeded"] != true || body["repaired"] != false || store.lastState != "" {
|
|
t.Fatalf("body=%v advanced=%q", body, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsQueueNotCompleted(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.queue.State = jobs.StateRunning
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsStageNotSucceeded(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.stageState = "failed"
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsWrongVersionSource(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.sealedVersion.Evidence["source_id"] = "other-source"
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsWrongCaptureJob(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.sealedVersion.Evidence["capture_job"] = "other-capture"
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsBadArchive(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.sealedVersion.SnapshotID = "missing-snapshot"
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsFailedJob(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
job.State = governance.LibraryJobFailed
|
|
store.jobs[0] = job
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileIsIdempotent(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
first := runReconcile(t, config, job.ID)
|
|
if first.Code != http.StatusOK {
|
|
t.Fatalf("first code=%d body=%s", first.Code, first.Body.String())
|
|
}
|
|
second := runReconcile(t, config, job.ID)
|
|
if second.Code != http.StatusOK {
|
|
t.Fatalf("second code=%d body=%s", second.Code, second.Body.String())
|
|
}
|
|
var body map[string]any
|
|
_ = json.Unmarshal(second.Body.Bytes(), &body)
|
|
if body["already_succeeded"] != true || len(store.operations) != 1 {
|
|
t.Fatalf("body=%v ops=%v", body, store.operations)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsMissingQueue(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.queue = nil
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsQueueScope(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.queue.Payload, _ = json.Marshal(map[string]string{"scope": "external", "capture_job": "cap-1"})
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsMissingVersion(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.sealedVersion = nil
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsWrongScopeVersion(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.sealedVersion.Scope = "external"
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsDifferentExistingSnapshot(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
job.SnapshotID = "other-snapshot"
|
|
store.jobs[0] = job
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileConditionalWriteRejectsConcurrentState(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.reconcileDenied = true
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRepairsExactLegacyMisclassification(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
finished := time.Now().UTC()
|
|
job.State = governance.LibraryJobFailed
|
|
job.Stage = ""
|
|
job.Error = legacyOrphanError
|
|
job.Recovery = legacyOrphanRecovery
|
|
job.FinishedAt = &finished
|
|
store.jobs[0] = job
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("code=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
var body map[string]any
|
|
_ = json.Unmarshal(rec.Body.Bytes(), &body)
|
|
if body["misclassification_repaired"] != true || store.lastState != governance.LibraryJobSucceeded {
|
|
t.Fatalf("body=%v advanced=%q", body, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsOtherFailedError(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
finished := time.Now().UTC()
|
|
job.State = governance.LibraryJobFailed
|
|
job.Stage = ""
|
|
job.Error = "capture export failed"
|
|
job.Recovery = legacyOrphanRecovery
|
|
job.FinishedAt = &finished
|
|
store.jobs[0] = job
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsRealFailureAndCancel(t *testing.T) {
|
|
for _, state := range []governance.LibraryJobState{governance.LibraryJobFailed, governance.LibraryJobCancelled} {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
job.State = state
|
|
job.Error = "real failure"
|
|
store.jobs[0] = job
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("state=%s code=%d advanced=%q", state, rec.Code, store.lastState)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileMisclassificationCASRejectsChange(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
finished := time.Now().UTC()
|
|
job.State = governance.LibraryJobFailed
|
|
job.Stage = ""
|
|
job.Error = legacyOrphanError
|
|
job.Recovery = legacyOrphanRecovery
|
|
job.FinishedAt = &finished
|
|
store.jobs[0] = job
|
|
store.reconcileDenied = true
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|