package provision import ( "errors" "strings" "testing" "time" "amadan.net/oficina/alta/homeop" family "amadan.net/rastrillo/rastrillo/crypto" ) // Tests added for Fable's review of b1c508b (oficina/alta #1). // Blocker 1: the address goes once the seed has committed and every // allowlist is written, even while an app is still parked — here Docs, // waiting on a /api/team that Docs on main does not serve. func TestOwnerForgottenWhileAnAppIsParked(t *testing.T) { h := newHarness(t) h.initialized() h.con.NoTeamProbe = true e := h.engine(nil) run := h.createLagged(e) r := h.drive(e, run, 40) if r.State != "parked" { t.Fatalf("run %s %s", r.State, r.Code) } for _, app := range allApps { if st, _, _ := h.store.Step(h.ctx(), "lagged", app, StepVerify); st.Code != "no-readiness-probe" { t.Fatalf("%s verify %s %s", app, st.State, st.Code) } } var n int for q, what := range map[string]string{ "SELECT count(*) FROM owner_inputs": "owner input", "SELECT count(*) FROM operator_requests WHERE action='credential.owner-seed'": "owner-seed request", "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", } { if err := h.store.DB().QueryRow(q).Scan(&n); err != nil || n != 0 { t.Fatalf("%s kept while Docs is parked (%d, %v)", what, n, err) } } _, _ = h.store.DB().Exec("PRAGMA wal_checkpoint(TRUNCATE)") if h.scan(ownerCanary) { t.Fatal("owner address in Alta's database files") } // Every app is in roster mode: no environment holds the address. for _, app := range allApps { vars, _, _ := h.con.Env(app, "lagged") for k, v := range vars { if v.Value == ownerCanary { t.Fatalf("%s %s holds the owner's address", app, k) } } } } // Blocker 2: the box's serving name (carlos ship's release version) and // /api/version (the binary's git describe) need not agree. Step 10 records // both and notes a difference; activation reads app_version from // /api/version and artifact_digest from the box's name. func TestServingNamesMayDiffer(t *testing.T) { h := newHarness(t) h.initialized() for _, a := range allApps { h.con.ServingName[a] = "v7" } e := h.engine(nil) run := h.createLagged(e) if r := h.drive(e, run, 40); r.State != "done" { t.Fatalf("%s %s", r.State, r.Code) } d, _, _ := h.store.Done(h.ctx(), "lagged", "docs", StepServing) if str(d, "serving") != "v7" || str(d, "app_version") != h.con.Versions["docs"] || d["names_differ"] != true { t.Fatalf("serving step %v", d) } if h.home.InstanceState(ConsumerID("lagged", "docs")) != "live" { t.Fatal("docs not activated") } } // Blocker 3: a mistyped owner burns the label, and the team page's owner // form and a new plan bring it back. func TestMistypedOwnerIsCorrectedUnderTheSameLabel(t *testing.T) { h := newHarness(t) h.initialized() e := h.engine(nil) _, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: "til-typo@lagged.test", Apps: []string{"sheets"}}) var fl *Fail if !errors.As(err, &fl) || fl.Code != "plan-refused-credential" { t.Fatalf("typo: %v", err) } runs, _ := h.store.Runs(h.ctx(), "lagged") if len(runs) != 1 || runs[0].State != "failed" { t.Fatalf("runs %+v", runs) } // Home's directory accepted the first plan's half and keeps it // pending until it expires; a new plan waits that out (Home answers // 409 meanwhile, and Alta retries the signed plan). h.tick(homeop.PlanLifetime * time.Second) if err := h.store.SetOwner(h.ctx(), "lagged", ownerCanary); err != nil { t.Fatal(err) } if err := e.Resubmit(h.ctx(), runs[0].ID); err != nil { t.Fatalf("resubmit: %v", err) } h.confirm(homeop.KindCreateTeam) if r := h.drive(e, runs[0].ID, 30); r.State != "done" { t.Fatalf("%s %s", r.State, r.Code) } if _, _, ok := h.home.Team("lagged"); !ok { t.Fatal("no team at Home") } } // Blocker 4: a record that does not verify under the pinned provisioning // key, or binds anything but what Alta asked for, is refused before any // environment write. func TestRecordVerification(t *testing.T) { for name, spoil := range map[string]func(*harness){ "another signing key": func(h *harness) { h.home.RecordSigner, _ = family.Generate() }, "another audience": func(h *harness) { h.home.RecordAudience = "https://other.docs.oficina.works" }, } { t.Run(name, func(t *testing.T) { h := newHarness(t) h.initialized() spoil(h) e := h.engine(nil) run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: ownerCanary, Apps: []string{"docs"}}) if err != nil { t.Fatal(err) } h.confirm(homeop.KindCreateTeam) r := h.drive(e, run, 10) if r.State != "failed" || r.Code != "record-unverified" { t.Fatalf("%s %s", r.State, r.Code) } if _, _, ok := h.con.Env("docs", "lagged"); ok { t.Fatal("an environment was written from an unverified record") } for _, req := range h.con.Requests { if strings.HasPrefix(req, "PUT ") { t.Fatalf("console write %q", req) } } }) } } // A request whose answer never came back, replayed after the window // closed and found never committed, parks for a new plan; it is not a // failure. func TestUnansweredRequestAfterTheWindowParks(t *testing.T) { h := newHarness(t) h.initialized() down := true h.home.Outage = func(a string) bool { return down && a == homeop.CredentialRegister } e := h.engine(nil) run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: ownerCanary, Apps: []string{"sheets"}}) if err != nil { t.Fatal(err) } h.confirm(homeop.KindCreateTeam) _ = e.Advance(h.ctx(), run) h.tick(homeop.WindowLifetime * time.Second) down = false _ = e.Advance(h.ctx(), run) r, _ := h.store.Run(h.ctx(), run) st, _, _ := h.store.Step(h.ctx(), "lagged", "sheets", StepRegister) if r.State != "parked" || st.Code != "window-expired" { t.Fatalf("run %s %s, register %s %s", r.State, r.Code, st.State, st.Code) } next, err := e.AddApps(h.ctx(), "lagged", []string{"sheets"}, RunOptions{}) if err != nil { t.Fatal(err) } h.confirm(homeop.KindAddApps) if r := h.drive(e, next, 30); r.State != "done" { t.Fatalf("%s %s", r.State, r.Code) } } // Fix review notes: step 10 parks while the box reports the instance not // awake, and while /api/version answers rastrillo's unstamped "dev". func TestServingParksAsleepAndUnstamped(t *testing.T) { h := newHarness(t) h.initialized() e := h.engine(nil) run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: ownerCanary, Apps: []string{"sheets"}}) if err != nil { t.Fatal(err) } h.confirm(homeop.KindCreateTeam) route := RouteHost("lagged", "sheets", account) for i := 0; i < 10; i++ { _ = e.Advance(h.ctx(), run) h.con.Walk() h.con.SetAsleep(route, true) } if st, _, _ := h.store.Step(h.ctx(), "lagged", "sheets", StepServing); st.State != "parked" || st.Code != "instance-waking" { t.Fatalf("asleep: serving %s %s", st.State, st.Code) } h.con.SetAsleep(route, false) h.con.VersionAnswer["sheets"] = "dev" _ = e.Advance(h.ctx(), run) if st, _, _ := h.store.Step(h.ctx(), "lagged", "sheets", StepServing); st.State != "parked" || st.Code != "version-unstamped" { t.Fatalf("dev: serving %s %s", st.State, st.Code) } } // Home (0cee9ba) admits an observed printable version token; step 10 // mirrors its rule, so a build Home would refuse parks there rather than // failing activation. A git-describe string activates. func TestVersionHomeWouldRefuseParksAtServing(t *testing.T) { h := newHarness(t) h.initialized() h.con.VersionAnswer["docs"] = "Bad gateway" e := h.engine(nil) run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: ownerCanary, Apps: []string{"docs"}}) if err != nil { t.Fatal(err) } h.confirm(homeop.KindCreateTeam) r := h.drive(e, run, 15) if st, _, _ := h.store.Step(h.ctx(), "lagged", "docs", StepServing); r.State != "parked" || st.Code != "version-unacceptable" { t.Fatalf("run %s %s, serving %s %s", r.State, r.Code, st.State, st.Code) } if h.home.Calls[homeop.DirectoryActivate] != 0 { t.Fatal("activation attempted with a version Home refuses") } h.con.VersionAnswer["docs"] = "v0.1.7-3-gabc1234" if r := h.drive(e, run, 20); r.State != "done" { t.Fatalf("%s %s", r.State, r.Code) } if h.home.InstanceState(ConsumerID("lagged", "docs")) != "live" { t.Fatal("a git-describe version did not activate") } } // With every app in roster mode no recipe holds the owner, so the seed is // the address's last use: it goes in the pass that seeds the team, before // any environment exists (here the platform walks never deliver, so every // app parks at step 7 awaiting its mail keys). func TestOwnerForgottenOnceSeeded(t *testing.T) { h := newHarness(t) h.initialized() for _, a := range Apps { // Only the seeded apps (Equipo, Base) write it, and this run has // neither (TestSeededAppsKeepTheOwnerUntilWritten). if NeedsOwner(a) != a.Seeded { t.Fatalf("%s writes the owner's address: %v", a.Slug, NeedsOwner(a)) } } e := h.engine(nil) run := h.createLagged(e) _ = e.Advance(h.ctx(), run) if _, _, ok := h.home.Team("lagged"); !ok { t.Fatal("not seeded") } for _, app := range allApps { if _, done, _ := h.store.Done(h.ctx(), "lagged", app, StepEnv); done { t.Fatalf("%s environment done without platform keys", app) } } if _, held, _ := h.store.Owner(h.ctx(), "lagged"); held { t.Fatal("owner address kept after the seed, though no recipe needs it") } var n int _ = h.store.DB().QueryRow("SELECT count(*) FROM operator_requests WHERE action='credential.owner-seed'").Scan(&n) if n != 0 { t.Fatal("owner-seed request kept after the seed") } } // Fable's P3 on 0efb351: before the seed the address is owner-seed's // input, so forgetting must not touch an unseeded team, even though no // recipe needs the owner any more. func TestForgetOwnerKeepsTheAddressBeforeTheSeed(t *testing.T) { h := newHarness(t) h.initialized() e := h.engine(nil) run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: ownerCanary, Apps: allApps}) if err != nil { t.Fatal(err) } r, _ := h.store.Run(h.ctx(), run) if err := e.forgetOwnerWhenWritten(h.ctx(), &pass{run: r}); err != nil { t.Fatal(err) } if got, held, _ := h.store.Owner(h.ctx(), "lagged"); !held || got != ownerCanary { t.Fatal("the address was forgotten before the seed: owner-seed has nothing to send") } } // Fable's P3 on 0efb351: SetSeeded writes the team row and the team step // in one transaction. A database left torn by the earlier code (sqid // recorded, step lost to a crash) still forgets the address and finishes. func TestTornSeedStillForgetsTheAddress(t *testing.T) { h := newHarness(t) h.initialized() crashed := false e := h.engine(func(k string) error { if k == "seed-team" && !crashed { crashed = true return errors.New("kill -9") } return nil }) run := h.createLagged(e) _ = e.Advance(h.ctx(), run) if !crashed { t.Fatal("never reached seed-team") } id, sqid, ok := h.home.Team("lagged") if !ok { t.Fatal("Home did not seed") } // The torn state: the team row as the old two-write SetSeeded left it // after its first write, the team step never recorded. ref := str(mustDone(h.ctx(), h.store, "lagged", "", StepOwner), "owner_ref") ins, _, _ := h.engine(nil).cfg.Home.Inspect(h.ctx(), homeop.Directory) 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 { t.Fatal(err) } if _, done, _ := h.store.Done(h.ctx(), "lagged", "", StepTeam); done { t.Fatal("setup: the team step is recorded") } // The crashed holder's lock goes stale, and the create-team window // closes: the rest runs under a new add-apps plan. h.tick(homeop.WindowLifetime * time.Second) next, err := h.engine(nil).AddApps(h.ctx(), "lagged", allApps, RunOptions{}) if err != nil { t.Fatal(err) } h.confirm(homeop.KindAddApps) if r := h.drive(h.engine(nil), next, 40); r.State != "done" { t.Fatalf("%s %s", r.State, r.Code) } if _, held, _ := h.store.Owner(h.ctx(), "lagged"); held { t.Fatal("a torn seed kept the address") } if _, done, _ := h.store.Done(h.ctx(), "lagged", "", StepTeam); !done { t.Fatal("the team step was not repaired") } checkEndState(t, h) }