119 lines
5.1 KiB
Go
119 lines
5.1 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/governance"
|
|
"knowledge-governance/internal/platform/postgres"
|
|
)
|
|
|
|
func resumeRequest(id string) *http.Request {
|
|
request := httptest.NewRequest(http.MethodPost, "/api/v1/library/jobs/"+id+"/resume", nil)
|
|
request.SetPathValue("id", id)
|
|
return request.WithContext(context.WithValue(request.Context(), sessionKey{},
|
|
sessionIdentity{Username: "admin", Role: roleAdmin}))
|
|
}
|
|
|
|
func resumableStore(job governance.LibraryJob) *fakeLibraryStore {
|
|
now := time.Now().UTC()
|
|
digest, _ := librarySyncParamsDigest(job.Scope, job.TargetSnapshotID)
|
|
if job.Kind == governance.LibraryJobRestore {
|
|
digest, _ = libraryRestoreParamsDigest(job.Scope, job.TargetSnapshotID)
|
|
}
|
|
store := &fakeLibraryStore{
|
|
jobs: []governance.LibraryJob{job},
|
|
grant: &governance.LibraryReleaseAuthorization{ID: "a", Scope: job.Scope,
|
|
SnapshotID: job.TargetSnapshotID, ParamsDigest: digest, ExpiresAt: now.Add(time.Hour), CreatedAt: now},
|
|
}
|
|
store.resumeOK = true
|
|
return store
|
|
}
|
|
|
|
func resumeConfig(store *fakeLibraryStore) Config {
|
|
return Config{Store: store, ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: true}
|
|
}
|
|
|
|
// 合法续作:202,并且只投递一次。
|
|
func TestR26ResumeAcceptedDeliversOnce(t *testing.T) {
|
|
job := governance.LibraryJob{ID: "job-ok", Scope: "internal", Kind: governance.LibraryJobSync,
|
|
State: governance.LibraryJobFailed, Stage: "resources", TargetSnapshotID: "snap-1"}
|
|
store := resumableStore(job)
|
|
recorder := httptest.NewRecorder()
|
|
libraryJobResume(recorder, resumeRequest("job-ok"), resumeConfig(store))
|
|
if recorder.Code != http.StatusAccepted {
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if len(store.resumeCalls) != 1 || store.resumeCalls[0] != "job-ok/library.sync" {
|
|
t.Fatalf("resume calls=%v", store.resumeCalls)
|
|
}
|
|
}
|
|
|
|
// 回滚阶段的作业:409,且文案不能一律宣称"仍持有维护窗口"(窗口可能已释放)。
|
|
func TestR26ResumeRollbackStageRequiresRecover(t *testing.T) {
|
|
job := governance.LibraryJob{ID: "job-rb", Scope: "internal", Kind: governance.LibraryJobRestore,
|
|
State: governance.LibraryJobFailed, Stage: "rollback", TargetSnapshotID: "snap-1"}
|
|
store := resumableStore(job)
|
|
store.resumeErr = postgres.ErrLibraryResumeRequiresRecover
|
|
recorder := httptest.NewRecorder()
|
|
libraryJobResume(recorder, resumeRequest("job-rb"), resumeConfig(store))
|
|
if recorder.Code != http.StatusConflict || !strings.Contains(recorder.Body.String(), "library_resume_requires_recover") {
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if strings.Contains(recorder.Body.String(), "保留维护窗口") {
|
|
t.Fatalf("must not always claim the window is held: %s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
// 租约仍有效:409 busy,不是 500。
|
|
func TestR26ResumeBusyIsConflict(t *testing.T) {
|
|
job := governance.LibraryJob{ID: "job-busy", Scope: "internal", Kind: governance.LibraryJobSync,
|
|
State: governance.LibraryJobFailed, Stage: "resources", TargetSnapshotID: "snap-1"}
|
|
store := resumableStore(job)
|
|
store.resumeErr = postgres.ErrLibraryResumeBusy
|
|
recorder := httptest.NewRecorder()
|
|
libraryJobResume(recorder, resumeRequest("job-busy"), resumeConfig(store))
|
|
if recorder.Code != http.StatusConflict || !strings.Contains(recorder.Body.String(), "library_resume_busy") {
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
// 入队失败(存储层事务回滚):500,且不得把作业当成已续作。
|
|
func TestR26ResumeEnqueueFailureIsServerError(t *testing.T) {
|
|
job := governance.LibraryJob{ID: "job-fail", Scope: "internal", Kind: governance.LibraryJobRestore,
|
|
State: governance.LibraryJobFailed, Stage: "resources", TargetSnapshotID: "snap-1"}
|
|
store := resumableStore(job)
|
|
store.resumeErr = errors.New("enqueue resumed job: injected")
|
|
recorder := httptest.NewRecorder()
|
|
libraryJobResume(recorder, resumeRequest("job-fail"), resumeConfig(store))
|
|
if recorder.Code != http.StatusInternalServerError {
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if strings.Contains(recorder.Body.String(), "resumed") {
|
|
t.Fatalf("failed enqueue must not claim success: %s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
// 重复请求:第二次被存储层拒绝(状态已推进),不会重复投递。
|
|
func TestR26ResumeDuplicateRequestDoesNotRedeliver(t *testing.T) {
|
|
job := governance.LibraryJob{ID: "job-dup", Scope: "internal", Kind: governance.LibraryJobSync,
|
|
State: governance.LibraryJobFailed, Stage: "resources", TargetSnapshotID: "snap-1"}
|
|
store := resumableStore(job)
|
|
first := httptest.NewRecorder()
|
|
libraryJobResume(first, resumeRequest("job-dup"), resumeConfig(store))
|
|
store.resumeOK = false // 第二次:业务作业已不是 failed
|
|
second := httptest.NewRecorder()
|
|
libraryJobResume(second, resumeRequest("job-dup"), resumeConfig(store))
|
|
if first.Code != http.StatusAccepted || second.Code != http.StatusConflict {
|
|
t.Fatalf("first=%d second=%d body=%s", first.Code, second.Code, second.Body.String())
|
|
}
|
|
if len(store.resumeCalls) != 2 {
|
|
t.Fatalf("expected 2 store calls, got %v", store.resumeCalls)
|
|
}
|
|
}
|