Files
pipelines/internal/server/run_delete_test.go
T
alan 31e89e872b Add run size_bytes and delete_runs to free disk space
Every run summary carries size_bytes (artifact tree, server-created Pi
session directories and an estimate of the run's database rows), cached in
pipeline_runs.size_bytes, invalidated by triggers, measured when a run
finishes and backfilled in the background. delete_runs removes terminal
runs with every scoped row and server-owned file, refuses active ones with
a reason, and pushes runs_removed to subscribers. pipelines runs rm is the
CLI form; retention apply now shares the complete row deletion.
2026-10-31 22:11:14 +00:00

127 lines
4.2 KiB
Go

package server
import (
"context"
"fmt"
"os"
"path/filepath"
"sync/atomic"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
)
// TestDeleteRunsOverWebSocket covers the wire contract: list_runs carries a
// measured size_bytes, delete_runs deletes a finished run (rows and artifact
// files), refuses an active run and an unknown id, and a second subscribed
// connection receives runs_removed for the deleted run.
func TestDeleteRunsOverWebSocket(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
store, handler, ids := eventStore(t, ctx, "one")
var block atomic.Bool
release := make(chan struct{})
defer close(release)
handler.runner.execute = func(ctx context.Context, _ stage.Definition, _ stage.Values) (stage.Result, error) {
if block.Load() {
select {
case <-release:
case <-ctx.Done():
return stage.Result{}, ctx.Err()
}
}
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
}
workspace := eventWorkspace(t)
finished, err := handler.runner.start(protocol.Message{PipelineID: ids[0], Version: 1, RequestKey: "del-1", Workspace: workspace, NotifyMode: "off", Input: map[string]any{"prompt": "done"}})
if err != nil {
t.Fatal(err)
}
handler.runner.wg.Wait()
artifacts := filepath.Join(handler.runner.artifactsRoot, fmt.Sprint(finished.ID))
if err := os.MkdirAll(filepath.Join(artifacts, "extra"), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(artifacts, "extra", "big.bin"), make([]byte, 50_000), 0o600); err != nil {
t.Fatal(err)
}
// Files written outside a recorded change do not invalidate the cache, so
// re-measure explicitly to include them.
if _, err := store.RefreshRunSize(ctx, handler.runner.artifactsRoot, finished.ID); err != nil {
t.Fatal(err)
}
block.Store(true)
// A different workspace: one run per workspace at a time.
active, err := handler.runner.start(protocol.Message{PipelineID: ids[0], Version: 1, RequestKey: "del-2", Workspace: eventWorkspace(t), NotifyMode: "off", Input: map[string]any{"prompt": "busy"}})
if err != nil {
t.Fatal(err)
}
watcher := dialRunEvents(t, ctx, handler)
watcher.send(protocol.Message{Type: protocol.Subscribe, ID: "sub"})
snapshotSize := int64(0)
for {
message := watcher.read()
if message.Type == protocol.Subscribe {
break
}
if message.Type == protocol.RunEvent && message.RunEvent.RunID == finished.ID {
snapshotSize = message.RunEvent.SizeBytes
}
}
if snapshotSize < 50_000 {
t.Fatalf("snapshot size_bytes = %d, want the measured artifacts", snapshotSize)
}
operator := dialRunEvents(t, ctx, handler)
operator.send(protocol.Message{Type: protocol.ListRuns, ID: "list"})
list := operator.read()
if list.Type != protocol.Runs {
t.Fatalf("list reply = %+v", list)
}
for _, run := range list.Runs {
if run.ID == finished.ID && run.SizeBytes != snapshotSize {
t.Fatalf("list size_bytes = %d, want %d", run.SizeBytes, snapshotSize)
}
}
operator.send(protocol.Message{Type: protocol.DeleteRuns, ID: "del", RunIDs: []int64{finished.ID, active.ID, 4242}})
reply := operator.read()
if reply.Type != protocol.RunsDeleted || reply.ID != "del" || reply.RunsDeleted == nil {
t.Fatalf("delete reply = %+v", reply)
}
result := reply.RunsDeleted
if fmt.Sprint(result.Deleted) != fmt.Sprint([]int64{finished.ID}) || len(result.Refused) != 2 || result.FreedBytes < 50_000 {
t.Fatalf("delete result = %+v", result)
}
reasons := map[int64]string{}
for _, refusal := range result.Refused {
reasons[refusal.RunID] = refusal.Reason
}
if reasons[active.ID] == "" || reasons[4242] != "run not found" {
t.Fatalf("refusals = %+v", result.Refused)
}
for {
message := watcher.read()
if message.Type != protocol.RunsRemoved {
continue
}
if fmt.Sprint(message.RunIDs) != fmt.Sprint([]int64{finished.ID}) {
t.Fatalf("runs_removed = %v", message.RunIDs)
}
break
}
if _, err := os.Stat(artifacts); !os.IsNotExist(err) {
t.Fatalf("artifacts remain: %v", err)
}
if _, err := store.GetRun(ctx, finished.ID); err == nil {
t.Fatal("deleted run still readable")
}
if _, err := store.GetRun(ctx, active.ID); err != nil {
t.Fatalf("active run lost: %v", err)
}
}