| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/alta/console" |
| 12 | "amadan.net/oficina/alta/homeop" |
| 13 | ) |
| 14 | |
| 15 | // checkEndState is what every successful lagged run must leave, however |
| 16 | // it got there: one team, six consumers, six instances bound to the owned |
| 17 | // environment, environments exactly the recipe, no key regenerated, no |
| 18 | // address and no platform value left in Alta. |
| 19 | func checkEndState(t *testing.T, h *harness) { |
| 20 | t.Helper() |
| 21 | if n := h.home.TeamCount(); n != 1 { |
| 22 | t.Fatalf("teams at Home = %d, want 1", n) |
| 23 | } |
| 24 | if n := h.home.ConsumerCount(); n != len(allApps) { |
| 25 | t.Fatalf("credential consumers = %d, want %d", n, len(allApps)) |
| 26 | } |
| 27 | if n := h.con.InstanceCount(); n != len(allApps) { |
| 28 | t.Fatalf("instances = %d, want %d", n, len(allApps)) |
| 29 | } |
| 30 | e := h.engine(nil) |
| 31 | for _, slug := range allApps { |
| 32 | app, _ := AppBySlug(slug) |
| 33 | env, _, ok := h.con.Instance(RouteHost("lagged", slug, account)) |
| 34 | if !ok || env != "lagged" { |
| 35 | t.Fatalf("%s instance bound to %q", slug, env) |
| 36 | } |
| 37 | if _, state, ok := h.con.Claim(PublicHost("lagged", slug)); !ok || state != "live" { |
| 38 | t.Fatalf("%s claim %q", slug, state) |
| 39 | } |
| 40 | if err := e.AuditEnvironment(h.ctx(), "lagged", app); err != nil { |
| 41 | t.Fatalf("%s audit: %v", slug, err) |
| 42 | } |
| 43 | vars, owner, _ := h.con.Env(slug, "lagged") |
| 44 | if owner != "alta" { |
| 45 | t.Fatalf("%s environment owner %q", slug, owner) |
| 46 | } |
| 47 | for k, v := range vars { |
| 48 | if v.Kind == "secret" && !console.PlatformManaged(k) && h.con.SecretWrites[slug+"/lagged/"+k] != 1 { |
| 49 | t.Fatalf("%s %s written %d times: a generated key was regenerated", slug, k, h.con.SecretWrites[slug+"/lagged/"+k]) |
| 50 | } |
| 51 | } |
| 52 | want := "pending" |
| 53 | if app.Roster { |
| 54 | want = "live" |
| 55 | } |
| 56 | if got := h.home.InstanceState(ConsumerID("lagged", slug)); got != want { |
| 57 | t.Fatalf("%s directory row %q, want %q", slug, got, want) |
| 58 | } |
| 59 | } |
| 60 | // The roster bearer reached Docs, sealed, and is Home's. |
| 61 | if v, err := h.con.Open("docs", "lagged", "DOCS_TEAM_ROSTER_TOKEN"); err != nil || len(v) != 43 { |
| 62 | t.Fatalf("roster token: %q %v", v, err) |
| 63 | } |
| 64 | // Generated keys are 32 random bytes. |
| 65 | if v, err := h.con.Open("calls", "lagged", "CALLS_DEK"); err != nil || len(b64(v)) != 32 { |
| 66 | t.Fatalf("CALLS_DEK: %v", err) |
| 67 | } |
| 68 | // Privacy (§12): no address after step 13 anywhere in Alta's DB, and |
| 69 | // no platform value ever. |
| 70 | var n int |
| 71 | _ = h.store.DB().QueryRow("SELECT count(*) FROM owner_inputs").Scan(&n) |
| 72 | if n != 0 { |
| 73 | t.Fatal("owner input survives step 13") |
| 74 | } |
| 75 | _ = h.store.DB().QueryRow("SELECT count(*) FROM operator_requests WHERE action='credential.owner-seed'").Scan(&n) |
| 76 | if n != 0 { |
| 77 | t.Fatal("owner-seed request survives step 13") |
| 78 | } |
| 79 | // Only a create-team plan's credential half carries the owner; an |
| 80 | // add-apps plan carries none and may stay until it expires. |
| 81 | _ = h.store.DB().QueryRow("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')").Scan(&n) |
| 82 | if n != 0 { |
| 83 | t.Fatal("a credential plan carrying the owner survives step 13") |
| 84 | } |
| 85 | _, _ = h.store.DB().Exec("PRAGMA wal_checkpoint(TRUNCATE)") |
| 86 | for _, needle := range []string{ownerCanary, "fakeconsole", "PLATFORM-VALUE-CANARY", "lagged.test"} { |
| 87 | if h.scan(needle) { |
| 88 | t.Fatalf("%q found in Alta's database files", needle) |
| 89 | } |
| 90 | } |
| 91 | for _, needle := range []string{ownerCanary, "lagged", "PLATFORM-VALUE-CANARY"} { |
| 92 | if strings.Contains(h.logs.String(), needle) { |
| 93 | t.Fatalf("%q in Alta's logs", needle) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestCreateTeamEndToEnd(t *testing.T) { |
| 99 | h := newHarness(t) |
| 100 | h.initialized() |
| 101 | e := h.engine(nil) |
| 102 | run := h.createLagged(e) |
| 103 | r := h.drive(e, run, 40) |
| 104 | if r.State != "done" { |
| 105 | steps, _ := h.store.Steps(h.ctx(), "lagged") |
| 106 | t.Fatalf("run %s %s; steps %+v", r.State, r.Code, steps) |
| 107 | } |
| 108 | checkEndState(t, h) |
| 109 | // Every Home write was under the one plan id Paul confirmed. |
| 110 | if h.home.Calls[homeop.CredentialOwnerSeed] != 1 || h.home.Calls[homeop.DirectorySeedTeam] != 1 { |
| 111 | t.Fatalf("owner-seed %d, seed-team %d", h.home.Calls[homeop.CredentialOwnerSeed], h.home.Calls[homeop.DirectorySeedTeam]) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // countActs runs a whole create-team and returns its acts in order. |
| 116 | func countActs(t *testing.T) []string { |
| 117 | h := newHarness(t) |
| 118 | h.initialized() |
| 119 | var acts []string |
| 120 | e := h.engine(func(k string) error { acts = append(acts, k); return nil }) |
| 121 | run := h.createLagged(e) |
| 122 | if r := h.drive(e, run, 40); r.State != "done" { |
| 123 | t.Fatalf("clean run %s %s", r.State, r.Code) |
| 124 | } |
| 125 | return acts |
| 126 | } |
| 127 | |
| 128 | // TestCrashAfterEveryAct is §12's crash injection: after every act (the |
| 129 | // remote effect has happened, Alta has not recorded it) the process dies; |
| 130 | // a fresh engine over the same database resumes by probing and reaches |
| 131 | // the same end state with no duplicate instance, consumer or team. |
| 132 | func TestCrashAfterEveryAct(t *testing.T) { |
| 133 | acts := countActs(t) |
| 134 | if len(acts) < 20 { |
| 135 | t.Fatalf("only %d acts: %v", len(acts), acts) |
| 136 | } |
| 137 | for i := range acts { |
| 138 | t.Run(fmt.Sprintf("%02d-%s", i, strings.ReplaceAll(acts[i], "/", "-")), func(t *testing.T) { |
| 139 | h := newHarness(t) |
| 140 | h.initialized() |
| 141 | n := 0 |
| 142 | crashed := false |
| 143 | e := h.engine(func(k string) error { |
| 144 | if n == i && !crashed { |
| 145 | crashed = true |
| 146 | return errors.New("kill -9") |
| 147 | } |
| 148 | n++ |
| 149 | return nil |
| 150 | }) |
| 151 | run := h.createLagged(e) |
| 152 | for j := 0; j < 40 && !crashed; j++ { |
| 153 | _ = e.Advance(h.ctx(), run) |
| 154 | h.con.Walk() |
| 155 | h.tick(5 * time.Second) |
| 156 | } |
| 157 | if !crashed { |
| 158 | t.Fatalf("act %d never reached", i) |
| 159 | } |
| 160 | // The crashed process's lock is stale on restart. |
| 161 | h.tick(11 * time.Minute) |
| 162 | if r, _ := h.store.Run(h.ctx(), run); r.State != "done" { |
| 163 | // The window may have closed during the restart: Paul |
| 164 | // confirms a fresh plan for the same apps. |
| 165 | if r2 := h.drive(h.engine(nil), run, 40); r2.State != "done" { |
| 166 | next, err := h.engine(nil).AddApps(h.ctx(), "lagged", allApps, RunOptions{}) |
| 167 | if err != nil { |
| 168 | t.Fatalf("resume plan: %v (run %s %s)", err, r2.State, r2.Code) |
| 169 | } |
| 170 | kind := homeop.KindAddApps |
| 171 | if _, _, seeded := h.home.Team("lagged"); !seeded { |
| 172 | kind = homeop.KindCreateTeam |
| 173 | } |
| 174 | h.confirm(kind) |
| 175 | if r3 := h.drive(h.engine(nil), next, 40); r3.State != "done" { |
| 176 | t.Fatalf("after crash at %s: %s %s", acts[i], r3.State, r3.Code) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | checkEndState(t, h) |
| 181 | }) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // TestLostAnswersAreRecoveredByReplay: Home commits and the answer is |
| 186 | // lost on the way back, for every verb. Nothing is inferred from it; the |
| 187 | // exact replay returns the stored result. |
| 188 | func TestLostAnswersAreRecoveredByReplay(t *testing.T) { |
| 189 | for _, action := range []string{homeop.CredentialOwnerSeed, homeop.DirectorySeedTeam, homeop.DirectoryProvision, homeop.CredentialRegister, homeop.DirectoryVerify, homeop.DirectoryActivate} { |
| 190 | t.Run(action, func(t *testing.T) { |
| 191 | h := newHarness(t) |
| 192 | h.initialized() |
| 193 | lost := false |
| 194 | h.home.LoseAnswer = func(a string) bool { |
| 195 | if a == action && !lost { |
| 196 | lost = true |
| 197 | return true |
| 198 | } |
| 199 | return false |
| 200 | } |
| 201 | e := h.engine(nil) |
| 202 | run := h.createLagged(e) |
| 203 | if r := h.drive(e, run, 40); r.State != "done" { |
| 204 | t.Fatalf("%s %s", r.State, r.Code) |
| 205 | } |
| 206 | if !lost { |
| 207 | t.Fatal("no answer was lost") |
| 208 | } |
| 209 | checkEndState(t, h) |
| 210 | }) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // TestExpiredReceiptTakesANewOwnerSeed: replay returns the same expired |
| 215 | // receipt by design, so an expired one leads to a new owner-seed, not a |
| 216 | // stuck step 4. |
| 217 | func TestExpiredReceiptTakesANewOwnerSeed(t *testing.T) { |
| 218 | h := newHarness(t) |
| 219 | h.initialized() |
| 220 | down := true |
| 221 | h.home.Outage = func(a string) bool { return down && a == homeop.DirectorySeedTeam } |
| 222 | e := h.engine(nil) |
| 223 | run := h.createLagged(e) |
| 224 | _ = e.Advance(h.ctx(), run) |
| 225 | if h.home.Calls[homeop.CredentialOwnerSeed] != 1 { |
| 226 | t.Fatalf("owner-seed calls %d", h.home.Calls[homeop.CredentialOwnerSeed]) |
| 227 | } |
| 228 | h.tick(6 * time.Minute) // the receipt (300 s) and the signed seed-team expire |
| 229 | down = false |
| 230 | if r := h.drive(e, run, 40); r.State != "done" { |
| 231 | t.Fatalf("%s %s", r.State, r.Code) |
| 232 | } |
| 233 | if h.home.Calls[homeop.CredentialOwnerSeed] != 2 { |
| 234 | t.Fatalf("owner-seed calls %d, want a second for the expired receipt", h.home.Calls[homeop.CredentialOwnerSeed]) |
| 235 | } |
| 236 | checkEndState(t, h) |
| 237 | } |
| 238 | |
| 239 | // TestStreamResyncAfterLostDatabase: Alta's stream rows are gone (a DB |
| 240 | // restored from before its last operations). Home refuses the stale |
| 241 | // sequence; Alta re-reads Home's head through a signed inspect and goes on. |
| 242 | func TestStreamResyncAfterLostDatabase(t *testing.T) { |
| 243 | h := newHarness(t) |
| 244 | h.initialized() |
| 245 | e := h.engine(nil) |
| 246 | run := h.createLagged(e) |
| 247 | _ = e.Advance(h.ctx(), run) |
| 248 | seq, _ := h.home.Stream(homeop.Directory) |
| 249 | if seq < 3 { |
| 250 | t.Fatalf("directory sequence %d", seq) |
| 251 | } |
| 252 | // Roll Alta's view back to sequence 1, as a restored backup would. |
| 253 | var oldHead string |
| 254 | if err := h.store.DB().QueryRow("SELECT head FROM operator_requests WHERE role='directory' AND sequence=1").Scan(&oldHead); err != nil { |
| 255 | t.Fatal(err) |
| 256 | } |
| 257 | if err := h.store.SetStream(h.ctx(), homeop.Directory, 1, oldHead); err != nil { |
| 258 | t.Fatal(err) |
| 259 | } |
| 260 | if r := h.drive(e, run, 40); r.State != "done" { |
| 261 | t.Fatalf("%s %s", r.State, r.Code) |
| 262 | } |
| 263 | got, head := h.home.Stream(homeop.Directory) |
| 264 | if s, hd, _, _ := h.store.Stream(h.ctx(), homeop.Directory); s != got || hd != head { |
| 265 | t.Fatalf("Alta's stream %d/%s, Home's %d/%s", s, hd, got, head) |
| 266 | } |
| 267 | audit, _ := h.store.AuditLog(h.ctx(), "lagged", 500) |
| 268 | found := false |
| 269 | for _, a := range audit { |
| 270 | found = found || a.Event == "stream-resync" |
| 271 | } |
| 272 | if !found { |
| 273 | t.Fatal("no stream-resync in the audit") |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | func TestWindowExpiryParksAndAddAppsResumes(t *testing.T) { |
| 278 | h := newHarness(t) |
| 279 | h.initialized() |
| 280 | h.con.DomainPolls = 1000 // domains do not issue within the window |
| 281 | e := h.engine(nil) |
| 282 | run := h.createLagged(e) |
| 283 | r := h.drive(e, run, 5) |
| 284 | if r.State != "parked" { |
| 285 | t.Fatalf("%s %s", r.State, r.Code) |
| 286 | } |
| 287 | h.tick(homeop.WindowLifetime * time.Second) |
| 288 | for _, a := range allApps { |
| 289 | h.con.SetDomainState(PublicHost("lagged", a), "live") |
| 290 | } |
| 291 | if r = h.drive(e, run, 10); r.State != "parked" || r.Code != "window-expired" { |
| 292 | t.Fatalf("after window: %s %s", r.State, r.Code) |
| 293 | } |
| 294 | next, err := e.AddApps(h.ctx(), "lagged", allApps, RunOptions{}) |
| 295 | if err != nil { |
| 296 | t.Fatal(err) |
| 297 | } |
| 298 | h.confirm(homeop.KindAddApps) |
| 299 | if r = h.drive(e, next, 40); r.State != "done" { |
| 300 | t.Fatalf("add-apps: %s %s", r.State, r.Code) |
| 301 | } |
| 302 | checkEndState(t, h) |
| 303 | } |
| 304 | |
| 305 | func TestLabelsAreNeverReused(t *testing.T) { |
| 306 | h := newHarness(t) |
| 307 | h.initialized() |
| 308 | e := h.engine(nil) |
| 309 | run := h.createLagged(e) |
| 310 | h.drive(e, run, 40) |
| 311 | _, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Again", Owner: ownerCanary, Apps: []string{"docs"}}) |
| 312 | var fl *Fail |
| 313 | if !errors.As(err, &fl) || fl.Code != "label-used" { |
| 314 | t.Fatalf("reused label: %v", err) |
| 315 | } |
| 316 | // "oficina" is the canonical apps' team, adopted on Home's own path, |
| 317 | // never a new team's label; refused before anything is recorded. |
| 318 | if _, err := e.CreateTeam(h.ctx(), NewTeam{Label: "oficina", Name: "Oficina", Owner: ownerCanary, Apps: []string{"docs"}}); !errors.As(err, &fl) || fl.Code != "label-reserved" { |
| 319 | t.Fatalf("reserved label: %v", err) |
| 320 | } |
| 321 | if used, err := h.store.LabelUsed(h.ctx(), "oficina"); err != nil || used { |
| 322 | t.Fatalf("a refused reserved label was recorded: %v %v", used, err) |
| 323 | } |
| 324 | // Ordinary add-apps and retire refuse it too: adopting the canonical |
| 325 | // apps is Home's own signed scope, never an ordinary plan under the label. |
| 326 | if _, err := e.AddApps(h.ctx(), "oficina", []string{"docs"}, RunOptions{}); !errors.As(err, &fl) || fl.Code != "label-reserved" { |
| 327 | t.Fatalf("add-apps under the reserved label: %v", err) |
| 328 | } |
| 329 | if _, err := e.RetireApps(h.ctx(), "oficina", []string{"docs"}, "oficina"); !errors.As(err, &fl) || fl.Code != "label-reserved" { |
| 330 | t.Fatalf("retire under the reserved label: %v", err) |
| 331 | } |
| 332 | // Alta's label rule is tighter than Home's. |
| 333 | for _, bad := range []string{"a", "Lagged", "-lag", "lag-", strings.Repeat("a", 33), "la_g"} { |
| 334 | if _, err := e.CreateTeam(h.ctx(), NewTeam{Label: bad, Name: "x", Owner: ownerCanary, Apps: []string{"docs"}}); !errors.As(err, &fl) || fl.Code != "label-shape" { |
| 335 | t.Fatalf("label %q: %v", bad, err) |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | func TestOwnerMustAlreadyExist(t *testing.T) { |
| 341 | h := newHarness(t) |
| 342 | h.initialized() |
| 343 | e := h.engine(nil) |
| 344 | _, err := e.CreateTeam(h.ctx(), NewTeam{Label: "alpha", Name: "Alpha", Owner: "nobody@nowhere.test", Apps: []string{"docs"}}) |
| 345 | var fl *Fail |
| 346 | if !errors.As(err, &fl) || fl.Code != "plan-refused-credential" { |
| 347 | t.Fatalf("unknown owner: %v", err) |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | func TestDocsMailUnderTheWrongPrefixRefuses(t *testing.T) { |
| 352 | h := newHarness(t) |
| 353 | h.initialized() |
| 354 | h.con.MailPrefix["docs"] = "CARLOS_SMTP" // Docs reads DOCS_SMTP_* only |
| 355 | e := h.engine(nil) |
| 356 | run := h.createLagged(e) |
| 357 | r := h.drive(e, run, 10) |
| 358 | if r.State != "failed" || r.Code != "mail-prefix-docs" { |
| 359 | t.Fatalf("%s %s", r.State, r.Code) |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | func TestPlatformKeysAwaitTheWalk(t *testing.T) { |
| 364 | h := newHarness(t) |
| 365 | h.initialized() |
| 366 | e := h.engine(nil) |
| 367 | run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "beta", Name: "Beta", Owner: ownerCanary, Apps: []string{"calls"}}) |
| 368 | if err != nil { |
| 369 | t.Fatal(err) |
| 370 | } |
| 371 | h.confirm(homeop.KindCreateTeam) |
| 372 | _ = e.Advance(h.ctx(), run) |
| 373 | st, _, _ := h.store.Step(h.ctx(), "beta", "calls", StepEnv) |
| 374 | if st.Code != "awaiting-platform-keys" { |
| 375 | t.Fatalf("env step %s %s", st.State, st.Code) |
| 376 | } |
| 377 | // No instance before PLATFORM keys are there (§6). |
| 378 | if h.con.InstanceCount() != 0 { |
| 379 | t.Fatal("instance created before the platform keys arrived") |
| 380 | } |
| 381 | h.con.Walk() |
| 382 | if r := h.drive(e, run, 20); r.State != "done" { |
| 383 | t.Fatalf("%s %s", r.State, r.Code) |
| 384 | } |
| 385 | vars, _, _ := h.con.Env("calls", "beta") |
| 386 | if _, ok := vars["CARLOS_RELAY"]; !ok { |
| 387 | t.Fatal("no relay identity") |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | func TestForkedEnvironmentKeepsKeysAndDropsTrack1Extras(t *testing.T) { |
| 392 | h := newHarness(t) |
| 393 | h.initialized() |
| 394 | // Paul: carlos env fork --from lagged --to lagged-team --scope alta, |
| 395 | // then the rebind. The fork carries lagged's own keys only. |
| 396 | h.con.SetEnv("memoria", "lagged-team", "alta", map[string]fakeVar{ |
| 397 | "MEMORIA_DEK": {Kind: "secret", Envelope: sealFor(t, h, "memoria", "till-data-key")}, |
| 398 | "MEMORIA_INSTANCE_KEY": {Kind: "secret", Envelope: sealFor(t, h, "memoria", "instance")}, |
| 399 | "MEMORIA_ORIGIN": {Kind: "env", Value: "https://lagged.memoria.oficina.works"}, |
| 400 | "MEMORIA_OWNERS": {Kind: "env", Value: ownerCanary}, |
| 401 | "MEMORIA_AI": {Kind: "env", Value: ""}, |
| 402 | "OFICINA_DASHBOARD_TEAM": {Kind: "env", Value: ""}, |
| 403 | }) |
| 404 | h.con.AddInstance("memoria", RouteHost("lagged", "memoria", account), "lagged-team") |
| 405 | h.con.AddClaim("memoria", PublicHost("lagged", "memoria"), RouteHost("lagged", "memoria", account), "live") |
| 406 | e := h.engine(nil) |
| 407 | run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: ownerCanary, Apps: []string{"memoria"}, Forked: true}) |
| 408 | if err != nil { |
| 409 | t.Fatal(err) |
| 410 | } |
| 411 | h.confirm(homeop.KindCreateTeam) |
| 412 | if r := h.drive(e, run, 20); r.State != "done" { |
| 413 | t.Fatalf("%s %s", r.State, r.Code) |
| 414 | } |
| 415 | vars, _, _ := h.con.Env("memoria", "lagged-team") |
| 416 | for _, k := range []string{"MEMORIA_AI", "OFICINA_DASHBOARD_TEAM"} { |
| 417 | if _, ok := vars[k]; ok { |
| 418 | t.Fatalf("%s survived the fork", k) |
| 419 | } |
| 420 | } |
| 421 | if v, _ := h.con.Open("memoria", "lagged-team", "MEMORIA_DEK"); v != "till-data-key" { |
| 422 | t.Fatal("the forked DEK was regenerated: Till's data would be unreadable") |
| 423 | } |
| 424 | if _, restarts, _ := h.con.Instance(RouteHost("lagged", "memoria", account)); restarts == 0 { |
| 425 | t.Fatal("config changed under a running instance without a restart") |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | func TestExtraKeyInAnUnforkedEnvironmentRefuses(t *testing.T) { |
| 430 | h := newHarness(t) |
| 431 | h.initialized() |
| 432 | e := h.engine(nil) |
| 433 | run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "gamma", Name: "Gamma", Owner: ownerCanary, Apps: []string{"sheets"}}) |
| 434 | if err != nil { |
| 435 | t.Fatal(err) |
| 436 | } |
| 437 | h.confirm(homeop.KindCreateTeam) |
| 438 | if r := h.drive(e, run, 20); r.State != "done" { |
| 439 | t.Fatalf("%s %s", r.State, r.Code) |
| 440 | } |
| 441 | vars, _, _ := h.con.Env("sheets", "gamma") |
| 442 | vars["SNEAKY"] = fakeVar{Kind: "env", Value: "x"} |
| 443 | h.con.SetEnv("sheets", "gamma", "alta", vars) |
| 444 | app, _ := AppBySlug("sheets") |
| 445 | var fl *Fail |
| 446 | if err := e.AuditEnvironment(h.ctx(), "gamma", app); !errors.As(err, &fl) || fl.Code != "audit-extra-key" { |
| 447 | t.Fatalf("audit with an extra key: %v", err) |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | func TestDeprovision(t *testing.T) { |
| 452 | h := newHarness(t) |
| 453 | h.initialized() |
| 454 | e := h.engine(nil) |
| 455 | run := h.createLagged(e) |
| 456 | h.drive(e, run, 40) |
| 457 | h.tick(homeop.WindowLifetime * time.Second) |
| 458 | var fl *Fail |
| 459 | if _, err := e.RetireApps(h.ctx(), "lagged", []string{"calls"}, "laged"); !errors.As(err, &fl) || fl.Code != "label-mismatch" { |
| 460 | t.Fatalf("mistyped label: %v", err) |
| 461 | } |
| 462 | ret, err := e.RetireApps(h.ctx(), "lagged", []string{"docs", "calls"}, "lagged") |
| 463 | if err != nil { |
| 464 | t.Fatal(err) |
| 465 | } |
| 466 | h.confirm(homeop.KindRetireApps) |
| 467 | if r := h.drive(e, ret, 10); r.State != "done" { |
| 468 | t.Fatalf("%s %s", r.State, r.Code) |
| 469 | } |
| 470 | for _, a := range []string{"docs", "calls"} { |
| 471 | if got := h.home.InstanceState(ConsumerID("lagged", a)); got != "disabled" { |
| 472 | t.Fatalf("%s directory %q", a, got) |
| 473 | } |
| 474 | if got := h.home.ConsumerState(ConsumerID("lagged", a)); got != "retired" { |
| 475 | t.Fatalf("%s credential %q", a, got) |
| 476 | } |
| 477 | if _, _, ok := h.con.Claim(PublicHost("lagged", a)); ok { |
| 478 | t.Fatalf("%s claim survives", a) |
| 479 | } |
| 480 | if _, _, ok := h.con.Instance(RouteHost("lagged", a, account)); ok { |
| 481 | t.Fatalf("%s instance survives", a) |
| 482 | } |
| 483 | // The owned environment survives (no delete verb exists). |
| 484 | if _, _, ok := h.con.Env(a, "lagged"); !ok { |
| 485 | t.Fatalf("%s environment gone", a) |
| 486 | } |
| 487 | } |
| 488 | // Retired hostnames are never reused. |
| 489 | h.tick(homeop.WindowLifetime * time.Second) |
| 490 | if _, err := e.AddApps(h.ctx(), "lagged", []string{"calls"}, RunOptions{}); !errors.As(err, &fl) || fl.Code != "app-retired" { |
| 491 | t.Fatalf("re-adding a retired app: %v", err) |
| 492 | } |
| 493 | // Untouched apps keep running. |
| 494 | if _, _, ok := h.con.Instance(RouteHost("lagged", "sheets", account)); !ok { |
| 495 | t.Fatal("sheets went with them") |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | func TestInitializeNeedsTheTypedAttestation(t *testing.T) { |
| 500 | h := newHarness(t) |
| 501 | e := h.engine(nil) |
| 502 | var fl *Fail |
| 503 | if _, err := e.StartInitialize(h.ctx(), "yes"); !errors.As(err, &fl) || fl.Code != "attestation-mismatch" { |
| 504 | t.Fatalf("%v", err) |
| 505 | } |
| 506 | // Before initialize, no team can be created. |
| 507 | if _, err := e.CreateTeam(h.ctx(), NewTeam{Label: "alpha", Name: "Alpha", Owner: ownerCanary, Apps: []string{"docs"}}); !errors.As(err, &fl) || fl.Code != "home-not-initialized" { |
| 508 | t.Fatalf("%v", err) |
| 509 | } |
| 510 | h.initialized() |
| 511 | if _, err := e.StartInitialize(h.ctx(), InitializeAttestation); !errors.As(err, &fl) || fl.Code != "already-initialized" { |
| 512 | t.Fatalf("%v", err) |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | func TestPlanIsTheSameOnBothRoles(t *testing.T) { |
| 517 | h := newHarness(t) |
| 518 | h.initialized() |
| 519 | e := h.engine(nil) |
| 520 | run := h.createLagged(e) |
| 521 | r, _ := h.store.Run(h.ctx(), run) |
| 522 | plans, _ := h.store.Plans(h.ctx(), r.PlanID) |
| 523 | if len(plans) != 2 { |
| 524 | t.Fatalf("plans %d", len(plans)) |
| 525 | } |
| 526 | for role, p := range plans { |
| 527 | raw, _ := homeop.EnvelopePayload(p.Signed) |
| 528 | var plan homeop.Plan |
| 529 | if err := homeop.Decode(raw, &plan); err != nil || plan.PlanID != r.PlanID { |
| 530 | t.Fatalf("%s plan %v", role, err) |
| 531 | } |
| 532 | if role == homeop.Directory && (plan.Label != "lagged" || plan.DisplayName != "Lagged" || plan.Owner != "") { |
| 533 | t.Fatalf("directory plan %+v", plan) |
| 534 | } |
| 535 | if role == homeop.Credential && (plan.Owner != ownerCanary || plan.Label != "" || plan.DisplayName != "") { |
| 536 | t.Fatalf("credential plan carries %+v", plan) |
| 537 | } |
| 538 | } |
| 539 | _ = context.Background |
| 540 | } |
| 541 | |