| 1 | package adopt |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/base64" |
| 6 | "encoding/json" |
| 7 | "reflect" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/alta/carlosop" |
| 12 | ) |
| 13 | |
| 14 | // bindingInput is a complete binding input with every field set. |
| 15 | func bindingInput() BindingInput { |
| 16 | at := time.Date(2026, 11, 16, 10, 0, 0, 0, time.UTC) |
| 17 | id := Identity{Account: "bds", App: "docs", RecordHost: "docs.oficina.works", Slug: "s-docs", |
| 18 | Channel: "stable", Region: "eu", CreatedAt: at.Add(-1000 * time.Hour), RecordSeq: 4, ReplicaAssignment: `{"boxes":["b1"]}`} |
| 19 | d := func(ops OpsShape, min int) Delivery { |
| 20 | return Delivery{Version: "v1.4.0", Header: "v1.4.0", Health: 200, Ops: ops, At: at.Add(time.Duration(min) * time.Minute)} |
| 21 | } |
| 22 | before := []carlosop.EffectiveVar{ |
| 23 | {Key: "DOCS_HOME_ISSUER", Kind: "env", Digest: "vd1:" + hex64("a"), From: "default"}, |
| 24 | {Key: "DOCS_INSTANCE_KEY", Kind: "secret", Digest: "vd1:" + hex64("b"), From: "default"}, |
| 25 | } |
| 26 | after := append(append([]carlosop.EffectiveVar(nil), before...), |
| 27 | carlosop.EffectiveVar{Key: "DOCS_AGENT_ISSUER", Kind: "env", Digest: "vd1:" + hex64("c"), From: "default"}, |
| 28 | carlosop.EffectiveVar{Key: "DOCS_AGENT_TEAM", Kind: "env", Digest: "vd1:" + hex64("d"), From: "default"}) |
| 29 | return BindingInput{ |
| 30 | Before: id, After: id, |
| 31 | BeforeVars: before, AfterVars: after, |
| 32 | BeforeLayers: map[string]string{"default": "cv1:" + hex64("1"), "overlay": "none"}, |
| 33 | AfterLayers: map[string]string{"default": "cv1:" + hex64("2"), "overlay": "none"}, |
| 34 | TeamSQID: fixtureSQID, Issuer: Issuer, ArtifactSHA256: hex64("e"), Version: "v1.4.0", |
| 35 | BeforeDelivery: d(OpsAbsent, 0), AfterDelivery: d(OpsAgent401, 5), NowDelivery: d(OpsAgent401, 60), |
| 36 | Check: DeliveryCheck{App: "docs", |
| 37 | Identity: id, Delivery: d(OpsAgent401, 10), AfterVersion: "cv1:" + hex64("2"), At: at.Add(10 * time.Minute)}, |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func hex64(c string) string { |
| 42 | out := "" |
| 43 | for len(out) < 64 { |
| 44 | out += c |
| 45 | } |
| 46 | return out |
| 47 | } |
| 48 | |
| 49 | func TestBindingIncludesDeliveryCheck(t *testing.T) { |
| 50 | in := bindingInput() |
| 51 | b := EnvironmentBinding(in) |
| 52 | raw, err := base64.RawURLEncoding.DecodeString(b) |
| 53 | if err != nil || len(raw) != sha256.Size { |
| 54 | t.Fatalf("binding is not base64url of 32 bytes: %d bytes, %v", len(raw), err) |
| 55 | } |
| 56 | if EnvironmentBinding(in) != b { |
| 57 | t.Fatal("the binding is not deterministic") |
| 58 | } |
| 59 | |
| 60 | // The first part names the scheme: the binding is the documented |
| 61 | // array, not some other encoding of the same values. |
| 62 | parts := []any{"oficina/existing-app-observation/v1", |
| 63 | in.Before, in.After, |
| 64 | [][4]string{{"DOCS_HOME_ISSUER", "env", "vd1:" + hex64("a"), "default"}, {"DOCS_INSTANCE_KEY", "secret", "vd1:" + hex64("b"), "default"}}, |
| 65 | [][4]string{{"DOCS_AGENT_ISSUER", "env", "vd1:" + hex64("c"), "default"}, {"DOCS_AGENT_TEAM", "env", "vd1:" + hex64("d"), "default"}, |
| 66 | {"DOCS_HOME_ISSUER", "env", "vd1:" + hex64("a"), "default"}, {"DOCS_INSTANCE_KEY", "secret", "vd1:" + hex64("b"), "default"}}, |
| 67 | in.BeforeLayers, in.AfterLayers, |
| 68 | in.TeamSQID, in.Issuer, in.ArtifactSHA256, in.Version, |
| 69 | in.BeforeDelivery, in.AfterDelivery, in.NowDelivery, |
| 70 | []any{in.Check.App, in.Check.Identity, in.Check.Delivery, in.Check.AfterVersion, in.Check.At}, |
| 71 | } |
| 72 | js, err := json.Marshal(parts) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | sum := sha256.Sum256(js) |
| 77 | if want := base64.RawURLEncoding.EncodeToString(sum[:]); b != want { |
| 78 | t.Fatalf("the binding is not base64url(sha256) of the documented array") |
| 79 | } |
| 80 | |
| 81 | // Every part moves it, the delivery check and the three deliveries |
| 82 | // above all. |
| 83 | edits := map[string]func(*BindingInput){ |
| 84 | "check app": func(in *BindingInput) { in.Check.App = "sheets" }, |
| 85 | "check identity": func(in *BindingInput) { in.Check.Identity.RecordSeq++ }, |
| 86 | "check delivery": func(in *BindingInput) { in.Check.Delivery.Header = "v1.4.1" }, |
| 87 | "check delivery ops": func(in *BindingInput) { in.Check.Delivery.Ops = OpsAgent200 }, |
| 88 | "check after version": func(in *BindingInput) { in.Check.AfterVersion = "cv1:" + hex64("3") }, |
| 89 | "check time": func(in *BindingInput) { in.Check.At = in.Check.At.Add(time.Second) }, |
| 90 | "before delivery": func(in *BindingInput) { in.BeforeDelivery.Ops = OpsApp401 }, |
| 91 | "after delivery": func(in *BindingInput) { in.AfterDelivery.Version = "v1.4.1" }, |
| 92 | "after delivery health": func(in *BindingInput) { in.AfterDelivery.Health = 503 }, |
| 93 | "now delivery": func(in *BindingInput) { in.NowDelivery.Redirected = true }, |
| 94 | "now delivery time": func(in *BindingInput) { in.NowDelivery.At = in.NowDelivery.At.Add(time.Hour) }, |
| 95 | "before identity": func(in *BindingInput) { in.Before.Region = "us" }, |
| 96 | "after identity": func(in *BindingInput) { in.After.Seed = `{"s":1}` }, |
| 97 | "before var gone": func(in *BindingInput) { in.BeforeVars = []carlosop.EffectiveVar{in.BeforeVars[0]} }, |
| 98 | "before var digest": func(in *BindingInput) { in.BeforeVars = withDigest(in.BeforeVars, 1, "vd1:"+hex64("9")) }, |
| 99 | "after var source": func(in *BindingInput) { in.AfterVars = withFrom(in.AfterVars, 0, "environment") }, |
| 100 | "before layers": func(in *BindingInput) { in.BeforeLayers = map[string]string{"default": "none"} }, |
| 101 | "after layers": func(in *BindingInput) { in.AfterLayers = map[string]string{"default": "none"} }, |
| 102 | "team": func(in *BindingInput) { in.TeamSQID = "other" }, |
| 103 | "issuer": func(in *BindingInput) { in.Issuer = "https://elsewhere.example" }, |
| 104 | "artifact": func(in *BindingInput) { in.ArtifactSHA256 = hex64("f") }, |
| 105 | "version": func(in *BindingInput) { in.Version = "v1.4.1" }, |
| 106 | } |
| 107 | for name, edit := range edits { |
| 108 | t.Run(name, func(t *testing.T) { |
| 109 | e := bindingInput() |
| 110 | edit(&e) |
| 111 | if EnvironmentBinding(e) == b { |
| 112 | t.Fatalf("changing the %s left the binding unchanged", name) |
| 113 | } |
| 114 | }) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func withDigest(vars []carlosop.EffectiveVar, i int, d string) []carlosop.EffectiveVar { |
| 119 | out := append([]carlosop.EffectiveVar(nil), vars...) |
| 120 | out[i].Digest = d |
| 121 | return out |
| 122 | } |
| 123 | |
| 124 | func withFrom(vars []carlosop.EffectiveVar, i int, from string) []carlosop.EffectiveVar { |
| 125 | out := append([]carlosop.EffectiveVar(nil), vars...) |
| 126 | out[i].From = from |
| 127 | return out |
| 128 | } |
| 129 | |
| 130 | func TestBindingIsOrderIndependent(t *testing.T) { |
| 131 | in := bindingInput() |
| 132 | want := EnvironmentBinding(in) |
| 133 | |
| 134 | shuffled := bindingInput() |
| 135 | reverse := func(v []carlosop.EffectiveVar) []carlosop.EffectiveVar { |
| 136 | out := make([]carlosop.EffectiveVar, len(v)) |
| 137 | for i := range v { |
| 138 | out[len(v)-1-i] = v[i] |
| 139 | } |
| 140 | return out |
| 141 | } |
| 142 | shuffled.BeforeVars = reverse(shuffled.BeforeVars) |
| 143 | shuffled.AfterVars = reverse(shuffled.AfterVars) |
| 144 | // Maps built in another order, and shadows (not part of a var's |
| 145 | // entry) differing, change nothing. |
| 146 | shuffled.BeforeLayers = map[string]string{"overlay": "none"} |
| 147 | shuffled.BeforeLayers["default"] = "cv1:" + hex64("1") |
| 148 | shuffled.AfterVars[0].Shadows = []string{"default"} |
| 149 | keep := append([]carlosop.EffectiveVar(nil), shuffled.AfterVars...) |
| 150 | if got := EnvironmentBinding(shuffled); got != want { |
| 151 | t.Fatal("the binding depends on the order of vars or layers") |
| 152 | } |
| 153 | if !reflect.DeepEqual(keep, shuffled.AfterVars) { |
| 154 | t.Fatal("EnvironmentBinding reordered its caller's vars") |
| 155 | } |
| 156 | } |
| 157 | |