为什么:只验证单阶段写入还不够,完整链路(授权→预备份→四类写入→校验)也要在真实服务上跑。 本机 WeKnora 测试栈一次性库与随机索引已通过,CI 无这些服务故在 test-scope 声明为可选。
This commit is contained in:
@@ -39,8 +39,7 @@ func TestLibraryProductionTargetIsolated(t *testing.T) {
|
||||
t.Fatalf("open isolated PG: %v", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
if _, err := pool.Exec(ctx, `DROP TABLE IF EXISTS knowledges,chunks,knowledge_tags,knowledge_tag_relations,
|
||||
resources,resource_bindings,wiki_pages,wiki_folders,wiki_page_issues,wiki_log_entries,knowledge_bases,schema_migrations CASCADE`); err != nil {
|
||||
if _, err := pool.Exec(ctx, `DROP SCHEMA public CASCADE; CREATE SCHEMA public`); err != nil {
|
||||
t.Fatalf("reset isolated schema: %v", err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, libraryTestSchema); err != nil {
|
||||
@@ -198,10 +197,117 @@ func TestLibraryProductionTargetIsolated(t *testing.T) {
|
||||
}
|
||||
|
||||
// fixtureObjectStore 只为让目标构造通过:本场景没有资源部件,不会被调用。
|
||||
// 端到端:授权 → 预备份 → 四类写入 → 校验,全走 libraryrelease.Run 的真实适配器。
|
||||
func TestLibraryReleaseRunIsolated(t *testing.T) {
|
||||
dsn, base := os.Getenv("KG_LIBRARY_TEST_DATABASE_URL"), os.Getenv("KG_LIBRARY_TEST_ELASTIC_URL")
|
||||
if dsn == "" || base == "" || os.Getenv("KG_LIBRARY_TEST_ISOLATED") != "1" {
|
||||
t.Skip("requires isolated PG and ES opt-in")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
|
||||
defer cancel()
|
||||
pool, err := pgxpool.New(ctx, dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("open isolated PG: %v", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
for _, statement := range []string{
|
||||
`DROP SCHEMA public CASCADE; CREATE SCHEMA public`,
|
||||
libraryTestSchema,
|
||||
`ALTER TABLE wiki_pages ADD COLUMN status text DEFAULT 'published'`,
|
||||
`ALTER TABLE knowledge_tag_relations ADD COLUMN tenant_id bigint`,
|
||||
`ALTER TABLE resources ADD COLUMN provider text DEFAULT 's3', ADD COLUMN size bigint DEFAULT 8`,
|
||||
} {
|
||||
if _, err := pool.Exec(ctx, statement); err != nil {
|
||||
t.Fatalf("fixture schema: %v", err)
|
||||
}
|
||||
}
|
||||
token := make([]byte, 8)
|
||||
if _, err := rand.Read(token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
index := "kg_library_test_" + hex.EncodeToString(token)
|
||||
client := &http.Client{Timeout: 20 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
}}
|
||||
call := func(method, path, body string, want int) {
|
||||
t.Helper()
|
||||
request, err := http.NewRequestWithContext(ctx, method, base+path, strings.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
t.Fatalf("fixture HTTP: %v", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != want {
|
||||
t.Fatalf("fixture %s %s => %d, want %d", method, path, response.StatusCode, want)
|
||||
}
|
||||
}
|
||||
call("PUT", "/"+index, `{"mappings":{"properties":{"tenant_id":{"type":"long"},"knowledge_base_id":{"type":"keyword"},"knowledge_id":{"type":"keyword"},"chunk_id":{"type":"keyword"},"content":{"type":"text"},"embedding":{"type":"dense_vector","dims":2}}}}`, 200)
|
||||
defer func() {
|
||||
call("DELETE", "/"+index, "", 200)
|
||||
call("HEAD", "/"+index, "", 404)
|
||||
}()
|
||||
archiveRoot := t.TempDir()
|
||||
if err := os.Chmod(archiveRoot, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
archive, err := librarysnapshot.Open(archiveRoot, 64*1024*1024)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer archive.Close()
|
||||
snapshotID := sealTargetFixture(t, archive, "internal", []string{"doc-safe"}, []string{"chunk-safe"}, "wiki-safe")
|
||||
// 生产"当前状态"也要自洽:库里有分块,索引里就必须有对应文档,
|
||||
// 否则预备份的覆盖校验会(正确地)拒绝。
|
||||
call("PUT", "/"+index+"/_doc/chunk-safe?refresh=true",
|
||||
`{"tenant_id":10000,"knowledge_base_id":"kb-safe","knowledge_id":"doc-safe","chunk_id":"chunk-safe","source_id":"chunk-safe","content":"chunk","embedding":[0.1,0.2]}`, 201)
|
||||
// 库里已有夹具种子行(doc-safe/chunk-safe),只需保证索引侧自洽。
|
||||
|
||||
target, err := NewProductionLibraryTarget(ctx, ProductionLibraryConfig{
|
||||
Postgres: dsn, SearchURL: base, SearchIndex: index, Objects: fixtureObjectStore{},
|
||||
ObjectsReader: fixtureObjectStore{}, ArchiveRoot: archiveRoot, MaxDocuments: 100, HTTPClient: client,
|
||||
}, ProductionScope{TenantID: 10000, KnowledgeBaseID: "kb-safe"})
|
||||
if err != nil {
|
||||
t.Fatalf("target: %v", err)
|
||||
}
|
||||
defer target.Close()
|
||||
mapping, err := target.Search().MappingSHA256(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
backups, err := NewProductionBackupStore(ProductionBackupConfig{
|
||||
Pool: target.Pool(), Search: target.Search(), Objects: fixtureObjectStore{}, ArchiveRoot: archiveRoot,
|
||||
Scope: "internal", Identity: target.Identity(), MaxDocuments: 100,
|
||||
Compatibility: librarysnapshot.Compatibility{WeKnoraVersion: "v0.7.1", SchemaVersion: 74,
|
||||
RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "text-embedding-v4",
|
||||
EmbeddingDimension: 1024, IndexMappingSHA256: mapping},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
request := libraryrelease.Request{Target: "internal", SnapshotID: snapshotID,
|
||||
ParamsDigest: "digest-e2e", Now: now,
|
||||
Grant: libraryrelease.Grant{Target: "internal", SnapshotID: snapshotID, ParamsDigest: "digest-e2e",
|
||||
ExpiresAt: now.Add(10 * time.Minute), AuthorizedBy: "acceptance"}}
|
||||
outcome := libraryrelease.Run(ctx, request, target, backups, nil)
|
||||
if outcome.State != "succeeded" {
|
||||
t.Fatalf("run outcome = %+v", outcome)
|
||||
}
|
||||
if outcome.Backup.SnapshotID == "" {
|
||||
t.Fatalf("run must create a backup first: %+v", outcome)
|
||||
}
|
||||
t.Logf("e2e run succeeded snapshot=%s backup=%s bytes=%d", snapshotID, outcome.Backup.SnapshotID, outcome.Backup.Bytes)
|
||||
}
|
||||
|
||||
type fixtureObjectStore struct{}
|
||||
|
||||
// Open 返回与夹具资源行 size=8 一致的字节,供预备份读取对象。
|
||||
func (fixtureObjectStore) Open(context.Context, string, string) (io.ReadCloser, error) {
|
||||
return nil, errors.New("fixture object store is read-only here")
|
||||
return io.NopCloser(strings.NewReader("12345678")), nil
|
||||
}
|
||||
|
||||
func (fixtureObjectStore) Put(context.Context, string, string, io.Reader, int64) error {
|
||||
|
||||
Reference in New Issue
Block a user