package provision import ( "strings" "testing" ) // The team bundle's seeded apps (oficina/alta design/team-bundle, plan // slices 1, 2 and 3b): Equipo and Base, own teams only. var bundleApps = []string{"docs", "sheets", "slides", "calendar", "calls", "memoria", "equipo", "base"} func keyMapOf(keys []Key) map[string]Key { m := map[string]Key{} for _, k := range keys { m[k.Name] = k } return m } func TestSeededRecipes(t *testing.T) { in := goldenInputs in.Label, in.Owner, in.Profile = "campbells", ownerCanary, ProfileOwn eq, _ := AppBySlug("equipo") base, _ := AppBySlug("base") e := keyMapOf(Recipe(eq, in)) want := map[string]string{ "EQUIPO_ORIGIN": "https://campbells.equipo.oficina.works", "EQUIPO_HOME_CONSUMER_ID": "campbells-equipo", "EQUIPO_OWNERS": ownerCanary, "EQUIPO_ASSISTANT_HOSTS": "https://campbells.base.oficina.works", } for name, v := range want { if e[name].Value != v || e[name].Class != Set { t.Errorf("equipo %s = %+v, want %q", name, e[name], v) } } if !e["EQUIPO_OWNERS"].OwnerAddress { t.Error("EQUIPO_OWNERS is not marked as the owner's address") } for _, name := range []string{"EQUIPO_INSTANCE_KEY", "EQUIPO_HOME_DEK_BASE64"} { if e[name].Class != Generated || !e[name].Secret { t.Errorf("equipo %s = %+v", name, e[name]) } } b := keyMapOf(Recipe(base, in)) want = map[string]string{ "BASE_ORIGIN": "https://campbells.base.oficina.works", "BASE_HOME_CONSUMER_ID": "campbells-base", "BASE_OWNER": ownerCanary, "BASE_NAME": "campbells", "BASE_SLUG": "campbells", "BASE_EQUIPO_ORIGIN": "https://campbells.equipo.oficina.works", "BASE_OFICINA_APPS": "docs=https://campbells.docs.oficina.works,sheets=https://campbells.sheets.oficina.works," + "slides=https://campbells.slides.oficina.works,calendar=https://campbells.calendar.oficina.works," + "calls=https://campbells.calls.oficina.works,memoria=https://campbells.memoria.oficina.works," + "equipo=https://campbells.equipo.oficina.works", } for name, v := range want { if b[name].Value != v || b[name].Class != Set { t.Errorf("base %s = %+v, want %q", name, b[name], v) } } for _, keys := range []map[string]Key{e, b} { for name := range keys { // No roster, no Store, no agent access, no execution: those // are absent (the plan), never written empty. for _, banned := range []string{"_TEAM_", "_PLATFORM_", "_APPS_DOMAIN", "_ASSERTION_KEY", "_AGENT_", "_EXECUTION_", "TENSORX", "HOUSE"} { if strings.Contains(name, banned) { t.Errorf("recipe writes %s", name) } } } } if !NeedsOwner(eq) || !NeedsOwner(base) { t.Error("a seeded recipe must write the owner, so the wait rule holds") } } func TestAppsRefusal(t *testing.T) { six := []string{"docs", "sheets", "slides", "calendar", "calls", "memoria"} activeSix := map[string]bool{} for _, a := range six { activeSix[a] = true } for _, tc := range []struct { name string label string apps []string active map[string]bool p Profile want string }{ {"six outside", "acme", six, nil, ProfileOutside, ""}, {"equipo outside", "acme", []string{"docs", "equipo"}, nil, ProfileOutside, "app-own-only"}, {"base outside", "acme", bundleApps, nil, ProfileOutside, "app-own-only"}, {"equipo own", "acme", []string{"docs", "equipo"}, nil, ProfileOwn, ""}, {"all own", "campbells", bundleApps, nil, ProfileOwn, ""}, {"base without equipo", "campbells", append(append([]string{}, six...), "base"), nil, ProfileOwn, "base-needs-team-apps"}, {"base alone, team has the rest", "campbells", []string{"base"}, map[string]bool{"docs": true, "sheets": true, "slides": true, "calendar": true, "calls": true, "memoria": true, "equipo": true}, ProfileOwn, ""}, {"base alone, team lacks calls", "campbells", []string{"base", "equipo"}, map[string]bool{"docs": true, "sheets": true, "slides": true, "calendar": true, "memoria": true}, ProfileOwn, "base-needs-team-apps"}, {"base label too long", "a-label-of-twenty-ch", bundleApps, nil, ProfileOwn, "label-too-long-for-base"}, {"equipo long label", "a-label-of-twenty-ch", []string{"equipo"}, activeSix, ProfileOwn, ""}, } { if got := AppsRefusal(tc.label, tc.apps, tc.active, tc.p); got != tc.want { t.Errorf("%s: %q, want %q", tc.name, got, tc.want) } } } func TestExecutionPair(t *testing.T) { eq, _ := AppBySlug("equipo") in := goldenInputs in.Label, in.Account, in.Profile = "campbells", "bds", ProfileOwn got := OperatorKeys(eq, in) want := []OperatorKey{ {Name: "EQUIPO_EXECUTION_ENABLED", Value: "1"}, {Name: "EQUIPO_EXECUTION_MARKER_PATH", Value: "/data/instances/campbells-equipo.bds.oncarlos.com/execution/witness"}, } if len(got) != 2 || got[0] != want[0] || got[1] != want[1] { t.Fatalf("own equipo operator keys %v", got) } in.Profile = ProfileOutside if len(OperatorKeys(eq, in)) != 0 { t.Fatal("an outside team admits Equipo execution") } in.Profile = ProfileOwn env := func(vals map[string]string) func(string) (string, string, bool) { return func(name string) (string, string, bool) { v, ok := vals[name] return "env", v, ok } } for _, tc := range []struct { name string vals map[string]string want OperatorState }{ {"none", map[string]string{}, OperatorNone}, {"half", map[string]string{"EQUIPO_EXECUTION_ENABLED": "1"}, OperatorPartial}, {"whole", map[string]string{"EQUIPO_EXECUTION_ENABLED": "1", "EQUIPO_EXECUTION_MARKER_PATH": want[1].Value}, OperatorWhole}, {"another instance's directory", map[string]string{"EQUIPO_EXECUTION_ENABLED": "1", "EQUIPO_EXECUTION_MARKER_PATH": "/data/instances/equipo.bds.oncarlos.com/execution/witness"}, OperatorInvalid}, {"enabled 0", map[string]string{"EQUIPO_EXECUTION_ENABLED": "0", "EQUIPO_EXECUTION_MARKER_PATH": want[1].Value}, OperatorInvalid}, } { if got := operatorState(OperatorKeys(eq, in), env(tc.vals)); got != tc.want { t.Errorf("%s: %v, want %v", tc.name, got, tc.want) } } } func (h *harness) createOwnBundle(e *Engine, label string) int64 { h.t.Helper() run, err := e.CreateTeam(h.ctx(), NewTeam{Label: label, Name: "Campbells", Owner: ownerCanary, Apps: bundleApps, Options: RunOptions{Profile: ProfileOwn}}) if err != nil { h.t.Fatal(err) } h.confirm("create-team") return run } // An own team with every app, Equipo and Base included, goes live end to // end: the seeded apps through their probe, each audited, and the owner's // address forgotten once the last recipe holding it is written. func TestOwnTeamWithEquipoAndBaseGoesLive(t *testing.T) { h := newHarness(t) h.initialized() e := h.engine(nil) run := h.createOwnBundle(e, "campbells") if r := h.drive(e, run, 80); r.State != "done" { for _, a := range bundleApps { for _, s := range AppSteps(mustApp(t, a)) { if st, _, _ := h.store.Step(h.ctx(), "campbells", a, s); st.State != "done" { t.Logf("%s %s: %s %s", a, s, st.State, st.Code) break } } } t.Fatalf("run %s %s", r.State, r.Code) } for _, a := range []string{"equipo", "base"} { if got := h.home.InstanceState(ConsumerID("campbells", a)); got != "live" { t.Errorf("%s directory row %q", a, got) } st, _, _ := h.store.Step(h.ctx(), "campbells", a, StepAudit) if st.State != "done" || st.Data["directory_pending"] != false { t.Errorf("%s audit %+v", a, st) } } if _, held, _ := h.store.Owner(h.ctx(), "campbells"); held { t.Error("the owner's address is still held after every recipe holding it is written") } } func mustApp(t *testing.T, slug string) App { t.Helper() a, ok := AppBySlug(slug) if !ok { t.Fatal(slug) } return a } // A seeded app's probe gates activation: an owner not yet seeded parks // and resumes; a probe naming another consumer parks; a roster app that // answers seeded is not the build its recipe was written for. func TestSeededProbeParks(t *testing.T) { for _, tc := range []struct { name string set func(h *harness) app string code string }{ {"owner pending", func(h *harness) { h.con.SeededRoster = "owner-pending" }, "equipo", "roster-owner-pending"}, {"wrong consumer", func(h *harness) { h.con.ProbeConsumer = map[string]string{"base": "campbells-equipo"} }, "base", "not-team-mode"}, {"roster app answers seeded", func(h *harness) { h.con.ProbeMode = map[string]string{"docs": "seeded"} }, "docs", "probe-mode-mismatch"}, } { t.Run(tc.name, func(t *testing.T) { h := newHarness(t) h.initialized() tc.set(h) e := h.engine(nil) run := h.createOwnBundle(e, "campbells") if r := h.drive(e, run, 60); r.State == "done" { t.Fatal("run finished past a bad probe") } st, _, _ := h.store.Step(h.ctx(), "campbells", tc.app, StepVerify) if st.State != "parked" || st.Code != tc.code { t.Fatalf("%s verify %s %s, want parked %s", tc.app, st.State, st.Code, tc.code) } if h.home.InstanceState(ConsumerID("campbells", tc.app)) == "live" { t.Fatal("activated past a bad probe") } }) } } // Add-apps can't slip an own-only app onto a team written under outside, // nor make a retyped (shape-checked only) address a seeded app's owner. func TestAddAppsOwnOnlyFits(t *testing.T) { for _, tc := range []struct { name string profile Profile want string }{ {"outside team, own run", ProfileOutside, "app-own-only"}, {"own team, owner forgotten", ProfileOwn, "owner-forgotten-seeded"}, } { t.Run(tc.name, func(t *testing.T) { h := newHarness(t) h.initialized() e := h.engine(nil) run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "campbells", Name: "Campbells", Owner: ownerCanary, Apps: []string{"docs"}, Options: RunOptions{Profile: tc.profile}}) if err != nil { t.Fatal(err) } h.confirm("create-team") if r := h.drive(e, run, 40); r.State != "done" { t.Fatalf("create: %s %s", r.State, r.Code) } if _, held, _ := h.store.Owner(h.ctx(), "campbells"); held { t.Fatal("the owner is still held with no recipe needing it") } h.tick(3600e9) _, err = e.AddApps(h.ctx(), "campbells", []string{"equipo"}, RunOptions{Profile: ProfileOwn}) failCode(t, err, tc.want) }) } }