| 1 | package ownermode |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 6 | "amadan.net/oficina/equipo/internal/release" |
| 7 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 8 | "errors" |
| 9 | "gorm.io/gorm" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | func TestOwnerEligibilityGuards(t *testing.T) { |
| 16 | now := time.Now() |
| 17 | for _, fault := range []string{"none", "foreign", "lookup", "stale", "future", "unavailable", "routing", "isolation", "revoked"} { |
| 18 | t.Run(fault, func(t *testing.T) { |
| 19 | c := planning.ConnectionRecord{Connection: planning.Connection{Person: "owner", RunnerID: "runner", LastSeen: now}} |
| 20 | r := runnerproto.ConnectionReport{OwnerMode: true, Available: true, Routing: runnerproto.ConnectionRouting{BindingID: "binding", AccountID: "account", ObservedAccountID: "account", CredentialHomeHash: strings.Repeat("a", 64), AccountPinned: true, NoFailover: true, TimelineRecovery: true, ProcessOwnership: true}} |
| 21 | person := "owner" |
| 22 | var lookup error |
| 23 | switch fault { |
| 24 | case "foreign": |
| 25 | person = "peer" |
| 26 | case "lookup": |
| 27 | lookup = errors.New("lookup failed") |
| 28 | case "stale": |
| 29 | c.LastSeen = now.Add(-90 * time.Second) |
| 30 | case "future": |
| 31 | c.LastSeen = now.Add(time.Second) |
| 32 | case "unavailable": |
| 33 | r.Available = false |
| 34 | case "routing": |
| 35 | r.Routing.NoFailover = false |
| 36 | case "revoked": |
| 37 | c.RevokedAt = &now |
| 38 | } |
| 39 | ok, err := Eligible(nil, c, r, now, func(runnerproto.ConnectionReport) bool { return fault != "isolation" }, func(*gorm.DB, string) (string, error) { return person, lookup }, errors.New("forbidden")) |
| 40 | if ok != (fault == "none") || fault == "lookup" && err == nil { |
| 41 | t.Fatal("eligibility guard failed", fault, ok, err) |
| 42 | } |
| 43 | }) |
| 44 | } |
| 45 | } |
| 46 | func TestOwnerEvidenceCannotLoseMode(t *testing.T) { |
| 47 | need := team.IsolationSnapshot{Mode: "owner mode", Status: "confined", ObservedTuples: []team.IsolationTuple{{Provider: "claude", ProviderVersion: "v", LauncherVersion: "v", BwrapVersion: "v", PolicyVersion: "v", IsolationConfigHash: strings.Repeat("a", 64)}}} |
| 48 | have := need |
| 49 | if !team.IsolationContains(have, need) { |
| 50 | t.Fatal("owner evidence rejected") |
| 51 | } |
| 52 | have.Mode = "" |
| 53 | if team.IsolationContains(have, need) { |
| 54 | t.Fatal("owner evidence relabelled as certified") |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // No release certifies a policy: only an owner-mode report, on the |
| 59 | // release's own versions, can become a coordinator. A report that is not |
| 60 | // owner mode is refused before its eligibility is even looked up. |
| 61 | func TestAutoCandidateIsOwnerModeOnly(t *testing.T) { |
| 62 | m := release.BuildManifest() |
| 63 | report := runnerproto.ConnectionReport{OwnerMode: true, Provider: "claude", ProviderVersion: m.Claude, LauncherVersion: m.LauncherVersion, BwrapVersion: m.BwrapVersion, PolicyVersion: m.PolicyVersion, Kind: "subscription"} |
| 64 | looked := 0 |
| 65 | eligible := func(*gorm.DB, planning.ConnectionRecord, runnerproto.ConnectionReport) (bool, error) { |
| 66 | looked++ |
| 67 | return true, nil |
| 68 | } |
| 69 | if ok, known, err := AutoCandidate(nil, planning.ConnectionRecord{}, report, m, eligible); !ok || !known || err != nil || looked != 1 { |
| 70 | t.Fatalf("the owner's report is not a candidate: %v %v %v", ok, known, err) |
| 71 | } |
| 72 | other := report |
| 73 | other.OwnerMode = false |
| 74 | if ok, known, err := AutoCandidate(nil, planning.ConnectionRecord{}, other, m, eligible); ok || known || err != nil || looked != 1 { |
| 75 | t.Fatalf("a report that is not owner mode is a candidate: %v %v %v, looked up %d times", ok, known, err, looked) |
| 76 | } |
| 77 | for name, change := range map[string]func(*runnerproto.ConnectionReport){ |
| 78 | "provider version": func(r *runnerproto.ConnectionReport) { r.ProviderVersion = "0.0.0" }, |
| 79 | "launcher": func(r *runnerproto.ConnectionReport) { r.LauncherVersion = "direct-v2" }, |
| 80 | "bwrap": func(r *runnerproto.ConnectionReport) { r.BwrapVersion = "0.10.0" }, |
| 81 | "policy": func(r *runnerproto.ConnectionReport) { r.PolicyVersion = "direct-v0" }, |
| 82 | "kind": func(r *runnerproto.ConnectionReport) { r.Kind = "api" }, |
| 83 | } { |
| 84 | stale := report |
| 85 | change(&stale) |
| 86 | if ok, _, err := AutoCandidate(nil, planning.ConnectionRecord{}, stale, m, eligible); ok || err != nil || looked != 1 { |
| 87 | t.Fatal("a report with another", name, "is a candidate") |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |