| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/alta/homeop" |
| 10 | family "amadan.net/rastrillo/rastrillo/crypto" |
| 11 | ) |
| 12 | |
| 13 | // Tests added for Fable's review of b1c508b (oficina/alta #1). |
| 14 | |
| 15 | // Blocker 1: the address goes once the seed has committed and every |
| 16 | // allowlist is written, even while an app is still parked — here Docs, |
| 17 | // waiting on a /api/team that Docs on main does not serve. |
| 18 | func TestOwnerForgottenWhileAnAppIsParked(t *testing.T) { |
| 19 | h := newHarness(t) |
| 20 | h.initialized() |
| 21 | h.con.NoTeamProbe = true |
| 22 | e := h.engine(nil) |
| 23 | run := h.createLagged(e) |
| 24 | r := h.drive(e, run, 40) |
| 25 | if r.State != "parked" { |
| 26 | t.Fatalf("run %s %s", r.State, r.Code) |
| 27 | } |
| 28 | for _, app := range allApps { |
| 29 | if st, _, _ := h.store.Step(h.ctx(), "lagged", app, StepVerify); st.Code != "no-readiness-probe" { |
| 30 | t.Fatalf("%s verify %s %s", app, st.State, st.Code) |
| 31 | } |
| 32 | } |
| 33 | var n int |
| 34 | for q, what := range map[string]string{ |
| 35 | "SELECT count(*) FROM owner_inputs": "owner input", |
| 36 | "SELECT count(*) FROM operator_requests WHERE action='credential.owner-seed'": "owner-seed request", |
| 37 | "SELECT count(*) FROM plan_submissions WHERE role='credential' AND sealed_plan IS NOT NULL AND plan_id IN (SELECT plan_id FROM runs WHERE kind='create-team')": "credential plan", |
| 38 | } { |
| 39 | if err := h.store.DB().QueryRow(q).Scan(&n); err != nil || n != 0 { |
| 40 | t.Fatalf("%s kept while Docs is parked (%d, %v)", what, n, err) |
| 41 | } |
| 42 | } |
| 43 | _, _ = h.store.DB().Exec("PRAGMA wal_checkpoint(TRUNCATE)") |
| 44 | if h.scan(ownerCanary) { |
| 45 | t.Fatal("owner address in Alta's database files") |
| 46 | } |
| 47 | // Every app is in roster mode: no environment holds the address. |
| 48 | for _, app := range allApps { |
| 49 | vars, _, _ := h.con.Env(app, "lagged") |
| 50 | for k, v := range vars { |
| 51 | if v.Value == ownerCanary { |
| 52 | t.Fatalf("%s %s holds the owner's address", app, k) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Blocker 2: the box's serving name (carlos ship's release version) and |
| 59 | // /api/version (the binary's git describe) need not agree. Step 10 records |
| 60 | // both and notes a difference; activation reads app_version from |
| 61 | // /api/version and artifact_digest from the box's name. |
| 62 | func TestServingNamesMayDiffer(t *testing.T) { |
| 63 | h := newHarness(t) |
| 64 | h.initialized() |
| 65 | for _, a := range allApps { |
| 66 | h.con.ServingName[a] = "v7" |
| 67 | } |
| 68 | e := h.engine(nil) |
| 69 | run := h.createLagged(e) |
| 70 | if r := h.drive(e, run, 40); r.State != "done" { |
| 71 | t.Fatalf("%s %s", r.State, r.Code) |
| 72 | } |
| 73 | d, _, _ := h.store.Done(h.ctx(), "lagged", "docs", StepServing) |
| 74 | if str(d, "serving") != "v7" || str(d, "app_version") != h.con.Versions["docs"] || d["names_differ"] != true { |
| 75 | t.Fatalf("serving step %v", d) |
| 76 | } |
| 77 | if h.home.InstanceState(ConsumerID("lagged", "docs")) != "live" { |
| 78 | t.Fatal("docs not activated") |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Blocker 3: a mistyped owner burns the label, and the team page's owner |
| 83 | // form and a new plan bring it back. |
| 84 | func TestMistypedOwnerIsCorrectedUnderTheSameLabel(t *testing.T) { |
| 85 | h := newHarness(t) |
| 86 | h.initialized() |
| 87 | e := h.engine(nil) |
| 88 | _, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: "til-typo@lagged.test", Apps: []string{"sheets"}}) |
| 89 | var fl *Fail |
| 90 | if !errors.As(err, &fl) || fl.Code != "plan-refused-credential" { |
| 91 | t.Fatalf("typo: %v", err) |
| 92 | } |
| 93 | runs, _ := h.store.Runs(h.ctx(), "lagged") |
| 94 | if len(runs) != 1 || runs[0].State != "failed" { |
| 95 | t.Fatalf("runs %+v", runs) |
| 96 | } |
| 97 | // Home's directory accepted the first plan's half and keeps it |
| 98 | // pending until it expires; a new plan waits that out (Home answers |
| 99 | // 409 meanwhile, and Alta retries the signed plan). |
| 100 | h.tick(homeop.PlanLifetime * time.Second) |
| 101 | if err := h.store.SetOwner(h.ctx(), "lagged", ownerCanary); err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | if err := e.Resubmit(h.ctx(), runs[0].ID); err != nil { |
| 105 | t.Fatalf("resubmit: %v", err) |
| 106 | } |
| 107 | h.confirm(homeop.KindCreateTeam) |
| 108 | if r := h.drive(e, runs[0].ID, 30); r.State != "done" { |
| 109 | t.Fatalf("%s %s", r.State, r.Code) |
| 110 | } |
| 111 | if _, _, ok := h.home.Team("lagged"); !ok { |
| 112 | t.Fatal("no team at Home") |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Blocker 4: a record that does not verify under the pinned provisioning |
| 117 | // key, or binds anything but what Alta asked for, is refused before any |
| 118 | // environment write. |
| 119 | func TestRecordVerification(t *testing.T) { |
| 120 | for name, spoil := range map[string]func(*harness){ |
| 121 | "another signing key": func(h *harness) { h.home.RecordSigner, _ = family.Generate() }, |
| 122 | "another audience": func(h *harness) { h.home.RecordAudience = "https://other.docs.oficina.works" }, |
| 123 | } { |
| 124 | t.Run(name, func(t *testing.T) { |
| 125 | h := newHarness(t) |
| 126 | h.initialized() |
| 127 | spoil(h) |
| 128 | e := h.engine(nil) |
| 129 | run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: ownerCanary, Apps: []string{"docs"}}) |
| 130 | if err != nil { |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | h.confirm(homeop.KindCreateTeam) |
| 134 | r := h.drive(e, run, 10) |
| 135 | if r.State != "failed" || r.Code != "record-unverified" { |
| 136 | t.Fatalf("%s %s", r.State, r.Code) |
| 137 | } |
| 138 | if _, _, ok := h.con.Env("docs", "lagged"); ok { |
| 139 | t.Fatal("an environment was written from an unverified record") |
| 140 | } |
| 141 | for _, req := range h.con.Requests { |
| 142 | if strings.HasPrefix(req, "PUT ") { |
| 143 | t.Fatalf("console write %q", req) |
| 144 | } |
| 145 | } |
| 146 | }) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // A request whose answer never came back, replayed after the window |
| 151 | // closed and found never committed, parks for a new plan; it is not a |
| 152 | // failure. |
| 153 | func TestUnansweredRequestAfterTheWindowParks(t *testing.T) { |
| 154 | h := newHarness(t) |
| 155 | h.initialized() |
| 156 | down := true |
| 157 | h.home.Outage = func(a string) bool { return down && a == homeop.CredentialRegister } |
| 158 | e := h.engine(nil) |
| 159 | run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: ownerCanary, Apps: []string{"sheets"}}) |
| 160 | if err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | h.confirm(homeop.KindCreateTeam) |
| 164 | _ = e.Advance(h.ctx(), run) |
| 165 | h.tick(homeop.WindowLifetime * time.Second) |
| 166 | down = false |
| 167 | _ = e.Advance(h.ctx(), run) |
| 168 | r, _ := h.store.Run(h.ctx(), run) |
| 169 | st, _, _ := h.store.Step(h.ctx(), "lagged", "sheets", StepRegister) |
| 170 | if r.State != "parked" || st.Code != "window-expired" { |
| 171 | t.Fatalf("run %s %s, register %s %s", r.State, r.Code, st.State, st.Code) |
| 172 | } |
| 173 | next, err := e.AddApps(h.ctx(), "lagged", []string{"sheets"}, RunOptions{}) |
| 174 | if err != nil { |
| 175 | t.Fatal(err) |
| 176 | } |
| 177 | h.confirm(homeop.KindAddApps) |
| 178 | if r := h.drive(e, next, 30); r.State != "done" { |
| 179 | t.Fatalf("%s %s", r.State, r.Code) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Fix review notes: step 10 parks while the box reports the instance not |
| 184 | // awake, and while /api/version answers rastrillo's unstamped "dev". |
| 185 | func TestServingParksAsleepAndUnstamped(t *testing.T) { |
| 186 | h := newHarness(t) |
| 187 | h.initialized() |
| 188 | e := h.engine(nil) |
| 189 | run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: ownerCanary, Apps: []string{"sheets"}}) |
| 190 | if err != nil { |
| 191 | t.Fatal(err) |
| 192 | } |
| 193 | h.confirm(homeop.KindCreateTeam) |
| 194 | route := RouteHost("lagged", "sheets", account) |
| 195 | for i := 0; i < 10; i++ { |
| 196 | _ = e.Advance(h.ctx(), run) |
| 197 | h.con.Walk() |
| 198 | h.con.SetAsleep(route, true) |
| 199 | } |
| 200 | if st, _, _ := h.store.Step(h.ctx(), "lagged", "sheets", StepServing); st.State != "parked" || st.Code != "instance-waking" { |
| 201 | t.Fatalf("asleep: serving %s %s", st.State, st.Code) |
| 202 | } |
| 203 | h.con.SetAsleep(route, false) |
| 204 | h.con.VersionAnswer["sheets"] = "dev" |
| 205 | _ = e.Advance(h.ctx(), run) |
| 206 | if st, _, _ := h.store.Step(h.ctx(), "lagged", "sheets", StepServing); st.State != "parked" || st.Code != "version-unstamped" { |
| 207 | t.Fatalf("dev: serving %s %s", st.State, st.Code) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // Home (0cee9ba) admits an observed printable version token; step 10 |
| 212 | // mirrors its rule, so a build Home would refuse parks there rather than |
| 213 | // failing activation. A git-describe string activates. |
| 214 | func TestVersionHomeWouldRefuseParksAtServing(t *testing.T) { |
| 215 | h := newHarness(t) |
| 216 | h.initialized() |
| 217 | h.con.VersionAnswer["docs"] = "<html>Bad gateway</html>" |
| 218 | e := h.engine(nil) |
| 219 | run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: ownerCanary, Apps: []string{"docs"}}) |
| 220 | if err != nil { |
| 221 | t.Fatal(err) |
| 222 | } |
| 223 | h.confirm(homeop.KindCreateTeam) |
| 224 | r := h.drive(e, run, 15) |
| 225 | if st, _, _ := h.store.Step(h.ctx(), "lagged", "docs", StepServing); r.State != "parked" || st.Code != "version-unacceptable" { |
| 226 | t.Fatalf("run %s %s, serving %s %s", r.State, r.Code, st.State, st.Code) |
| 227 | } |
| 228 | if h.home.Calls[homeop.DirectoryActivate] != 0 { |
| 229 | t.Fatal("activation attempted with a version Home refuses") |
| 230 | } |
| 231 | h.con.VersionAnswer["docs"] = "v0.1.7-3-gabc1234" |
| 232 | if r := h.drive(e, run, 20); r.State != "done" { |
| 233 | t.Fatalf("%s %s", r.State, r.Code) |
| 234 | } |
| 235 | if h.home.InstanceState(ConsumerID("lagged", "docs")) != "live" { |
| 236 | t.Fatal("a git-describe version did not activate") |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // With every app in roster mode no recipe holds the owner, so the seed is |
| 241 | // the address's last use: it goes in the pass that seeds the team, before |
| 242 | // any environment exists (here the platform walks never deliver, so every |
| 243 | // app parks at step 7 awaiting its mail keys). |
| 244 | func TestOwnerForgottenOnceSeeded(t *testing.T) { |
| 245 | h := newHarness(t) |
| 246 | h.initialized() |
| 247 | for _, a := range Apps { |
| 248 | // Only the seeded apps (Equipo, Base) write it, and this run has |
| 249 | // neither (TestSeededAppsKeepTheOwnerUntilWritten). |
| 250 | if NeedsOwner(a) != a.Seeded { |
| 251 | t.Fatalf("%s writes the owner's address: %v", a.Slug, NeedsOwner(a)) |
| 252 | } |
| 253 | } |
| 254 | e := h.engine(nil) |
| 255 | run := h.createLagged(e) |
| 256 | _ = e.Advance(h.ctx(), run) |
| 257 | if _, _, ok := h.home.Team("lagged"); !ok { |
| 258 | t.Fatal("not seeded") |
| 259 | } |
| 260 | for _, app := range allApps { |
| 261 | if _, done, _ := h.store.Done(h.ctx(), "lagged", app, StepEnv); done { |
| 262 | t.Fatalf("%s environment done without platform keys", app) |
| 263 | } |
| 264 | } |
| 265 | if _, held, _ := h.store.Owner(h.ctx(), "lagged"); held { |
| 266 | t.Fatal("owner address kept after the seed, though no recipe needs it") |
| 267 | } |
| 268 | var n int |
| 269 | _ = h.store.DB().QueryRow("SELECT count(*) FROM operator_requests WHERE action='credential.owner-seed'").Scan(&n) |
| 270 | if n != 0 { |
| 271 | t.Fatal("owner-seed request kept after the seed") |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // Fable's P3 on 0efb351: before the seed the address is owner-seed's |
| 276 | // input, so forgetting must not touch an unseeded team, even though no |
| 277 | // recipe needs the owner any more. |
| 278 | func TestForgetOwnerKeepsTheAddressBeforeTheSeed(t *testing.T) { |
| 279 | h := newHarness(t) |
| 280 | h.initialized() |
| 281 | e := h.engine(nil) |
| 282 | run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: ownerCanary, Apps: allApps}) |
| 283 | if err != nil { |
| 284 | t.Fatal(err) |
| 285 | } |
| 286 | r, _ := h.store.Run(h.ctx(), run) |
| 287 | if err := e.forgetOwnerWhenWritten(h.ctx(), &pass{run: r}); err != nil { |
| 288 | t.Fatal(err) |
| 289 | } |
| 290 | if got, held, _ := h.store.Owner(h.ctx(), "lagged"); !held || got != ownerCanary { |
| 291 | t.Fatal("the address was forgotten before the seed: owner-seed has nothing to send") |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // Fable's P3 on 0efb351: SetSeeded writes the team row and the team step |
| 296 | // in one transaction. A database left torn by the earlier code (sqid |
| 297 | // recorded, step lost to a crash) still forgets the address and finishes. |
| 298 | func TestTornSeedStillForgetsTheAddress(t *testing.T) { |
| 299 | h := newHarness(t) |
| 300 | h.initialized() |
| 301 | crashed := false |
| 302 | e := h.engine(func(k string) error { |
| 303 | if k == "seed-team" && !crashed { |
| 304 | crashed = true |
| 305 | return errors.New("kill -9") |
| 306 | } |
| 307 | return nil |
| 308 | }) |
| 309 | run := h.createLagged(e) |
| 310 | _ = e.Advance(h.ctx(), run) |
| 311 | if !crashed { |
| 312 | t.Fatal("never reached seed-team") |
| 313 | } |
| 314 | id, sqid, ok := h.home.Team("lagged") |
| 315 | if !ok { |
| 316 | t.Fatal("Home did not seed") |
| 317 | } |
| 318 | // The torn state: the team row as the old two-write SetSeeded left it |
| 319 | // after its first write, the team step never recorded. |
| 320 | ref := str(mustDone(h.ctx(), h.store, "lagged", "", StepOwner), "owner_ref") |
| 321 | ins, _, _ := h.engine(nil).cfg.Home.Inspect(h.ctx(), homeop.Directory) |
| 322 | if _, err := h.store.DB().Exec("UPDATE teams SET team_id=?,team_sqid=?,epoch=1,generation=?,owner_ref=? WHERE label='lagged'", id, sqid, *ins.Generation, ref); err != nil { |
| 323 | t.Fatal(err) |
| 324 | } |
| 325 | if _, done, _ := h.store.Done(h.ctx(), "lagged", "", StepTeam); done { |
| 326 | t.Fatal("setup: the team step is recorded") |
| 327 | } |
| 328 | // The crashed holder's lock goes stale, and the create-team window |
| 329 | // closes: the rest runs under a new add-apps plan. |
| 330 | h.tick(homeop.WindowLifetime * time.Second) |
| 331 | next, err := h.engine(nil).AddApps(h.ctx(), "lagged", allApps, RunOptions{}) |
| 332 | if err != nil { |
| 333 | t.Fatal(err) |
| 334 | } |
| 335 | h.confirm(homeop.KindAddApps) |
| 336 | if r := h.drive(h.engine(nil), next, 40); r.State != "done" { |
| 337 | t.Fatalf("%s %s", r.State, r.Code) |
| 338 | } |
| 339 | if _, held, _ := h.store.Owner(h.ctx(), "lagged"); held { |
| 340 | t.Fatal("a torn seed kept the address") |
| 341 | } |
| 342 | if _, done, _ := h.store.Done(h.ctx(), "lagged", "", StepTeam); !done { |
| 343 | t.Fatal("the team step was not repaired") |
| 344 | } |
| 345 | checkEndState(t, h) |
| 346 | } |
| 347 | |