package equipotest import ( "context" "errors" "net/http/httptest" "os" "path/filepath" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/equipo/team" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runnerproto" ) func TestCancelTaskFencesBeforeAcknowledgement(t *testing.T) { f := runnerFixtureNew(t, true) report, err := f.s.CancelTask(connectionContext, ownerActor, f.a.TaskID) executionMust(t, err) if report.Contained { t.Fatal("cancel acknowledged before process verification") } if err := f.x.Core.ValidateHandle(connectionContext, f.claim.Scope, f.claim.Handle); err == nil { t.Fatal("cancel retained authority") } attempts, err := f.x.Core.Attempts(connectionContext, f.claim.Scope) executionMust(t, err) if len(attempts) != 1 || attempts[0].Containment == team.ContainmentContained { t.Fatal("cancel fabricated containment") } if _, err = f.s.CancelTask(connectionContext, equipo.Actor{Address: "stranger@example.com"}, f.a.TaskID); err == nil { t.Fatal("stranger cancelled") } } func TestRunnerBugFailureEndsFixedRun(t *testing.T) { f := runnerFixtureNew(t, true) q := f.request() q.Result = &team.AttemptResult{Outcome: team.OutcomeFailure} _, err := f.x.Mutate(connectionContext, f.principal, "complete", q) executionMust(t, err) job, err := f.x.Core.GetJob(connectionContext, f.claim.Scope) executionMust(t, err) if job.State != team.JobFailed { t.Fatal("fixed run invented another attempt", job.State) } } func TestCancelLostRotationReply(t *testing.T) { f := runnerFixtureNew(t, true) f.x.SetRunnerValidators(equipo.RunnerValidators{Containment: func(_ context.Context, _ equipo.Assignment, _ equipo.RunnerRequest) error { return nil }}) q := f.request() q.RequestID = "lost-rotation" q.Proof = flowJSON(runnerproto.ContainmentProof{Version: 2, Scope: q.Scope, Handle: q.Handle, CheckedAt: time.Now().UTC()}) _, err := f.x.Cleanup(connectionContext, f.claim.CleanupToken, "contained", q) executionMust(t, err) altered := q altered.Proof = []byte(`{"changed":true}`) if _, err := f.x.Cleanup(connectionContext, f.claim.CleanupToken, "contained", altered); err == nil { t.Fatal("changed containment recovered credentials") } value, err := f.x.Cleanup(connectionContext, f.claim.CleanupToken, "contained", q) executionMust(t, err) rotated := value.(equipo.CredentialRotation) if rotated.Bearer.Token == "" { t.Fatal("lost rotation did not recover") } principal, err := f.s.AuthenticateRunner(connectionContext, rotated.Bearer.Token) executionMust(t, err) if _, err = f.x.Mutate(connectionContext, principal, "renew", q); err == nil { t.Fatal("rotation opened admission before journal acknowledgement") } executionMust(t, f.x.AcknowledgeRotation(connectionContext, principal, q.RequestID)) if _, err = f.x.Cleanup(connectionContext, f.claim.CleanupToken, "contained", q); err == nil { t.Fatal("old cleanup recovered after acknowledgement") } } func TestCancelUnstartedTaskComplete(t *testing.T) { f := runnerFixtureNew(t, false) report, err := f.s.CancelTask(connectionContext, ownerActor, f.a.TaskID) executionMust(t, err) task, err := f.s.Task(connectionContext, ownerActor, f.a.TaskID) executionMust(t, err) if !report.Contained || task.State != "cancelled" { t.Fatal("unstarted task retained stop request", report.Contained, task.State) } } func TestRunnerRestartLostClaimReply(t *testing.T) { f := runnerFixtureNew(t, true) f.clock.Advance(61 * time.Second) q := runnerproto.ClaimRequest{Version: 2, AssignmentID: f.a.ID, Revision: f.a.Revision, RequestID: "claim"} recovered, err := f.x.RecoverClaim(connectionContext, f.principal, q) executionMust(t, err) if recovered.CleanupToken == "" || !recovered.LeaseDeadline.IsZero() || !recovered.AttemptDeadline.IsZero() || recovered.Handle.Attempt != f.claim.Handle.Attempt { t.Fatal("recovery restored dispatch authority") } q.RequestID = "other" if reply, e := f.x.RecoverClaim(connectionContext, f.principal, q); e != nil || !reply.NotClaimed || reply.CleanupToken != "" { t.Fatal("foreign claim request recovered authority") } } func TestRunnerHTTPExpiredLeaseDefinitive(t *testing.T) { f := runnerFixtureNew(t, true) token := runnerBearerFor(t, f) server := httptest.NewServer(f.s.RunnerHandler()) defer server.Close() dir := t.TempDir() executionMust(t, os.Chmod(dir, 0700)) journal, err := runner.OpenJournal(connectionContext, filepath.Join(dir, "journal.db")) executionMust(t, err) defer journal.Close() api, err := runner.NewHTTPInstance(server.URL, journal, runnerproto.RunnerCredential{Token: token, ExpiresAt: time.Now().Add(time.Hour)}) executionMust(t, err) f.clock.Advance(61 * time.Second) if err = api.Call(connectionContext, "validate", f.request(), nil); !errors.Is(err, provider.ErrIdentity) { t.Fatal("expired lease was not a definitive denial", err) } }