Files
knowledge-governance/internal/libraryfreeze/archive_safety_r31_test.go
T
2026-11-01 23:06:48 +08:00

159 lines
6.1 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 libraryfreeze
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"testing"
)
func r31fail(t *testing.T, e *Coordinator, job string) {
t.Helper()
if _, err := e.RunWindow(context.Background(), job, func(context.Context) error { return nil },
func(context.Context, *Window) (string, error) {
return "", errors.New("preflight failed before any target write")
}, func(error) bool { return true }); err == nil {
t.Fatal("window should fail")
}
}
// 关键不变量:新尝试准备的任何一步失败时,**当前 journal 必须仍然可读**,
// 旧证据不能因为"先 rename 走"而丢失。
func TestR31PrepareFailureKeepsCurrentJournal(t *testing.T) {
root, _, e := instanceFixture(t)
r31fail(t, e, "job-r31-a")
before, err := os.ReadFile(filepath.Join(root, "job-r31-a.json"))
if err != nil {
t.Fatalf("current journal after first failure: %v", err)
}
_, err = e.RunWindow(context.Background(), "job-r31-a",
func(context.Context) error { return errors.New("prepare blew up") },
func(context.Context, *Window) (string, error) { return r30digest, nil }, func(error) bool { return true })
if err == nil || !strings.Contains(err.Error(), "prepare blew up") {
t.Fatalf("want prepare error, got %v", err)
}
after, err := os.ReadFile(filepath.Join(root, "job-r31-a.json"))
if err != nil {
t.Fatalf("current journal disappeared after failed prepare: %v", err)
}
if string(after) != string(before) {
t.Fatalf("current journal changed after failed prepare:\n before=%s\n after=%s", before, after)
}
}
// 连续失败:每次归档一份、名字按被归档请求自己的 attempt 派生,绝不互相覆盖。
func TestR31RepeatedFailuresArchiveDistinctGenerations(t *testing.T) {
root, _, e := instanceFixture(t)
// 第一次失败只产生 current;第二次开始才会把上一代归档(attempt1),第三次归档 attempt2。
r31fail(t, e, "job-r31-b")
r31fail(t, e, "job-r31-b")
firstArchive, err := os.ReadFile(filepath.Join(root, "archive", "job-r31-b-attempt1.json"))
if err != nil {
t.Fatalf("first archive: %v", err)
}
r31fail(t, e, "job-r31-b")
secondArchive, err := os.ReadFile(filepath.Join(root, "archive", "job-r31-b-attempt2.json"))
if err != nil {
t.Fatalf("second archive: %v", err)
}
stillFirst, err := os.ReadFile(filepath.Join(root, "archive", "job-r31-b-attempt1.json"))
if err != nil {
t.Fatalf("first archive was removed: %v", err)
}
if string(stillFirst) != string(firstArchive) {
t.Fatalf("first archive overwritten:\n before=%s\n after=%s", firstArchive, stillFirst)
}
if string(secondArchive) == string(firstArchive) {
t.Fatal("two generations must not be the same bytes")
}
if _, err := os.Stat(filepath.Join(root, "job-r31-b.json")); err != nil {
t.Fatalf("current journal missing after repeated failures: %v", err)
}
}
// 归档目标已存在且内容不同:必须拒绝,不许覆盖。
func TestR31ArchiveCollisionRefusesToOverwrite(t *testing.T) {
root, _, e := instanceFixture(t)
r31fail(t, e, "job-r31-c")
// 连续失败会依次归档 attempt1;这里在第二次失败前先占住 attempt2 的位置。
if err := os.MkdirAll(filepath.Join(root, "archive"), 0o700); err != nil {
t.Fatal(err)
}
squatter := filepath.Join(root, "archive", "job-r31-c-attempt2.json")
if err := os.WriteFile(squatter, []byte(`{"squatter":true}`), 0o600); err != nil {
t.Fatal(err)
}
r31fail(t, e, "job-r31-c")
body, err := os.ReadFile(squatter)
if err != nil {
t.Fatal(err)
}
if string(body) != `{"squatter":true}` {
t.Fatalf("archive target was overwritten: %s", body)
}
}
// v1 旧 journal 没有 attempt 字段(=0):归档必须叫 attempt0,新尝试是 1,之后不会再撞名。
func TestR31LegacyJournalArchivesAsAttemptZero(t *testing.T) {
root, _, e := instanceFixture(t)
// 先用真实失败造一份 journal,再把 attempt/superseded_from 去掉,模拟 v1 旧记录(attempt 缺省=0)。
r31fail(t, e, "job-r31-d")
raw, err := os.ReadFile(filepath.Join(root, "job-r31-d.json"))
if err != nil {
t.Fatal(err)
}
var doc map[string]any
if err := json.Unmarshal(raw, &doc); err != nil {
t.Fatal(err)
}
delete(doc, "attempt")
delete(doc, "superseded_from")
legacyBytes, err := json.Marshal(doc)
if err != nil {
t.Fatal(err)
}
_ = legacyBytes
if err := os.WriteFile(filepath.Join(root, "job-r31-d.json"), legacyBytes, 0o600); err != nil {
t.Fatal(err)
}
if _, err := e.RunWindow(context.Background(), "job-r31-d", func(context.Context) error { return nil },
func(ctx context.Context, w *Window) (string, error) {
if err := w.MarkContentRisk(ctx); err != nil {
return "", err
}
return r30digest, nil
}, func(error) bool { return true }); err != nil {
t.Fatalf("legacy resume: %v", err)
}
archived, err := os.ReadFile(filepath.Join(root, "archive", "job-r31-d-attempt0.json"))
if err != nil || !strings.Contains(string(archived), `"state":"failed"`) ||
!strings.Contains(string(archived), `"job":"job-r31-d"`) {
t.Fatalf("legacy archive wrong: %v %.200s", err, archived)
}
j, err := e.load("job-r31-d")
if err != nil {
t.Fatal(err)
}
if j.Attempt != 1 || j.SupersededFrom != "archive/job-r31-d-attempt0.json" {
t.Fatalf("attempt bookkeeping: attempt=%d superseded=%q", j.Attempt, j.SupersededFrom)
}
}
// 入口关过但没确认重开:不能当成"已收尾",必须拒绝开新窗口。
func TestR31RefusesNewAttemptWhenIngressNotReopened(t *testing.T) {
root, _, e := instanceFixture(t)
closed := `{"version":1,"source":"target-instance","job":"job-r31-e","state":"failed","attempt":2,` +
`"level":"instance","ingress":"gate","ingress_closed":true,"gate":{"closed":true,"drained":true,"closed_at":"2026-01-01T00:00:00Z","drained_at":"2026-01-01T00:00:00Z"}}`
if err := os.WriteFile(filepath.Join(root, "job-r31-e.json"), []byte(closed), 0o600); err != nil {
t.Fatal(err)
}
_, err := e.RunWindow(context.Background(), "job-r31-e", func(context.Context) error { return nil },
func(context.Context, *Window) (string, error) { return r30digest, nil }, func(error) bool { return true })
if err == nil {
t.Fatal("must refuse: entry was closed and never proven reopened")
}
}