carlos / platform Public

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

Plain git — no account needed to clone.

Download

Download this file

1package hostops
2
3import (
4 "context"
5 "encoding/json"
6 "strings"
7 "testing"
8 "time"
9
10 "github.com/carlosframework/platform/internal/hostclaim"
11 "github.com/carlosframework/platform/internal/provision"
12 "github.com/carlosframework/platform/internal/release"
13)
14
15func TestLifecycleRecordProductionWrapper(t *testing.T) {
16 for _, mode := range []string{"moving", "restoring", "flip", "foreign-home", "matched-home", "changed-home", "missing-home", "wrong-op", "wrong-region", "absent", "stale-claim", "metadata", "wrong-kind", "admission-not-op", "unarchived", "restore-region", "restore-stale-source-region", "restore-elsewhere", "restore-no-journal", "restore-committed"} {
17 t.Run(mode, func(t *testing.T) {
18 ctx := context.Background()
19 raw := release.NewDirStore(t.TempDir())
20 now := time.Now().UTC()
21 host := "app.example"
22 inc, err := hostclaim.Incarnation("bab", "app", host, now)
23 if err != nil {
24 t.Fatal(err)
25 }
26 key := provision.RecordKey("bab", "app", host)
27 claim := hostclaim.Claim{V: 2, Host: host, Gen: 1, Holder: &hostclaim.Holder{Family: "instance", Account: "bab", App: "app", Key: key, Acquisition: "acq", CreatedAt: now, Incarnation: inc, Location: "instances/" + host}, Op: hostclaim.Op{ID: "move", Kind: hostclaim.KindMoving, Phase: "sealed", Intent: map[string]string{"app_id": "canonical", "admission_id": "move", "source": "ie-box", "target": "uk-box", "to": "uk"}}}
28 // BeginAdmittedMove's intent carries no source region: a rollback
29 // returns to the record's original Move.From.
30 regID := "move"
31 switch mode {
32 case "foreign-home", "matched-home", "changed-home":
33 claim.Op.Intent["home_region"] = "ie"
34 case "admission-not-op":
35 // A move registration under another id is drainable.
36 regID = "registration"
37 claim.Op.Intent["admission_id"] = regID
38 case "restore-stale-source-region":
39 // A pre-contract key neither steers nor blocks the rollback.
40 claim.Op.Intent["source_region"] = "fr"
41 }
42 state := "cutting"
43 if strings.HasPrefix(mode, "restore-") {
44 claim.Op.Kind = hostclaim.KindLanding
45 state = "landing"
46 if mode == "restore-committed" {
47 claim.Op.Phase = "committed"
48 }
49 }
50 if mode == "restoring" {
51 claim.Op.Kind = hostclaim.KindRestoring
52 claim.Op.Phase = ""
53 state = "rolling_back"
54 }
55 cb, _ := json.Marshal(claim)
56 ck, _ := hostclaim.Key(host)
57 if _, err = raw.Put(ctx, ck, cb, ""); err != nil {
58 t.Fatal(err)
59 }
60 o, err := hostclaim.Read(ctx, raw, host)
61 if err != nil {
62 t.Fatal(err)
63 }
64 rec := map[string]any{"schema": 1, "host": host, "slug": "app", "account": "bab", "app": "app", "channel": "edge", "region": "ie", "created_at": now, "acq": "acq", "move": map[string]any{"id": "move", "incarnation": inc, "from": "ie", "to": "uk", "state": state, "cut_phase": "archived"}}
65 if strings.HasPrefix(mode, "restore-") {
66 rec["region"] = "uk"
67 }
68 body, _ := json.Marshal(rec)
69 tag, err := raw.Put(ctx, key, body, "")
70 if err != nil {
71 t.Fatal(err)
72 }
73 admission := AppAdmission{V: 1, AppID: "canonical", Revision: 1, Epoch: 1, State: "closed", Registrations: map[string]Registration{regID: {Kind: "move", AppID: "canonical", Epoch: 1, Host: host, Incarnation: inc}}, Effects: map[string]Effect{}}
74 if mode == "wrong-kind" {
75 reg := admission.Registrations[regID]
76 reg.Kind = "create"
77 admission.Registrations[regID] = reg
78 }
79 ab, _ := json.Marshal(admission)
80 if _, err = raw.Put(ctx, "apps/bab/app/admission.json", ab, ""); err != nil {
81 t.Fatal(err)
82 }
83 mv := rec["move"].(map[string]any)
84 mv["updated_at"] = now
85 switch mode {
86 case "flip", "unarchived":
87 rec["region"] = "uk"
88 mv["state"] = "landing"
89 mv["cut_phase"] = "flipped"
90 mv["flipped_at"] = now
91 mv["landing_deadline"] = now.Add(time.Minute)
92 if mode == "unarchived" {
93 mv["cut_phase"] = "requested"
94 }
95 case "restore-region", "restore-stale-source-region", "restore-elsewhere", "restore-no-journal", "restore-committed":
96 rec["region"] = "ie"
97 if mode == "restore-elsewhere" {
98 // Not the original Move.From.
99 rec["region"] = "de"
100 }
101 mv["state"] = "rolling_back"
102 if mode != "restore-no-journal" {
103 mv["rollback"] = map[string]any{"id": "rollback", "requested_at": now, "target_phase": "releasing", "source_phase": "waiting", "release_deadline": now.Add(15 * time.Minute)}
104 }
105 case "wrong-op":
106 mv["id"] = "another"
107 case "wrong-region":
108 rec["region"] = "de"
109 case "absent":
110 if err = raw.Delete(ctx, key); err != nil {
111 t.Fatal(err)
112 }
113 case "stale-claim":
114 if _, err = hostclaim.Update(ctx, raw, o, func(*hostclaim.Claim) error { return nil }); err != nil {
115 t.Fatal(err)
116 }
117 case "metadata":
118 rec["channel"] = "other"
119 }
120 body, _ = json.Marshal(rec)
121 wrapped := conditionalRecordStore{raw: raw}
122 if mode == "matched-home" || mode == "missing-home" {
123 wrapped.homeRegion = "ie"
124 }
125 if mode == "changed-home" {
126 wrapped.homeRegion = "uk"
127 }
128 permitted := context.WithValue(ctx, lifecycleRecordKey{}, lifecycleRecordPermit{o})
129 _, err = wrapped.Put(permitted, key, body, tag)
130 // missing-home is an operation that started before any designation was
131 // published: it stays alias-free and is not stranded by a later one.
132 allowed := mode == "matched-home" || mode == "missing-home" || mode == "moving" || mode == "restoring" || mode == "flip" || mode == "restore-region" || mode == "restore-stale-source-region"
133 if (err == nil) != allowed {
134 t.Fatalf("allowed=%v err=%v", allowed, err)
135 }
136 if !allowed {
137 return
138 }
139 if _, err = wrapped.Put(ctx, key, body, tag); err == nil {
140 t.Fatal("ordinary writer crossed lifecycle gate")
141 }
142 })
143 }
144}
145