| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "slices" |
| 9 | "strings" |
| 10 | "sync" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/alta/console" |
| 15 | "amadan.net/oficina/alta/homeop" |
| 16 | "github.com/go-json-experiment/json/jsontext" |
| 17 | ) |
| 18 | |
| 19 | // fakeObserver is the CARLOS half as the engine meets it: it answers a |
| 20 | // fixed observation, or refuses. |
| 21 | type fakeObserver struct { |
| 22 | mu sync.Mutex |
| 23 | obs Observation |
| 24 | err error |
| 25 | calls []string |
| 26 | } |
| 27 | |
| 28 | func (f *fakeObserver) Observe(_ context.Context, app, team string) (Observation, error) { |
| 29 | f.mu.Lock() |
| 30 | defer f.mu.Unlock() |
| 31 | f.calls = append(f.calls, app+"/"+team) |
| 32 | if f.err != nil { |
| 33 | return Observation{}, f.err |
| 34 | } |
| 35 | return f.obs, nil |
| 36 | } |
| 37 | |
| 38 | func (f *fakeObserver) set(err error) { |
| 39 | f.mu.Lock() |
| 40 | defer f.mu.Unlock() |
| 41 | f.err = err |
| 42 | } |
| 43 | |
| 44 | func (f *fakeObserver) count() int { |
| 45 | f.mu.Lock() |
| 46 | defer f.mu.Unlock() |
| 47 | return len(f.calls) |
| 48 | } |
| 49 | |
| 50 | // testRefusal is a refusal as adopt.Refusal answers it. |
| 51 | type testRefusal struct{ codes []string } |
| 52 | |
| 53 | func (r *testRefusal) Error() string { return "refused" } |
| 54 | func (r *testRefusal) RefusalCodes() []string { return r.codes } |
| 55 | |
| 56 | var goodObservation = Observation{AppVersion: "v1.4.0", ArtifactDigest: artifactDigest("v1.4.0"), EnvironmentBinding: artifactDigest("binding")} |
| 57 | |
| 58 | func newObserver() *fakeObserver { return &fakeObserver{obs: goodObservation} } |
| 59 | |
| 60 | // offline is a transport that counts every request and answers none: the |
| 61 | // console and the instances' own origins as an adoption must never meet |
| 62 | // them. |
| 63 | type offline struct { |
| 64 | mu sync.Mutex |
| 65 | n int |
| 66 | } |
| 67 | |
| 68 | func (o *offline) RoundTrip(*http.Request) (*http.Response, error) { |
| 69 | o.mu.Lock() |
| 70 | defer o.mu.Unlock() |
| 71 | o.n++ |
| 72 | return nil, errors.New("offline: an adoption made a network call") |
| 73 | } |
| 74 | |
| 75 | func (o *offline) count() int { |
| 76 | o.mu.Lock() |
| 77 | defer o.mu.Unlock() |
| 78 | return o.n |
| 79 | } |
| 80 | |
| 81 | type adoptHarness struct { |
| 82 | *harness |
| 83 | net *offline |
| 84 | } |
| 85 | |
| 86 | // newAdoption is an initialized Home whose env manifest holds the six |
| 87 | // canonical consumers, as production's HOME_CONSUMERS does. |
| 88 | func newAdoption(t *testing.T, apps ...string) *adoptHarness { |
| 89 | t.Helper() |
| 90 | h := newHarness(t) |
| 91 | h.initialized() |
| 92 | if len(apps) == 0 { |
| 93 | apps = Slugs() |
| 94 | } |
| 95 | for _, a := range apps { |
| 96 | h.home.AddEnvConsumer(a, homeop.CanonicalOrigin(a)) |
| 97 | } |
| 98 | return &adoptHarness{harness: h, net: &offline{}} |
| 99 | } |
| 100 | |
| 101 | // engine is Alta's engine as alta-adopt builds it: no console or public |
| 102 | // network at all, and the given Observer (nil for none). |
| 103 | func (a *adoptHarness) adopter(hook func(string) error, obs Observer) *Engine { |
| 104 | e := a.engine(hook) |
| 105 | e.cfg.Observer = obs |
| 106 | e.cfg.Console = &console.Client{HTTP: &http.Client{Transport: a.net}, Base: "https://console.invalid", Account: account, Tokens: a.tokens} |
| 107 | e.cfg.Public = &http.Client{Transport: a.net} |
| 108 | return e |
| 109 | } |
| 110 | |
| 111 | func (a *adoptHarness) proposed() string { |
| 112 | a.t.Helper() |
| 113 | ins, o, _ := a.engine(nil).cfg.Home.Inspect(a.ctx(), homeop.Directory) |
| 114 | if o != homeop.OK || ins.TeamSQID == "" { |
| 115 | a.t.Fatalf("inspect %v", o) |
| 116 | } |
| 117 | return ins.TeamSQID |
| 118 | } |
| 119 | |
| 120 | // planA runs Plan A to its end. |
| 121 | func (a *adoptHarness) planA(e *Engine) int64 { |
| 122 | a.t.Helper() |
| 123 | run, err := e.StartAdoption(a.ctx(), "Oficina", ownerCanary) |
| 124 | if err != nil { |
| 125 | a.t.Fatal(err) |
| 126 | } |
| 127 | a.confirm(homeop.KindCreateTeam) |
| 128 | if r := a.drive(e, run, 20); r.State != "done" || r.Code != "awaiting-configuration" { |
| 129 | a.t.Fatalf("plan A %s %s", r.State, r.Code) |
| 130 | } |
| 131 | return run |
| 132 | } |
| 133 | |
| 134 | // activate starts and confirms a Plan B/C run once Plan A's windows have |
| 135 | // closed. |
| 136 | func (a *adoptHarness) activate(e *Engine, apps ...string) int64 { |
| 137 | a.t.Helper() |
| 138 | a.tick(homeop.WindowLifetime * time.Second) |
| 139 | run, err := e.ActivateAdopted(a.ctx(), apps) |
| 140 | if err != nil { |
| 141 | a.t.Fatal(err) |
| 142 | } |
| 143 | a.confirm(homeop.KindAddApps) |
| 144 | return run |
| 145 | } |
| 146 | |
| 147 | func (a *adoptHarness) plans(run int64) map[string]homeop.Plan { |
| 148 | a.t.Helper() |
| 149 | r, err := a.store.Run(a.ctx(), run) |
| 150 | if err != nil { |
| 151 | a.t.Fatal(err) |
| 152 | } |
| 153 | subs, err := a.store.Plans(a.ctx(), r.PlanID) |
| 154 | if err != nil { |
| 155 | a.t.Fatal(err) |
| 156 | } |
| 157 | out := map[string]homeop.Plan{} |
| 158 | for role, s := range subs { |
| 159 | raw, err := homeop.EnvelopePayload(s.Signed) |
| 160 | if err != nil { |
| 161 | a.t.Fatalf("%s plan: %v", role, err) |
| 162 | } |
| 163 | var p homeop.Plan |
| 164 | if err := homeop.Decode(raw, &p); err != nil { |
| 165 | a.t.Fatalf("%s plan: %v", role, err) |
| 166 | } |
| 167 | out[role] = p |
| 168 | } |
| 169 | return out |
| 170 | } |
| 171 | |
| 172 | // signed decodes the operation input of the last request Alta holds for |
| 173 | // app's purpose. |
| 174 | func (a *adoptHarness) signed(app, purpose string, v any) homeop.Authorization { |
| 175 | a.t.Helper() |
| 176 | req, ok, err := a.store.LastRequest(a.ctx(), AdoptionLabel, app, purpose) |
| 177 | if err != nil || !ok { |
| 178 | a.t.Fatalf("no %s request for %s: %v", purpose, app, err) |
| 179 | } |
| 180 | var w struct { |
| 181 | Version int `json:"version"` |
| 182 | Authorization jsontext.Value `json:"signed_authorization"` |
| 183 | Input jsontext.Value `json:"operation_input"` |
| 184 | } |
| 185 | if err := homeop.Decode(req.Bytes, &w); err != nil { |
| 186 | a.t.Fatal(err) |
| 187 | } |
| 188 | if err := homeop.Decode(w.Input, v); err != nil { |
| 189 | a.t.Fatal(err) |
| 190 | } |
| 191 | r, err := homeop.ReadRequest(req.Bytes) |
| 192 | if err != nil { |
| 193 | a.t.Fatal(err) |
| 194 | } |
| 195 | return r.Authorization |
| 196 | } |
| 197 | |
| 198 | func (a *adoptHarness) evidence(app string) homeop.ActivationEvidence { |
| 199 | a.t.Helper() |
| 200 | var in homeop.ActivationInput |
| 201 | a.signed(app, "activate", &in) |
| 202 | return in.Evidence |
| 203 | } |
| 204 | |
| 205 | func (a *adoptHarness) step(app, name string) Step { |
| 206 | a.t.Helper() |
| 207 | s, ok, err := a.store.Step(a.ctx(), AdoptionLabel, app, name) |
| 208 | if err != nil || !ok { |
| 209 | a.t.Fatalf("no %s step for %s: %v", name, app, err) |
| 210 | } |
| 211 | return s |
| 212 | } |
| 213 | |
| 214 | // onceEach: no committed operation repeats beyond its exact replays, |
| 215 | // which Requests never lists. |
| 216 | func onceEach(t *testing.T, reqs []string) { |
| 217 | t.Helper() |
| 218 | seen := map[string]int{} |
| 219 | for _, r := range reqs { |
| 220 | if seen[r]++; seen[r] > 1 { |
| 221 | t.Fatalf("%s committed twice: %v", r, reqs) |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | func countOf(reqs []string, entry string) int { |
| 227 | n := 0 |
| 228 | for _, r := range reqs { |
| 229 | if r == entry { |
| 230 | n++ |
| 231 | } |
| 232 | } |
| 233 | return n |
| 234 | } |
| 235 | |
| 236 | func failCode(t *testing.T, err error, code string) { |
| 237 | t.Helper() |
| 238 | var fl *Fail |
| 239 | if !errors.As(err, &fl) || fl.Code != code { |
| 240 | t.Fatalf("got %v, want failed: %s", err, code) |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | func TestAdoptionPlanACarriesExistingTeamOnBothRoles(t *testing.T) { |
| 245 | a := newAdoption(t) |
| 246 | e := a.adopter(nil, nil) |
| 247 | _, err := e.StartAdoption(a.ctx(), "Oficina team", ownerCanary) |
| 248 | failCode(t, err, "name-not-oficina") |
| 249 | _, err = e.StartAdoption(a.ctx(), "Oficina", "Paul <"+ownerCanary+">") |
| 250 | failCode(t, err, "owner-shape") |
| 251 | if used, _ := a.store.LabelUsed(a.ctx(), AdoptionLabel); used { |
| 252 | t.Fatal("a refused start recorded the team") |
| 253 | } |
| 254 | proposed := a.proposed() |
| 255 | run, err := e.StartAdoption(a.ctx(), "Oficina", ownerCanary) |
| 256 | if err != nil { |
| 257 | t.Fatal(err) |
| 258 | } |
| 259 | if r, _ := a.store.Run(a.ctx(), run); !r.Adoption || r.Kind != homeop.KindCreateTeam || r.Label != AdoptionLabel || strings.Join(r.Apps, ",") != strings.Join(Slugs(), ",") { |
| 260 | t.Fatalf("run %+v", r) |
| 261 | } |
| 262 | checkA := func(plans map[string]homeop.Plan) { |
| 263 | t.Helper() |
| 264 | if len(plans) != 2 { |
| 265 | t.Fatalf("roles %d", len(plans)) |
| 266 | } |
| 267 | for role, p := range plans { |
| 268 | if p.ExistingTeam != proposed || p.Kind != homeop.KindCreateTeam || strings.Join(p.Apps, ",") != strings.Join(Slugs(), ",") { |
| 269 | t.Fatalf("%s plan existing %q kind %s apps %v", role, p.ExistingTeam, p.Kind, p.Apps) |
| 270 | } |
| 271 | } |
| 272 | if d := plans[homeop.Directory]; d.Label != AdoptionLabel || d.DisplayName != "Oficina" || d.Owner != "" { |
| 273 | t.Fatal("directory plan fields") |
| 274 | } |
| 275 | if c := plans[homeop.Credential]; c.Owner != ownerCanary || c.Label != "" || c.DisplayName != "" { |
| 276 | t.Fatal("credential plan fields") |
| 277 | } |
| 278 | } |
| 279 | first := a.plans(run) |
| 280 | checkA(first) |
| 281 | _, err = e.StartAdoption(a.ctx(), "Oficina", ownerCanary) |
| 282 | failCode(t, err, "label-used") |
| 283 | // Unconfirmed until it expired: a fresh plan binds the same team. |
| 284 | a.tick((homeop.PlanLifetime + 1) * time.Second) |
| 285 | if err := e.Resubmit(a.ctx(), run); err != nil { |
| 286 | t.Fatal(err) |
| 287 | } |
| 288 | again := a.plans(run) |
| 289 | if again[homeop.Directory].PlanID == first[homeop.Directory].PlanID { |
| 290 | t.Fatal("resubmit reused the expired plan") |
| 291 | } |
| 292 | checkA(again) |
| 293 | a.confirm(homeop.KindCreateTeam) |
| 294 | if r := a.drive(e, run, 20); r.State != "done" || r.Code != "awaiting-configuration" { |
| 295 | t.Fatalf("plan A %s %s", r.State, r.Code) |
| 296 | } |
| 297 | // The address went with the seed: no input, no owner-seed request, no |
| 298 | // credential plan bytes, nowhere in the files. |
| 299 | var n int |
| 300 | _ = a.store.DB().QueryRow("SELECT (SELECT count(*) FROM owner_inputs)+(SELECT count(*) FROM operator_requests WHERE action='credential.owner-seed')+(SELECT count(*) FROM plan_submissions WHERE role='credential' AND sealed_plan IS NOT NULL)").Scan(&n) |
| 301 | if n != 0 { |
| 302 | t.Fatalf("%d rows still carry the owner", n) |
| 303 | } |
| 304 | _, _ = a.store.DB().Exec("PRAGMA wal_checkpoint(TRUNCATE)") |
| 305 | if a.scan(ownerCanary) || strings.Contains(a.logs.String(), ownerCanary) { |
| 306 | t.Fatal("the owner's address survives the seed") |
| 307 | } |
| 308 | // Plan B: add-apps on both roles, the seeded team, no owner. |
| 309 | team, _ := a.store.Team(a.ctx(), AdoptionLabel) |
| 310 | b := a.activate(a.adopter(nil, newObserver()), "calendar") |
| 311 | plans := a.plans(b) |
| 312 | if len(plans) != 2 { |
| 313 | t.Fatalf("plan B roles %d", len(plans)) |
| 314 | } |
| 315 | for role, p := range plans { |
| 316 | if p.ExistingTeam != team.TeamSQID || p.Kind != homeop.KindAddApps || strings.Join(p.Apps, ",") != "calendar" || p.Owner != "" || p.DisplayName != "" { |
| 317 | t.Fatalf("plan B %s: existing %q kind %s apps %v", role, p.ExistingTeam, p.Kind, p.Apps) |
| 318 | } |
| 319 | } |
| 320 | if plans[homeop.Directory].Label != AdoptionLabel || plans[homeop.Credential].Label != "" { |
| 321 | t.Fatal("plan B labels") |
| 322 | } |
| 323 | // Plan B unconfirmed until it expired: re-signed for the same team. |
| 324 | a.tick((homeop.PlanLifetime + 1) * time.Second) |
| 325 | if err := e.Resubmit(a.ctx(), b); err != nil { |
| 326 | t.Fatal(err) |
| 327 | } |
| 328 | for role, p := range a.plans(b) { |
| 329 | if p.ExistingTeam != team.TeamSQID || p.Kind != homeop.KindAddApps || p.PlanID == plans[role].PlanID { |
| 330 | t.Fatalf("resubmitted plan B %s: existing %q", role, p.ExistingTeam) |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | func TestAdoptionSeedsAtProposedSQIDOnly(t *testing.T) { |
| 336 | t.Run("at the proposal", func(t *testing.T) { |
| 337 | a := newAdoption(t) |
| 338 | proposed := a.proposed() |
| 339 | a.planA(a.adopter(nil, nil)) |
| 340 | _, sqid, ok := a.home.Team(AdoptionLabel) |
| 341 | team, _ := a.store.Team(a.ctx(), AdoptionLabel) |
| 342 | if !ok || sqid != proposed || team.TeamSQID != proposed || a.home.TeamCount() != 1 { |
| 343 | t.Fatalf("seeded at %q, Alta holds %q, proposed %q", sqid, team.TeamSQID, proposed) |
| 344 | } |
| 345 | var seed homeop.SeedInput |
| 346 | a.signed("", "seed-team", &seed) |
| 347 | if seed.TeamSQID != proposed || seed.Label != AdoptionLabel || seed.Name != "Oficina" { |
| 348 | t.Fatal("seed-team input") |
| 349 | } |
| 350 | if a.home.Calls[homeop.CredentialOwnerSeed] != 1 || a.home.Calls[homeop.DirectorySeedTeam] != 1 { |
| 351 | t.Fatalf("owner-seed %d, seed-team %d", a.home.Calls[homeop.CredentialOwnerSeed], a.home.Calls[homeop.DirectorySeedTeam]) |
| 352 | } |
| 353 | }) |
| 354 | t.Run("proposal moved", func(t *testing.T) { |
| 355 | // Alta's copy of the plan binds another SQID than the directory |
| 356 | // now proposes: nothing is signed against the proposal. |
| 357 | a := newAdoption(t) |
| 358 | e := a.adopter(nil, nil) |
| 359 | run, err := e.StartAdoption(a.ctx(), "Oficina", ownerCanary) |
| 360 | if err != nil { |
| 361 | t.Fatal(err) |
| 362 | } |
| 363 | r, _ := a.store.Run(a.ctx(), run) |
| 364 | p := a.plans(run)[homeop.Directory] |
| 365 | p.ExistingTeam = "txxz" |
| 366 | raw, err := homeop.SignPlan(a.key, p) |
| 367 | if err != nil { |
| 368 | t.Fatal(err) |
| 369 | } |
| 370 | sealed, err := a.store.seal(planPurpose(r.PlanID, homeop.Directory), raw) |
| 371 | if err != nil { |
| 372 | t.Fatal(err) |
| 373 | } |
| 374 | if _, err := a.store.DB().Exec("UPDATE plan_submissions SET sealed_plan=? WHERE plan_id=? AND role='directory'", sealed, r.PlanID); err != nil { |
| 375 | t.Fatal(err) |
| 376 | } |
| 377 | a.confirm(homeop.KindCreateTeam) |
| 378 | if r := a.drive(e, run, 5); r.State != "failed" || r.Code != "proposal-moved" { |
| 379 | t.Fatalf("run %s %s", r.State, r.Code) |
| 380 | } |
| 381 | if a.home.Calls[homeop.CredentialOwnerSeed] != 0 || a.home.TeamCount() != 0 { |
| 382 | t.Fatal("an owner-seed was sent for a moved proposal") |
| 383 | } |
| 384 | }) |
| 385 | t.Run("plan unreadable", func(t *testing.T) { |
| 386 | a := newAdoption(t) |
| 387 | e := a.adopter(nil, nil) |
| 388 | run, err := e.StartAdoption(a.ctx(), "Oficina", ownerCanary) |
| 389 | if err != nil { |
| 390 | t.Fatal(err) |
| 391 | } |
| 392 | a.confirm(homeop.KindCreateTeam) |
| 393 | if _, err := a.store.DB().Exec("UPDATE plan_submissions SET sealed_plan=NULL WHERE role='directory'"); err != nil { |
| 394 | t.Fatal(err) |
| 395 | } |
| 396 | if r := a.drive(e, run, 5); r.State != "failed" || r.Code != "plan-unreadable" { |
| 397 | t.Fatalf("run %s %s", r.State, r.Code) |
| 398 | } |
| 399 | if a.home.Calls[homeop.CredentialOwnerSeed] != 0 { |
| 400 | t.Fatal("an owner-seed was sent without the confirmed plan") |
| 401 | } |
| 402 | }) |
| 403 | } |
| 404 | |
| 405 | // Each entry point refuses what comes before its phase, before anything |
| 406 | // is recorded or planned. |
| 407 | func TestAdoptionRefusesOutOfOrder(t *testing.T) { |
| 408 | fresh := &adoptHarness{harness: newHarness(t), net: &offline{}} |
| 409 | _, err := fresh.adopter(nil, nil).StartAdoption(fresh.ctx(), "Oficina", ownerCanary) |
| 410 | failCode(t, err, "home-not-initialized") |
| 411 | for _, role := range []string{homeop.Credential, homeop.Directory} { |
| 412 | a := newAdoption(t) |
| 413 | if role == homeop.Credential { |
| 414 | a.cred.Close() |
| 415 | } else { |
| 416 | a.dir.Close() |
| 417 | } |
| 418 | _, err := a.adopter(nil, nil).StartAdoption(a.ctx(), "Oficina", ownerCanary) |
| 419 | var fl *Fail |
| 420 | if !errors.As(err, &fl) || !strings.HasPrefix(fl.Code, "home-"+role+"-") { |
| 421 | t.Fatalf("%s unreachable: %v", role, err) |
| 422 | } |
| 423 | if used, _ := a.store.LabelUsed(a.ctx(), AdoptionLabel); used { |
| 424 | t.Fatal("recorded with Home unreachable") |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // A fresh Plan A binds the proposal Home answers now, or none. |
| 429 | r := newAdoption(t) |
| 430 | planned, err := r.adopter(nil, nil).StartAdoption(r.ctx(), "Oficina", ownerCanary) |
| 431 | if err != nil { |
| 432 | t.Fatal(err) |
| 433 | } |
| 434 | r.dir.Close() |
| 435 | failCode(t, r.adopter(nil, nil).Resubmit(r.ctx(), planned), "no-proposed-team") |
| 436 | |
| 437 | // docs is missing from the env manifest: Plan A seeds, provisions docs |
| 438 | // and stops at its register. |
| 439 | a := newAdoption(t, "sheets", "slides", "calendar", "calls", "memoria") |
| 440 | e := a.adopter(nil, newObserver()) |
| 441 | _, err = e.ActivateAdopted(a.ctx(), []string{"calendar"}) |
| 442 | failCode(t, err, "no-team") |
| 443 | _, err = e.DisableAdopted(a.ctx(), "calendar", AdoptionLabel) |
| 444 | failCode(t, err, "no-team") |
| 445 | run, err := e.StartAdoption(a.ctx(), "Oficina", ownerCanary) |
| 446 | if err != nil { |
| 447 | t.Fatal(err) |
| 448 | } |
| 449 | _, err = e.ActivateAdopted(a.ctx(), []string{"calendar"}) |
| 450 | failCode(t, err, "team-not-seeded") |
| 451 | _, err = e.DisableAdopted(a.ctx(), "calendar", AdoptionLabel) |
| 452 | failCode(t, err, "team-not-seeded") |
| 453 | a.confirm(homeop.KindCreateTeam) |
| 454 | if r := a.drive(e, run, 10); r.State != "failed" || r.Code != "register-refused" { |
| 455 | t.Fatalf("plan A %s %s", r.State, r.Code) |
| 456 | } |
| 457 | a.tick(homeop.WindowLifetime * time.Second) |
| 458 | for code, apps := range map[string][]string{ |
| 459 | "no-apps": nil, |
| 460 | "unknown-app": {"calendar", "mail"}, |
| 461 | "not-registered": {"docs"}, |
| 462 | } { |
| 463 | _, err := e.ActivateAdopted(a.ctx(), apps) |
| 464 | failCode(t, err, code) |
| 465 | } |
| 466 | _, err = e.ActivateAdopted(a.ctx(), []string{"calendar", "calendar"}) |
| 467 | failCode(t, err, "unknown-app") |
| 468 | _, err = e.DisableAdopted(a.ctx(), "sheets", AdoptionLabel) |
| 469 | failCode(t, err, "not-provisioned") |
| 470 | var n int |
| 471 | _ = a.store.DB().QueryRow("SELECT count(*) FROM runs WHERE kind<>'create-team' AND label=?", AdoptionLabel).Scan(&n) |
| 472 | if n != 0 { |
| 473 | t.Fatal("a refused call recorded a run") |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | func TestAdoptionRegisterInsertsNothing(t *testing.T) { |
| 478 | a := newAdoption(t) |
| 479 | a.planA(a.adopter(nil, nil)) |
| 480 | if n := a.home.ConsumerCount(); n != 0 { |
| 481 | t.Fatalf("register inserted %d credential consumers", n) |
| 482 | } |
| 483 | env := map[string]string{"directory": dirOrigin, "alta": "https://alta.example.test"} |
| 484 | for _, s := range Slugs() { |
| 485 | env[s] = homeop.CanonicalOrigin(s) |
| 486 | } |
| 487 | want := homeop.ManifestDigest(env) |
| 488 | reqs := a.home.Requests() |
| 489 | for _, s := range Slugs() { |
| 490 | if got := str(a.step(s, StepRegister).Data, "manifest_digest"); got != want { |
| 491 | t.Fatalf("%s manifest digest is not the env manifest's", s) |
| 492 | } |
| 493 | if countOf(reqs, homeop.CredentialRegister+":"+s) != 1 || countOf(reqs, homeop.DirectoryProvision+":"+s) != 1 { |
| 494 | t.Fatalf("%s: %v", s, reqs) |
| 495 | } |
| 496 | if got := a.home.InstanceState(s); got != "pending" { |
| 497 | t.Fatalf("%s directory row %q", s, got) |
| 498 | } |
| 499 | } |
| 500 | // Without the exact env entry Home refuses, and Alta stops there. |
| 501 | b := newAdoption(t, "docs", "sheets", "slides", "calendar", "calls") |
| 502 | e := b.adopter(nil, nil) |
| 503 | run, err := e.StartAdoption(b.ctx(), "Oficina", ownerCanary) |
| 504 | if err != nil { |
| 505 | t.Fatal(err) |
| 506 | } |
| 507 | b.confirm(homeop.KindCreateTeam) |
| 508 | if r := b.drive(e, run, 20); r.State != "failed" || r.Code != "register-refused" { |
| 509 | t.Fatalf("run %s %s", r.State, r.Code) |
| 510 | } |
| 511 | if b.home.ConsumerCount() != 0 { |
| 512 | t.Fatal("a refused register inserted a consumer") |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | func TestAdoptionMakesNoConsoleCalls(t *testing.T) { |
| 517 | a := newAdoption(t) |
| 518 | obs := newObserver() |
| 519 | e := a.adopter(nil, obs) |
| 520 | a.planA(e) |
| 521 | run := a.activate(e, "calendar") |
| 522 | if r := a.drive(e, run, 10); r.State != "done" || r.Code != "" { |
| 523 | t.Fatalf("plan B %s %s", r.State, r.Code) |
| 524 | } |
| 525 | a.tick(homeop.WindowLifetime * time.Second) |
| 526 | off, err := e.DisableAdopted(a.ctx(), "calendar", AdoptionLabel) |
| 527 | if err != nil { |
| 528 | t.Fatal(err) |
| 529 | } |
| 530 | if err := a.home.Confirm(homeop.Directory, ""); err != nil { |
| 531 | t.Fatal(err) |
| 532 | } |
| 533 | if r := a.drive(e, off, 5); r.State != "done" { |
| 534 | t.Fatalf("disable %s %s", r.State, r.Code) |
| 535 | } |
| 536 | if n := a.net.count(); n != 0 { |
| 537 | t.Fatalf("%d console or instance requests", n) |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | func TestActivateAdoptedUsesExistingPolicy(t *testing.T) { |
| 542 | a := newAdoption(t) |
| 543 | obs := newObserver() |
| 544 | e := a.adopter(nil, obs) |
| 545 | a.planA(e) |
| 546 | run := a.activate(e, "calendar") |
| 547 | if r := a.drive(e, run, 10); r.State != "done" { |
| 548 | t.Fatalf("plan B %s %s", r.State, r.Code) |
| 549 | } |
| 550 | team, _ := a.store.Team(a.ctx(), AdoptionLabel) |
| 551 | ev := a.evidence("calendar") |
| 552 | if ev.Policy != homeop.ExistingActivationPolicy || ev.Consumer != "calendar" || ev.App != "calendar" || ev.Audience != "https://calendar.oficina.works" || ev.AliasHost != "calendar.oficina.works" || ev.TeamSQID != team.TeamSQID || ev.Owner != team.OwnerRef { |
| 553 | t.Fatalf("evidence %+v", ev) |
| 554 | } |
| 555 | if ev.AppVersion != goodObservation.AppVersion || ev.ArtifactDigest != goodObservation.ArtifactDigest || ev.EnvironmentBinding != goodObservation.EnvironmentBinding { |
| 556 | t.Fatal("evidence does not carry the observation") |
| 557 | } |
| 558 | if ev.ManifestDigest != str(a.step("calendar", StepRegister).Data, "manifest_digest") { |
| 559 | t.Fatal("evidence does not carry the register result's manifest") |
| 560 | } |
| 561 | if strings.Join(obs.calls, ",") != "calendar/"+team.TeamSQID { |
| 562 | t.Fatalf("observed %v", obs.calls) |
| 563 | } |
| 564 | reqs := a.home.Requests() |
| 565 | onceEach(t, reqs) |
| 566 | if countOf(reqs, homeop.DirectoryActivate+":calendar") != 1 || a.home.InstanceState("calendar") != "live" { |
| 567 | t.Fatalf("requests %v", reqs) |
| 568 | } |
| 569 | for _, s := range Slugs() { |
| 570 | if s != "calendar" && a.home.InstanceState(s) != "pending" { |
| 571 | t.Fatalf("%s activated by a calendar plan", s) |
| 572 | } |
| 573 | } |
| 574 | a.tick(homeop.WindowLifetime * time.Second) |
| 575 | _, err := e.ActivateAdopted(a.ctx(), []string{"calendar"}) |
| 576 | failCode(t, err, "app-active") |
| 577 | } |
| 578 | |
| 579 | func TestActivateAdoptedWithoutObserverFails(t *testing.T) { |
| 580 | a := newAdoption(t) |
| 581 | a.planA(a.adopter(nil, nil)) |
| 582 | a.tick(homeop.WindowLifetime * time.Second) |
| 583 | bare := a.adopter(nil, nil) |
| 584 | _, err := bare.ActivateAdopted(a.ctx(), []string{"calendar"}) |
| 585 | failCode(t, err, "no-observer") |
| 586 | var n int |
| 587 | _ = a.store.DB().QueryRow("SELECT count(*) FROM runs WHERE kind='add-apps'").Scan(&n) |
| 588 | if n != 0 { |
| 589 | t.Fatal("a run was recorded without an observer") |
| 590 | } |
| 591 | if err := a.home.Confirm(homeop.Directory, ""); err == nil { |
| 592 | t.Fatal("a plan was submitted without an observer") |
| 593 | } |
| 594 | // An engine without one that advances the run never activates. |
| 595 | run, err := a.adopter(nil, newObserver()).ActivateAdopted(a.ctx(), []string{"calendar"}) |
| 596 | if err != nil { |
| 597 | t.Fatal(err) |
| 598 | } |
| 599 | a.confirm(homeop.KindAddApps) |
| 600 | if r := a.drive(bare, run, 5); r.State != "failed" || r.Code != "no-observer" { |
| 601 | t.Fatalf("run %s %s", r.State, r.Code) |
| 602 | } |
| 603 | if countOf(a.home.Requests(), homeop.DirectoryActivate+":calendar") != 0 { |
| 604 | t.Fatal("activated without an observer") |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | func TestActivateAdoptedParksOnObserverRefusal(t *testing.T) { |
| 609 | a := newAdoption(t) |
| 610 | obs := newObserver() |
| 611 | obs.set(&testRefusal{codes: []string{"delivery-check-superseded", "missing-delivery-check"}}) |
| 612 | e := a.adopter(nil, obs) |
| 613 | a.planA(e) |
| 614 | run := a.activate(e, "calendar") |
| 615 | if r := a.drive(e, run, 3); r.State != "parked" || r.Code != "observe-delivery-check-superseded,missing-delivery-check" { |
| 616 | t.Fatalf("run %s %s", r.State, r.Code) |
| 617 | } |
| 618 | if s := a.step("calendar", StepActivate); s.State != "parked" || s.Code != "observe-delivery-check-superseded,missing-delivery-check" { |
| 619 | t.Fatalf("step %s %s", s.State, s.Code) |
| 620 | } |
| 621 | obs.set(errors.New("carlos unreachable")) |
| 622 | if r := a.drive(e, run, 2); r.State != "parked" || r.Code != "observer-unavailable" { |
| 623 | t.Fatalf("run %s %s", r.State, r.Code) |
| 624 | } |
| 625 | if countOf(a.home.Requests(), homeop.DirectoryActivate+":calendar") != 0 || a.home.Calls[homeop.DirectoryActivate] != 0 { |
| 626 | t.Fatal("activation sent on a refused observation") |
| 627 | } |
| 628 | // The refusal clears: the same run activates. |
| 629 | obs.set(nil) |
| 630 | if r := a.drive(e, run, 3); r.State != "done" { |
| 631 | t.Fatalf("run %s %s", r.State, r.Code) |
| 632 | } |
| 633 | |
| 634 | // An observation Home would refuse is never signed. |
| 635 | for name, bad := range map[string]Observation{ |
| 636 | "version": {AppVersion: "v1 4", ArtifactDigest: goodObservation.ArtifactDigest, EnvironmentBinding: goodObservation.EnvironmentBinding}, |
| 637 | "artifact": {AppVersion: "v1", ArtifactDigest: "c2hh", EnvironmentBinding: goodObservation.EnvironmentBinding}, |
| 638 | "binding": {AppVersion: "v1", ArtifactDigest: goodObservation.ArtifactDigest, EnvironmentBinding: ""}, |
| 639 | } { |
| 640 | t.Run(name, func(t *testing.T) { |
| 641 | b := newAdoption(t) |
| 642 | e := b.adopter(nil, &fakeObserver{obs: bad}) |
| 643 | b.planA(e) |
| 644 | run := b.activate(e, "calendar") |
| 645 | if r := b.drive(e, run, 3); r.State != "failed" || r.Code != "observation-shape" { |
| 646 | t.Fatalf("run %s %s", r.State, r.Code) |
| 647 | } |
| 648 | if b.home.Calls[homeop.DirectoryActivate] != 0 { |
| 649 | t.Fatal("a malformed observation was sent") |
| 650 | } |
| 651 | }) |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | func TestActivateAdoptedStartedNotBeforeRecordedIssued(t *testing.T) { |
| 656 | for _, lose := range []bool{false, true} { |
| 657 | t.Run(fmt.Sprintf("verify answer lost %v", lose), func(t *testing.T) { |
| 658 | a := newAdoption(t) |
| 659 | e := a.adopter(nil, newObserver()) |
| 660 | a.planA(e) |
| 661 | if lose { |
| 662 | lost := false |
| 663 | a.home.LoseAnswer = func(action string) bool { |
| 664 | if action == homeop.DirectoryVerify && !lost { |
| 665 | lost = true |
| 666 | return true |
| 667 | } |
| 668 | return false |
| 669 | } |
| 670 | } |
| 671 | run := a.activate(e, "calendar") |
| 672 | if r := a.drive(e, run, 10); r.State != "done" { |
| 673 | t.Fatalf("run %s %s", r.State, r.Code) |
| 674 | } |
| 675 | var vin homeop.VerificationInput |
| 676 | a.signed("calendar", "verify", &vin) |
| 677 | recorded := num(a.step("calendar", StepVerify).Data, "issued_at") |
| 678 | ev := a.evidence("calendar") |
| 679 | if ev.Started != recorded || recorded < vin.Issued { |
| 680 | t.Fatalf("started %d, recorded %d, Home's issued %d", ev.Started, recorded, vin.Issued) |
| 681 | } |
| 682 | if lose && recorded <= vin.Issued { |
| 683 | t.Fatal("a replayed attempt recorded Home's issued_at, which its result does not carry") |
| 684 | } |
| 685 | if !lose && recorded != vin.Issued { |
| 686 | t.Fatal("a fresh attempt did not record the issued_at it signed") |
| 687 | } |
| 688 | if ev.Completed < ev.Started { |
| 689 | t.Fatal("completed before started") |
| 690 | } |
| 691 | if countOf(a.home.Requests(), homeop.DirectoryVerify+":calendar") != 1 { |
| 692 | t.Fatal("verify committed twice") |
| 693 | } |
| 694 | }) |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | func TestActivateAdoptedReverifiesLapsedAttempt(t *testing.T) { |
| 699 | a := newAdoption(t) |
| 700 | obs := newObserver() |
| 701 | obs.set(&testRefusal{codes: []string{"missing-delivery-check"}}) |
| 702 | e := a.adopter(nil, obs) |
| 703 | a.planA(e) |
| 704 | run := a.activate(e, "calendar") |
| 705 | a.drive(e, run, 2) |
| 706 | first := a.signed("calendar", "verify", &homeop.VerificationInput{}) |
| 707 | // The attempt lapses while the observation is refused. |
| 708 | a.tick(600 * time.Second) |
| 709 | obs.set(nil) |
| 710 | if r := a.drive(e, run, 5); r.State != "done" { |
| 711 | t.Fatalf("run %s %s", r.State, r.Code) |
| 712 | } |
| 713 | var vin homeop.VerificationInput |
| 714 | second := a.signed("calendar", "verify", &vin) |
| 715 | if second.OperationID == first.OperationID { |
| 716 | t.Fatal("the lapsed attempt was not replaced") |
| 717 | } |
| 718 | ev := a.evidence("calendar") |
| 719 | if ev.Attempt != second.OperationID || ev.Started != num(a.step("calendar", StepVerify).Data, "issued_at") || ev.Started < vin.Issued { |
| 720 | t.Fatal("activation does not carry the new attempt") |
| 721 | } |
| 722 | reqs := a.home.Requests() |
| 723 | if countOf(reqs, homeop.DirectoryVerify+":calendar") != 2 || countOf(reqs, homeop.DirectoryActivate+":calendar") != 1 { |
| 724 | t.Fatalf("requests %v", reqs) |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | func TestOrdinaryRunsStillRefuseOficina(t *testing.T) { |
| 729 | a := newAdoption(t) |
| 730 | e := a.adopter(nil, nil) |
| 731 | _, err := a.engine(nil).CreateTeam(a.ctx(), NewTeam{Label: AdoptionLabel, Name: "Oficina", Owner: ownerCanary, Apps: []string{"docs"}}) |
| 732 | failCode(t, err, "label-reserved") |
| 733 | a.planA(e) |
| 734 | a.tick(homeop.WindowLifetime * time.Second) |
| 735 | _, err = a.engine(nil).AddApps(a.ctx(), AdoptionLabel, []string{"calendar"}, RunOptions{}) |
| 736 | failCode(t, err, "label-reserved") |
| 737 | _, err = a.engine(nil).RetireApps(a.ctx(), AdoptionLabel, []string{"calendar"}, AdoptionLabel) |
| 738 | failCode(t, err, "label-reserved") |
| 739 | // A run under the label that is not an adoption never advances. |
| 740 | before := len(a.home.Requests()) |
| 741 | id, err := a.store.NewRun(a.ctx(), homeop.KindAddApps, AdoptionLabel, []string{"calendar"}, RunOptions{}) |
| 742 | if err != nil { |
| 743 | t.Fatal(err) |
| 744 | } |
| 745 | failCode(t, e.Advance(a.ctx(), id), "label-reserved") |
| 746 | if r, _ := a.store.Run(a.ctx(), id); r.State != "failed" || r.Code != "label-reserved" { |
| 747 | t.Fatalf("run %s %s", r.State, r.Code) |
| 748 | } |
| 749 | if len(a.home.Requests()) != before { |
| 750 | t.Fatal("an ordinary run under oficina reached Home") |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | func TestDisableAdoptedRequiresTypedLabel(t *testing.T) { |
| 755 | a := newAdoption(t) |
| 756 | e := a.adopter(nil, newObserver()) |
| 757 | a.planA(e) |
| 758 | run := a.activate(e, "calendar") |
| 759 | if r := a.drive(e, run, 10); r.State != "done" { |
| 760 | t.Fatalf("plan B %s %s", r.State, r.Code) |
| 761 | } |
| 762 | a.tick(homeop.WindowLifetime * time.Second) |
| 763 | for _, typed := range []string{"", "Oficina", "oficin", " oficina", "calendar"} { |
| 764 | _, err := e.DisableAdopted(a.ctx(), "calendar", typed) |
| 765 | failCode(t, err, "label-mismatch") |
| 766 | } |
| 767 | _, err := e.DisableAdopted(a.ctx(), "mail", AdoptionLabel) |
| 768 | failCode(t, err, "unknown-app") |
| 769 | var n int |
| 770 | _ = a.store.DB().QueryRow("SELECT count(*) FROM runs WHERE kind='retire-apps'").Scan(&n) |
| 771 | if n != 0 { |
| 772 | t.Fatal("a refused disable recorded a run") |
| 773 | } |
| 774 | before := a.home.Requests() |
| 775 | off, err := e.DisableAdopted(a.ctx(), "calendar", AdoptionLabel) |
| 776 | if err != nil { |
| 777 | t.Fatal(err) |
| 778 | } |
| 779 | plans := a.plans(off) |
| 780 | d, ok := plans[homeop.Directory] |
| 781 | if len(plans) != 1 || !ok || d.Kind != homeop.KindRetireApps || d.Label != AdoptionLabel || d.ExistingTeam != "" || strings.Join(d.Apps, ",") != "calendar" { |
| 782 | t.Fatalf("disable plans %d", len(plans)) |
| 783 | } |
| 784 | if err := a.home.Confirm(homeop.Directory, ""); err != nil { |
| 785 | t.Fatal(err) |
| 786 | } |
| 787 | if r := a.drive(e, off, 5); r.State != "done" { |
| 788 | t.Fatalf("disable %s %s", r.State, r.Code) |
| 789 | } |
| 790 | after := a.home.Requests() |
| 791 | if got := after[len(before):]; strings.Join(got, ",") != homeop.DirectoryDisable+":calendar" { |
| 792 | t.Fatalf("disable sent %v", got) |
| 793 | } |
| 794 | if a.home.InstanceState("calendar") != "disabled" || a.home.Calls[homeop.CredentialRetire] != 0 { |
| 795 | t.Fatal("disable state") |
| 796 | } |
| 797 | // The app stays the suite's: its env consumer, its team row. |
| 798 | team, _ := a.store.Team(a.ctx(), AdoptionLabel) |
| 799 | if team.Apps["calendar"] != "active" { |
| 800 | t.Fatalf("calendar %q", team.Apps["calendar"]) |
| 801 | } |
| 802 | a.tick(homeop.WindowLifetime * time.Second) |
| 803 | _, err = e.ActivateAdopted(a.ctx(), []string{"calendar"}) |
| 804 | failCode(t, err, "app-disabled") |
| 805 | } |
| 806 | |
| 807 | // adoptionActs runs Plan A and Plan B for calendar cleanly and returns |
| 808 | // each plan's acts in order. |
| 809 | func adoptionActs(t *testing.T) (planA, planB []string) { |
| 810 | a := newAdoption(t) |
| 811 | var acts *[]string |
| 812 | e := a.adopter(func(k string) error { *acts = append(*acts, k); return nil }, newObserver()) |
| 813 | acts = &planA |
| 814 | a.planA(e) |
| 815 | acts = &planB |
| 816 | run := a.activate(e, "calendar") |
| 817 | if r := a.drive(e, run, 10); r.State != "done" { |
| 818 | t.Fatalf("clean plan B %s %s", r.State, r.Code) |
| 819 | } |
| 820 | return planA, planB |
| 821 | } |
| 822 | |
| 823 | // crashAt returns a hook that fails at the i-th act, once. |
| 824 | func crashAt(i int, crashed *bool) func(string) error { |
| 825 | n := 0 |
| 826 | return func(string) error { |
| 827 | if n == i && !*crashed { |
| 828 | *crashed = true |
| 829 | return errors.New("kill -9") |
| 830 | } |
| 831 | n++ |
| 832 | return nil |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | // TestAdoptionCrashAfterEveryAct: Alta dies after each act has taken |
| 837 | // effect at Home and before it is recorded; a fresh engine resumes by |
| 838 | // exact replay and nothing is committed twice. |
| 839 | func TestAdoptionCrashAfterEveryAct(t *testing.T) { |
| 840 | planA, planB := adoptionActs(t) |
| 841 | if len(planA) != 2+2*len(Apps) || strings.Join(planB, ",") != "verify,activate" { |
| 842 | t.Fatalf("acts %v / %v", planA, planB) |
| 843 | } |
| 844 | for i := range planA { |
| 845 | t.Run(fmt.Sprintf("A%02d-%s", i, planA[i]), func(t *testing.T) { |
| 846 | a := newAdoption(t) |
| 847 | crashed := false |
| 848 | e := a.adopter(crashAt(i, &crashed), nil) |
| 849 | run, err := e.StartAdoption(a.ctx(), "Oficina", ownerCanary) |
| 850 | if err != nil { |
| 851 | t.Fatal(err) |
| 852 | } |
| 853 | a.confirm(homeop.KindCreateTeam) |
| 854 | for j := 0; j < 20 && !crashed; j++ { |
| 855 | _ = e.Advance(a.ctx(), run) |
| 856 | a.tick(5 * time.Second) |
| 857 | } |
| 858 | if !crashed { |
| 859 | t.Fatalf("act %d never reached", i) |
| 860 | } |
| 861 | a.tick(time.Minute) |
| 862 | if r := a.drive(a.adopter(nil, nil), run, 20); r.State != "done" || r.Code != "awaiting-configuration" { |
| 863 | t.Fatalf("after crash at %s: %s %s", planA[i], r.State, r.Code) |
| 864 | } |
| 865 | if a.home.TeamCount() != 1 { |
| 866 | t.Fatalf("teams %d", a.home.TeamCount()) |
| 867 | } |
| 868 | for _, s := range Slugs() { |
| 869 | if a.home.InstanceState(s) != "pending" { |
| 870 | t.Fatalf("%s %q", s, a.home.InstanceState(s)) |
| 871 | } |
| 872 | } |
| 873 | reqs := a.home.Requests() |
| 874 | onceEach(t, reqs) |
| 875 | if len(reqs) != 1+len(planA) || a.home.ConsumerCount() != 0 { |
| 876 | t.Fatalf("requests %v", reqs) |
| 877 | } |
| 878 | }) |
| 879 | } |
| 880 | for i := range planB { |
| 881 | t.Run(fmt.Sprintf("B%02d-%s", i, planB[i]), func(t *testing.T) { |
| 882 | a := newAdoption(t) |
| 883 | a.planA(a.adopter(nil, nil)) |
| 884 | crashed := false |
| 885 | obs := newObserver() |
| 886 | e := a.adopter(crashAt(i, &crashed), obs) |
| 887 | run := a.activate(e, "calendar") |
| 888 | for j := 0; j < 10 && !crashed; j++ { |
| 889 | _ = e.Advance(a.ctx(), run) |
| 890 | a.tick(5 * time.Second) |
| 891 | } |
| 892 | if !crashed { |
| 893 | t.Fatalf("act %d never reached", i) |
| 894 | } |
| 895 | a.tick(time.Minute) |
| 896 | if r := a.drive(a.adopter(nil, obs), run, 10); r.State != "done" { |
| 897 | t.Fatalf("after crash at %s: %s %s", planB[i], r.State, r.Code) |
| 898 | } |
| 899 | // The activation was observed once and replayed exactly. |
| 900 | if obs.count() != 1 || a.home.InstanceState("calendar") != "live" { |
| 901 | t.Fatalf("observed %d times", obs.count()) |
| 902 | } |
| 903 | reqs := a.home.Requests() |
| 904 | onceEach(t, reqs) |
| 905 | if !slices.Contains(reqs, homeop.DirectoryActivate+":calendar") { |
| 906 | t.Fatalf("requests %v", reqs) |
| 907 | } |
| 908 | }) |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | // Plan A interrupted after the seed, its window then closing: the team is |
| 913 | // seeded, some apps registered. A continuation under a fresh add-apps |
| 914 | // window for the stored SQID completes only the missing steps. |
| 915 | func TestAdoptionContinuesAfterTheWindowCloses(t *testing.T) { |
| 916 | a := newAdoption(t) |
| 917 | e := a.adopter(nil, nil) |
| 918 | _, err := e.ContinueAdoption(a.ctx()) |
| 919 | failCode(t, err, "no-team") |
| 920 | run, err := e.StartAdoption(a.ctx(), "Oficina", ownerCanary) |
| 921 | if err != nil { |
| 922 | t.Fatal(err) |
| 923 | } |
| 924 | _, err = e.ContinueAdoption(a.ctx()) |
| 925 | failCode(t, err, "team-not-seeded") |
| 926 | a.confirm(homeop.KindCreateTeam) |
| 927 | // Dies after sheets' register committed at Home (acts: owner-seed, |
| 928 | // seed-team, then each app's provision and register: docs, sheets). |
| 929 | crashed := false |
| 930 | dying := a.adopter(crashAt(5, &crashed), nil) |
| 931 | for j := 0; j < 10 && !crashed; j++ { |
| 932 | _ = dying.Advance(a.ctx(), run) |
| 933 | a.tick(5 * time.Second) |
| 934 | } |
| 935 | if !crashed { |
| 936 | t.Fatal("never reached sheets' register") |
| 937 | } |
| 938 | a.tick(homeop.WindowLifetime * time.Second) |
| 939 | if r := a.drive(e, run, 3); r.State != "parked" || r.Code != "window-expired" { |
| 940 | t.Fatalf("stranded run %s %s", r.State, r.Code) |
| 941 | } |
| 942 | // The lost answer was recovered by exact replay, outside the window. |
| 943 | if _, ok, _ := a.store.Done(a.ctx(), AdoptionLabel, "sheets", StepRegister); !ok { |
| 944 | t.Fatal("sheets' committed register was not recovered") |
| 945 | } |
| 946 | failCode(t, e.Resubmit(a.ctx(), run), "use-continue-adoption") |
| 947 | _, err = e.StartAdoption(a.ctx(), "Oficina", ownerCanary) |
| 948 | failCode(t, err, "label-used") |
| 949 | team, _ := a.store.Team(a.ctx(), AdoptionLabel) |
| 950 | next, err := e.ContinueAdoption(a.ctx()) |
| 951 | if err != nil { |
| 952 | t.Fatal(err) |
| 953 | } |
| 954 | if r, _ := a.store.Run(a.ctx(), run); r.State != "superseded" { |
| 955 | t.Fatalf("stranded run %s", r.State) |
| 956 | } |
| 957 | missing := "slides,calendar,calls,memoria,equipo,base" |
| 958 | if r, _ := a.store.Run(a.ctx(), next); !r.Adoption || r.Kind != homeop.KindCreateTeam || strings.Join(r.Apps, ",") != missing { |
| 959 | t.Fatalf("continuation run %+v", r) |
| 960 | } |
| 961 | plans := a.plans(next) |
| 962 | if len(plans) != 2 { |
| 963 | t.Fatalf("roles %d", len(plans)) |
| 964 | } |
| 965 | for role, p := range plans { |
| 966 | if p.Kind != homeop.KindAddApps || p.ExistingTeam != team.TeamSQID || strings.Join(p.Apps, ",") != missing || p.Owner != "" || p.DisplayName != "" { |
| 967 | t.Fatalf("%s continuation plan: kind %s existing %q apps %v", role, p.Kind, p.ExistingTeam, p.Apps) |
| 968 | } |
| 969 | } |
| 970 | if plans[homeop.Directory].Label != AdoptionLabel { |
| 971 | t.Fatal("continuation directory label") |
| 972 | } |
| 973 | a.confirm(homeop.KindAddApps) |
| 974 | if r := a.drive(e, next, 10); r.State != "done" || r.Code != "awaiting-configuration" { |
| 975 | t.Fatalf("continuation %s %s", r.State, r.Code) |
| 976 | } |
| 977 | if a.home.TeamCount() != 1 || a.home.ConsumerCount() != 0 { |
| 978 | t.Fatal("teams or credential consumers") |
| 979 | } |
| 980 | for _, s := range Slugs() { |
| 981 | if a.home.InstanceState(s) != "pending" { |
| 982 | t.Fatalf("%s %q", s, a.home.InstanceState(s)) |
| 983 | } |
| 984 | if _, ok, _ := a.store.Done(a.ctx(), AdoptionLabel, s, StepRegister); !ok { |
| 985 | t.Fatalf("%s not registered", s) |
| 986 | } |
| 987 | } |
| 988 | reqs := a.home.Requests() |
| 989 | onceEach(t, reqs) |
| 990 | if len(reqs) != 1+2+2*len(Apps) || a.home.Calls[homeop.DirectorySeedTeam] != 1 || a.home.Calls[homeop.CredentialOwnerSeed] != 1 { |
| 991 | t.Fatalf("requests %v", reqs) |
| 992 | } |
| 993 | _, err = e.ContinueAdoption(a.ctx()) |
| 994 | failCode(t, err, "plan-a-complete") |
| 995 | // Plan B follows as usual. |
| 996 | ob := a.adopter(nil, newObserver()) |
| 997 | b := a.activate(ob, "calendar") |
| 998 | if r := a.drive(ob, b, 10); r.State != "done" || a.home.InstanceState("calendar") != "live" { |
| 999 | t.Fatalf("plan B %s %s", r.State, r.Code) |
| 1000 | } |
| 1001 | } |
| 1002 | |