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.
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/wstest"
|
||||
)
|
||||
|
||||
func TestCLIRunsRmSendsDeleteRuns(t *testing.T) {
|
||||
var got protocol.Message
|
||||
endpoint := wstest.Server(t, func(request protocol.Message) protocol.Message {
|
||||
got = request
|
||||
return protocol.Message{Type: protocol.RunsDeleted, RunsDeleted: &protocol.RunsDeletedResult{
|
||||
Deleted: []int64{3},
|
||||
Refused: []protocol.RunDeleteRefusal{{RunID: 4, Reason: "run is running; cancel it or wait for it to finish"}},
|
||||
FreedBytes: 2048,
|
||||
}}
|
||||
})
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var out, errOut bytes.Buffer
|
||||
app := App{Out: &out, Err: &errOut, In: strings.NewReader("")}
|
||||
err := app.Run(ctx, []string{"--url", endpoint, "runs", "rm", "3", "4", "--yes", "--json"})
|
||||
var exit *ExitError
|
||||
if !errors.As(err, &exit) || exit.Code != 1 {
|
||||
t.Fatalf("err = %v, want exit 1 for a refusal", err)
|
||||
}
|
||||
if got.Type != protocol.DeleteRuns || fmt.Sprint(got.RunIDs) != "[3 4]" {
|
||||
t.Fatalf("wire request = %+v", got)
|
||||
}
|
||||
var decoded protocol.RunsDeletedResult
|
||||
if err := json.Unmarshal(out.Bytes(), &decoded); err != nil {
|
||||
t.Fatalf("stdout is not JSON: %v\n%s", err, out.String())
|
||||
}
|
||||
if fmt.Sprint(decoded.Deleted) != "[3]" || decoded.FreedBytes != 2048 || len(decoded.Refused) != 1 {
|
||||
t.Fatalf("decoded = %+v", decoded)
|
||||
}
|
||||
|
||||
// Without --yes a declined prompt sends nothing.
|
||||
got = protocol.Message{}
|
||||
declined := App{Out: &bytes.Buffer{}, Err: &bytes.Buffer{}, In: strings.NewReader("n\n")}
|
||||
if err := declined.Run(ctx, []string{"--url", endpoint, "runs", "rm", "3"}); err == nil {
|
||||
t.Fatal("declined deletion succeeded")
|
||||
}
|
||||
if got.Type != "" {
|
||||
t.Fatalf("declined deletion sent %+v", got)
|
||||
}
|
||||
if err := declined.Run(ctx, []string{"--url", endpoint, "runs", "rm", "x", "--yes"}); err == nil {
|
||||
t.Fatal("non-numeric id accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user