package adopt import ( "context" "crypto/sha256" "encoding/base64" "net/http" "strings" "testing" ) // redeployed serves release (body version, header header) from x's app, // as a redeploy Paul approved leaves it: the config's artifact for the app // is the new release's, and nothing else moved. It answers the artifact. func (x *observerFixture) redeployed(version, header string) string { x.site.version, x.site.header = version, header art := sha256Hex(header) x.o.Artifacts[x.app.Slug] = art return art } func (x *observerFixture) recheck(artifact string) error { return CheckDelivery(context.Background(), x.s, x.c, x.h, x.app, artifact) } func (x *observerFixture) check(t *testing.T) DeliveryCheck { t.Helper() c, ok, err := x.s.DeliveryCheck(context.Background(), x.app.Slug) if err != nil || !ok { t.Fatalf("no delivery check (ok %v, err %v)", ok, err) } return c } // A redeploy to an approved release, after the write landed, is checked // again by configure's resume with nothing written or restarted, and the // check says why: the release changed from the baseline's to the // approved one. The gate then admits the app on the new release. func TestApprovedRedeployRecordsCheckAndActivates(t *testing.T) { cases := map[string]struct{ app, version, header string }{ "same names": {"calendar", "v1.5.0", "v1.5.0"}, "describe name": {"calendar", "v1.4.0-3-gabcdef1", "abcdef1"}, "app 401 first": {"slides", "v1.5.0", "v1.5.0"}, } for name, tc := range cases { t.Run(name, func(t *testing.T) { x := newObserverFixture(t, tc.app) if tc.app != CanaryApp { canaryActive(t, x.s) } host := routeOf(x.app) puts, restarts := x.puts(), x.f.RestartCount(host) before := x.check(t) art := x.redeployed(tc.version, tc.header) // Today's refusal, before the check is made again. x.refused(t, "delivery-check-superseded", "not-delivered") if err := x.recheck(art); err != nil { t.Fatalf("CheckDelivery after an approved redeploy: %v", err) } got := x.check(t) r := got.Redeploy if r == nil { t.Fatal("the check does not record the redeploy") } if r.Reason != RedeployApproved || r.Artifact != art || r.From != (Release{valueVersion, valueVersion}) || r.To != (Release{tc.version, tc.header}) { t.Fatalf("redeploy recorded as %+v", *r) } if got.Delivery.Version != tc.version || got.Delivery.Header != tc.header { t.Fatal("the check is not bound to the new served release") } if got.AfterVersion != before.AfterVersion || len(SameIdentity(got.Identity, before.Identity)) > 0 { t.Fatal("the check moved off the after snapshot") } if x.puts() != puts || x.f.RestartCount(host) != restarts || x.f.AppWideRestarts() != 0 { t.Fatal("the redeploy check wrote or restarted") } obs, err := x.observe() if err != nil { t.Fatalf("Observe after the redeploy check: %v", err) } sum := sha256.Sum256([]byte(tc.header)) if obs.AppVersion != tc.version || obs.ArtifactDigest != base64.RawURLEncoding.EncodeToString(sum[:]) { t.Fatal("the observation is not of the new release") } }) } } // A check made with the release unchanged records no redeploy. func TestDeliveryCheckOnUnchangedReleaseRecordsNoRedeploy(t *testing.T) { x := newObserverFixture(t, "calendar") if x.check(t).Redeploy != nil { t.Fatal("a check on the baseline's release records a redeploy") } setupOK(t, x.recheck(sha256Hex(valueVersion))) if x.check(t).Redeploy != nil { t.Fatal("a repeated check on the baseline's release records a redeploy") } } // Every guard of the redeploy check refuses on its own, with today's // refusal first and the check left as it was. func TestApprovedRedeployRefusals(t *testing.T) { const next = "v1.5.0" cases := map[string]struct { app string edit func(t *testing.T, x *observerFixture) string // answers the artifact want []string }{ "unapproved release": {edit: func(t *testing.T, x *observerFixture) string { x.redeployed(next, next) return sha256Hex(valueVersion) }, want: []string{"redeploy-artifact-mismatch"}}, "another approved release": {edit: func(t *testing.T, x *observerFixture) string { x.redeployed(next, next) return sha256Hex("v1.6.0") }, want: []string{"redeploy-artifact-mismatch"}}, "config changed": {edit: func(t *testing.T, x *observerFixture) string { x.f.PlatformWrite(x.app.Slug, "", x.app.Prefix+"_PLATFORM_NOTE", "n") return x.redeployed(next, next) }, want: []string{"redeploy-config-added:" + "CALENDAR_PLATFORM_NOTE"}}, "preserved key changed": {edit: func(t *testing.T, x *observerFixture) string { x.f.SetVar(x.app.Slug, "", x.app.Prefix+"_ORIGIN", "https://elsewhere.example") return x.redeployed(next, next) }, want: []string{"redeploy-drift-changed:CALENDAR_ORIGIN", "redeploy-config-changed:CALENDAR_ORIGIN"}}, "identity changed": {edit: func(t *testing.T, x *observerFixture) string { in := fixtureInstance(x.app) in.RecordSeq++ x.f.AddInstance(in) return x.redeployed(next, next) }, want: []string{"redeploy-identity-record"}}, "storage changed": {edit: func(t *testing.T, x *observerFixture) string { in := fixtureInstance(x.app) in.Slug = "s-recreated" x.f.AddInstance(in) return x.redeployed(next, next) }, want: []string{"redeploy-identity-storage"}}, "domain re-pointed": {edit: func(t *testing.T, x *observerFixture) string { in := fixtureInstance(x.app) in.Host = "calendar-2.bds.oncarlos.com" x.f.AddInstance(in) x.f.AttachDomain(x.app.Slug, CanonicalHost(x.app), in.Host, "live") return x.redeployed(next, next) }, want: []string{"redeploy-identity-alias", "redeploy-identity-instance"}}, "operations absent": {edit: func(t *testing.T, x *observerFixture) string { x.site.ops = nil return x.redeployed(next, next) }, want: []string{"redeploy-operations-absent"}}, "operations lookalike": {edit: func(t *testing.T, x *observerFixture) string { x.site.ops = respond(200, map[string]string{"Content-Type": "application/json"}, `{"operations":[]}`) return x.redeployed(next, next) }, want: []string{"redeploy-operations-other"}}, "operations app 401": {app: "slides", edit: func(t *testing.T, x *observerFixture) string { x.site.ops = app401 return x.redeployed(next, next) }, want: []string{"redeploy-operations-app-401"}}, "keys dropped": {edit: func(t *testing.T, x *observerFixture) string { keys := AgentKeys(x.app) x.f.Unset(x.app.Slug, "", keys[0]) x.f.Unset(x.app.Slug, "", keys[1]) return x.redeployed(next, next) }, want: []string{"redeploy-agent-key-CALENDAR_AGENT_ISSUER", "redeploy-agent-key-CALENDAR_AGENT_TEAM"}}, "key rewritten": {edit: func(t *testing.T, x *observerFixture) string { x.f.SetVar(x.app.Slug, "", AgentKeys(x.app)[1], "tm_other") return x.redeployed(next, next) }, want: []string{"redeploy-agent-key-CALENDAR_AGENT_TEAM"}}, "write not landed": {edit: func(t *testing.T, x *observerFixture) string { x.rejournal(t, func(e *evidence) { e.write.State = "sent" }) return x.redeployed(next, next) }, want: []string{"redeploy-write-not-landed"}}, "body and header disagree": {edit: func(t *testing.T, x *observerFixture) string { art := x.redeployed(next, "v1.5.1") return art }, want: []string{"redeploy-version-header-mismatch"}}, "body unchanged": {edit: func(t *testing.T, x *observerFixture) string { return x.redeployed(valueVersion, next) }, want: []string{"redeploy-release-unchanged"}}, "redirect": {edit: func(t *testing.T, x *observerFixture) string { x.site.ops = respond(http.StatusFound, map[string]string{"Location": "/elsewhere"}, "") return x.redeployed(next, next) }, want: []string{"redeploy-redirected"}}, "team mode": {edit: func(t *testing.T, x *observerFixture) string { x.f.SetVar(x.app.Slug, "", x.app.Prefix+"_TEAM_MODE", "on") return x.redeployed(next, next) }, want: []string{"redeploy-team-mode-CALENDAR_TEAM_MODE"}}, "home issuer": {edit: func(t *testing.T, x *observerFixture) string { x.f.SetVar(x.app.Slug, "", x.app.Prefix+"_HOME_ISSUER", "https://elsewhere.example") return x.redeployed(next, next) }, want: []string{"redeploy-home-issuer-mismatch"}}, "unhealthy": {edit: func(t *testing.T, x *observerFixture) string { x.site.health = 503 return x.redeployed(next, next) }, want: []string{"redeploy-healthz"}}, } for name, tc := range cases { t.Run(name, func(t *testing.T) { app := tc.app if app == "" { app = "calendar" } x := newObserverFixture(t, app) before := x.check(t) art := tc.edit(t, x) err := x.recheck(art) if err == nil { t.Fatal("the redeploy check passed") } noValues(t, err) msg := err.Error() if !strings.HasPrefix(msg, "adopt: check-not-delivered: adopt: the ") { t.Fatalf("err = %v, want today's refusal first", err) } for _, code := range tc.want { if !strings.Contains(msg, code) { t.Fatalf("err = %v, want %s", err, code) } } after := x.check(t) if after.Redeploy != nil || after.Delivery != before.Delivery || !after.At.Equal(before.At) { t.Fatal("a refused redeploy check replaced the check") } }) } } // A release that did not change is refused as it always was: no redeploy // guard is consulted and the refusal is today's, word for word. func TestRedeployPathOnlyWhenReleaseChanged(t *testing.T) { cases := map[string]struct { edit func(x *observerFixture) want string }{ "operations absent": {func(x *observerFixture) { x.site.ops = nil }, "adopt: check-not-delivered: adopt: the delegated-access door is not answering"}, "unhealthy": {func(x *observerFixture) { x.site.health = 503 }, "adopt: check-not-delivered: adopt: the app is not healthy"}, "header only": {func(x *observerFixture) { x.site.version = "v1.5.0" }, "adopt: check-not-delivered: adopt: the served version changed"}, } for name, tc := range cases { t.Run(name, func(t *testing.T) { x := newObserverFixture(t, "calendar") tc.edit(x) if err := x.recheck(sha256Hex(x.site.header)); err == nil || err.Error() != tc.want { t.Fatalf("err = %v, want %q", err, tc.want) } }) } } // The gate holds a redeploy check to the release it recorded and to the // baseline it was measured from; a plain check never covers a release // change. func TestGateHoldsRedeployCheck(t *testing.T) { const next = "v1.5.0" staged := func(t *testing.T) *observerFixture { t.Helper() x := newObserverFixture(t, "calendar") setupOK(t, x.recheck(x.redeployed(next, next))) if _, err := x.observe(); err != nil { t.Fatalf("setup: Observe: %v", err) } return x } t.Run("redeployed again", func(t *testing.T) { x := staged(t) x.redeployed("v1.6.0", "v1.6.0") x.refused(t, "delivery-check-superseded") }) t.Run("back on the baseline's release", func(t *testing.T) { x := staged(t) x.redeployed(valueVersion, valueVersion) x.refused(t, "delivery-check-superseded", "not-delivered") }) t.Run("approval withdrawn", func(t *testing.T) { x := staged(t) x.o.Artifacts[x.app.Slug] = sha256Hex(valueVersion) x.refused(t, "artifact-mismatch", "not-delivered") }) t.Run("operations stopped answering", func(t *testing.T) { x := staged(t) x.site.ops = nil wantCodes(t, x.refused(t), "not-delivered") }) t.Run("keys dropped", func(t *testing.T) { x := staged(t) keys := AgentKeys(x.app) x.f.Unset(x.app.Slug, "", keys[0]) x.f.Unset(x.app.Slug, "", keys[1]) x.refused(t, "agent-key-CALENDAR_AGENT_ISSUER", "agent-key-CALENDAR_AGENT_TEAM") }) edits := map[string]func(c *DeliveryCheck){ "from another baseline": func(c *DeliveryCheck) { c.Redeploy.From.Version = "v1.3.0" }, "to another release": func(c *DeliveryCheck) { c.Redeploy.To.Header = "v1.5.1" }, "another artifact": func(c *DeliveryCheck) { c.Redeploy.Artifact = sha256Hex("v1.5.1") }, "another reason": func(c *DeliveryCheck) { c.Redeploy.Reason = "operator" }, "no redeploy recorded": func(c *DeliveryCheck) { c.Redeploy = nil }, } for name, edit := range edits { t.Run(name, func(t *testing.T) { x := staged(t) x.rejournal(t, func(e *evidence) { edit(e.check) }) wantCodes(t, x.refused(t), "not-delivered") }) } } // The binding commits to the redeploy a check records, and is unchanged // for a check that records none. func TestBindingCommitsToRedeploy(t *testing.T) { in := bindingInput() plain := EnvironmentBinding(in) in.Check.Redeploy = &Redeploy{From: Release{"v1.4.0", "v1.4.0"}, To: Release{"v1.5.0", "v1.5.0"}, Artifact: hex64("e"), Reason: RedeployApproved} with := EnvironmentBinding(in) if with == plain { t.Fatal("the binding does not commit to the redeploy") } edits := map[string]func(r *Redeploy){ "from version": func(r *Redeploy) { r.From.Version = "v1.3.0" }, "from header": func(r *Redeploy) { r.From.Header = "v1.3.0" }, "to version": func(r *Redeploy) { r.To.Version = "v1.5.1" }, "to header": func(r *Redeploy) { r.To.Header = "v1.5.1" }, "artifact": func(r *Redeploy) { r.Artifact = hex64("d") }, "reason": func(r *Redeploy) { r.Reason = "operator" }, } for name, edit := range edits { e := in r := *in.Check.Redeploy edit(&r) e.Check.Redeploy = &r if EnvironmentBinding(e) == with { t.Fatalf("the binding does not commit to the redeploy's %s", name) } } }