5e1482e349
build / build (push) Successful in 1m40s
根因:生产可恢复性只有 PRODUCTION_BACKUP_RESTORE_VERIFIED 这个手填 env 开关, 页面没有任何入口去真跑一次恢复;CLI(librarydrill)又不是 worker 能复用的实现, 所以"已验证"既证明不了跑过、也证明不了跑的是当前目标。 改动: - 共享实现 internal/libraryverify(维护隔离→捕获→基线校验→canary→恢复→复核→关窗口), cmd/librarydrill 改为薄封装,CLI 与 worker 只允许一份执行体。 - verify 成为独立业务作业:kind + VerifyStages(capture/verify/canary/restore/finalize), 与 sync/restore/baseline 共用同一把写互斥;迁移 79 加 kind 约束、重建部分唯一索引、 新建 library_restore_verifications(按 target_id 绑定,换库/索引/桶旧证据自动失效, 同 target+scope+job 幂等)。 - worker:admitLibraryJob + runOwnedLibraryAttempt 之后调用共享实现,阶段实时写 library_job_stages,结束后写真实终态;通过写 passed 证据,失败写 failed+原因(不伪成功); 中断接管按同一作业 id 收掉维护窗口。 - API:POST/GET /api/v1/library/verifications(管理员;闸口关闭/维护中拒绝;返回完整 整库目标、最近证据、失败原因),OpenAPI 同步。 - 闸口:同步/恢复/续作的证据判断改为读当前目标证据;历史 env 仅在证据缺失时兜底, 证据存在时以证据为准。 - 页面:生产保护与恢复面板加「恢复验证」块(完整 KB/索引/桶、状态、最近证据、失败原因、 管理员按钮、5s 轮询),作业类型补 verify 文案。 测试:make verify 16/16 全绿(go 1387 例 + web 454 例 + 契约/迁移/密钥/依赖锁定); 新增用例覆盖证据四条件、闸口证据优先、页面无证据/失败/通过三种呈现与管理员可见性。
228 lines
9.9 KiB
Go
228 lines
9.9 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/governance"
|
|
"knowledge-governance/internal/platform/weknora"
|
|
)
|
|
|
|
type productionBindingStoreFake struct {
|
|
internal, external, saver string
|
|
savedInternal, savedExternal string
|
|
saveCallCount int
|
|
}
|
|
|
|
func (fake *productionBindingStoreFake) LoadProductionKBBindings(context.Context) (string, string, string, error) {
|
|
return fake.internal, fake.external, fake.saver, nil
|
|
}
|
|
|
|
func (fake *productionBindingStoreFake) SaveProductionKBBindings(_ context.Context, internal, external, updatedBy string, _ time.Time) error {
|
|
fake.savedInternal, fake.savedExternal = internal, external
|
|
fake.saver = updatedBy
|
|
fake.saveCallCount++
|
|
fake.internal, fake.external = internal, external
|
|
return nil
|
|
}
|
|
|
|
type productionListerFake struct {
|
|
bases []weknora.KnowledgeBase
|
|
err error
|
|
baseURL string
|
|
}
|
|
|
|
func (fake *productionListerFake) ListProductionKnowledgeBases(_ context.Context, baseURL string) ([]weknora.KnowledgeBase, error) {
|
|
fake.baseURL = baseURL
|
|
return fake.bases, fake.err
|
|
}
|
|
|
|
func productionBindingsHandler(t *testing.T, store ProductionBindingStore, lister ProductionKnowledgeBaseLister, settingsURL string) http.Handler {
|
|
t.Helper()
|
|
jobStore := &fakeJobStore{}
|
|
jobStore.activeUISettingsResult = governance.UISettings{ProductionWeKnoraBaseURL: settingsURL}
|
|
handler, err := NewHandler(Config{
|
|
Store: jobStore,
|
|
ProductionBindings: store, ProductionKnowledgeBases: lister,
|
|
Now: func() time.Time { return time.Date(2026, time.October, 8, 8, 0, 0, 0, time.UTC) },
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewHandler: %v", err)
|
|
}
|
|
return handler
|
|
}
|
|
|
|
func productionBases() []weknora.KnowledgeBase {
|
|
return []weknora.KnowledgeBase{
|
|
{ID: "kb-prod-internal", Name: "对内知识库-wiki", KnowledgeCount: 2069},
|
|
{ID: "kb-prod-external", Name: "对外知识库-wiki", KnowledgeCount: 526},
|
|
}
|
|
}
|
|
|
|
// The picker reads the production endpoint from 「服务配置 · 生产地址」 and reports
|
|
// the bound bases by NAME — ids never reach the page.
|
|
func TestProductionBindingsReadUsesServiceConfiguredEndpoint(t *testing.T) {
|
|
store := &productionBindingStoreFake{internal: "kb-prod-internal"}
|
|
lister := &productionListerFake{bases: productionBases()}
|
|
handler := productionBindingsHandler(t, store, lister, "https://weknora-test.dobot.cn/")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/production-kb-bindings", nil))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if lister.baseURL != "https://weknora-test.dobot.cn/" {
|
|
t.Fatalf("lister baseURL = %q, want the operator-configured production address", lister.baseURL)
|
|
}
|
|
body := recorder.Body.String()
|
|
if !strings.Contains(body, "对内知识库-wiki") || !strings.Contains(body, `"production_connected":true`) {
|
|
t.Fatalf("body = %s", body)
|
|
}
|
|
}
|
|
|
|
// A chosen base that does not exist on the production endpoint must be refused
|
|
// before anything is written.
|
|
func TestProductionBindingsSaveRejectsUnknownBase(t *testing.T) {
|
|
store := &productionBindingStoreFake{}
|
|
handler := productionBindingsHandler(t, store, &productionListerFake{bases: productionBases()}, "https://weknora-test.dobot.cn/")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPut, "/api/v1/production-kb-bindings",
|
|
strings.NewReader(`{"internal_kb_id":"kb-ghost","external_kb_id":""}`)))
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if store.saveCallCount != 0 {
|
|
t.Fatal("a rejected binding must not be stored")
|
|
}
|
|
}
|
|
|
|
func TestProductionBindingsSaveStoresValidatedPair(t *testing.T) {
|
|
store := &productionBindingStoreFake{}
|
|
handler := productionBindingsHandler(t, store, &productionListerFake{bases: productionBases()}, "https://weknora-test.dobot.cn/")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPut, "/api/v1/production-kb-bindings",
|
|
strings.NewReader(`{"internal_kb_id":"kb-prod-internal","external_kb_id":"kb-prod-external"}`)))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if store.savedInternal != "kb-prod-internal" || store.savedExternal != "kb-prod-external" {
|
|
t.Fatalf("saved = %q/%q", store.savedInternal, store.savedExternal)
|
|
}
|
|
var envelope struct {
|
|
Data productionBindingView `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &envelope); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if envelope.Data.InternalKBName != "对内知识库-wiki" || !envelope.Data.ProductionConnected {
|
|
t.Fatalf("view = %#v", envelope.Data)
|
|
}
|
|
}
|
|
|
|
// A jittery production endpoint must not block the operator, but the response
|
|
// must say the binding could not be checked.
|
|
func TestProductionBindingsSaveWarnsWhenEndpointUnavailable(t *testing.T) {
|
|
store := &productionBindingStoreFake{}
|
|
handler := productionBindingsHandler(t, store, &productionListerFake{err: errors.New("dial tcp: timeout")}, "https://weknora-test.dobot.cn/")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPut, "/api/v1/production-kb-bindings",
|
|
strings.NewReader(`{"internal_kb_id":"kb-prod-internal","external_kb_id":""}`)))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if store.saveCallCount != 1 {
|
|
t.Fatal("an unverifiable (not invalid) binding must still be stored")
|
|
}
|
|
var envelope struct {
|
|
Data productionBindingView `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &envelope); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if envelope.Data.ProductionConnected || !strings.Contains(envelope.Data.ProductionMessage, "不可用") {
|
|
t.Fatalf("view = %#v", envelope.Data)
|
|
}
|
|
}
|
|
|
|
// 对内 and 对外 must not point at the same production base.
|
|
func TestProductionBindingsSaveRejectsIdenticalBases(t *testing.T) {
|
|
store := &productionBindingStoreFake{}
|
|
handler := productionBindingsHandler(t, store, &productionListerFake{bases: productionBases()}, "https://weknora-test.dobot.cn/")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPut, "/api/v1/production-kb-bindings",
|
|
strings.NewReader(`{"internal_kb_id":"kb-prod-internal","external_kb_id":"kb-prod-internal"}`)))
|
|
if recorder.Code != http.StatusBadRequest || store.saveCallCount != 0 {
|
|
t.Fatalf("status = %d saves = %d", recorder.Code, store.saveCallCount)
|
|
}
|
|
}
|
|
|
|
func TestProductionBindingOptionsReportUnavailableReason(t *testing.T) {
|
|
handler := productionBindingsHandler(t, &productionBindingStoreFake{}, &productionListerFake{err: errors.New("no key")}, "https://weknora-test.dobot.cn/")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/production-kb-bindings/options", nil))
|
|
if recorder.Code != http.StatusBadGateway {
|
|
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
recorder = httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/production-kb-bindings/options", nil))
|
|
// and with a working endpoint the options carry names, not just ids
|
|
handler = productionBindingsHandler(t, &productionBindingStoreFake{}, &productionListerFake{bases: productionBases()}, "https://weknora-test.dobot.cn/")
|
|
recorder = httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/production-kb-bindings/options", nil))
|
|
if recorder.Code != http.StatusOK || !strings.Contains(recorder.Body.String(), "对外知识库-wiki") {
|
|
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
// 页面必须说清"整库发布真正写哪个 KB/端点"。这条投影只能来自部署配置,
|
|
// 不能从可编辑的文档级绑定推出来,也不能把口令带进响应。
|
|
func TestLibraryPublishTargetIsDerivedFromDeploymentConfig(t *testing.T) {
|
|
config := Config{
|
|
// 只放形状,不放任何可用凭据:口令段是占位符,主机是 .invalid。
|
|
LibraryProductionDatabaseURL: "postgres://governance@db.invalid:5432/weknora_test?sslmode=disable",
|
|
LibraryProductionInternalKB: "a8e9c8ad-411c-4c53-8421-599d29e9b179",
|
|
LibraryProductionExternalKB: "2e980686-2233-4c4e-b5a6-8b0069477b8e",
|
|
LibraryProductionTenantID: 10000,
|
|
LibraryProductionIndex: "weknora_test_chunks",
|
|
LibraryProductionS3Bucket: "dobot-weknora-test",
|
|
}
|
|
target := config.libraryPublishTarget()
|
|
if !target.Configured || target.MissingReason != "" {
|
|
t.Fatalf("完整配置应判为已配置: %+v", target)
|
|
}
|
|
if target.InternalKBID != "a8e9c8ad-411c-4c53-8421-599d29e9b179" || target.TenantID != 10000 {
|
|
t.Fatalf("必须回显实际整库目标而不是占位: %+v", target)
|
|
}
|
|
if target.Database != "db.invalid:5432/weknora_test" {
|
|
t.Fatalf("端点应只含 host:port/db,得到 %q", target.Database)
|
|
}
|
|
// 回显只能是端点:连接串里的用户信息(口令、账号)一个字符都不能进响应。
|
|
if strings.Contains(target.Database, "@") || strings.Contains(target.Source, "@") ||
|
|
strings.Contains(target.Source, "postgres://") {
|
|
t.Fatalf("响应里不能出现连接串用户信息: %+v", target)
|
|
}
|
|
if target.ElasticIndex != "weknora_test_chunks" || target.ObjectBucket != "dobot-weknora-test" {
|
|
t.Fatalf("索引与桶也要显示: %+v", target)
|
|
}
|
|
}
|
|
|
|
func TestLibraryPublishTargetNamesMissingConfigInsteadOfGuessing(t *testing.T) {
|
|
target := Config{}.libraryPublishTarget()
|
|
if target.Configured {
|
|
t.Fatal("缺配置时不能报告已配置")
|
|
}
|
|
for _, key := range []string{"LIBRARY_PRODUCTION_DATABASE_URL", "LIBRARY_PRODUCTION_KB_INTERNAL"} {
|
|
if !strings.Contains(target.MissingReason, key) {
|
|
t.Fatalf("缺失说明应点名 %s,得到 %q", key, target.MissingReason)
|
|
}
|
|
}
|
|
if target.InternalKBID != "" || target.Database != "(未配置)" {
|
|
t.Fatalf("缺配置就不要编造目标: %+v", target)
|
|
}
|
|
}
|