Files
knowledge-governance/internal/libraryhandoff/handoff_test.go
T
姚章浩 6400fd1bbe feat(library): 快照搬运先于维护、双向校验与云端版本回传
为什么改:云端 release-executor 与目标同机后,本机封存的快照必须"先到、先验证"
才允许开维护窗口。此前只有 pack/unpack 两条人工命令:UI 点发布时执行器读到的是
本机归档路径,云端根本没有这份快照,作业只能在装配阶段失败;而预备份落在云端,
本机页面也读不到。用户已批准补齐这条链路。

搬运(新包 internal/libraryhandoff):本机 Load 逐 blob 重算 → Pack → SSH 流式写入
云端暂存 → 原子改名 → 云端 snapshot-unpack 再逐 blob 校验 → 读回版本清单摘要自证 →
scope/tenant/知识库身份与治理库作业一致。只有全部通过才投递 library.sync|restore,
失败时发布作业根本不存在(目标不进维护、不写知识)。远端命令是固定 argv,容器名与
路径只来自部署配置;同 ID 异内容拒绝;同一快照本机 flock 串行化;暂存 bundle 用后即删,
归档本体永不自动清理。

回传:capture 角色按已成功作业的 snapshot_id 扫描,把本机缺失的云端快照 Fetch 回来
(云端 pack → SSH 流回 → 本机 Unpack 逐 blob 校验)。回传成功后 API/页面既有的列表、
详情、差异、存储与历史恢复路径读到的就是真实字节;失败则版本不出现在本机,不会被误报成可恢复。

角色:搬运与回传只注册在 capture;若接线到 full(同时消费发布作业)启动即失败,
避免与云端执行器抢同一批作业。release-executor 仍不拥有治理库 DDL,也不消费
QA/邮件/来源扫描。

验证:internal/libraryhandoff 全绿(跨进程 push/fetch、身份不符拒绝、幂等复用、
并发串行、篡改与未知快照不落版本、路径穿越拒绝、远端错误不当作"没有");
cmd/worker 在隔离 fixture 库(独立测试 PG)上验证"校验通过才入队发布、失败不入队";
httpapi 队列决策单测;相关包 go test 全绿;go vet 全仓通过。

未部署、未推送、未构建镜像:云端接线与验收仍由父执行。真实云链路(含 SSH 通道)
本轮没有云侧写操作,只有本机跨进程与隔离 fixture 证据。
2026-10-30 18:58:34 +08:00

