- 新增 library_versions:测试封存、生产生效、生产预备份三类分开登记,
生产生效版本唯一(旧版本必须先 supersede),测试快照不能顶替生产版本
- 新增 library_jobs + library_job_stages:业务作业身份、阶段、错误与恢复说明,
分项计数用 NULL 表示"总量未知";互斥与幂等由数据库唯一索引保证
- 新增 GET /api/v1/library/overview、/jobs、/jobs/{id};POST /syncs 闸口未齐
返回 503 缺项且不建作业
- 封存作业落库:先建业务作业再入队,worker 推进 export 阶段并登记 test_sealed
- 发布管理读真实状态:生产未知不再顶替、差异 unknown、启动同步按缺项禁用、
同步明细/四类分项来自持久阶段;阶段勾选只认当前作业证据
- 准备信息标注封存时间与范围,不声称实时测试库已就绪
This commit is contained in:
@@ -0,0 +1,230 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
// 生产闸口与整库状态契约的针对性测试:不做数据库集成,只验证
|
||||
// "证据不齐就不建作业" 与 "三类版本不互相顶替" 这两条硬规则。
|
||||
|
||||
// 复用既有 fakeJobStore 提供 Store 的其余方法,只覆盖整库作业这几个。
|
||||
type fakeLibraryStore struct {
|
||||
fakeJobStore
|
||||
versions []governance.LibraryVersion
|
||||
jobs []governance.LibraryJob
|
||||
createN int
|
||||
lastJob governance.LibraryJob
|
||||
lastState governance.LibraryJobState
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) CreateLibraryJob(_ context.Context, job governance.LibraryJob, _ []string, _ time.Time) (governance.LibraryJob, bool, error) {
|
||||
f.createN++
|
||||
f.lastJob = job
|
||||
return job, false, nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) LibraryJob(_ context.Context, id string) (governance.LibraryJob, error) {
|
||||
return governance.LibraryJob{ID: id}, nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) ListLibraryJobs(_ context.Context, scope string, _ int) ([]governance.LibraryJob, error) {
|
||||
result := make([]governance.LibraryJob, 0, len(f.jobs))
|
||||
for _, job := range f.jobs {
|
||||
if job.Scope == scope {
|
||||
result = append(result, job)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) RecordLibraryVersion(_ context.Context, version governance.LibraryVersion) error {
|
||||
f.versions = append(f.versions, version)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) LibraryVersions(_ context.Context, scope string, _ int) ([]governance.LibraryVersion, error) {
|
||||
result := make([]governance.LibraryVersion, 0, len(f.versions))
|
||||
for _, version := range f.versions {
|
||||
if version.Scope == scope {
|
||||
result = append(result, version)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) UpdateLibraryStage(_ context.Context, _, _, _ string, _, _, _, _ *int64, _ string, _ time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) AdvanceLibraryJob(_ context.Context, _ string, state governance.LibraryJobState, _, _, _, _, _ string, _ time.Time) error {
|
||||
f.lastState = state
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) AttachLibraryQueueJob(_ context.Context, _, _ string, _ time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) CancelLibraryJob(_ context.Context, _, _ string, _ time.Time) error {
|
||||
f.lastState = governance.LibraryJobCancelled
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestLibrarySyncGateRequiresEveryPieceOfEvidence(t *testing.T) {
|
||||
known := libraryKnownView{State: "known", Version: &libraryVersionView{ID: "v1"}}
|
||||
cases := []struct {
|
||||
name string
|
||||
config Config
|
||||
production libraryKnownView
|
||||
prebackup libraryKnownView
|
||||
target string
|
||||
allowed bool
|
||||
reason string
|
||||
}{
|
||||
{
|
||||
name: "default installation is closed",
|
||||
config: Config{},
|
||||
production: libraryKnownView{State: "unknown"},
|
||||
prebackup: libraryKnownView{State: "unknown"},
|
||||
target: "snap-1",
|
||||
allowed: false,
|
||||
reason: "生产写入闸口关闭",
|
||||
},
|
||||
{
|
||||
name: "flags on but restore evidence missing",
|
||||
config: Config{ProductionReleaseRequestsEnabled: true,
|
||||
ProductionWeKnoraBaseURL: "http://prod.invalid"},
|
||||
production: known,
|
||||
prebackup: known,
|
||||
target: "snap-1",
|
||||
allowed: false,
|
||||
reason: "没有已核验的生产恢复演练证据",
|
||||
},
|
||||
{
|
||||
name: "everything present",
|
||||
config: Config{ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: true,
|
||||
ProductionWeKnoraBaseURL: "http://prod.invalid"},
|
||||
production: known,
|
||||
prebackup: known,
|
||||
target: "snap-1",
|
||||
allowed: true,
|
||||
},
|
||||
}
|
||||
for _, testCase := range cases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
gate := librarySyncGate(testCase.config, testCase.production, testCase.prebackup, testCase.target)
|
||||
if gate.SyncAllowed != testCase.allowed {
|
||||
t.Fatalf("sync_allowed = %v, want %v (%v)", gate.SyncAllowed, testCase.allowed, gate.Reasons)
|
||||
}
|
||||
if testCase.reason != "" {
|
||||
joined := strings.Join(gate.Reasons, "|")
|
||||
if !strings.Contains(joined, testCase.reason) {
|
||||
t.Fatalf("reasons %v missing %q", gate.Reasons, testCase.reason)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 闸口关闭时 POST /api/v1/library/syncs 必须直接拒绝,并且**不创建**作业:
|
||||
// 不能留下一个永远失败的空作业,也不能让按钮看起来接通了。
|
||||
func TestLibrarySyncEnqueueRefusesWithoutCreatingJob(t *testing.T) {
|
||||
store := &fakeLibraryStore{}
|
||||
config := Config{Store: store}
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/library/syncs",
|
||||
strings.NewReader(`{"scope":"internal","target_snapshot_id":"snap-1"}`))
|
||||
recorder := httptest.NewRecorder()
|
||||
librarySyncEnqueue(recorder, request, config)
|
||||
if recorder.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status = %d, want 503", recorder.Code)
|
||||
}
|
||||
if store.createN != 0 {
|
||||
t.Fatalf("created %d jobs while gate is closed", store.createN)
|
||||
}
|
||||
var body struct {
|
||||
Code string `json:"code"`
|
||||
Reasons []string `json:"reasons"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("decode body: %v", err)
|
||||
}
|
||||
if body.Code != "library_sync_blocked" || len(body.Reasons) == 0 {
|
||||
t.Fatalf("unexpected refusal body: %s", recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// 即使闸口打开,缺少与目标快照绑定的预备份也必须拒绝。
|
||||
func TestLibrarySyncEnqueueRequiresPrebackupForTarget(t *testing.T) {
|
||||
store := &fakeLibraryStore{versions: []governance.LibraryVersion{{
|
||||
ID: "ver-prod-1", Scope: "internal", Kind: governance.LibraryVersionProductionEffective,
|
||||
SnapshotID: "prod-snap", VersionLabel: "prod-2026.10", CreatedAt: time.Now().UTC(),
|
||||
}}}
|
||||
config := Config{Store: store, ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: true,
|
||||
ProductionWeKnoraBaseURL: "http://prod.invalid"}
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/library/syncs",
|
||||
strings.NewReader(`{"scope":"internal","target_snapshot_id":"snap-1"}`))
|
||||
recorder := httptest.NewRecorder()
|
||||
librarySyncEnqueue(recorder, request, config)
|
||||
if recorder.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status = %d, want 503", recorder.Code)
|
||||
}
|
||||
if !strings.Contains(recorder.Body.String(), "预备份") {
|
||||
t.Fatalf("refusal must name the missing prebackup: %s", recorder.Body.String())
|
||||
}
|
||||
if store.createN != 0 {
|
||||
t.Fatalf("created %d jobs without a prebackup", store.createN)
|
||||
}
|
||||
}
|
||||
|
||||
// 三类版本必须分开读:production_effective 才是生产当前,测试封存不能顶替。
|
||||
func TestLibraryOverviewKeepsVersionKindsSeparate(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
store := &fakeLibraryStore{versions: []governance.LibraryVersion{
|
||||
{ID: "ver-test", Scope: "internal", Kind: governance.LibraryVersionTestSealed, SnapshotID: "snap-1", CreatedAt: now},
|
||||
{ID: "ver-prod", Scope: "internal", Kind: governance.LibraryVersionProductionEffective,
|
||||
SnapshotID: "prod-snap", VersionLabel: "prod-2026.10", CreatedAt: now},
|
||||
{ID: "ver-backup-other", Scope: "internal", Kind: governance.LibraryVersionProductionPrebackup,
|
||||
SnapshotID: "other-snap", CreatedAt: now},
|
||||
}}
|
||||
config := Config{Store: store, LibraryArchiveRoot: t.TempDir()}
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/library/overview?scope=internal", nil)
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryOverviewHandler(recorder, request, config)
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d: %s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
var body struct {
|
||||
Production libraryKnownView `json:"production"`
|
||||
Prebackup libraryKnownView `json:"prebackup"`
|
||||
Diff struct {
|
||||
State string `json:"state"`
|
||||
} `json:"diff"`
|
||||
Gate struct {
|
||||
SyncAllowed bool `json:"sync_allowed"`
|
||||
} `json:"gate"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("decode overview: %v", err)
|
||||
}
|
||||
if body.Production.State != "known" || body.Production.Version == nil || body.Production.Version.ID != "ver-prod" {
|
||||
t.Fatalf("production must be the effective version, got %+v", body.Production)
|
||||
}
|
||||
// 预备份只对匹配目标快照的那一份生效;这里目标为空(归档不可用),所以是未知。
|
||||
if body.Prebackup.State != "unknown" {
|
||||
t.Fatalf("prebackup must stay unknown without a matching target snapshot: %+v", body.Prebackup)
|
||||
}
|
||||
if body.Diff.State != "unknown" {
|
||||
t.Fatalf("diff must be unknown until the comparison is wired, got %q", body.Diff.State)
|
||||
}
|
||||
if body.Gate.SyncAllowed {
|
||||
t.Fatal("gate must stay closed")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user