carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package park
2
3import (
4 "context"
5 "errors"
6 "strings"
7 "testing"
8 "time"
9
10 "github.com/carlosframework/platform/internal/hostclaim"
11 "github.com/carlosframework/platform/internal/provision"
12)
13
14// Home-region parks, part 1 (Paul, 2026-10-11). The home placement's code is
15// "", which the engine has always read as unset and refused. Home gets its
16// own engine region, HomeRegion, outside the placement-code alphabet, mapped
17// once from the placement code (PlacementRegion); "" keeps its meaning.
18
19// TestHomeRegionIsReserved: HomeRegion is not a placement code, so no named
20// region can ever equal it, and PlacementRegion agrees with the placement
21// validator on what a code is.
22func TestHomeRegionIsReserved(t *testing.T) {
23 if HomeRegion == "" || provision.ValidRegion(HomeRegion) {
24 t.Fatalf("HomeRegion %q is a placement code", HomeRegion)
25 }
26 if got := PlacementRegion(""); got != HomeRegion {
27 t.Fatalf("the home's code maps to %q, want %q", got, HomeRegion)
28 }
29 for _, code := range []string{"ie", "uk", "home", "none", "0", "a-b", "eu-west-1", strings.Repeat("a", 16)} {
30 if got := PlacementRegion(code); got != code || got == HomeRegion {
31 t.Fatalf("named code %q maps to %q", code, got)
32 }
33 }
34 for _, code := range []string{HomeRegion, hostclaim.RootGeneration, "IE", "ie/", "/", "a b", "ie\x00", "..", "région", strings.Repeat("a", 17)} {
35 if got := PlacementRegion(code); got != "" {
36 t.Fatalf("invalid code %q maps to %q, want unset", code, got)
37 }
38 }
39 // The local alphabet is the validator's, never wider.
40 for _, code := range []string{"", "a", "-", "z9", "ie", "IE", "@home", "@", "a_b", "a.b", strings.Repeat("x", 16), strings.Repeat("x", 17)} {
41 if (PlacementRegion(code) != "") != provision.ValidRegion(code) {
42 t.Fatalf("PlacementRegion(%q) = %q disagrees with provision.ValidRegion = %v", code, PlacementRegion(code), provision.ValidRegion(code))
43 }
44 }
45}
46
47// choreIn rewrites the fixture's one chore (and the claim's touched region)
48// to region.
49func choreIn(t *testing.T, f *fixture, region string) {
50 t.Helper()
51 obs, err := hostclaim.Read(t.Context(), f.e.Claims, f.w.Host)
52 if err != nil {
53 t.Fatal(err)
54 }
55 f.ob.Region, f.ob.Owner = region, "region:"+region
56 if _, err := hostclaim.Update(t.Context(), f.e.Claims, obs, func(c *hostclaim.Claim) error {
57 c.Obligations = []hostclaim.Obligation{f.ob}
58 c.Touched.Regions = []string{region}
59 return nil
60 }); err != nil {
61 t.Fatal(err)
62 }
63}
64
65// engineIn points the fixture's engine at region, its view seen from seen.
66func engineIn(f *fixture, region, seen string) {
67 f.e.Region = region
68 f.e.View = func(_ context.Context, o hostclaim.Observed) (View, error) {
69 return View{ClaimETag: o.ETag, Region: seen, AuthoritativeRegion: "uk"}, nil
70 }
71}
72
73// TestHomeAndNamedEnginesNeverRunEachOthersChores: through Engine.Run, an
74// engine for home never acts on a named region's chore, a named region's
75// engine (one named "home" among them) never acts on home's, and a home
76// engine whose view is seen from another region acts on nothing. Each
77// refusal leaves the bucket and the claim as they were.
78func TestHomeAndNamedEnginesNeverRunEachOthersChores(t *testing.T) {
79 for _, tc := range []struct {
80 name string
81 chore, engine, view string
82 want error
83 }{
84 {"home engine, named chore", "ie", HomeRegion, HomeRegion, ErrUnsafe},
85 {"named engine, home chore", HomeRegion, "ie", "ie", ErrUnsafe},
86 {"engine named home, home chore", HomeRegion, "home", "home", ErrUnsafe},
87 {"home engine, home chore, view from ie", HomeRegion, HomeRegion, "ie", ErrChanged},
88 {"unset engine, unset view, home chore", HomeRegion, "", "", ErrUnsafe},
89 } {
90 t.Run(tc.name, func(t *testing.T) {
91 f := newFixture(t)
92 f.seed()
93 choreIn(t, f, tc.chore)
94 engineIn(f, tc.engine, tc.view)
95 before := f.b.Snapshot()
96 if err := f.e.Run(t.Context(), f.w); !errors.Is(err, tc.want) {
97 t.Fatalf("run: %v, want %v", err, tc.want)
98 }
99 if f.b.Snapshot() != before {
100 t.Fatal("the bucket changed")
101 }
102 obs, err := hostclaim.Read(t.Context(), f.e.Claims, f.w.Host)
103 if err != nil || len(obs.Claim.Obligations) != 1 || obs.Claim.Obligations[0].Region != tc.chore || len(obs.Claim.QuietAfter) != 0 {
104 t.Fatalf("the claim changed: %+v %v", obs.Claim, err)
105 }
106 })
107 }
108}
109
110// TestHomeEngineParksUnderItsOwnRegion: a home engine runs a home chore to
111// its acknowledgement. The archive's keys carry no region and no empty
112// segment; the manifest, the quiet window and the coverage name HomeRegion,
113// never "" and never a named region.
114func TestHomeEngineParksUnderItsOwnRegion(t *testing.T) {
115 f := newFixture(t)
116 source := f.seed()
117 choreIn(t, f, HomeRegion)
118 engineIn(f, HomeRegion, HomeRegion)
119 if err := f.e.Run(t.Context(), f.w); err != nil {
120 t.Fatal(err)
121 }
122 obs, err := hostclaim.Read(t.Context(), f.e.Claims, f.w.Host)
123 if err != nil || len(obs.Claim.Obligations) != 0 {
124 t.Fatalf("still owed: %+v %v", obs.Claim, err)
125 }
126 if got, want := obs.Claim.QuietAfter[HomeRegion], f.ob.Park.EvidenceAt.Add(SignatureAge+time.Second); !got.Equal(want) || len(obs.Claim.QuietAfter) != 1 {
127 t.Fatalf("quiet_after %v, want only %s at %v", obs.Claim.QuietAfter, HomeRegion, want)
128 }
129 m, err := f.e.manifest(t.Context(), f.w, f.ob)
130 if err != nil || m == nil || m.Region != HomeRegion || len(m.Delete) != len(source) {
131 t.Fatalf("manifest %+v %v", m, err)
132 }
133 base := "parked/" + f.ob.Park.Incarnation + "/" + f.w.Host + "/" + f.ob.Retirement + "/"
134 for _, a := range m.Archive {
135 k := a.Destination.Key
136 if !strings.HasPrefix(k, base+"instances/") || strings.Contains(k, "//") || strings.Contains(k, HomeRegion) {
137 t.Fatalf("archive key %q is not under %sinstances/ or carries a region", k, base)
138 }
139 }
140 if vs, err := f.e.list(t.Context(), f.w.Host, f.ob.Park.Location); err != nil || len(vs) != 0 {
141 t.Fatalf("residue %v %v", vs, err)
142 }
143}
144
145// TestHomeLiveSetIsRegionQualified: the live set the engine protects is
146// qualified by HomeRegion. Admissions recorded in HomeRegion form the home's
147// unfinished chain; the same admissions recorded in a named region spelled
148// "home" do not, and a view seen from a named region protects nothing of
149// home's. A pending chore in a named region never stands for home's.
150func TestHomeLiveSetIsRegionQualified(t *testing.T) {
151 inRegion := func(t *testing.T, region string) (*fixture, hostclaim.Observed, View) {
152 f, obs, v, _ := sweepFixture(t)
153 obs, err := hostclaim.Update(t.Context(), f.e.Claims, obs, func(c *hostclaim.Claim) error {
154 for i := range c.Holder.Admissions {
155 c.Holder.Admissions[i].Region, c.Holder.Admissions[i].PredecessorRegion = region, region
156 }
157 return nil
158 })
159 if err != nil {
160 t.Fatal(err)
161 }
162 v.ClaimETag, v.Region, v.AuthoritativeRegion = obs.ETag, HomeRegion, HomeRegion
163 return f, obs, v
164 }
165 _, obs, v := inRegion(t, HomeRegion)
166 live, err := LiveGenerations(obs.Claim, v, "")
167 if err != nil || !live["g3"] || !live["g2"] || !live["g1"] {
168 t.Fatalf("home's own chain: %v %v", live, err)
169 }
170 _, obs, v = inRegion(t, "home")
171 if live, err := LiveGenerations(obs.Claim, v, ""); !errors.Is(err, ErrUnsafe) {
172 t.Fatalf("a chain recorded in the named region home stood for HomeRegion's: %v %v", live, err)
173 }
174 _, obs, v = inRegion(t, HomeRegion)
175 v.Region = "ie"
176 if live, err := LiveGenerations(obs.Claim, v, ""); err != nil || len(live) != 0 {
177 t.Fatalf("seen from ie, home's live set: %v %v", live, err)
178 }
179
180 // A pending chore blocks Decide only in its own region.
181 f, obs, v := inRegion(t, HomeRegion)
182 vs, err := f.e.Discover(t.Context(), f.w.Host)
183 if err != nil {
184 t.Fatal(err)
185 }
186 for _, region := range []string{"ie", "home", HomeRegion} {
187 ob := f.ob
188 ob.Region, ob.Owner = region, "region:"+region
189 c := *obs.Claim
190 c.Obligations = []hostclaim.Obligation{ob}
191 d, err := Decide(hostclaim.Observed{Host: obs.Host, Claim: &c, ETag: obs.ETag}, v, vs, f.ob.Park.Incarnation)
192 if err != nil {
193 t.Fatalf("%s: %v", region, err)
194 }
195 if blocked := len(d.Candidates) == 0; blocked != (region == HomeRegion) {
196 t.Fatalf("a pending chore in %s: candidates %+v", region, d.Candidates)
197 }
198 }
199}
200
201// TestHomeSweepQueuesUnderHomeRegion: a home engine's sweep queues its
202// chores as HomeRegion's, owned by region:HomeRegion, runs them and keys the
203// quiet window by HomeRegion.
204func TestHomeSweepQueuesUnderHomeRegion(t *testing.T) {
205 f := newFixture(t)
206 f.seed()
207 obs, _ := hostclaim.Read(t.Context(), f.e.Claims, f.w.Host)
208 if _, err := hostclaim.Update(t.Context(), f.e.Claims, obs, func(c *hostclaim.Claim) error {
209 c.Obligations = nil
210 return hostclaim.FreeAfterPark("op")(c)
211 }); err != nil {
212 t.Fatal(err)
213 }
214 engineIn(f, HomeRegion, HomeRegion)
215 var queued []hostclaim.Obligation
216 f.e.Authorize = func(_ context.Context, _ hostclaim.Observed, o hostclaim.Obligation) error {
217 queued = append(queued, o)
218 return nil
219 }
220 d, err := f.e.Sweep(t.Context(), f.w.Host, f.ob.Park.Incarnation, f.ob.Park.EvidenceAt, time.Second)
221 if err != nil || len(d.Candidates) != 1 {
222 t.Fatalf("%+v %v", d, err)
223 }
224 if len(queued) == 0 {
225 t.Fatal("nothing was queued")
226 }
227 for _, o := range queued {
228 if o.Region != HomeRegion || o.Owner != "region:"+HomeRegion {
229 t.Fatalf("queued %+v, want region %s", o, HomeRegion)
230 }
231 }
232 obs, err = hostclaim.Read(t.Context(), f.e.Claims, f.w.Host)
233 if err != nil || !obs.Claim.Free() || len(obs.Claim.Obligations) != 0 {
234 t.Fatalf("%+v %v", obs, err)
235 }
236 if _, ok := obs.Claim.QuietAfter[HomeRegion]; !ok || len(obs.Claim.QuietAfter) != 1 {
237 t.Fatalf("quiet_after %v", obs.Claim.QuietAfter)
238 }
239}
240
241// TestUnsetRegionStillRefuses: "" is still unset at every engine entry, and
242// an invalid placement code maps to it.
243func TestUnsetRegionStillRefuses(t *testing.T) {
244 f, obs, v, vs := sweepFixture(t)
245 v.Region = ""
246 if _, err := Decide(obs, v, vs, f.ob.Park.Incarnation); !errors.Is(err, ErrUnsafe) {
247 t.Fatalf("decide from an unset region: %v", err)
248 }
249 v.Region, v.AuthoritativeRegion = "ie", ""
250 if _, err := LiveGenerations(obs.Claim, v, ""); !errors.Is(err, ErrUnsafe) {
251 t.Fatalf("an unset authoritative region: %v", err)
252 }
253 g := newFixture(t)
254 g.seed()
255 for _, code := range []string{"IE", "@home", "a/b"} {
256 engineIn(g, PlacementRegion(code), PlacementRegion(code))
257 before := g.b.Snapshot()
258 if err := g.e.Run(t.Context(), g.w); !errors.Is(err, ErrUnsafe) || g.b.Snapshot() != before {
259 t.Fatalf("an engine for invalid code %q: %v", code, err)
260 }
261 }
262}
263