package provision import ( "errors" "strings" "testing" "time" "amadan.net/oficina/alta/homeop" ) // The engine half of the adverse suite: whatever the CARLOS half does // wrong or fails to do, an adopted app's activation is never signed and // Home never commits a directory.activate-consumer for any app. // neverActivated fails if Home was asked to activate any consumer. func (a *adoptHarness) neverActivated() { a.t.Helper() for _, r := range a.home.Requests() { if strings.HasPrefix(r, homeop.DirectoryActivate) { a.t.Fatalf("Home committed %s", r) } } if a.home.Calls[homeop.DirectoryActivate] != 0 { a.t.Fatal("Home was sent an activation") } for _, s := range Slugs() { if st := a.home.InstanceState(s); st == "live" { a.t.Fatalf("%s is live", s) } } } // gateCodes is every code the activation gate refuses with. The family // prefixes (identity-, drift-, config-, team-mode-, agent-key-) carry a // field or key name after them. var gateCodes = []string{ "unknown-app", "team-not-sqid", "missing-home-preflight", "missing-baseline", "missing-write", "missing-after", "missing-delivery-check", "rollback-recorded", "issuer-mismatch", "team-mismatch", "canary-not-active", "artifact-unreviewed", "identity-storage", "identity-instance", "identity-alias", "identity-environment", "identity-owned", "identity-channel", "identity-region", "identity-moving", "identity-record", "after-not-preserved", "drift-changed:X_KEY", "drift-gone:X_KEY", "config-added:X_KEY", "team-mode-X_TEAM_ROSTER", "agent-key-X_AGENT_TEAM", "agent-key-X_AGENT_ISSUER", "delivery-check-superseded", "not-delivered", "version-header-mismatch", "version-unacceptable", "artifact-mismatch", } // Each code the gate can refuse with parks the activation under that // code, and nothing is activated. The canary is the app that goes first; // one code is repeated on a later app. func TestAdverseActivationParksOnEveryGateCode(t *testing.T) { for _, app := range []string{"calendar", "docs"} { for _, code := range gateCodes { if app == "docs" && code != "canary-not-active" { continue } t.Run(app+"/"+code, func(t *testing.T) { a := newAdoption(t) obs := newObserver() obs.set(&testRefusal{codes: []string{code}}) e := a.adopter(nil, obs) a.planA(e) run := a.activate(e, app) r := a.drive(e, run, 3) if r.State != "parked" || r.Code != "observe-"+code { t.Fatalf("run %s %q, want parked: observe-%s", r.State, r.Code, code) } if s := a.step(app, StepActivate); s.State != "parked" || s.Code != "observe-"+code { t.Fatalf("step %s %q", s.State, s.Code) } if obs.count() == 0 { t.Fatal("the observer was never asked") } a.neverActivated() }) } } } // Several refusals at once are all kept, and none is dropped for the // first. func TestAdverseActivationKeepsEveryRefusalCode(t *testing.T) { a := newAdoption(t) obs := newObserver() obs.set(&testRefusal{codes: []string{"after-not-preserved", "delivery-check-superseded", "identity-storage"}}) e := a.adopter(nil, obs) a.planA(e) run := a.activate(e, "calendar") if r := a.drive(e, run, 3); r.State != "parked" || r.Code != "observe-after-not-preserved,delivery-check-superseded,identity-storage" { t.Fatalf("run %s %q", r.State, r.Code) } a.neverActivated() } // A refusal that carries no code is still a refusal. func TestAdverseActivationRefusalWithoutCodesStillParks(t *testing.T) { a := newAdoption(t) obs := newObserver() obs.set(&testRefusal{}) e := a.adopter(nil, obs) a.planA(e) run := a.activate(e, "calendar") if r := a.drive(e, run, 3); r.State != "parked" || r.Code != "observe-" { t.Fatalf("run %s %q, want parked: observe-", r.State, r.Code) } a.neverActivated() } // An observer that cannot read the app never lets the activation through. func TestAdverseActivationObserverUnavailable(t *testing.T) { for name, err := range map[string]error{ "plain error": errors.New("carlos unreachable"), "wrapped": errors.Join(errors.New("read"), errors.New("timeout")), } { t.Run(name, func(t *testing.T) { a := newAdoption(t) obs := newObserver() obs.set(err) e := a.adopter(nil, obs) a.planA(e) run := a.activate(e, "calendar") if r := a.drive(e, run, 3); r.State != "parked" || r.Code != "observer-unavailable" { t.Fatalf("run %s %q", r.State, r.Code) } a.neverActivated() }) } } // An observation Home would refuse is never signed, whatever it is // shaped like. func TestAdverseActivationRefusesMalformedObservation(t *testing.T) { good := goodObservation cases := map[string]Observation{ "empty": {}, "version empty": {ArtifactDigest: good.ArtifactDigest, EnvironmentBinding: good.EnvironmentBinding}, "version with a space": {AppVersion: "v1 4", ArtifactDigest: good.ArtifactDigest, EnvironmentBinding: good.EnvironmentBinding}, "version leads dash": {AppVersion: "-v1", ArtifactDigest: good.ArtifactDigest, EnvironmentBinding: good.EnvironmentBinding}, "version too long": {AppVersion: "v" + strings.Repeat("1", 128), ArtifactDigest: good.ArtifactDigest, EnvironmentBinding: good.EnvironmentBinding}, "artifact not base64": {AppVersion: good.AppVersion, ArtifactDigest: "not base64!", EnvironmentBinding: good.EnvironmentBinding}, "artifact empty": {AppVersion: good.AppVersion, EnvironmentBinding: good.EnvironmentBinding}, "binding not base64": {AppVersion: good.AppVersion, ArtifactDigest: good.ArtifactDigest, EnvironmentBinding: "not base64!"}, "binding empty": {AppVersion: good.AppVersion, ArtifactDigest: good.ArtifactDigest}, } for name, bad := range cases { t.Run(name, func(t *testing.T) { a := newAdoption(t) e := a.adopter(nil, &fakeObserver{obs: bad}) a.planA(e) run := a.activate(e, "calendar") if r := a.drive(e, run, 3); r.State != "failed" || r.Code != "observation-shape" { t.Fatalf("run %s %q", r.State, r.Code) } a.neverActivated() }) } } // Without an observer, nothing activates, however the run came to be. func TestAdverseActivationNeedsAnObserver(t *testing.T) { a := newAdoption(t) e := a.adopter(nil, newObserver()) a.planA(e) run := a.activate(e, "calendar") bare := a.adopter(nil, nil) if r := a.drive(bare, run, 3); r.State != "failed" || r.Code != "no-observer" { t.Fatalf("run %s %q", r.State, r.Code) } a.neverActivated() _, err := bare.ActivateAdopted(a.ctx(), []string{"calendar"}) failCode(t, err, "no-observer") a.neverActivated() } // An attempt that lapsed while the gate was refusing is verified again // before any observation is asked for, and still nothing activates. func TestAdverseActivationLapsedAttemptNeverActivates(t *testing.T) { a := newAdoption(t) obs := newObserver() obs.set(&testRefusal{codes: []string{"missing-delivery-check"}}) e := a.adopter(nil, obs) a.planA(e) run := a.activate(e, "calendar") a.drive(e, run, 2) a.tick(600 * time.Second) if r := a.drive(e, run, 1); r.State != "parked" || r.Code != "attempt-expired" { t.Fatalf("run %s %q", r.State, r.Code) } a.neverActivated() } // A run that is out of order, for an app already disabled or active, or // for a team not yet seeded, is refused before any plan is made. func TestAdverseActivationOutOfOrderNeverPlans(t *testing.T) { t.Run("not registered", func(t *testing.T) { a := newAdoption(t, "sheets", "slides", "calendar", "calls", "memoria") e := a.adopter(nil, newObserver()) run, err := e.StartAdoption(a.ctx(), "Oficina", ownerCanary) if err != nil { t.Fatal(err) } a.confirm(homeop.KindCreateTeam) a.drive(e, run, 10) a.tick(homeop.WindowLifetime * time.Second) _, err = e.ActivateAdopted(a.ctx(), []string{"docs"}) failCode(t, err, "not-registered") a.neverActivated() }) t.Run("no team", func(t *testing.T) { a := newAdoption(t) _, err := a.adopter(nil, newObserver()).ActivateAdopted(a.ctx(), []string{"calendar"}) failCode(t, err, "no-team") a.neverActivated() }) t.Run("already active", func(t *testing.T) { a := newAdoption(t) e := a.adopter(nil, newObserver()) a.planA(e) run := a.activate(e, "calendar") if r := a.drive(e, run, 5); r.State != "done" { t.Fatalf("run %s %q", r.State, r.Code) } before := countOf(a.home.Requests(), homeop.DirectoryActivate+":calendar") a.tick(homeop.WindowLifetime * time.Second) _, err := e.ActivateAdopted(a.ctx(), []string{"calendar"}) failCode(t, err, "app-active") if countOf(a.home.Requests(), homeop.DirectoryActivate+":calendar") != before { t.Fatal("an active app was activated again") } }) }