2026-11-30 14:40:34 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"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/scriptorium"
|
|
|
|
|
"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/fileops"
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type lockedAuditaProbe struct {
|
|
|
|
|
audita.FakeRunner
|
|
|
|
|
check func(context.Context, string, string) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *lockedAuditaProbe) Preflight(ctx context.Context, req audita.PolishRequest) (audita.PreflightResult, error) {
|
|
|
|
|
return audita.PreflightResult{}, p.check(ctx, "", req.ReportDir)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type lockedNotariusProbe struct {
|
|
|
|
|
notarius.FakeRunner
|
|
|
|
|
check func(context.Context, string, string) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *lockedNotariusProbe) Preflight(ctx context.Context, req notarius.ReadinessRequest) (notarius.ReadinessResult, error) {
|
|
|
|
|
if err := p.check(ctx, req.InputPath, filepath.Dir(req.ReportPath)); err != nil {
|
|
|
|
|
return notarius.ReadinessResult{}, err
|
|
|
|
|
}
|
|
|
|
|
return p.FakeRunner.Preflight(ctx, req)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSessionValidationHoldsLockUntilProbeFilesAreFinished(t *testing.T) {
|
|
|
|
|
for _, tool := range []string{"audita", "notarius"} {
|
|
|
|
|
t.Run(tool, func(t *testing.T) {
|
|
|
|
|
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
|
|
|
|
cfg, err := config.Load(pipeline, campaign, session)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if tool == "notarius" {
|
|
|
|
|
configureNotariusProbe(t, cfg)
|
|
|
|
|
}
|
|
|
|
|
cfg.Pipeline.Workspace.CleanupAfterPublish = true
|
|
|
|
|
cfg.Pipeline.Publish = &config.PublishConfig{Enabled: boolPtr(true), UploadRun: boolPtr(true)}
|
|
|
|
|
cfg.Pipeline.Storage.Backend = config.StorageBackendS3
|
|
|
|
|
cfg.Pipeline.Storage.S3 = &config.StorageS3Config{Bucket: "archive", RootPrefix: "dnd"}
|
2026-11-30 15:26:06 +00:00
|
|
|
plan, err := BuildBoundedPlan("prepare", "prepare")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if _, err = executePlan(context.Background(), cfg, plan, RunOptions{}); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
2026-11-30 14:40:34 +00:00
|
|
|
m, err := (&manifest.LocalStore{}).Load(context.Background(), paths.ManifestPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
m.RunID = "published-run"
|
|
|
|
|
m.S3SessionPrefix = artifacts.S3SessionPrefix("dnd", m.Campaign, m.SessionID)
|
|
|
|
|
m.MarkStageSucceeded("publish", time.Now().UTC(), nil)
|
|
|
|
|
m.Stages["publish"].Metadata = cleanupPublishMetadata(m)
|
|
|
|
|
if err := (&manifest.LocalStore{}).Save(context.Background(), paths.ManifestPath, m); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
entered, release := make(chan struct{}), make(chan struct{})
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
check := func(ctx context.Context, input, logs string) error {
|
|
|
|
|
close(entered)
|
|
|
|
|
select {
|
|
|
|
|
case <-release:
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return ctx.Err()
|
|
|
|
|
}
|
|
|
|
|
if input != "" {
|
|
|
|
|
if _, err := os.ReadFile(input); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fileops.WriteFileAtomic(filepath.Join(logs, "finished.yml"), []byte("ready: true\n"), fileops.WorkspaceFileMode)
|
|
|
|
|
}
|
|
|
|
|
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
|
|
|
|
deps.newScriptorium = func() scriptorium.Runner { return &scriptorium.FakeRunner{} }
|
|
|
|
|
deps.newAudita = func(*config.Config) (audita.Runner, error) {
|
|
|
|
|
if tool == "audita" {
|
|
|
|
|
return &lockedAuditaProbe{check: check}, nil
|
|
|
|
|
}
|
|
|
|
|
return &audita.FakeRunner{}, nil
|
|
|
|
|
}
|
|
|
|
|
deps.newNotarius = func() notarius.Runner { return &lockedNotariusProbe{check: check} }
|
|
|
|
|
done := make(chan error, 1)
|
2026-11-30 15:26:06 +00:00
|
|
|
go func() { _, err := inspectConfiguredForTest(ctx, cfg, deps); done <- err }()
|
2026-11-30 14:40:34 +00:00
|
|
|
select {
|
|
|
|
|
case <-entered:
|
|
|
|
|
case err := <-done:
|
|
|
|
|
t.Fatalf("validation stopped before probe: %v", err)
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
t.Fatal(ctx.Err())
|
|
|
|
|
}
|
|
|
|
|
store := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root)
|
|
|
|
|
lock, err := store.AcquireSessionLockFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
|
|
|
|
if lock != nil {
|
|
|
|
|
_ = store.ReleaseSessionLock(lock)
|
|
|
|
|
}
|
|
|
|
|
if !errors.Is(err, artifacts.ErrLockConflict) {
|
|
|
|
|
close(release)
|
|
|
|
|
<-done
|
|
|
|
|
t.Fatalf("active validation did not exclude cleanup's session lock: %v", err)
|
|
|
|
|
}
|
|
|
|
|
close(release)
|
|
|
|
|
if err := <-done; err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
summary, err := executePlanWithDependencies(ctx, cfg, BoundedPlan{stages: []stage.Stage{selectedCleanupStage(t)}}, RunOptions{}, cleanupOrchestrationDependencies())
|
|
|
|
|
if err != nil || !summary.Complete {
|
|
|
|
|
t.Fatalf("cleanup after validation: %#v, %v", summary, err)
|
|
|
|
|
}
|
|
|
|
|
if _, err := os.Lstat(paths.RunsDir); !errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
t.Fatalf("completed probes survived cleanup: %v", err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSessionValidationCancellationWhileSessionIsLocked(t *testing.T) {
|
|
|
|
|
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
|
|
|
|
cfg, err := config.Load(pipeline, campaign, session)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
paths := seedValidationPrepared(t, cfg)
|
|
|
|
|
store := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root)
|
|
|
|
|
lock, err := store.AcquireSessionLockFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer store.ReleaseSessionLock(lock)
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
|
|
|
|
defer cancel()
|
|
|
|
|
probe := &audita.FakeRunner{}
|
|
|
|
|
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
|
|
|
|
deps.newAudita = func(*config.Config) (audita.Runner, error) { return probe, nil }
|
|
|
|
|
deps.newScriptorium = func() scriptorium.Runner { return &scriptorium.FakeRunner{} }
|
2026-11-30 15:26:06 +00:00
|
|
|
report, err := inspectConfiguredForTest(ctx, cfg, deps)
|
|
|
|
|
if !errors.Is(err, context.DeadlineExceeded) || report.Ready || len(probe.PreflightRequests) != 0 {
|
2026-11-30 14:40:34 +00:00
|
|
|
t.Fatalf("waiting validation: %+v, %v", report, err)
|
|
|
|
|
}
|
|
|
|
|
if _, err := os.Lstat(paths.RunsDir); !errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
t.Fatalf("waiting validation created probe paths: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|