oficina / alta Public

Clone
git clone https://amadan.net/oficina/alta

Plain git — no account needed to clone.

Download

Download this file

1package adopt
2
3import (
4 "context"
5 "crypto/sha256"
6 "encoding/base64"
7 "net/http"
8 "strings"
9 "testing"
10)
11
12// redeployed serves release (body version, header header) from x's app,
13// as a redeploy Paul approved leaves it: the config's artifact for the app
14// is the new release's, and nothing else moved. It answers the artifact.
15func (x *observerFixture) redeployed(version, header string) string {
16 x.site.version, x.site.header = version, header
17 art := sha256Hex(header)
18 x.o.Artifacts[x.app.Slug] = art
19 return art
20}
21
22func (x *observerFixture) recheck(artifact string) error {
23 return CheckDelivery(context.Background(), x.s, x.c, x.h, x.app, artifact)
24}
25
26func (x *observerFixture) check(t *testing.T) DeliveryCheck {
27 t.Helper()
28 c, ok, err := x.s.DeliveryCheck(context.Background(), x.app.Slug)
29 if err != nil || !ok {
30 t.Fatalf("no delivery check (ok %v, err %v)", ok, err)
31 }
32 return c
33}
34
35// A redeploy to an approved release, after the write landed, is checked
36// again by configure's resume with nothing written or restarted, and the
37// check says why: the release changed from the baseline's to the
38// approved one. The gate then admits the app on the new release.
39func TestApprovedRedeployRecordsCheckAndActivates(t *testing.T) {
40 cases := map[string]struct{ app, version, header string }{
41 "same names": {"calendar", "v1.5.0", "v1.5.0"},
42 "describe name": {"calendar", "v1.4.0-3-gabcdef1", "abcdef1"},
43 "app 401 first": {"slides", "v1.5.0", "v1.5.0"},
44 }
45 for name, tc := range cases {
46 t.Run(name, func(t *testing.T) {
47 x := newObserverFixture(t, tc.app)
48 if tc.app != CanaryApp {
49 canaryActive(t, x.s)
50 }
51 host := routeOf(x.app)
52 puts, restarts := x.puts(), x.f.RestartCount(host)
53 before := x.check(t)
54 art := x.redeployed(tc.version, tc.header)
55
56 // Today's refusal, before the check is made again.
57 x.refused(t, "delivery-check-superseded", "not-delivered")
58
59 if err := x.recheck(art); err != nil {
60 t.Fatalf("CheckDelivery after an approved redeploy: %v", err)
61 }
62 got := x.check(t)
63 r := got.Redeploy
64 if r == nil {
65 t.Fatal("the check does not record the redeploy")
66 }
67 if r.Reason != RedeployApproved || r.Artifact != art ||
68 r.From != (Release{valueVersion, valueVersion}) || r.To != (Release{tc.version, tc.header}) {
69 t.Fatalf("redeploy recorded as %+v", *r)
70 }
71 if got.Delivery.Version != tc.version || got.Delivery.Header != tc.header {
72 t.Fatal("the check is not bound to the new served release")
73 }
74 if got.AfterVersion != before.AfterVersion || len(SameIdentity(got.Identity, before.Identity)) > 0 {
75 t.Fatal("the check moved off the after snapshot")
76 }
77 if x.puts() != puts || x.f.RestartCount(host) != restarts || x.f.AppWideRestarts() != 0 {
78 t.Fatal("the redeploy check wrote or restarted")
79 }
80
81 obs, err := x.observe()
82 if err != nil {
83 t.Fatalf("Observe after the redeploy check: %v", err)
84 }
85 sum := sha256.Sum256([]byte(tc.header))
86 if obs.AppVersion != tc.version || obs.ArtifactDigest != base64.RawURLEncoding.EncodeToString(sum[:]) {
87 t.Fatal("the observation is not of the new release")
88 }
89 })
90 }
91}
92
93// A check made with the release unchanged records no redeploy.
94func TestDeliveryCheckOnUnchangedReleaseRecordsNoRedeploy(t *testing.T) {
95 x := newObserverFixture(t, "calendar")
96 if x.check(t).Redeploy != nil {
97 t.Fatal("a check on the baseline's release records a redeploy")
98 }
99 setupOK(t, x.recheck(sha256Hex(valueVersion)))
100 if x.check(t).Redeploy != nil {
101 t.Fatal("a repeated check on the baseline's release records a redeploy")
102 }
103}
104
105// Every guard of the redeploy check refuses on its own, with today's
106// refusal first and the check left as it was.
107func TestApprovedRedeployRefusals(t *testing.T) {
108 const next = "v1.5.0"
109 cases := map[string]struct {
110 app string
111 edit func(t *testing.T, x *observerFixture) string // answers the artifact
112 want []string
113 }{
114 "unapproved release": {edit: func(t *testing.T, x *observerFixture) string {
115 x.redeployed(next, next)
116 return sha256Hex(valueVersion)
117 }, want: []string{"redeploy-artifact-mismatch"}},
118 "another approved release": {edit: func(t *testing.T, x *observerFixture) string {
119 x.redeployed(next, next)
120 return sha256Hex("v1.6.0")
121 }, want: []string{"redeploy-artifact-mismatch"}},
122 "config changed": {edit: func(t *testing.T, x *observerFixture) string {
123 x.f.PlatformWrite(x.app.Slug, "", x.app.Prefix+"_PLATFORM_NOTE", "n")
124 return x.redeployed(next, next)
125 }, want: []string{"redeploy-config-added:" + "CALENDAR_PLATFORM_NOTE"}},
126 "preserved key changed": {edit: func(t *testing.T, x *observerFixture) string {
127 x.f.SetVar(x.app.Slug, "", x.app.Prefix+"_ORIGIN", "https://elsewhere.example")
128 return x.redeployed(next, next)
129 }, want: []string{"redeploy-drift-changed:CALENDAR_ORIGIN", "redeploy-config-changed:CALENDAR_ORIGIN"}},
130 "identity changed": {edit: func(t *testing.T, x *observerFixture) string {
131 in := fixtureInstance(x.app)
132 in.RecordSeq++
133 x.f.AddInstance(in)
134 return x.redeployed(next, next)
135 }, want: []string{"redeploy-identity-record"}},
136 "storage changed": {edit: func(t *testing.T, x *observerFixture) string {
137 in := fixtureInstance(x.app)
138 in.Slug = "s-recreated"
139 x.f.AddInstance(in)
140 return x.redeployed(next, next)
141 }, want: []string{"redeploy-identity-storage"}},
142 "domain re-pointed": {edit: func(t *testing.T, x *observerFixture) string {
143 in := fixtureInstance(x.app)
144 in.Host = "calendar-2.bds.oncarlos.com"
145 x.f.AddInstance(in)
146 x.f.AttachDomain(x.app.Slug, CanonicalHost(x.app), in.Host, "live")
147 return x.redeployed(next, next)
148 }, want: []string{"redeploy-identity-alias", "redeploy-identity-instance"}},
149 "operations absent": {edit: func(t *testing.T, x *observerFixture) string {
150 x.site.ops = nil
151 return x.redeployed(next, next)
152 }, want: []string{"redeploy-operations-absent"}},
153 "operations lookalike": {edit: func(t *testing.T, x *observerFixture) string {
154 x.site.ops = respond(200, map[string]string{"Content-Type": "application/json"}, `{"operations":[]}`)
155 return x.redeployed(next, next)
156 }, want: []string{"redeploy-operations-other"}},
157 "operations app 401": {app: "slides", edit: func(t *testing.T, x *observerFixture) string {
158 x.site.ops = app401
159 return x.redeployed(next, next)
160 }, want: []string{"redeploy-operations-app-401"}},
161 "keys dropped": {edit: func(t *testing.T, x *observerFixture) string {
162 keys := AgentKeys(x.app)
163 x.f.Unset(x.app.Slug, "", keys[0])
164 x.f.Unset(x.app.Slug, "", keys[1])
165 return x.redeployed(next, next)
166 }, want: []string{"redeploy-agent-key-CALENDAR_AGENT_ISSUER", "redeploy-agent-key-CALENDAR_AGENT_TEAM"}},
167 "key rewritten": {edit: func(t *testing.T, x *observerFixture) string {
168 x.f.SetVar(x.app.Slug, "", AgentKeys(x.app)[1], "tm_other")
169 return x.redeployed(next, next)
170 }, want: []string{"redeploy-agent-key-CALENDAR_AGENT_TEAM"}},
171 "write not landed": {edit: func(t *testing.T, x *observerFixture) string {
172 x.rejournal(t, func(e *evidence) { e.write.State = "sent" })
173 return x.redeployed(next, next)
174 }, want: []string{"redeploy-write-not-landed"}},
175 "body and header disagree": {edit: func(t *testing.T, x *observerFixture) string {
176 art := x.redeployed(next, "v1.5.1")
177 return art
178 }, want: []string{"redeploy-version-header-mismatch"}},
179 "body unchanged": {edit: func(t *testing.T, x *observerFixture) string {
180 return x.redeployed(valueVersion, next)
181 }, want: []string{"redeploy-release-unchanged"}},
182 "redirect": {edit: func(t *testing.T, x *observerFixture) string {
183 x.site.ops = respond(http.StatusFound, map[string]string{"Location": "/elsewhere"}, "")
184 return x.redeployed(next, next)
185 }, want: []string{"redeploy-redirected"}},
186 "team mode": {edit: func(t *testing.T, x *observerFixture) string {
187 x.f.SetVar(x.app.Slug, "", x.app.Prefix+"_TEAM_MODE", "on")
188 return x.redeployed(next, next)
189 }, want: []string{"redeploy-team-mode-CALENDAR_TEAM_MODE"}},
190 "home issuer": {edit: func(t *testing.T, x *observerFixture) string {
191 x.f.SetVar(x.app.Slug, "", x.app.Prefix+"_HOME_ISSUER", "https://elsewhere.example")
192 return x.redeployed(next, next)
193 }, want: []string{"redeploy-home-issuer-mismatch"}},
194 "unhealthy": {edit: func(t *testing.T, x *observerFixture) string {
195 x.site.health = 503
196 return x.redeployed(next, next)
197 }, want: []string{"redeploy-healthz"}},
198 }
199 for name, tc := range cases {
200 t.Run(name, func(t *testing.T) {
201 app := tc.app
202 if app == "" {
203 app = "calendar"
204 }
205 x := newObserverFixture(t, app)
206 before := x.check(t)
207 art := tc.edit(t, x)
208 err := x.recheck(art)
209 if err == nil {
210 t.Fatal("the redeploy check passed")
211 }
212 noValues(t, err)
213 msg := err.Error()
214 if !strings.HasPrefix(msg, "adopt: check-not-delivered: adopt: the ") {
215 t.Fatalf("err = %v, want today's refusal first", err)
216 }
217 for _, code := range tc.want {
218 if !strings.Contains(msg, code) {
219 t.Fatalf("err = %v, want %s", err, code)
220 }
221 }
222 after := x.check(t)
223 if after.Redeploy != nil || after.Delivery != before.Delivery || !after.At.Equal(before.At) {
224 t.Fatal("a refused redeploy check replaced the check")
225 }
226 })
227 }
228}
229
230// A release that did not change is refused as it always was: no redeploy
231// guard is consulted and the refusal is today's, word for word.
232func TestRedeployPathOnlyWhenReleaseChanged(t *testing.T) {
233 cases := map[string]struct {
234 edit func(x *observerFixture)
235 want string
236 }{
237 "operations absent": {func(x *observerFixture) { x.site.ops = nil }, "adopt: check-not-delivered: adopt: the delegated-access door is not answering"},
238 "unhealthy": {func(x *observerFixture) { x.site.health = 503 }, "adopt: check-not-delivered: adopt: the app is not healthy"},
239 "header only": {func(x *observerFixture) { x.site.version = "v1.5.0" }, "adopt: check-not-delivered: adopt: the served version changed"},
240 }
241 for name, tc := range cases {
242 t.Run(name, func(t *testing.T) {
243 x := newObserverFixture(t, "calendar")
244 tc.edit(x)
245 if err := x.recheck(sha256Hex(x.site.header)); err == nil || err.Error() != tc.want {
246 t.Fatalf("err = %v, want %q", err, tc.want)
247 }
248 })
249 }
250}
251
252// The gate holds a redeploy check to the release it recorded and to the
253// baseline it was measured from; a plain check never covers a release
254// change.
255func TestGateHoldsRedeployCheck(t *testing.T) {
256 const next = "v1.5.0"
257 staged := func(t *testing.T) *observerFixture {
258 t.Helper()
259 x := newObserverFixture(t, "calendar")
260 setupOK(t, x.recheck(x.redeployed(next, next)))
261 if _, err := x.observe(); err != nil {
262 t.Fatalf("setup: Observe: %v", err)
263 }
264 return x
265 }
266 t.Run("redeployed again", func(t *testing.T) {
267 x := staged(t)
268 x.redeployed("v1.6.0", "v1.6.0")
269 x.refused(t, "delivery-check-superseded")
270 })
271 t.Run("back on the baseline's release", func(t *testing.T) {
272 x := staged(t)
273 x.redeployed(valueVersion, valueVersion)
274 x.refused(t, "delivery-check-superseded", "not-delivered")
275 })
276 t.Run("approval withdrawn", func(t *testing.T) {
277 x := staged(t)
278 x.o.Artifacts[x.app.Slug] = sha256Hex(valueVersion)
279 x.refused(t, "artifact-mismatch", "not-delivered")
280 })
281 t.Run("operations stopped answering", func(t *testing.T) {
282 x := staged(t)
283 x.site.ops = nil
284 wantCodes(t, x.refused(t), "not-delivered")
285 })
286 t.Run("keys dropped", func(t *testing.T) {
287 x := staged(t)
288 keys := AgentKeys(x.app)
289 x.f.Unset(x.app.Slug, "", keys[0])
290 x.f.Unset(x.app.Slug, "", keys[1])
291 x.refused(t, "agent-key-CALENDAR_AGENT_ISSUER", "agent-key-CALENDAR_AGENT_TEAM")
292 })
293 edits := map[string]func(c *DeliveryCheck){
294 "from another baseline": func(c *DeliveryCheck) { c.Redeploy.From.Version = "v1.3.0" },
295 "to another release": func(c *DeliveryCheck) { c.Redeploy.To.Header = "v1.5.1" },
296 "another artifact": func(c *DeliveryCheck) { c.Redeploy.Artifact = sha256Hex("v1.5.1") },
297 "another reason": func(c *DeliveryCheck) { c.Redeploy.Reason = "operator" },
298 "no redeploy recorded": func(c *DeliveryCheck) { c.Redeploy = nil },
299 }
300 for name, edit := range edits {
301 t.Run(name, func(t *testing.T) {
302 x := staged(t)
303 x.rejournal(t, func(e *evidence) { edit(e.check) })
304 wantCodes(t, x.refused(t), "not-delivered")
305 })
306 }
307}
308
309// The binding commits to the redeploy a check records, and is unchanged
310// for a check that records none.
311func TestBindingCommitsToRedeploy(t *testing.T) {
312 in := bindingInput()
313 plain := EnvironmentBinding(in)
314 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}
315 with := EnvironmentBinding(in)
316 if with == plain {
317 t.Fatal("the binding does not commit to the redeploy")
318 }
319 edits := map[string]func(r *Redeploy){
320 "from version": func(r *Redeploy) { r.From.Version = "v1.3.0" },
321 "from header": func(r *Redeploy) { r.From.Header = "v1.3.0" },
322 "to version": func(r *Redeploy) { r.To.Version = "v1.5.1" },
323 "to header": func(r *Redeploy) { r.To.Header = "v1.5.1" },
324 "artifact": func(r *Redeploy) { r.Artifact = hex64("d") },
325 "reason": func(r *Redeploy) { r.Reason = "operator" },
326 }
327 for name, edit := range edits {
328 e := in
329 r := *in.Check.Redeploy
330 edit(&r)
331 e.Check.Redeploy = &r
332 if EnvironmentBinding(e) == with {
333 t.Fatalf("the binding does not commit to the redeploy's %s", name)
334 }
335 }
336}
337