Clean the complete session run history
This commit is contained in:
@@ -138,52 +138,6 @@ func existingManagedView(parent *os.Root, name, category string) (*string, error
|
||||
return &target, nil
|
||||
}
|
||||
|
||||
// ManagedAttemptTargets returns run-relative diagnostic directories selected by
|
||||
// the session's managed navigation links. It never follows link targets.
|
||||
func ManagedAttemptTargets(sessionRoot, runID string, names []string) ([]string, error) {
|
||||
if err := pathsafe.ValidateOpaqueSegment(runID); err != nil {
|
||||
return nil, fmt.Errorf("run identity: %w", err)
|
||||
}
|
||||
root, err := os.OpenRoot(sessionRoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer root.Close()
|
||||
var targets []string
|
||||
for _, category := range []string{"logs", "debug"} {
|
||||
info, err := root.Lstat(category)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return nil, fmt.Errorf("managed %s directory is unsafe", category)
|
||||
}
|
||||
parent, err := root.OpenRoot(category)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, name := range names {
|
||||
if err := pathsafe.ValidateOpaqueSegment(name); err != nil {
|
||||
_ = parent.Close()
|
||||
return nil, fmt.Errorf("invalid managed %s view %q: %w", category, name, err)
|
||||
}
|
||||
target, err := existingManagedView(parent, name, category)
|
||||
if err != nil {
|
||||
_ = parent.Close()
|
||||
return nil, err
|
||||
}
|
||||
if target != nil && *target == attemptTarget(runID, name, category) {
|
||||
targets = append(targets, filepath.Join(name, category))
|
||||
}
|
||||
}
|
||||
_ = parent.Close()
|
||||
}
|
||||
return targets, nil
|
||||
}
|
||||
|
||||
// InspectManagedAttemptViews reports whether any recognized managed view exists.
|
||||
// Dangling links count as present; their targets are never followed.
|
||||
func InspectManagedAttemptViews(sessionRoot string, names []string) (bool, error) {
|
||||
|
||||
@@ -98,22 +98,6 @@ func TestAttemptViewsRejectConflictingAndUnsafePaths(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedAttemptTargetsRejectForeignLinks(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if _, err := BeginAttemptViews(root, "run-one", "polish"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Remove(filepath.Join(root, "logs", "polish")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink("../../outside", filepath.Join(root, "logs", "polish")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := ManagedAttemptTargets(root, "run-one", []string{"polish"}); err == nil {
|
||||
t.Fatal("accepted foreign diagnostic target")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttemptViewsRejectUnsafeIdentities(t *testing.T) {
|
||||
for _, pair := range [][2]string{{"../escape", "polish"}, {"run", "../outside"}, {"run", ""}, {".preflight", "polish"}} {
|
||||
if _, err := BeginAttemptViews(t.TempDir(), pair[0], pair[1]); err == nil {
|
||||
|
||||
@@ -2,16 +2,19 @@ package stage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
)
|
||||
|
||||
const (
|
||||
cleanupSkipReason = "cleanup policies are disabled"
|
||||
cleanupSemanticConfigVersion = 1
|
||||
cleanupSemanticConfigVersion = 2
|
||||
)
|
||||
|
||||
type cleanupStage struct{}
|
||||
@@ -42,7 +45,18 @@ func (cleanupStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, fmt.Errorf("cleanup canceled before %s: %w", target.policy, err)
|
||||
}
|
||||
_, err := cleanRunWithRetainedBundle(target.root, target.target, true)
|
||||
_, err := inspectCleanupTarget(target.root, target.target)
|
||||
if err == nil {
|
||||
err = fileops.RemoveManagedAttemptViews(filepath.Dir(target.target), cleanupManagedNames())
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
if err = ctx.Err(); err == nil {
|
||||
err = fileops.RemoveAllUnderRoot(target.root, target.target)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("apply %s: %w", target.policy, err)
|
||||
}
|
||||
@@ -79,7 +93,7 @@ func (cleanupStage) ValidateResume(ctx context.Context, env *Env, m *manifest.Ma
|
||||
if err := ctx.Err(); err != nil {
|
||||
return ResumeValidation{}, fmt.Errorf("validate cleanup resume for %s: %w", target.policy, err)
|
||||
}
|
||||
pending, err := cleanRunWithRetainedBundle(target.root, target.target, false)
|
||||
pending, err := inspectCleanupTarget(target.root, target.target)
|
||||
if err != nil {
|
||||
return ResumeValidation{}, err
|
||||
}
|
||||
@@ -97,3 +111,63 @@ func normalizedCleanupRoot(root string) string {
|
||||
}
|
||||
return filepath.Clean(root)
|
||||
}
|
||||
|
||||
func cleanupManagedNames() []string {
|
||||
names := []string{"restore"}
|
||||
for _, item := range All() {
|
||||
names = append(names, item.Name())
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// inspectCleanupTarget checks the session path and runs directory without creating
|
||||
// anything, then inspects managed views independently of their targets.
|
||||
func inspectCleanupTarget(workspaceRoot, runsDir string) (bool, error) {
|
||||
workspace, err := filepath.Abs(workspaceRoot)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
session, err := filepath.Abs(filepath.Dir(runsDir))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
rel, err := filepath.Rel(workspace, session)
|
||||
if err != nil || rel == "." || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
return false, fmt.Errorf("cleanup session must be below workspace root")
|
||||
}
|
||||
root, err := os.OpenRoot(workspace)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer root.Close()
|
||||
current := ""
|
||||
for _, component := range strings.Split(rel, string(filepath.Separator)) {
|
||||
current = filepath.Join(current, component)
|
||||
info, err := root.Lstat(current)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return false, fmt.Errorf("unsafe cleanup session directory %q", current)
|
||||
}
|
||||
}
|
||||
info, err := root.Lstat(filepath.Join(rel, "runs"))
|
||||
runsExist := err == nil
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return false, err
|
||||
}
|
||||
if runsExist && (!info.IsDir() || info.Mode()&os.ModeSymlink != 0) {
|
||||
return false, fmt.Errorf("unsafe run cleanup directory %q", runsDir)
|
||||
}
|
||||
viewsExist, err := fileops.InspectManagedAttemptViews(session, cleanupManagedNames())
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return runsExist || viewsExist, nil
|
||||
}
|
||||
|
||||
@@ -73,13 +73,8 @@ func buildCleanupPlan(env *Env, m *manifest.Manifest) (cleanupPlan, error) {
|
||||
return cleanupPlan{}, fmt.Errorf("pipeline.workspace.root is required for cleanup")
|
||||
}
|
||||
plan.targets = append(plan.targets, cleanupPlanTarget{
|
||||
root: root,
|
||||
target: artifacts.SessionRunRootForCampaign(
|
||||
root,
|
||||
campaign,
|
||||
sessionID,
|
||||
publishedRunID,
|
||||
),
|
||||
root: root,
|
||||
target: artifacts.SessionRunsDirForCampaign(root, campaign, sessionID),
|
||||
policy: cleanupWorkspacePolicy,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -20,14 +20,14 @@ func TestBuildCleanupPlanDisabledNeedsNoPublishEvidence(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildCleanupPlanSelectsCanonicalPublishedRunTarget(t *testing.T) {
|
||||
func TestBuildCleanupPlanSelectsCanonicalSessionRunsTarget(t *testing.T) {
|
||||
env, m, runID := cleanupPlanningFixture(t)
|
||||
env.Config.Pipeline.Workspace.CleanupAfterPublish = true
|
||||
plan, err := buildCleanupPlan(env, m)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := artifacts.SessionRunRootForCampaign(env.Config.Pipeline.Workspace.Root, m.Campaign, m.SessionID, runID)
|
||||
want := artifacts.SessionRunsDirForCampaign(env.Config.Pipeline.Workspace.Root, m.Campaign, m.SessionID)
|
||||
if !plan.enabled || plan.publishedRunID != runID || len(plan.targets) != 1 || plan.targets[0].target != want || plan.targets[0].policy != cleanupWorkspacePolicy {
|
||||
t.Fatalf("cleanup plan = %#v", plan)
|
||||
}
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
package stage
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
)
|
||||
|
||||
// cleanRunWithRetainedBundle removes unprotected run data while preserving
|
||||
// managed diagnostic targets and explicitly session-durable bundle evidence.
|
||||
func cleanRunWithRetainedBundle(workspaceRoot, runRoot string, remove bool) (bool, error) {
|
||||
runRoot, err := filepath.Abs(runRoot)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
info, err := os.Lstat(runRoot)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return false, fmt.Errorf("unsafe run cleanup directory %q", runRoot)
|
||||
}
|
||||
runsDir := filepath.Dir(runRoot)
|
||||
if filepath.Base(runsDir) != "runs" {
|
||||
return false, fmt.Errorf("invalid run cleanup directory %q", runRoot)
|
||||
}
|
||||
sessionRoot := filepath.Dir(runsDir)
|
||||
names := []string{"restore"}
|
||||
for _, stage := range All() {
|
||||
names = append(names, stage.Name())
|
||||
}
|
||||
targets, err := fileops.ManagedAttemptTargets(sessionRoot, filepath.Base(runRoot), names)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("resolve managed diagnostic targets: %w", err)
|
||||
}
|
||||
keep := make(map[string]bool, len(targets)+2)
|
||||
for _, target := range targets {
|
||||
keep[filepath.Clean(target)] = true
|
||||
}
|
||||
extract := filepath.Join(runRoot, "extract")
|
||||
info, err = os.Lstat(extract)
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return false, err
|
||||
}
|
||||
if err == nil {
|
||||
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return false, fmt.Errorf("unsafe extract cleanup directory %q", extract)
|
||||
}
|
||||
marker := filepath.Join(extract, "notarius-retention.json")
|
||||
info, err = os.Lstat(marker)
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return false, err
|
||||
}
|
||||
if err == nil {
|
||||
if !info.Mode().IsRegular() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return false, fmt.Errorf("unsafe Notarius retention marker %q", marker)
|
||||
}
|
||||
body, err := fileops.ReadFileBounded(marker, artifacts.MaxExtractionPayloadBytes)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("read Notarius retention marker: %w", err)
|
||||
}
|
||||
var record struct {
|
||||
Role string `json:"role"`
|
||||
BundleRoot string `json:"bundle_root"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &record); err != nil || record.Role != "session_durable" {
|
||||
return false, fmt.Errorf("invalid Notarius retention marker %q", marker)
|
||||
}
|
||||
if err := validateExtractBundleRoot(filepath.Join(extract, "notarius-output"), record.BundleRoot); err != nil {
|
||||
return false, fmt.Errorf("invalid retained Notarius bundle: %w", err)
|
||||
}
|
||||
keep[filepath.Join("extract", "notarius-output")] = true
|
||||
keep[filepath.Join("extract", "notarius-retention.json")] = true
|
||||
}
|
||||
}
|
||||
var prune func(string, string) (bool, error)
|
||||
prune = func(full, relative string) (bool, error) {
|
||||
if keep[relative] {
|
||||
return false, nil
|
||||
}
|
||||
prefix := relative
|
||||
if prefix != "" {
|
||||
prefix += string(filepath.Separator)
|
||||
}
|
||||
ancestor := false
|
||||
for target := range keep {
|
||||
if strings.HasPrefix(target, prefix) {
|
||||
ancestor = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !ancestor {
|
||||
if remove {
|
||||
if err := fileops.RemoveAllUnderRoot(workspaceRoot, full); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
info, err := os.Lstat(full)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return false, fmt.Errorf("unsafe protected cleanup ancestor %q", full)
|
||||
}
|
||||
entries, err := os.ReadDir(full)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
pending := false
|
||||
for _, entry := range entries {
|
||||
child := filepath.Join(relative, entry.Name())
|
||||
found, err := prune(filepath.Join(full, entry.Name()), child)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
pending = pending || found
|
||||
}
|
||||
return pending, nil
|
||||
}
|
||||
return prune(runRoot, "")
|
||||
}
|
||||
@@ -27,98 +27,81 @@ func TestCleanupStageDisabledSelfSkips(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanupPreservesLatestViewsAcrossRunsAndRemovesOldDiagnostics(t *testing.T) {
|
||||
func TestCleanupRemovesSessionRunsAndManagedViews(t *testing.T) {
|
||||
env, m, published := cleanupPlanningFixture(t)
|
||||
env.Config.Pipeline.Workspace.CleanupAfterPublish = true
|
||||
session := artifacts.NewLocalStore(env.Config.Pipeline.Workspace.Root).SessionPathsFor(m.Campaign, m.SessionID)
|
||||
if err := os.MkdirAll(session.Root, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := fileops.BeginAttemptViews(session.Root, published, "polish"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := fileops.BeginAttemptViews(session.Root, published, "merge"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := fileops.BeginAttemptViews(session.Root, "newer-run", "merge"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root := artifacts.SessionRunRootForCampaign(env.Config.Pipeline.Workspace.Root, m.Campaign, m.SessionID, published)
|
||||
kept := filepath.Join(root, "polish", "logs", "audit.log")
|
||||
old := filepath.Join(root, "merge", "logs", "merge.log")
|
||||
writeStageTestFile(t, kept, "current")
|
||||
writeStageTestFile(t, old, "old")
|
||||
writeStageTestFile(t, filepath.Join(root, "polish", "outputs", "work.json"), "work")
|
||||
for _, category := range []string{"logs", "debug"} {
|
||||
writeStageTestFile(t, filepath.Join(session.Root, category, "README.txt"), "operator notes")
|
||||
writeStageTestFile(t, filepath.Join(session.Root, category, "notes", "keep.txt"), "keep")
|
||||
if err := os.Symlink("../../outside", filepath.Join(session.Root, category, "operator-link")); err != nil {
|
||||
for _, pair := range [][2]string{{published, "polish"}, {"older-failure", "extract"}, {"restore-run", "restore"}} {
|
||||
if _, err := fileops.BeginAttemptViews(session.Root, pair[0], pair[1]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeStageTestFile(t, filepath.Join(session.RunsDir, pair[0], pair[1], "logs", "diagnostic"), "data")
|
||||
}
|
||||
if _, err := (cleanupStage{}).Run(context.Background(), env, m); err != nil {
|
||||
t.Fatal(err)
|
||||
writeStageTestFile(t, filepath.Join(session.RunsDir, ".preflight", "probe"), "data")
|
||||
var preserved []string
|
||||
for _, dir := range []string{session.InputsDir, session.AudioDir, session.TranscriptsDir, session.ArtifactsDir} {
|
||||
path := filepath.Join(dir, "keep")
|
||||
writeStageTestFile(t, path, "keep")
|
||||
preserved = append(preserved, path)
|
||||
}
|
||||
if _, err := os.Stat(kept); err != nil {
|
||||
t.Fatalf("latest diagnostic: %v", err)
|
||||
otherSession := artifacts.SessionRunsDirForCampaign(env.Config.Pipeline.Workspace.Root, m.Campaign, "another-session")
|
||||
for _, path := range []string{filepath.Join(otherSession, "run", "data"), filepath.Join(env.Config.Pipeline.Workspace.Root, "state", "cache"), filepath.Join(session.LogsDir, "operator-notes")} {
|
||||
writeStageTestFile(t, path, "keep")
|
||||
preserved = append(preserved, path)
|
||||
}
|
||||
if _, err := os.Stat(old); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("old diagnostic remains: %v", err)
|
||||
}
|
||||
validation, err := (cleanupStage{}).ValidateResume(context.Background(), env, m)
|
||||
if err != nil || !validation.Resumable {
|
||||
t.Fatalf("retained targets should be resumable: %#v, %v", validation, err)
|
||||
}
|
||||
if _, err := fileops.BeginAttemptViews(session.Root, "newer-run", "polish"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
validation, err = (cleanupStage{}).ValidateResume(context.Background(), env, m)
|
||||
if err != nil || validation.Resumable {
|
||||
t.Fatalf("rotated diagnostics should be eligible: %#v, %v", validation, err)
|
||||
}
|
||||
if _, err := (cleanupStage{}).Run(context.Background(), env, m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(root); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("old run remains after rotation: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(session.Root, "logs", "merge")); err != nil {
|
||||
t.Fatalf("newer merge view: %v", err)
|
||||
}
|
||||
for _, category := range []string{"logs", "debug"} {
|
||||
for _, name := range []string{"README.txt", "notes/keep.txt", "operator-link"} {
|
||||
if _, err := os.Lstat(filepath.Join(session.Root, category, name)); err != nil {
|
||||
t.Fatalf("unrelated content removed: %s/%s: %v", category, name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanupStageRemovesOnlySelectedPublishedRunTarget(t *testing.T) {
|
||||
env, m, publishedRunID := cleanupPlanningFixture(t)
|
||||
env.Config.Pipeline.Workspace.CleanupAfterPublish = true
|
||||
target := artifacts.SessionRunRootForCampaign(env.Config.Pipeline.Workspace.Root, m.Campaign, m.SessionID, publishedRunID)
|
||||
session := artifacts.NewLocalStore(env.Config.Pipeline.Workspace.Root).SessionPathsFor(m.Campaign, m.SessionID)
|
||||
audio := filepath.Join(session.AudioDir, "alice.flac")
|
||||
other := artifacts.SessionRunRootForCampaign(env.Config.Pipeline.Workspace.Root, m.Campaign, m.SessionID, "other-run")
|
||||
writeStageTestFile(t, filepath.Join(target, "result.txt"), "result")
|
||||
writeStageTestFile(t, filepath.Join(other, "result.txt"), "other")
|
||||
writeStageTestFile(t, audio, "audio")
|
||||
result, err := (cleanupStage{}).Run(context.Background(), env, m)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Lstat(target); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("target still exists: %v", err)
|
||||
if _, err := os.Lstat(session.RunsDir); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("runs remain: %v", err)
|
||||
}
|
||||
for _, path := range []string{audio, filepath.Join(other, "result.txt")} {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
t.Fatalf("preserved %q: %v", path, err)
|
||||
for _, path := range preserved {
|
||||
if data, err := os.ReadFile(path); err != nil || string(data) != "keep" {
|
||||
t.Fatalf("preserved %s = %q, %v", path, data, err)
|
||||
}
|
||||
}
|
||||
for _, category := range []string{"logs", "debug"} {
|
||||
for _, name := range []string{"polish", "extract", "restore"} {
|
||||
if _, err := os.Lstat(filepath.Join(session.Root, category, name)); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("view remains: %s/%s: %v", category, name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !reflect.DeepEqual(result.Metadata["completed_policies"], []string{cleanupWorkspacePolicy}) {
|
||||
t.Fatalf("metadata = %#v", result.Metadata)
|
||||
}
|
||||
validation, err := (cleanupStage{}).ValidateResume(context.Background(), env, m)
|
||||
if err != nil || !validation.Resumable {
|
||||
t.Fatalf("clean resume = %#v, %v", validation, err)
|
||||
}
|
||||
if err := os.Mkdir(session.RunsDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
validation, err = (cleanupStage{}).ValidateResume(context.Background(), env, m)
|
||||
if err != nil || validation.Resumable {
|
||||
t.Fatalf("empty runs resume = %#v, %v", validation, err)
|
||||
}
|
||||
if err := os.Remove(session.RunsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink("../runs/gone/extract/logs", filepath.Join(session.LogsDir, "extract")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
validation, err = (cleanupStage{}).ValidateResume(context.Background(), env, m)
|
||||
if err != nil || validation.Resumable {
|
||||
t.Fatalf("dangling view resume = %#v, %v", validation, err)
|
||||
}
|
||||
if _, err := (cleanupStage{}).Run(context.Background(), env, m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
validation, err = (cleanupStage{}).ValidateResume(context.Background(), env, m)
|
||||
if err != nil || !validation.Resumable {
|
||||
t.Fatalf("repaired resume = %#v, %v", validation, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanupStageAcceptsAlreadyAbsentRootAndTarget(t *testing.T) {
|
||||
@@ -270,3 +253,50 @@ func TestCleanupResumeRequiresAbsentCurrentTargetsAndValidPublishEvidence(t *tes
|
||||
t.Fatalf("disabled cleanup resume = %#v, error %v", validation, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanupRejectsUnsafeRunsAndPreservesUnauthorizedMaterial(t *testing.T) {
|
||||
for _, scenario := range []string{"disabled", "invalid publication", "symlink runs", "unsafe view"} {
|
||||
t.Run(scenario, func(t *testing.T) {
|
||||
env, m, _ := cleanupPlanningFixture(t)
|
||||
env.Config.Pipeline.Workspace.CleanupAfterPublish = scenario != "disabled"
|
||||
session := artifacts.NewLocalStore(env.Config.Pipeline.Workspace.Root).SessionPathsFor(m.Campaign, m.SessionID)
|
||||
if err := os.MkdirAll(session.Root, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
target := filepath.Join(session.RunsDir, "run", "keep")
|
||||
if scenario == "symlink runs" {
|
||||
outside := t.TempDir()
|
||||
target = filepath.Join(outside, "keep")
|
||||
writeStageTestFile(t, target, "keep")
|
||||
if err := os.Symlink(outside, session.RunsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
writeStageTestFile(t, target, "keep")
|
||||
}
|
||||
if scenario == "invalid publication" {
|
||||
m.Stages["publish"].Metadata["uploaded"] = false
|
||||
}
|
||||
if scenario == "unsafe view" {
|
||||
if err := os.Mkdir(session.LogsDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink("../../outside", filepath.Join(session.LogsDir, "extract")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
_, err := (cleanupStage{}).Run(context.Background(), env, m)
|
||||
if scenario == "disabled" && err != nil || scenario != "disabled" && err == nil {
|
||||
t.Fatalf("cleanup error = %v", err)
|
||||
}
|
||||
if scenario == "symlink runs" || scenario == "unsafe view" {
|
||||
if _, err := (cleanupStage{}).ValidateResume(context.Background(), env, m); err == nil {
|
||||
t.Fatal("unsafe inspection accepted")
|
||||
}
|
||||
}
|
||||
if data, err := os.ReadFile(target); err != nil || string(data) != "keep" {
|
||||
t.Fatalf("target = %q, %v", data, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user