| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/alta/homeop" |
| 10 | ) |
| 11 | |
| 12 | // The engine half of the adverse suite: whatever the CARLOS half does |
| 13 | // wrong or fails to do, an adopted app's activation is never signed and |
| 14 | // Home never commits a directory.activate-consumer for any app. |
| 15 | |
| 16 | // neverActivated fails if Home was asked to activate any consumer. |
| 17 | func (a *adoptHarness) neverActivated() { |
| 18 | a.t.Helper() |
| 19 | for _, r := range a.home.Requests() { |
| 20 | if strings.HasPrefix(r, homeop.DirectoryActivate) { |
| 21 | a.t.Fatalf("Home committed %s", r) |
| 22 | } |
| 23 | } |
| 24 | if a.home.Calls[homeop.DirectoryActivate] != 0 { |
| 25 | a.t.Fatal("Home was sent an activation") |
| 26 | } |
| 27 | for _, s := range Slugs() { |
| 28 | if st := a.home.InstanceState(s); st == "live" { |
| 29 | a.t.Fatalf("%s is live", s) |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // gateCodes is every code the activation gate refuses with. The family |
| 35 | // prefixes (identity-, drift-, config-, team-mode-, agent-key-) carry a |
| 36 | // field or key name after them. |
| 37 | var gateCodes = []string{ |
| 38 | "unknown-app", "team-not-sqid", |
| 39 | "missing-home-preflight", "missing-baseline", "missing-write", "missing-after", "missing-delivery-check", |
| 40 | "rollback-recorded", "issuer-mismatch", "team-mismatch", "canary-not-active", "artifact-unreviewed", |
| 41 | "identity-storage", "identity-instance", "identity-alias", "identity-environment", "identity-owned", |
| 42 | "identity-channel", "identity-region", "identity-moving", "identity-record", |
| 43 | "after-not-preserved", "drift-changed:X_KEY", "drift-gone:X_KEY", "config-added:X_KEY", |
| 44 | "team-mode-X_TEAM_ROSTER", "agent-key-X_AGENT_TEAM", "agent-key-X_AGENT_ISSUER", |
| 45 | "delivery-check-superseded", "not-delivered", "version-header-mismatch", "version-unacceptable", "artifact-mismatch", |
| 46 | } |
| 47 | |
| 48 | // Each code the gate can refuse with parks the activation under that |
| 49 | // code, and nothing is activated. The canary is the app that goes first; |
| 50 | // one code is repeated on a later app. |
| 51 | func TestAdverseActivationParksOnEveryGateCode(t *testing.T) { |
| 52 | for _, app := range []string{"calendar", "docs"} { |
| 53 | for _, code := range gateCodes { |
| 54 | if app == "docs" && code != "canary-not-active" { |
| 55 | continue |
| 56 | } |
| 57 | t.Run(app+"/"+code, func(t *testing.T) { |
| 58 | a := newAdoption(t) |
| 59 | obs := newObserver() |
| 60 | obs.set(&testRefusal{codes: []string{code}}) |
| 61 | e := a.adopter(nil, obs) |
| 62 | a.planA(e) |
| 63 | run := a.activate(e, app) |
| 64 | r := a.drive(e, run, 3) |
| 65 | if r.State != "parked" || r.Code != "observe-"+code { |
| 66 | t.Fatalf("run %s %q, want parked: observe-%s", r.State, r.Code, code) |
| 67 | } |
| 68 | if s := a.step(app, StepActivate); s.State != "parked" || s.Code != "observe-"+code { |
| 69 | t.Fatalf("step %s %q", s.State, s.Code) |
| 70 | } |
| 71 | if obs.count() == 0 { |
| 72 | t.Fatal("the observer was never asked") |
| 73 | } |
| 74 | a.neverActivated() |
| 75 | }) |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Several refusals at once are all kept, and none is dropped for the |
| 81 | // first. |
| 82 | func TestAdverseActivationKeepsEveryRefusalCode(t *testing.T) { |
| 83 | a := newAdoption(t) |
| 84 | obs := newObserver() |
| 85 | obs.set(&testRefusal{codes: []string{"after-not-preserved", "delivery-check-superseded", "identity-storage"}}) |
| 86 | e := a.adopter(nil, obs) |
| 87 | a.planA(e) |
| 88 | run := a.activate(e, "calendar") |
| 89 | if r := a.drive(e, run, 3); r.State != "parked" || r.Code != "observe-after-not-preserved,delivery-check-superseded,identity-storage" { |
| 90 | t.Fatalf("run %s %q", r.State, r.Code) |
| 91 | } |
| 92 | a.neverActivated() |
| 93 | } |
| 94 | |
| 95 | // A refusal that carries no code is still a refusal. |
| 96 | func TestAdverseActivationRefusalWithoutCodesStillParks(t *testing.T) { |
| 97 | a := newAdoption(t) |
| 98 | obs := newObserver() |
| 99 | obs.set(&testRefusal{}) |
| 100 | e := a.adopter(nil, obs) |
| 101 | a.planA(e) |
| 102 | run := a.activate(e, "calendar") |
| 103 | if r := a.drive(e, run, 3); r.State != "parked" || r.Code != "observe-" { |
| 104 | t.Fatalf("run %s %q, want parked: observe-", r.State, r.Code) |
| 105 | } |
| 106 | a.neverActivated() |
| 107 | } |
| 108 | |
| 109 | // An observer that cannot read the app never lets the activation through. |
| 110 | func TestAdverseActivationObserverUnavailable(t *testing.T) { |
| 111 | for name, err := range map[string]error{ |
| 112 | "plain error": errors.New("carlos unreachable"), |
| 113 | "wrapped": errors.Join(errors.New("read"), errors.New("timeout")), |
| 114 | } { |
| 115 | t.Run(name, func(t *testing.T) { |
| 116 | a := newAdoption(t) |
| 117 | obs := newObserver() |
| 118 | obs.set(err) |
| 119 | e := a.adopter(nil, obs) |
| 120 | a.planA(e) |
| 121 | run := a.activate(e, "calendar") |
| 122 | if r := a.drive(e, run, 3); r.State != "parked" || r.Code != "observer-unavailable" { |
| 123 | t.Fatalf("run %s %q", r.State, r.Code) |
| 124 | } |
| 125 | a.neverActivated() |
| 126 | }) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // An observation Home would refuse is never signed, whatever it is |
| 131 | // shaped like. |
| 132 | func TestAdverseActivationRefusesMalformedObservation(t *testing.T) { |
| 133 | good := goodObservation |
| 134 | cases := map[string]Observation{ |
| 135 | "empty": {}, |
| 136 | "version empty": {ArtifactDigest: good.ArtifactDigest, EnvironmentBinding: good.EnvironmentBinding}, |
| 137 | "version with a space": {AppVersion: "v1 4", ArtifactDigest: good.ArtifactDigest, EnvironmentBinding: good.EnvironmentBinding}, |
| 138 | "version leads dash": {AppVersion: "-v1", ArtifactDigest: good.ArtifactDigest, EnvironmentBinding: good.EnvironmentBinding}, |
| 139 | "version too long": {AppVersion: "v" + strings.Repeat("1", 128), ArtifactDigest: good.ArtifactDigest, EnvironmentBinding: good.EnvironmentBinding}, |
| 140 | "artifact not base64": {AppVersion: good.AppVersion, ArtifactDigest: "not base64!", EnvironmentBinding: good.EnvironmentBinding}, |
| 141 | "artifact empty": {AppVersion: good.AppVersion, EnvironmentBinding: good.EnvironmentBinding}, |
| 142 | "binding not base64": {AppVersion: good.AppVersion, ArtifactDigest: good.ArtifactDigest, EnvironmentBinding: "not base64!"}, |
| 143 | "binding empty": {AppVersion: good.AppVersion, ArtifactDigest: good.ArtifactDigest}, |
| 144 | } |
| 145 | for name, bad := range cases { |
| 146 | t.Run(name, func(t *testing.T) { |
| 147 | a := newAdoption(t) |
| 148 | e := a.adopter(nil, &fakeObserver{obs: bad}) |
| 149 | a.planA(e) |
| 150 | run := a.activate(e, "calendar") |
| 151 | if r := a.drive(e, run, 3); r.State != "failed" || r.Code != "observation-shape" { |
| 152 | t.Fatalf("run %s %q", r.State, r.Code) |
| 153 | } |
| 154 | a.neverActivated() |
| 155 | }) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Without an observer, nothing activates, however the run came to be. |
| 160 | func TestAdverseActivationNeedsAnObserver(t *testing.T) { |
| 161 | a := newAdoption(t) |
| 162 | e := a.adopter(nil, newObserver()) |
| 163 | a.planA(e) |
| 164 | run := a.activate(e, "calendar") |
| 165 | bare := a.adopter(nil, nil) |
| 166 | if r := a.drive(bare, run, 3); r.State != "failed" || r.Code != "no-observer" { |
| 167 | t.Fatalf("run %s %q", r.State, r.Code) |
| 168 | } |
| 169 | a.neverActivated() |
| 170 | _, err := bare.ActivateAdopted(a.ctx(), []string{"calendar"}) |
| 171 | failCode(t, err, "no-observer") |
| 172 | a.neverActivated() |
| 173 | } |
| 174 | |
| 175 | // An attempt that lapsed while the gate was refusing is verified again |
| 176 | // before any observation is asked for, and still nothing activates. |
| 177 | func TestAdverseActivationLapsedAttemptNeverActivates(t *testing.T) { |
| 178 | a := newAdoption(t) |
| 179 | obs := newObserver() |
| 180 | obs.set(&testRefusal{codes: []string{"missing-delivery-check"}}) |
| 181 | e := a.adopter(nil, obs) |
| 182 | a.planA(e) |
| 183 | run := a.activate(e, "calendar") |
| 184 | a.drive(e, run, 2) |
| 185 | a.tick(600 * time.Second) |
| 186 | if r := a.drive(e, run, 1); r.State != "parked" || r.Code != "attempt-expired" { |
| 187 | t.Fatalf("run %s %q", r.State, r.Code) |
| 188 | } |
| 189 | a.neverActivated() |
| 190 | } |
| 191 | |
| 192 | // A run that is out of order, for an app already disabled or active, or |
| 193 | // for a team not yet seeded, is refused before any plan is made. |
| 194 | func TestAdverseActivationOutOfOrderNeverPlans(t *testing.T) { |
| 195 | t.Run("not registered", func(t *testing.T) { |
| 196 | a := newAdoption(t, "sheets", "slides", "calendar", "calls", "memoria") |
| 197 | e := a.adopter(nil, newObserver()) |
| 198 | run, err := e.StartAdoption(a.ctx(), "Oficina", ownerCanary) |
| 199 | if err != nil { |
| 200 | t.Fatal(err) |
| 201 | } |
| 202 | a.confirm(homeop.KindCreateTeam) |
| 203 | a.drive(e, run, 10) |
| 204 | a.tick(homeop.WindowLifetime * time.Second) |
| 205 | _, err = e.ActivateAdopted(a.ctx(), []string{"docs"}) |
| 206 | failCode(t, err, "not-registered") |
| 207 | a.neverActivated() |
| 208 | }) |
| 209 | t.Run("no team", func(t *testing.T) { |
| 210 | a := newAdoption(t) |
| 211 | _, err := a.adopter(nil, newObserver()).ActivateAdopted(a.ctx(), []string{"calendar"}) |
| 212 | failCode(t, err, "no-team") |
| 213 | a.neverActivated() |
| 214 | }) |
| 215 | t.Run("already active", func(t *testing.T) { |
| 216 | a := newAdoption(t) |
| 217 | e := a.adopter(nil, newObserver()) |
| 218 | a.planA(e) |
| 219 | run := a.activate(e, "calendar") |
| 220 | if r := a.drive(e, run, 5); r.State != "done" { |
| 221 | t.Fatalf("run %s %q", r.State, r.Code) |
| 222 | } |
| 223 | before := countOf(a.home.Requests(), homeop.DirectoryActivate+":calendar") |
| 224 | a.tick(homeop.WindowLifetime * time.Second) |
| 225 | _, err := e.ActivateAdopted(a.ctx(), []string{"calendar"}) |
| 226 | failCode(t, err, "app-active") |
| 227 | if countOf(a.home.Requests(), homeop.DirectoryActivate+":calendar") != before { |
| 228 | t.Fatal("an active app was activated again") |
| 229 | } |
| 230 | }) |
| 231 | } |
| 232 | |