169 lines
8.1 KiB
Go
169 lines
8.1 KiB
Go
package app
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/audita"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/notarius"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
|
)
|
|
|
|
func configureNotariusProbe(t *testing.T, cfg *config.Config) {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
binary := filepath.Join(dir, "notarius")
|
|
path := filepath.Join(dir, "config.yml")
|
|
if err := os.WriteFile(binary, []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte("pipelines: {}\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg.Pipeline.Notarius = &config.NotariusConfig{Enabled: true, Binary: binary, ConfigPath: path, PipelineID: "campaign", WorkingDirectory: dir, Timeout: "1m", Outputs: map[string]config.NotariusOutputConfig{"events": {LaneID: "events"}}}
|
|
}
|
|
|
|
func TestSessionValidationUsesPreparedNotariusProbeWithoutChangingManifest(t *testing.T) {
|
|
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
|
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
configureNotariusProbe(t, cfg)
|
|
paths := seedValidationPrepared(t, cfg)
|
|
before, err := os.ReadFile(paths.ManifestPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
native := ¬arius.FakeRunner{PreflightResult: ¬arius.ReadinessResult{Pipeline: "campaign", Lanes: []string{"events"}, InspectionComplete: true, AssessmentComplete: true, Ready: true, Checks: []notarius.ReadinessCheck{{ID: "cache", Result: "warning", Message: "cache inaccessible"}}}}
|
|
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
|
deps.newAudita = func(*config.Config) (audita.Runner, error) { return &audita.FakeRunner{}, nil }
|
|
deps.newNotarius = func() notarius.Runner { return native }
|
|
report, err := inspectSessionValidation(context.Background(), cfg, deps)
|
|
if err != nil || !report.Valid || len(native.PreflightRequests) != 1 {
|
|
t.Fatalf("report=%#v requests=%#v err=%v", report, native.PreflightRequests, err)
|
|
}
|
|
request := native.PreflightRequests[0]
|
|
if !strings.Contains(request.InputDescription, "synthetic") || !strings.HasPrefix(request.OutputRoot, artifacts.SessionValidationNotariusDir(paths)) {
|
|
t.Fatalf("probe = %#v", request)
|
|
}
|
|
if !findingMatches(report.Findings, "INFO", "notarius", "synthetic transcript") {
|
|
t.Fatalf("findings = %#v", report.Findings)
|
|
}
|
|
if !findingMatches(report.Findings, "WARN", "notarius", "cache inaccessible") {
|
|
t.Fatalf("cache warning missing: %#v", report.Findings)
|
|
}
|
|
after, err := os.ReadFile(paths.ManifestPath)
|
|
if err != nil || !bytes.Equal(before, after) {
|
|
t.Fatalf("manifest changed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNotariusPreflightFollowsPrepareAndSkipsReusedWork(t *testing.T) {
|
|
cfg := testConfig(t)
|
|
configureNotariusProbe(t, cfg)
|
|
var prepared, extracted int
|
|
failure := errors.New("synthetic provider failure")
|
|
native := ¬arius.FakeRunner{PreflightErr: failure}
|
|
selected := []stage.Stage{preparationFixture{runs: &prepared}, countingStage{name: "extract", runs: &extracted}}
|
|
summary, err := executeStages(context.Background(), cfg, selected, RunOptions{Env: &Env{Notarius: native}})
|
|
if !errors.Is(err, failure) || summary == nil || prepared != 1 || extracted != 0 || len(native.PreflightRequests) != 1 {
|
|
t.Fatalf("summary=%#v prepared=%d extracted=%d requests=%d err=%v", summary, prepared, extracted, len(native.PreflightRequests), err)
|
|
}
|
|
stored, err := (&manifest.LocalStore{}).Load(context.Background(), summary.ManifestPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if stored.Stages["prepare"] == nil || stored.Stages["prepare"].Status != manifest.StatusSucceeded || stored.Stages["extract"] != nil {
|
|
t.Fatalf("stages = %#v", stored.Stages)
|
|
}
|
|
request := native.PreflightRequests[0]
|
|
if !strings.Contains(request.InputDescription, "synthetic") || !strings.Contains(request.OutputRoot, filepath.Join("runs", ".preflight")) {
|
|
t.Fatalf("request = %#v", request)
|
|
}
|
|
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
|
for _, view := range []string{artifacts.SessionLatestLogsPath(paths, "extract"), artifacts.SessionLatestDebugPath(paths, "extract")} {
|
|
if _, err := os.Lstat(view); !os.IsNotExist(err) {
|
|
t.Fatalf("probe changed latest view %q: %v", view, err)
|
|
}
|
|
}
|
|
native.PreflightErr = nil
|
|
summary, err = executeStages(context.Background(), cfg, selected, RunOptions{Env: &Env{Notarius: native}})
|
|
if err != nil || prepared != 1 || extracted != 1 || summary.StageResults[0].Outcome != "reused" {
|
|
t.Fatalf("retry=%#v prepared=%d extracted=%d err=%v", summary, prepared, extracted, err)
|
|
}
|
|
_, err = executeStages(context.Background(), cfg, selected, RunOptions{Env: &Env{Notarius: native}})
|
|
if err != nil || len(native.PreflightRequests) != 2 || extracted != 1 {
|
|
t.Fatalf("reuse calls=%d extracted=%d err=%v", len(native.PreflightRequests), extracted, err)
|
|
}
|
|
initialized := 0
|
|
deps := invocationDependencies{newNotarius: func() notarius.Runner { initialized++; return ¬arius.FakeRunner{} }}
|
|
_, err = executePlanWithDependencies(context.Background(), cfg, BoundedPlan{stages: selected}, RunOptions{}, deps)
|
|
if err != nil || initialized != 0 {
|
|
t.Fatalf("reused work initialized Notarius %d times: %v", initialized, err)
|
|
}
|
|
}
|
|
|
|
func TestUnrelatedWorkDoesNotInitializeNotarius(t *testing.T) {
|
|
cfg := testConfig(t)
|
|
configureNotariusProbe(t, cfg)
|
|
initialized, attempts := 0, 0
|
|
deps := invocationDependencies{newNotarius: func() notarius.Runner { initialized++; return ¬arius.FakeRunner{} }}
|
|
_, err := executePlanWithDependencies(context.Background(), cfg, BoundedPlan{stages: []stage.Stage{countingStage{name: "prepare", runs: &attempts}}}, RunOptions{}, deps)
|
|
if err != nil || initialized != 0 || attempts != 1 {
|
|
t.Fatalf("initialized=%d attempts=%d error=%v", initialized, attempts, err)
|
|
}
|
|
}
|
|
|
|
func TestForcedNotariusPreflightUsesRefresh(t *testing.T) {
|
|
cfg := testConfig(t)
|
|
configureNotariusProbe(t, cfg)
|
|
cache := "bypass"
|
|
cfg.Pipeline.Notarius.Cache = &cache
|
|
native := ¬arius.FakeRunner{}
|
|
var attempts int
|
|
_, err := executeStages(context.Background(), cfg, []stage.Stage{countingStage{name: "extract", runs: &attempts}}, RunOptions{Force: true, Env: &Env{Notarius: native}})
|
|
if err != nil || attempts != 1 || len(native.PreflightRequests) != 1 || native.PreflightRequests[0].Cache == nil || *native.PreflightRequests[0].Cache != "refresh" {
|
|
t.Fatalf("requests=%#v attempts=%d err=%v", native.PreflightRequests, attempts, err)
|
|
}
|
|
}
|
|
|
|
func TestNotariusPreflightFailurePreservesCompletedExtraction(t *testing.T) {
|
|
cfg := testConfig(t)
|
|
configureNotariusProbe(t, cfg)
|
|
store := &manifest.LocalStore{}
|
|
seed := manifest.New(cfg.Session.SessionID, time.Now().UTC())
|
|
seed.Campaign = cfg.Session.Campaign
|
|
seed.MarkStageSucceeded("extract", time.Now().UTC(), nil)
|
|
seed.Stages["extract"].Metadata = map[string]any{"prior": "retained"}
|
|
if err := store.Save(context.Background(), manifestPathFor(cfg), seed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before, err := store.Load(context.Background(), manifestPathFor(cfg))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
failure := errors.New("notarius provider unavailable")
|
|
native := ¬arius.FakeRunner{PreflightErr: failure}
|
|
var attempts int
|
|
summary, err := executeStages(context.Background(), cfg, []stage.Stage{countingStage{name: "extract", runs: &attempts}}, RunOptions{Force: true, Env: &Env{Notarius: native}})
|
|
if !errors.Is(err, failure) || summary == nil || attempts != 0 || summary.StageResults[0].Attempted {
|
|
t.Fatalf("summary=%#v attempts=%d error=%v", summary, attempts, err)
|
|
}
|
|
after, err := store.Load(context.Background(), manifestPathFor(cfg))
|
|
if err != nil || !reflect.DeepEqual(before.Stages["extract"], after.Stages["extract"]) {
|
|
t.Fatalf("completed extraction changed: before=%#v after=%#v error=%v", before.Stages["extract"], after.Stages["extract"], err)
|
|
}
|
|
}
|