| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 9 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 10 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 11 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 12 | ) |
| 13 | |
| 14 | // No release certifies a policy, so the only route this runner launches |
| 15 | // is its own person's, in owner mode, confined, on this runner. Every |
| 16 | // other selection is refused before an account or a workspace is opened. |
| 17 | func TestLaunchRefusesAnyoneButTheOwner(t *testing.T) { |
| 18 | owner := runnerproto.Selection{Provider: "claude", Person: "ana", RunnerID: "runner-1", Isolation: team.IsolationSnapshot{Mode: "owner mode", Status: "confined"}} |
| 19 | if !ownerRoute("ana", "runner-1", owner) { |
| 20 | t.Fatal("the owner's confined route is not an owner route") |
| 21 | } |
| 22 | r := &Runtime{cfg: configFixture(t), api: &HTTPInstance{bearer: runnerproto.RunnerCredential{Person: "ana", RunnerID: "runner-1"}}} |
| 23 | for name, change := range map[string]func(*runnerproto.Selection){ |
| 24 | "another person": func(s *runnerproto.Selection) { s.Person = "ben" }, |
| 25 | "no person": func(s *runnerproto.Selection) { s.Person = "" }, |
| 26 | "another runner": func(s *runnerproto.Selection) { s.RunnerID = "runner-2" }, |
| 27 | "no runner": func(s *runnerproto.Selection) { s.RunnerID = "" }, |
| 28 | "tainted runner": func(s *runnerproto.Selection) { s.Isolation.RunnerTainted = true }, |
| 29 | "not confined": func(s *runnerproto.Selection) { s.Isolation.Status = "unconfined" }, |
| 30 | "no mode": func(s *runnerproto.Selection) { s.Isolation.Mode = "" }, |
| 31 | "a verified mode": func(s *runnerproto.Selection) { s.Isolation.Mode = "verified" }, |
| 32 | } { |
| 33 | t.Run(name, func(t *testing.T) { |
| 34 | s := owner |
| 35 | change(&s) |
| 36 | if ownerRoute("ana", "runner-1", s) { |
| 37 | t.Fatal("an owner route") |
| 38 | } |
| 39 | // A named mode that is not the owner's is an identity error; no |
| 40 | // mode at all used to mean "certified", and is now refused. |
| 41 | want := provider.ErrUncertified |
| 42 | if s.Isolation.Mode != "" { |
| 43 | want = provider.ErrIdentity |
| 44 | } |
| 45 | claim := RunClaim{Assignment: runnerproto.Assignment{StepKey: "implement", Selection: s}} |
| 46 | if _, cleanup, err := r.directLaunch(context.Background(), claim, workspace.Workspace{Path: t.TempDir()}, &ToolProxy{}); !errors.Is(err, want) || cleanup != nil { |
| 47 | t.Fatalf("direct launch: %v, cleanup %v", err, cleanup != nil) |
| 48 | } |
| 49 | coordinator := runnerproto.CoordinatorClaim{Brief: runnerproto.RunnerBrief{Selection: s}, Binding: runnerproto.CoordinatorBinding{Person: s.Person}} |
| 50 | if _, cleanup, err := r.coordinatorLaunch(context.Background(), coordinator, &ToolProxy{}); !errors.Is(err, want) || cleanup != nil { |
| 51 | t.Fatalf("coordinator launch: %v, cleanup %v", err, cleanup != nil) |
| 52 | } |
| 53 | }) |
| 54 | } |
| 55 | // With no instance connection there is no owner to match. |
| 56 | if _, _, err := (&Runtime{cfg: r.cfg}).directLaunch(context.Background(), RunClaim{Assignment: runnerproto.Assignment{StepKey: "implement", Selection: runnerproto.Selection{Provider: "claude", Person: "ana", RunnerID: "runner-1", Isolation: team.IsolationSnapshot{Status: "confined"}}}}, workspace.Workspace{}, &ToolProxy{}); !errors.Is(err, provider.ErrUncertified) { |
| 57 | t.Fatalf("launch without an instance: %v", err) |
| 58 | } |
| 59 | } |
| 60 | |