357 lines
12 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package libraryhandoff
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"os"
"os/exec"
"path/filepath"
"sync"
"testing"
"time"
"knowledge-governance/internal/librarysnapshot"
)
var libraryctlBinary string
// TestMain 构建真正的 libraryctl 二进制:local 通道的"云端"动作必须跨进程执行,
// 否则"跨进程证据"只是同一次调用里的函数调用。
func TestMain(m *testing.M) {
dir, err := os.MkdirTemp("", "handoff-bin-")
if err != nil {
panic(err)
}
binary := filepath.Join(dir, "libraryctl")
build := exec.Command("go", "build", "-o", binary, "knowledge-governance/cmd/libraryctl")
build.Stderr = os.Stderr
if err := build.Run(); err != nil {
panic("build libraryctl: " + err.Error())
}
libraryctlBinary = binary
code := m.Run()
os.RemoveAll(dir)
os.Exit(code)
}
const (
fixtureTenant = 10000
fixtureKB = "kb-handoff-fixture"
)
func newArchive(t *testing.T) (string, *librarysnapshot.Archive) {
t.Helper()
root := t.TempDir()
if err := os.Chmod(root, 0700); err != nil {
t.Fatal(err)
}
archive, err := librarysnapshot.Open(root, 1024*1024)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { archive.Close() })
return root, archive
}
func seal(t *testing.T, archive *librarysnapshot.Archive) string {
t.Helper()
part, err := archive.Put(bytes.NewBufferString("[]"))
if err != nil {
t.Fatal(err)
}
mapping := sha256.Sum256([]byte("fixture-mapping"))
manifest := librarysnapshot.Manifest{
Format: librarysnapshot.Format, Scope: "internal", TenantID: fixtureTenant,
KnowledgeBaseID: fixtureKB, CreatedAt: time.Date(2026, 10, 30, 0, 0, 0, 0, time.UTC),
Compatibility: librarysnapshot.Compatibility{
WeKnoraVersion: "v0.7.1", SchemaVersion: 74, RetrieverEngine: "elasticsearch_v8",
EmbeddingModel: "fixture-model", EmbeddingDimension: 1024,
IndexMappingSHA256: hex.EncodeToString(mapping[:]),
},
}
for _, name := range []string{"knowledges", "chunks", "resources", "resource_bindings", "knowledge_tags", "knowledge_tag_relations"} {
current := part
current.Kind, current.Name = "database", name
manifest.Parts = append(manifest.Parts, current)
}
for _, name := range []string{"wiki_pages", "wiki_folders", "wiki_page_issues", "wiki_log_entries"} {
current := part
current.Kind, current.Name = "wiki", name
manifest.Parts = append(manifest.Parts, current)
}
index := part
index.Kind, index.Name = "index", "chunks"
manifest.Parts = append(manifest.Parts, index)
id, err := archive.Seal(manifest)
if err != nil {
t.Fatal(err)
}
return id
}
// fixture 在同一个临时根下模拟"云端主机":bundles/ 是暂存,archive/ 是执行器归档根。
type fixture struct {
root string
cfg Config
remoteA string
}
func newFixture(t *testing.T, localArchive string) fixture {
t.Helper()
root := t.TempDir()
remoteArchive := filepath.Join(root, "archive")
staging := filepath.Join(root, "staging")
for _, dir := range []string{remoteArchive, staging, filepath.Join(root, "bundles")} {
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatal(err)
}
}
t.Setenv("LIBRARY_HANDOFF_LIBRARYCTL", libraryctlBinary)
return fixture{root: root, remoteA: remoteArchive, cfg: Config{
Transport: TransportLocal, LocalRoot: root,
ExecutorContainer: "fixture-executor",
BundleDir: filepath.Join(root, "bundles"),
RemoteArchive: remoteArchive,
LocalArchive: localArchive,
StagingDir: staging,
MaxBundleBytes: 8 << 20,
OpTimeout: 60 * time.Second,
}}
}
// withLocalArchive 复用**同一台云端 fixture**(同一 root),只换本机归档根:
// 读回必须打到"发布执行器所在的那台机器"的归档,而不是另起一份空目录。
func withLocalArchive(t *testing.T, up fixture, localArchive string) Config {
t.Helper()
staging := filepath.Join(t.TempDir(), "staging")
if err := os.MkdirAll(staging, 0700); err != nil {
t.Fatal(err)
}
config := up.cfg
config.LocalArchive = localArchive
config.StagingDir = staging
return config
}
func TestPushThenFetchKeepsIdentityAndBytesAcrossProcesses(t *testing.T) {
sourceRoot, source := newArchive(t)
id := seal(t, source)
up := newFixture(t, sourceRoot)
pusher, err := New(up.cfg, nil)
if err != nil {
t.Fatal(err)
}
want := Identity{SnapshotID: id, Scope: "internal", TenantID: fixtureTenant, KnowledgeBaseID: fixtureKB}
pushed, err := pusher.Push(context.Background(), want)
if err != nil {
t.Fatalf("Push() error = %v", err)
}
if pushed.Reused || pushed.Parts != 11 || pushed.Snapshot != id {
t.Fatalf("push evidence = %#v", pushed)
}
// 云端确实落了版本清单,且摘要自证。
raw, err := os.ReadFile(filepath.Join(up.remoteA, "versions", id+".json"))
if err != nil {
t.Fatalf("remote manifest missing: %v", err)
}
sum := sha256.Sum256(raw)
if hex.EncodeToString(sum[:]) != id {
t.Fatal("remote manifest digest mismatch")
}
// 暂存目录不残留传输 bundle。
if entries, _ := os.ReadDir(up.cfg.BundleDir); len(entries) != 0 {
t.Fatalf("bundle staging not cleaned: %v", entries)
}
// 另一台"本机"取回:跨目录 + 跨进程(local 通道执行真正的 libraryctl)。
destinationRoot, _ := newArchive(t)
fetcher, err := New(withLocalArchive(t, up, destinationRoot), nil)
if err != nil {
t.Fatal(err)
}
fetched, err := fetcher.Fetch(context.Background(), id)
if err != nil {
t.Fatalf("Fetch() error = %v", err)
}
if fetched.Snapshot != id || fetched.Parts != 11 || fetched.KnowledgeBase != fixtureKB {
t.Fatalf("fetch evidence = %#v", fetched)
}
verify, err := librarysnapshot.Open(destinationRoot, 1024*1024)
if err != nil {
t.Fatal(err)
}
defer verify.Close()
if _, err := verify.Load(id); err != nil {
t.Fatalf("fetched snapshot does not verify: %v", err)
}
}
func TestPushRejectsIdentityThatDoesNotMatchTheSnapshot(t *testing.T) {
sourceRoot, source := newArchive(t)
id := seal(t, source)
up := newFixture(t, sourceRoot)
pusher, err := New(up.cfg, nil)
if err != nil {
t.Fatal(err)
}
wrong := Identity{SnapshotID: id, Scope: "internal", TenantID: fixtureTenant, KnowledgeBaseID: "kb-other"}
if _, err := pusher.Push(context.Background(), wrong); err == nil {
t.Fatal("Push() accepted a snapshot whose KB does not match the request")
}
if _, err := os.Stat(filepath.Join(up.remoteA, "versions", id+".json")); !errors.Is(err, os.ErrNotExist) {
t.Fatal("a rejected push left a version on the executor")
}
}
func TestPushIsIdempotentAndRejectsMissingLocalSnapshot(t *testing.T) {
sourceRoot, source := newArchive(t)
id := seal(t, source)
up := newFixture(t, sourceRoot)
pusher, err := New(up.cfg, nil)
if err != nil {
t.Fatal(err)
}
want := Identity{SnapshotID: id, Scope: "internal", TenantID: fixtureTenant, KnowledgeBaseID: fixtureKB}
if _, err := pusher.Push(context.Background(), want); err != nil {
t.Fatal(err)
}
again, err := pusher.Push(context.Background(), want)
if err != nil || !again.Reused {
t.Fatalf("second push = %#v, %v (want reused)", again, err)
}
missing := Identity{SnapshotID: hex.EncodeToString(bytes.Repeat([]byte{7}, 32)), Scope: "internal",
TenantID: fixtureTenant, KnowledgeBaseID: fixtureKB}
if _, err := pusher.Push(context.Background(), missing); err == nil {
t.Fatal("Push() accepted a snapshot that does not exist locally")
}
}
func TestConcurrentPushOfSameSnapshotSerialises(t *testing.T) {
sourceRoot, source := newArchive(t)
id := seal(t, source)
up := newFixture(t, sourceRoot)
pusher, err := New(up.cfg, nil)
if err != nil {
t.Fatal(err)
}
want := Identity{SnapshotID: id, Scope: "internal", TenantID: fixtureTenant, KnowledgeBaseID: fixtureKB}
var wg sync.WaitGroup
errs := make([]error, 3)
for i := 0; i < 3; i++ {
wg.Add(1)
go func(index int) {
defer wg.Done()
_, errs[index] = pusher.Push(context.Background(), want)
}(i)
}
wg.Wait()
for index, err := range errs {
if err != nil {
t.Fatalf("concurrent push %d failed: %v", index, err)
}
}
}
func TestFetchRefusesTamperedRemoteArchive(t *testing.T) {
sourceRoot, source := newArchive(t)
id := seal(t, source)
up := newFixture(t, sourceRoot)
pusher, err := New(up.cfg, nil)
if err != nil {
t.Fatal(err)
}
if _, err := pusher.Push(context.Background(), Identity{SnapshotID: id, Scope: "internal",
TenantID: fixtureTenant, KnowledgeBaseID: fixtureKB}); err != nil {
t.Fatal(err)
}
// 篡改云端一个 blob:云端 libraryctl snapshot-pack 会在 Load 时逐 blob 重算并失败。
blobs, err := os.ReadDir(filepath.Join(up.remoteA, "blobs"))
if err != nil || len(blobs) == 0 {
t.Fatalf("remote blobs = %v, %v", blobs, err)
}
target := filepath.Join(up.remoteA, "blobs", blobs[0].Name())
if err := os.Chmod(target, 0600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(target, []byte("tampered"), 0600); err != nil {
t.Fatal(err)
}
destinationRoot, _ := newArchive(t)
fetcher, err := New(withLocalArchive(t, up, destinationRoot), nil)
if err != nil {
t.Fatal(err)
}
if _, err := fetcher.Fetch(context.Background(), id); err == nil {
t.Fatal("Fetch() imported a tampered snapshot")
}
if _, err := os.Stat(filepath.Join(destinationRoot, "versions", id+".json")); !errors.Is(err, os.ErrNotExist) {
t.Fatal("a failed fetch left a version locally")
}
}
func TestFetchRefusesUnknownRemoteSnapshotWithoutClaimingRecoverable(t *testing.T) {
destinationRoot, _ := newArchive(t)
empty := newFixture(t, destinationRoot)
fetcher, err := New(empty.cfg, nil)
if err != nil {
t.Fatal(err)
}
unknown := hex.EncodeToString(bytes.Repeat([]byte{9}, 32))
if _, err := fetcher.Fetch(context.Background(), unknown); err == nil {
t.Fatal("Fetch() reported success for a snapshot that exists nowhere")
}
if _, err := os.Stat(filepath.Join(destinationRoot, "versions", unknown+".json")); !errors.Is(err, os.ErrNotExist) {
t.Fatal("unknown snapshot produced a local version")
}
}
func TestHandoffRejectsCallerControlledNamesAndPaths(t *testing.T) {
if _, err := bundleName("../../etc/passwd"); err == nil {
t.Fatal("bundleName accepted a traversal name")
}
if _, err := bundleName("ABCDEF"); err == nil {
t.Fatal("bundleName accepted a non-digest name")
}
if _, err := New(Config{Transport: TransportSSH, SSHTarget: "x", SSHConfig: "/tmp/x"}, nil); err == nil {
t.Fatal("New() accepted an incomplete SSH configuration")
}
if _, err := New(Config{Transport: TransportLocal}, nil); err == nil {
t.Fatal("New() accepted local transport without an isolation root")
}
}
// 断线/超时:远端读版本清单一直失败必须报错,不能当成"没有版本"然后继续搬。
func TestPushSurfacesRemoteErrorsInsteadOfAssumingAbsent(t *testing.T) {
sourceRoot, source := newArchive(t)
id := seal(t, source)
up := newFixture(t, sourceRoot)
handoff := &Handoff{config: Config{LocalArchive: sourceRoot, MaxBundleBytes: 1 << 20, OpTimeout: time.Second},
remote: errorRemote{}, now: time.Now}
_, err := handoff.Push(context.Background(), Identity{SnapshotID: id, Scope: "internal",
TenantID: fixtureTenant, KnowledgeBaseID: fixtureKB})
if err == nil {
t.Fatal("Push() ignored a remote transport error")
}
_ = up
}
type errorRemote struct{}
func (errorRemote) PutBundle(context.Context, string, io.Reader) error {
return errors.New("network down")
}
func (errorRemote) OpenBundle(context.Context, string) (io.ReadCloser, error) {
return nil, errors.New("network down")
}
func (errorRemote) RemoveBundle(context.Context, string) error { return errors.New("network down") }
func (errorRemote) RunLibraryctl(context.Context, ...string) ([]byte, error) {
return nil, errors.New("network down")
}
func (errorRemote) ReadVersionManifest(context.Context, string) ([]byte, error) {
return nil, errors.New("network down")
}