138 lines
5.6 KiB
Go
138 lines
5.6 KiB
Go
|
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
||
|
|
)
|
||
|
|
|
||
|
|
type viewObservingStage struct {
|
||
|
|
name string
|
||
|
|
skip bool
|
||
|
|
failure error
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s viewObservingStage) Name() string { return s.name }
|
||
|
|
func (s viewObservingStage) Run(_ context.Context, env *stage.Env, m *manifest.Manifest) (*stage.StageResult, error) {
|
||
|
|
paths := env.ArtifactStore.SessionPathsFor(env.Config.Session.Campaign, m.SessionID)
|
||
|
|
for _, category := range []string{"logs", "debug"} {
|
||
|
|
link := filepath.Join(paths.Root, category, s.name)
|
||
|
|
target, err := os.Readlink(link)
|
||
|
|
want := "../runs/" + m.RunID + "/" + s.name + "/" + category
|
||
|
|
if err != nil || target != want {
|
||
|
|
return nil, fmt.Errorf("attempt view %s = %q, want %q: %w", link, target, want, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if s.failure != nil {
|
||
|
|
return nil, s.failure
|
||
|
|
}
|
||
|
|
if s.skip {
|
||
|
|
return &stage.StageResult{Disposition: stage.StageDispositionSkipped, SkipReason: "disabled"}, nil
|
||
|
|
}
|
||
|
|
return &stage.StageResult{}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestAttemptViewsTrackAttemptsFailureSkipAndReuse(t *testing.T) {
|
||
|
|
cfg := testConfig(t)
|
||
|
|
run := func(id string, selected stage.Stage, force bool) (*RunSummary, error) {
|
||
|
|
deps := invocationDependencies{newRunID: func() (string, error) { return id, nil }}
|
||
|
|
return executePlanWithDependencies(context.Background(), cfg, BoundedPlan{stages: []stage.Stage{selected}}, RunOptions{Force: force}, deps)
|
||
|
|
}
|
||
|
|
if _, err := run("run-one", viewObservingStage{name: "prepare"}, false); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if _, err := run("run-two", viewObservingStage{name: "notify"}, false); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||
|
|
for name, id := range map[string]string{"prepare": "run-one", "notify": "run-two"} {
|
||
|
|
target, err := os.Readlink(artifacts.SessionLatestLogsPath(paths, name))
|
||
|
|
if err != nil || target != "../runs/"+id+"/"+name+"/logs" {
|
||
|
|
t.Fatalf("%s view=%q, %v", name, target, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
failure := errors.New("synthetic failure")
|
||
|
|
if _, err := run("run-three", viewObservingStage{name: "prepare", failure: failure}, true); !errors.Is(err, failure) {
|
||
|
|
t.Fatalf("failure=%v", err)
|
||
|
|
}
|
||
|
|
target, err := os.Readlink(artifacts.SessionLatestDebugPath(paths, "prepare"))
|
||
|
|
if err != nil || target != "../runs/run-three/prepare/debug" {
|
||
|
|
t.Fatalf("failed attempt view=%q, %v", target, err)
|
||
|
|
}
|
||
|
|
entries, err := os.ReadDir(filepath.Join(paths.RunsDir, "run-three", "prepare", "debug"))
|
||
|
|
if err != nil || len(entries) != 0 {
|
||
|
|
t.Fatalf("empty debug=%#v, %v", entries, err)
|
||
|
|
}
|
||
|
|
if _, err := run("run-four", viewObservingStage{name: "prepare", skip: true}, true); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
target, err = os.Readlink(artifacts.SessionLatestDebugPath(paths, "prepare"))
|
||
|
|
if err != nil || target != "../runs/run-three/prepare/debug" {
|
||
|
|
t.Fatalf("self-skip changed view=%q, %v", target, err)
|
||
|
|
}
|
||
|
|
if _, err := run("run-five", viewObservingStage{name: "notify"}, false); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
summary, err := run("run-six", viewObservingStage{name: "notify"}, false)
|
||
|
|
if err != nil || summary.StageResults[0].Outcome != "reused" {
|
||
|
|
t.Fatalf("reuse=%#v, %v", summary, err)
|
||
|
|
}
|
||
|
|
if _, err := os.Stat(filepath.Join(paths.RunsDir, "run-six")); !os.IsNotExist(err) {
|
||
|
|
t.Fatalf("reuse created run: %v", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
type workspaceFreeStage struct{}
|
||
|
|
|
||
|
|
func (workspaceFreeStage) Name() string { return "cleanup" }
|
||
|
|
func (workspaceFreeStage) RequiresRunWorkspace() bool { return false }
|
||
|
|
func (workspaceFreeStage) Run(context.Context, *stage.Env, *manifest.Manifest) (*stage.StageResult, error) {
|
||
|
|
return &stage.StageResult{}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestWorkspaceFreeAttemptDoesNotCreateViews(t *testing.T) {
|
||
|
|
cfg := testConfig(t)
|
||
|
|
deps := invocationDependencies{newRunID: func() (string, error) { return "cleanup-run", nil }}
|
||
|
|
if _, err := executePlanWithDependencies(context.Background(), cfg, BoundedPlan{stages: []stage.Stage{workspaceFreeStage{}}}, RunOptions{}, deps); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||
|
|
for _, path := range []string{filepath.Join(paths.RunsDir, "cleanup-run"), artifacts.SessionLatestLogsPath(paths, "cleanup"), artifacts.SessionLatestDebugPath(paths, "cleanup")} {
|
||
|
|
if _, err := os.Lstat(path); !os.IsNotExist(err) {
|
||
|
|
t.Fatalf("workspace-free attempt created %q: %v", path, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDisabledExtractionDoesNotTouchConflictingView(t *testing.T) {
|
||
|
|
cfg := testConfig(t)
|
||
|
|
store := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root)
|
||
|
|
paths, err := store.EnsureLayoutFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
conflict := artifacts.SessionLatestLogsPath(paths, "extract")
|
||
|
|
if err := os.WriteFile(conflict, []byte("keep"), 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
deps := invocationDependencies{newRunID: func() (string, error) { return "disabled-run", nil }}
|
||
|
|
summary, err := executePlanWithDependencies(context.Background(), cfg, BoundedPlan{stages: []stage.Stage{mustSelectStage(t, "extract")}}, RunOptions{}, deps)
|
||
|
|
if err != nil || len(summary.StageResults) != 1 || summary.StageResults[0].Outcome != "skipped" {
|
||
|
|
t.Fatalf("summary=%#v error=%v", summary, err)
|
||
|
|
}
|
||
|
|
data, err := os.ReadFile(conflict)
|
||
|
|
if err != nil || string(data) != "keep" {
|
||
|
|
t.Fatalf("conflict=%q error=%v", data, err)
|
||
|
|
}
|
||
|
|
if _, err := os.Stat(filepath.Join(paths.RunsDir, "disabled-run")); !os.IsNotExist(err) {
|
||
|
|
t.Fatalf("skipped extraction created a run directory: %v", err)
|
||
|
|
}
|
||
|
|
}
|