59 lines
2.0 KiB
Go
59 lines
2.0 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// A run must never fail because the runner persisted the same settled visit
|
|
// twice: an ambiguous commit, a retried finish or a late settlement can repeat
|
|
// the write. Identical evidence is a replay; different evidence still fails.
|
|
func TestFinishStepAcceptsAnIdenticalEvidenceReplay(t *testing.T) {
|
|
ctx := context.Background()
|
|
s, err := Open(filepath.Join(t.TempDir(), "replay.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer s.Close()
|
|
if _, err = s.Submit(ctx, "audit", Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := s.List(ctx)
|
|
def, _ := s.Get(ctx, items[0].ID, 1)
|
|
id, _, err := s.CreateRun(ctx, def, "key", "workspace", false, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = s.BeginStep(ctx, id, 1, "one", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
changes := &protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{{Path: "a.txt", Kind: "added", Patch: "+a\n"}}}
|
|
step := protocol.RunStep{
|
|
Sequence: 1, Status: "running", FileChanges: changes,
|
|
Artifacts: []protocol.StageArtifact{{Name: "out.txt", SizeBytes: 3, ContentType: "text/plain", URL: "/artifacts/1/1/out.txt"}},
|
|
Control: &protocol.ControlEvidence{Visit: 1},
|
|
}
|
|
if err = s.FinishStep(ctx, id, step); err != nil {
|
|
t.Fatalf("first evidence write: %v", err)
|
|
}
|
|
step.Status = "running"
|
|
if err = s.FinishStep(ctx, id, step); err != nil {
|
|
t.Fatalf("identical replay must be accepted, got %v", err)
|
|
}
|
|
step.FileChanges = &protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{{Path: "a.txt", Kind: "modified", Patch: "-a\n+b\n"}}}
|
|
step.Status = "running"
|
|
if err = s.FinishStep(ctx, id, step); err != nil {
|
|
t.Fatalf("a later settlement of the same visit must persist: %v", err)
|
|
}
|
|
run, err := s.GetRun(ctx, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := run.Steps[0].FileChanges; got == nil || len(got.Files) != 1 || got.Files[0].Kind != "modified" {
|
|
t.Fatalf("latest evidence not stored: %+v", got)
|
|
}
|
|
}
|