170 lines
7.4 KiB
Go
170 lines
7.4 KiB
Go
|
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"reflect"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/audita"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
||
|
|
)
|
||
|
|
|
||
|
|
type preparationFixture struct {
|
||
|
|
runs *int
|
||
|
|
stale *bool
|
||
|
|
}
|
||
|
|
|
||
|
|
func (p preparationFixture) Name() string { return "prepare" }
|
||
|
|
func (p preparationFixture) ValidateResume(context.Context, *stage.Env, *manifest.Manifest) (stage.ResumeValidation, error) {
|
||
|
|
if p.stale != nil && *p.stale {
|
||
|
|
return stage.NonResumable("source changed"), nil
|
||
|
|
}
|
||
|
|
return stage.Resumable(), nil
|
||
|
|
}
|
||
|
|
func (p preparationFixture) Run(_ context.Context, env *stage.Env, m *manifest.Manifest) (*stage.StageResult, error) {
|
||
|
|
*p.runs++
|
||
|
|
paths := env.ArtifactStore.SessionPathsFor(env.Config.Session.Campaign, m.SessionID)
|
||
|
|
m.Inputs = nil
|
||
|
|
for _, d := range artifactpolicy.GlossaryInputSources() {
|
||
|
|
path := filepath.Join(paths.InputsDir, d.Filename)
|
||
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(path, []byte("synthetic: {entries: []}\n"), 0644); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
checksum, err := env.ArtifactStore.Checksum(path)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
m.Inputs = append(m.Inputs, manifest.InputRecord{Kind: d.ManifestKind, Path: path, Checksum: checksum})
|
||
|
|
}
|
||
|
|
return &stage.StageResult{}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDownstreamPreflightAfterPreparePreservesStateOnFailure(t *testing.T) {
|
||
|
|
cfg := testConfig(t)
|
||
|
|
var prepared, polished int
|
||
|
|
failure := errors.New("synthetic provider failure")
|
||
|
|
runner := &audita.FakeRunner{PreflightErr: failure}
|
||
|
|
selected := []stage.Stage{preparationFixture{runs: &prepared}, countingStage{name: "polish", runs: &polished}}
|
||
|
|
summary, err := executeStages(context.Background(), cfg, selected, RunOptions{Env: &Env{Audita: runner}})
|
||
|
|
if !errors.Is(err, failure) || summary == nil || prepared != 1 || polished != 0 || len(runner.PreflightRequests) != 1 {
|
||
|
|
t.Fatalf("summary=%+v prepared=%d polished=%d calls=%d err=%v", summary, prepared, polished, len(runner.PreflightRequests), err)
|
||
|
|
}
|
||
|
|
if summary.StageResults[0].Outcome != "completed" || summary.StageResults[1].Attempted || summary.StageResults[1].Outcome != "not_attempted" {
|
||
|
|
t.Fatalf("stage results: %+v", summary.StageResults)
|
||
|
|
}
|
||
|
|
stored, err := (&manifest.LocalStore{}).Load(context.Background(), summary.ManifestPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if stored.Stages["prepare"].Status != manifest.StatusSucceeded || stored.Stages["polish"] != nil {
|
||
|
|
t.Fatalf("manifest: %+v", stored.Stages)
|
||
|
|
}
|
||
|
|
runner.PreflightErr = nil
|
||
|
|
summary, err = executeStages(context.Background(), cfg, selected, RunOptions{Env: &Env{Audita: runner}})
|
||
|
|
if err != nil || prepared != 1 || polished != 1 || summary.StageResults[0].Outcome != "reused" {
|
||
|
|
t.Fatalf("retry: %+v prepared=%d polished=%d err=%v", summary, prepared, polished, err)
|
||
|
|
}
|
||
|
|
if len(runner.PreflightRequests) != 2 || len(runner.PreflightRequests[1].GlossaryFiles) != 4 {
|
||
|
|
t.Fatalf("requests: %+v", runner.PreflightRequests)
|
||
|
|
}
|
||
|
|
again, err := executeStages(context.Background(), cfg, selected, RunOptions{Env: &Env{Audita: runner}})
|
||
|
|
if err != nil || len(runner.PreflightRequests) != 2 || again.StageResults[1].Outcome != "reused" {
|
||
|
|
t.Fatalf("reuse: %+v calls=%d err=%v", again, len(runner.PreflightRequests), err)
|
||
|
|
}
|
||
|
|
if _, err := os.Stat(summaryRunRoot(cfg, again.RunID)); !errors.Is(err, os.ErrNotExist) {
|
||
|
|
t.Fatalf("reused invocation created run directory: %v", err)
|
||
|
|
}
|
||
|
|
initialized := 0
|
||
|
|
deps := invocationDependencies{newAudita: func(*config.Config) (audita.Runner, error) { initialized++; return &audita.FakeRunner{}, nil }}
|
||
|
|
_, err = executePlanWithDependencies(context.Background(), cfg, BoundedPlan{stages: selected}, RunOptions{}, deps)
|
||
|
|
if err != nil || initialized != 0 {
|
||
|
|
t.Fatalf("reused work initialized Audita: %d %v", initialized, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPrepareOnlyDoesNotInitializeDownstream(t *testing.T) {
|
||
|
|
cfg := testConfig(t)
|
||
|
|
var prepared int
|
||
|
|
calls := 0
|
||
|
|
deps := invocationDependencies{newAudita: func(*config.Config) (audita.Runner, error) { calls++; return &audita.FakeRunner{}, nil }}
|
||
|
|
_, err := executePlanWithDependencies(context.Background(), cfg, BoundedPlan{stages: []stage.Stage{countingStage{name: "prepare", runs: &prepared}}}, RunOptions{}, deps)
|
||
|
|
if err != nil || prepared != 1 || calls != 0 {
|
||
|
|
t.Fatalf("prepare=%d calls=%d err=%v", prepared, calls, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestFailedPreparationDoesNotRunDownstreamPreflight(t *testing.T) {
|
||
|
|
cfg := testConfig(t)
|
||
|
|
failure := errors.New("preparation failed")
|
||
|
|
runner := &audita.FakeRunner{}
|
||
|
|
var polished int
|
||
|
|
summary, err := executeStages(context.Background(), cfg, []stage.Stage{failingStage{name: "prepare", err: failure}, countingStage{name: "polish", runs: &polished}}, RunOptions{Env: &Env{Audita: runner}})
|
||
|
|
if !errors.Is(err, failure) || len(runner.PreflightRequests) != 0 || polished != 0 || summary == nil || summary.StageResults[1].Attempted {
|
||
|
|
t.Fatalf("summary=%+v calls=%d err=%v", summary, len(runner.PreflightRequests), err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestAuditaPreflightFailureDoesNotInvalidateForcedPolish(t *testing.T) {
|
||
|
|
cfg := testConfig(t)
|
||
|
|
var prepared, polished int
|
||
|
|
runner := &audita.FakeRunner{}
|
||
|
|
selected := []stage.Stage{preparationFixture{runs: &prepared}, countingStage{name: "polish", runs: &polished}}
|
||
|
|
first, err := executeStages(context.Background(), cfg, selected, RunOptions{Env: &Env{Audita: runner}})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
store := &manifest.LocalStore{}
|
||
|
|
before, err := store.Load(context.Background(), first.ManifestPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
failure := errors.New("provider unavailable")
|
||
|
|
runner.PreflightErr = failure
|
||
|
|
second, err := executeStages(context.Background(), cfg, []stage.Stage{countingStage{name: "polish", runs: &polished}}, RunOptions{Force: true, Env: &Env{Audita: runner}})
|
||
|
|
if !errors.Is(err, failure) || second == nil || second.StageResults[0].Attempted || polished != 1 {
|
||
|
|
t.Fatalf("summary=%+v runs=%d err=%v", second, polished, err)
|
||
|
|
}
|
||
|
|
after, err := store.Load(context.Background(), first.ManifestPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if !reflect.DeepEqual(before.Stages["polish"], after.Stages["polish"]) {
|
||
|
|
t.Fatalf("preflight changed prior polish: before=%+v after=%+v", before.Stages["polish"], after.Stages["polish"])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPreparationInvalidationExposesPolishToPreflight(t *testing.T) {
|
||
|
|
cfg := testConfig(t)
|
||
|
|
var prepared, polished int
|
||
|
|
stale := false
|
||
|
|
runner := &audita.FakeRunner{}
|
||
|
|
selected := []stage.Stage{preparationFixture{runs: &prepared, stale: &stale}, countingStage{name: "polish", runs: &polished}}
|
||
|
|
_, err := executeStages(context.Background(), cfg, selected, RunOptions{Env: &Env{Audita: runner}})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
stale = true
|
||
|
|
failure := errors.New("provider unavailable after preparation")
|
||
|
|
runner.PreflightErr = failure
|
||
|
|
summary, err := executeStages(context.Background(), cfg, selected, RunOptions{Env: &Env{Audita: runner}})
|
||
|
|
if !errors.Is(err, failure) || prepared != 2 || polished != 1 || summary.StageResults[0].Outcome != "completed" || summary.StageResults[1].Attempted {
|
||
|
|
t.Fatalf("summary=%+v prepare=%d polish=%d err=%v", summary, prepared, polished, err)
|
||
|
|
}
|
||
|
|
stored, err := (&manifest.LocalStore{}).Load(context.Background(), summary.ManifestPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if stored.Stages["polish"].Status != manifest.StatusStale {
|
||
|
|
t.Fatalf("polish state: %+v", stored.Stages["polish"])
|
||
|
|
}
|
||
|
|
}
|