| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // The team bundle's seeded apps (oficina/alta design/team-bundle, plan |
| 9 | // slices 1, 2 and 3b): Equipo and Base, own teams only. |
| 10 | |
| 11 | var bundleApps = []string{"docs", "sheets", "slides", "calendar", "calls", "memoria", "equipo", "base"} |
| 12 | |
| 13 | func keyMapOf(keys []Key) map[string]Key { |
| 14 | m := map[string]Key{} |
| 15 | for _, k := range keys { |
| 16 | m[k.Name] = k |
| 17 | } |
| 18 | return m |
| 19 | } |
| 20 | |
| 21 | func TestSeededRecipes(t *testing.T) { |
| 22 | in := goldenInputs |
| 23 | in.Label, in.Owner, in.Profile = "campbells", ownerCanary, ProfileOwn |
| 24 | eq, _ := AppBySlug("equipo") |
| 25 | base, _ := AppBySlug("base") |
| 26 | |
| 27 | e := keyMapOf(Recipe(eq, in)) |
| 28 | want := map[string]string{ |
| 29 | "EQUIPO_ORIGIN": "https://campbells.equipo.oficina.works", |
| 30 | "EQUIPO_HOME_CONSUMER_ID": "campbells-equipo", |
| 31 | "EQUIPO_OWNERS": ownerCanary, |
| 32 | "EQUIPO_ASSISTANT_HOSTS": "https://campbells.base.oficina.works", |
| 33 | } |
| 34 | for name, v := range want { |
| 35 | if e[name].Value != v || e[name].Class != Set { |
| 36 | t.Errorf("equipo %s = %+v, want %q", name, e[name], v) |
| 37 | } |
| 38 | } |
| 39 | if !e["EQUIPO_OWNERS"].OwnerAddress { |
| 40 | t.Error("EQUIPO_OWNERS is not marked as the owner's address") |
| 41 | } |
| 42 | for _, name := range []string{"EQUIPO_INSTANCE_KEY", "EQUIPO_HOME_DEK_BASE64"} { |
| 43 | if e[name].Class != Generated || !e[name].Secret { |
| 44 | t.Errorf("equipo %s = %+v", name, e[name]) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | b := keyMapOf(Recipe(base, in)) |
| 49 | want = map[string]string{ |
| 50 | "BASE_ORIGIN": "https://campbells.base.oficina.works", |
| 51 | "BASE_HOME_CONSUMER_ID": "campbells-base", |
| 52 | "BASE_OWNER": ownerCanary, |
| 53 | "BASE_NAME": "campbells", |
| 54 | "BASE_SLUG": "campbells", |
| 55 | "BASE_EQUIPO_ORIGIN": "https://campbells.equipo.oficina.works", |
| 56 | "BASE_OFICINA_APPS": "docs=https://campbells.docs.oficina.works,sheets=https://campbells.sheets.oficina.works," + |
| 57 | "slides=https://campbells.slides.oficina.works,calendar=https://campbells.calendar.oficina.works," + |
| 58 | "calls=https://campbells.calls.oficina.works,memoria=https://campbells.memoria.oficina.works," + |
| 59 | "equipo=https://campbells.equipo.oficina.works", |
| 60 | } |
| 61 | for name, v := range want { |
| 62 | if b[name].Value != v || b[name].Class != Set { |
| 63 | t.Errorf("base %s = %+v, want %q", name, b[name], v) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | for _, keys := range []map[string]Key{e, b} { |
| 68 | for name := range keys { |
| 69 | // No roster, no Store, no agent access, no execution: those |
| 70 | // are absent (the plan), never written empty. |
| 71 | for _, banned := range []string{"_TEAM_", "_PLATFORM_", "_APPS_DOMAIN", "_ASSERTION_KEY", "_AGENT_", "_EXECUTION_", "TENSORX", "HOUSE"} { |
| 72 | if strings.Contains(name, banned) { |
| 73 | t.Errorf("recipe writes %s", name) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | if !NeedsOwner(eq) || !NeedsOwner(base) { |
| 79 | t.Error("a seeded recipe must write the owner, so the wait rule holds") |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestAppsRefusal(t *testing.T) { |
| 84 | six := []string{"docs", "sheets", "slides", "calendar", "calls", "memoria"} |
| 85 | activeSix := map[string]bool{} |
| 86 | for _, a := range six { |
| 87 | activeSix[a] = true |
| 88 | } |
| 89 | for _, tc := range []struct { |
| 90 | name string |
| 91 | label string |
| 92 | apps []string |
| 93 | active map[string]bool |
| 94 | p Profile |
| 95 | want string |
| 96 | }{ |
| 97 | {"six outside", "acme", six, nil, ProfileOutside, ""}, |
| 98 | {"equipo outside", "acme", []string{"docs", "equipo"}, nil, ProfileOutside, "app-own-only"}, |
| 99 | {"base outside", "acme", bundleApps, nil, ProfileOutside, "app-own-only"}, |
| 100 | {"equipo own", "acme", []string{"docs", "equipo"}, nil, ProfileOwn, ""}, |
| 101 | {"all own", "campbells", bundleApps, nil, ProfileOwn, ""}, |
| 102 | {"base without equipo", "campbells", append(append([]string{}, six...), "base"), nil, ProfileOwn, "base-needs-team-apps"}, |
| 103 | {"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, ""}, |
| 104 | {"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"}, |
| 105 | {"base label too long", "a-label-of-twenty-ch", bundleApps, nil, ProfileOwn, "label-too-long-for-base"}, |
| 106 | {"equipo long label", "a-label-of-twenty-ch", []string{"equipo"}, activeSix, ProfileOwn, ""}, |
| 107 | } { |
| 108 | if got := AppsRefusal(tc.label, tc.apps, tc.active, tc.p); got != tc.want { |
| 109 | t.Errorf("%s: %q, want %q", tc.name, got, tc.want) |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func TestExecutionPair(t *testing.T) { |
| 115 | eq, _ := AppBySlug("equipo") |
| 116 | in := goldenInputs |
| 117 | in.Label, in.Account, in.Profile = "campbells", "bds", ProfileOwn |
| 118 | got := OperatorKeys(eq, in) |
| 119 | want := []OperatorKey{ |
| 120 | {Name: "EQUIPO_EXECUTION_ENABLED", Value: "1"}, |
| 121 | {Name: "EQUIPO_EXECUTION_MARKER_PATH", Value: "/data/instances/campbells-equipo.bds.oncarlos.com/execution/witness"}, |
| 122 | } |
| 123 | if len(got) != 2 || got[0] != want[0] || got[1] != want[1] { |
| 124 | t.Fatalf("own equipo operator keys %v", got) |
| 125 | } |
| 126 | in.Profile = ProfileOutside |
| 127 | if len(OperatorKeys(eq, in)) != 0 { |
| 128 | t.Fatal("an outside team admits Equipo execution") |
| 129 | } |
| 130 | in.Profile = ProfileOwn |
| 131 | env := func(vals map[string]string) func(string) (string, string, bool) { |
| 132 | return func(name string) (string, string, bool) { |
| 133 | v, ok := vals[name] |
| 134 | return "env", v, ok |
| 135 | } |
| 136 | } |
| 137 | for _, tc := range []struct { |
| 138 | name string |
| 139 | vals map[string]string |
| 140 | want OperatorState |
| 141 | }{ |
| 142 | {"none", map[string]string{}, OperatorNone}, |
| 143 | {"half", map[string]string{"EQUIPO_EXECUTION_ENABLED": "1"}, OperatorPartial}, |
| 144 | {"whole", map[string]string{"EQUIPO_EXECUTION_ENABLED": "1", "EQUIPO_EXECUTION_MARKER_PATH": want[1].Value}, OperatorWhole}, |
| 145 | {"another instance's directory", map[string]string{"EQUIPO_EXECUTION_ENABLED": "1", "EQUIPO_EXECUTION_MARKER_PATH": "/data/instances/equipo.bds.oncarlos.com/execution/witness"}, OperatorInvalid}, |
| 146 | {"enabled 0", map[string]string{"EQUIPO_EXECUTION_ENABLED": "0", "EQUIPO_EXECUTION_MARKER_PATH": want[1].Value}, OperatorInvalid}, |
| 147 | } { |
| 148 | if got := operatorState(OperatorKeys(eq, in), env(tc.vals)); got != tc.want { |
| 149 | t.Errorf("%s: %v, want %v", tc.name, got, tc.want) |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | func (h *harness) createOwnBundle(e *Engine, label string) int64 { |
| 155 | h.t.Helper() |
| 156 | run, err := e.CreateTeam(h.ctx(), NewTeam{Label: label, Name: "Campbells", Owner: ownerCanary, Apps: bundleApps, Options: RunOptions{Profile: ProfileOwn}}) |
| 157 | if err != nil { |
| 158 | h.t.Fatal(err) |
| 159 | } |
| 160 | h.confirm("create-team") |
| 161 | return run |
| 162 | } |
| 163 | |
| 164 | // An own team with every app, Equipo and Base included, goes live end to |
| 165 | // end: the seeded apps through their probe, each audited, and the owner's |
| 166 | // address forgotten once the last recipe holding it is written. |
| 167 | func TestOwnTeamWithEquipoAndBaseGoesLive(t *testing.T) { |
| 168 | h := newHarness(t) |
| 169 | h.initialized() |
| 170 | e := h.engine(nil) |
| 171 | run := h.createOwnBundle(e, "campbells") |
| 172 | if r := h.drive(e, run, 80); r.State != "done" { |
| 173 | for _, a := range bundleApps { |
| 174 | for _, s := range AppSteps(mustApp(t, a)) { |
| 175 | if st, _, _ := h.store.Step(h.ctx(), "campbells", a, s); st.State != "done" { |
| 176 | t.Logf("%s %s: %s %s", a, s, st.State, st.Code) |
| 177 | break |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | t.Fatalf("run %s %s", r.State, r.Code) |
| 182 | } |
| 183 | for _, a := range []string{"equipo", "base"} { |
| 184 | if got := h.home.InstanceState(ConsumerID("campbells", a)); got != "live" { |
| 185 | t.Errorf("%s directory row %q", a, got) |
| 186 | } |
| 187 | st, _, _ := h.store.Step(h.ctx(), "campbells", a, StepAudit) |
| 188 | if st.State != "done" || st.Data["directory_pending"] != false { |
| 189 | t.Errorf("%s audit %+v", a, st) |
| 190 | } |
| 191 | } |
| 192 | if _, held, _ := h.store.Owner(h.ctx(), "campbells"); held { |
| 193 | t.Error("the owner's address is still held after every recipe holding it is written") |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func mustApp(t *testing.T, slug string) App { |
| 198 | t.Helper() |
| 199 | a, ok := AppBySlug(slug) |
| 200 | if !ok { |
| 201 | t.Fatal(slug) |
| 202 | } |
| 203 | return a |
| 204 | } |
| 205 | |
| 206 | // A seeded app's probe gates activation: an owner not yet seeded parks |
| 207 | // and resumes; a probe naming another consumer parks; a roster app that |
| 208 | // answers seeded is not the build its recipe was written for. |
| 209 | func TestSeededProbeParks(t *testing.T) { |
| 210 | for _, tc := range []struct { |
| 211 | name string |
| 212 | set func(h *harness) |
| 213 | app string |
| 214 | code string |
| 215 | }{ |
| 216 | {"owner pending", func(h *harness) { h.con.SeededRoster = "owner-pending" }, "equipo", "roster-owner-pending"}, |
| 217 | {"wrong consumer", func(h *harness) { h.con.ProbeConsumer = map[string]string{"base": "campbells-equipo"} }, "base", "not-team-mode"}, |
| 218 | {"roster app answers seeded", func(h *harness) { h.con.ProbeMode = map[string]string{"docs": "seeded"} }, "docs", "probe-mode-mismatch"}, |
| 219 | } { |
| 220 | t.Run(tc.name, func(t *testing.T) { |
| 221 | h := newHarness(t) |
| 222 | h.initialized() |
| 223 | tc.set(h) |
| 224 | e := h.engine(nil) |
| 225 | run := h.createOwnBundle(e, "campbells") |
| 226 | if r := h.drive(e, run, 60); r.State == "done" { |
| 227 | t.Fatal("run finished past a bad probe") |
| 228 | } |
| 229 | st, _, _ := h.store.Step(h.ctx(), "campbells", tc.app, StepVerify) |
| 230 | if st.State != "parked" || st.Code != tc.code { |
| 231 | t.Fatalf("%s verify %s %s, want parked %s", tc.app, st.State, st.Code, tc.code) |
| 232 | } |
| 233 | if h.home.InstanceState(ConsumerID("campbells", tc.app)) == "live" { |
| 234 | t.Fatal("activated past a bad probe") |
| 235 | } |
| 236 | }) |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // Add-apps can't slip an own-only app onto a team written under outside, |
| 241 | // nor make a retyped (shape-checked only) address a seeded app's owner. |
| 242 | func TestAddAppsOwnOnlyFits(t *testing.T) { |
| 243 | for _, tc := range []struct { |
| 244 | name string |
| 245 | profile Profile |
| 246 | want string |
| 247 | }{ |
| 248 | {"outside team, own run", ProfileOutside, "app-own-only"}, |
| 249 | {"own team, owner forgotten", ProfileOwn, "owner-forgotten-seeded"}, |
| 250 | } { |
| 251 | t.Run(tc.name, func(t *testing.T) { |
| 252 | h := newHarness(t) |
| 253 | h.initialized() |
| 254 | e := h.engine(nil) |
| 255 | run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "campbells", Name: "Campbells", Owner: ownerCanary, Apps: []string{"docs"}, Options: RunOptions{Profile: tc.profile}}) |
| 256 | if err != nil { |
| 257 | t.Fatal(err) |
| 258 | } |
| 259 | h.confirm("create-team") |
| 260 | if r := h.drive(e, run, 40); r.State != "done" { |
| 261 | t.Fatalf("create: %s %s", r.State, r.Code) |
| 262 | } |
| 263 | if _, held, _ := h.store.Owner(h.ctx(), "campbells"); held { |
| 264 | t.Fatal("the owner is still held with no recipe needing it") |
| 265 | } |
| 266 | h.tick(3600e9) |
| 267 | _, err = e.AddApps(h.ctx(), "campbells", []string{"equipo"}, RunOptions{Profile: ProfileOwn}) |
| 268 | failCode(t, err, tc.want) |
| 269 | }) |
| 270 | } |
| 271 | } |
| 272 | |