carlos / platform Public

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

Plain git — no account needed to clone.

Download

Download this file

1package release
2
3import (
4 "context"
5 "crypto/ed25519"
6 "os"
7 "strings"
8 "testing"
9
10 "github.com/carlosframework/platform/internal/registry"
11)
12
13// matFixture is the shape every test below needs: a dir store with a shipped
14// release, a signed channel pointer, a registry holding the route(s), a fresh
15// optDir with NOTHING staged, and a dataDir for the bake clock. Built inline
16// the way adopt_test.go builds its fixtures (testKeys/shipRelease/addRoute/
17// testRegistry) — this package has no shared fixture builder and inventing a
18// parallel harness for three tests would be the drift, not the reuse.
19type matFixture struct {
20 store Store
21 priv ed25519.PrivateKey
22 pub ed25519.PublicKey
23 reg *registry.Registry
24 optDir string
25 dataDir string
26}
27
28func newMatFixture(t *testing.T) *matFixture {
29 t.Helper()
30 priv, pub := testKeys(t)
31 return &matFixture{
32 store: NewDirStore(t.TempDir()),
33 priv: priv,
34 pub: pub,
35 reg: testRegistry(t),
36 optDir: t.TempDir(),
37 dataDir: t.TempDir(),
38 }
39}
40
41// TestMaterializeHostStagesChannelHead is the cold-wake case the activator
42// hits: an exec-backed route with nothing staged at all. MaterializeHost must
43// leave the live symlink pointing at the channel head — and must touch ONLY
44// the named host, since the wake it serves is that host's alone.
45func TestMaterializeHostStagesChannelHead(t *testing.T) {
46 env := newMatFixture(t)
47 ctx := context.Background()
48
49 // canary/* has no bake window, so the head is immediately adoptable.
50 shipRelease(t, env.store, "bab", "matapp", "abc1234", adoptPlatform, []byte("cold wake build"))
51 if err := Promote(ctx, env.store, env.priv, "bab", "matapp", "canary/dev", "abc1234"); err != nil {
52 t.Fatalf("Promote: %v", err)
53 }
54 addRoute(t, env.reg, "mat.test", "matapp", "canary/dev", false)
55 // A sibling on the same channel, equally unstaged: waking one host must not
56 // stage the fleet.
57 addRoute(t, env.reg, "other.test", "matapp", "canary/dev", false)
58
59 if err := MaterializeHost(ctx, env.reg, env.store, env.pub, env.optDir, env.dataDir, "mat.test"); err != nil {
60 t.Fatalf("MaterializeHost: %v", err)
61 }
62
63 v, err := LiveVersion(env.optDir, "mat.test")
64 if err != nil || v != "abc1234" {
65 t.Fatalf("live = %q, %v; want abc1234", v, err)
66 }
67 if v, _ := LiveVersion(env.optDir, "other.test"); v != "" {
68 t.Fatalf("sibling host was materialized too (%q) — a wake stages one host", v)
69 }
70}
71
72// TestMaterializeHostRefusesHeldHead: a cold wake must NOT become a bake
73// bypass (spec §5). The app is declared production and the route follows beta
74// (24h hold) with a head promoted moments ago, so the pass correctly stages
75// nothing — and MaterializeHost must report that as an error, because the
76// caller's next move is exec'ing a binary that isn't there.
77func TestMaterializeHostRefusesHeldHead(t *testing.T) {
78 env := newMatFixture(t)
79 ctx := context.Background()
80
81 shipRelease(t, env.store, "bab", "matapp", "abc1234", adoptPlatform, []byte("held build"))
82 if err := Promote(ctx, env.store, env.priv, "bab", "matapp", "beta", "abc1234"); err != nil {
83 t.Fatalf("Promote: %v", err)
84 }
85 if err := SetProduction(ctx, env.store, env.priv, "bab", "matapp", true, "test"); err != nil {
86 t.Fatalf("SetProduction: %v", err)
87 }
88 addRoute(t, env.reg, "mat.test", "matapp", "beta", false)
89
90 err := MaterializeHost(ctx, env.reg, env.store, env.pub, env.optDir, env.dataDir, "mat.test")
91 if err == nil {
92 t.Fatal("materialize of a bake-held head must refuse")
93 }
94 if !strings.Contains(err.Error(), "mat.test") {
95 t.Errorf("error %q does not name the host", err)
96 }
97 if v, _ := LiveVersion(env.optDir, "mat.test"); v != "" {
98 t.Fatalf("held head was staged anyway: %q", v)
99 }
100}
101
102// TestMaterializeHostUnknownHost: a host the registry has never heard of
103// matches no route, so the pass is a no-op and the symlink stays absent. That
104// must surface as an error rather than a silent success the activator would
105// read as "go ahead and exec".
106func TestMaterializeHostUnknownHost(t *testing.T) {
107 env := newMatFixture(t)
108 if err := MaterializeHost(context.Background(), env.reg, env.store, env.pub, env.optDir, env.dataDir, "nope.test"); err == nil {
109 t.Fatal("unknown host must error")
110 }
111}
112
113// TestMaterializeHostDanglingLiveLink is the state this hook is ACTUALLY
114// called in as often as "no link at all": ExecBackend.ensureLiveLink invokes
115// Materialize when os.Stat of the link fails, and os.Stat FOLLOWS symlinks —
116// so "link present, staged version deleted" (an operator's /opt cleanup, a
117// partially restored volume) arrives here indistinguishably from "link
118// absent".
119//
120// The pass cannot repair it while the channel head is unchanged: adoptHost
121// reads the version off the dangling link with os.Readlink, sees cur == ver,
122// and returns having staged nothing. So the only honest answer is an error
123// naming the target — a nil here would be answered by the activator exec'ing
124// a path that does not resolve, which is the confusing failure this whole
125// function exists to remove.
126func TestMaterializeHostDanglingLiveLink(t *testing.T) {
127 env := newMatFixture(t)
128 ctx := context.Background()
129
130 shipRelease(t, env.store, "bab", "matapp", "abc1234", adoptPlatform, []byte("cold wake build"))
131 if err := Promote(ctx, env.store, env.priv, "bab", "matapp", "canary/dev", "abc1234"); err != nil {
132 t.Fatalf("Promote: %v", err)
133 }
134 addRoute(t, env.reg, "mat.test", "matapp", "canary/dev", false)
135
136 // The box staged and pointed the head, then lost the version store under
137 // it — the link survives and still reads "abc1234".
138 if err := StageArtifact(env.optDir, "bab", "abc1234", "matapp", []byte("cold wake build")); err != nil {
139 t.Fatalf("StageArtifact: %v", err)
140 }
141 if err := PointLive(env.optDir, "mat.test", "bab", "abc1234", "matapp"); err != nil {
142 t.Fatalf("PointLive: %v", err)
143 }
144 if err := os.RemoveAll(VersionPath(env.optDir, "bab", "abc1234", "matapp")); err != nil {
145 t.Fatalf("remove staged version: %v", err)
146 }
147 if v, _ := LiveVersion(env.optDir, "mat.test"); v != "abc1234" {
148 t.Fatalf("fixture is wrong: LiveVersion = %q, want the dangling link to still read abc1234", v)
149 }
150
151 err := MaterializeHost(ctx, env.reg, env.store, env.pub, env.optDir, env.dataDir, "mat.test")
152 if err == nil {
153 // If a future change makes the pass restage here, that is also a
154 // correct outcome — but only if the binary is genuinely back.
155 if _, statErr := os.Stat(LiveLink(env.optDir, "mat.test")); statErr != nil {
156 t.Fatalf("materialize reported success with a dangling live link (%v) — the activator would exec a path that does not resolve", statErr)
157 }
158 return
159 }
160 if !strings.Contains(err.Error(), "abc1234") {
161 t.Errorf("error %q does not name the missing target", err)
162 }
163}
164
165// TestMaterializeHostNothingPromoted: the route exists and the app has
166// shipped, but its channel has no pointer — nothing to stage, and the same
167// error contract as a held head (the link is what is judged, not the pass).
168func TestMaterializeHostNothingPromoted(t *testing.T) {
169 env := newMatFixture(t)
170 ctx := context.Background()
171
172 shipRelease(t, env.store, "bab", "matapp", "abc1234", adoptPlatform, []byte("never promoted"))
173 addRoute(t, env.reg, "mat.test", "matapp", "canary/dev", false)
174
175 if err := MaterializeHost(ctx, env.reg, env.store, env.pub, env.optDir, env.dataDir, "mat.test"); err == nil {
176 t.Fatal("a channel with nothing promoted must error, not report success")
177 }
178 if v, _ := LiveVersion(env.optDir, "mat.test"); v != "" {
179 t.Fatalf("live = %q, want absent", v)
180 }
181}
182
183// shipStatic ships a STATIC release: one "site" artifact and no build for any
184// box's platform, which is the whole shape of the bug below.
185func shipStatic(t *testing.T, s Store, acct, app, version string, body []byte) {
186 t.Helper()
187 m := Manifest{
188 App: app, Version: version, Kind: "static",
189 Artifacts: map[string]Artifact{"site": {}},
190 }
191 if err := Ship(context.Background(), s, acct, m, map[string][]byte{"site": body}); err != nil {
192 t.Fatalf("Ship static %s/%s: %v", app, version, err)
193 }
194}
195
196// TestMaterializeHostNamesAStaticHeadUnderAnInstance is skills#29, as it
197// happened: a static site shipped and promoted, and an instance record
198// created for it (the CLI's --kind default). The wake can never succeed —
199// there is no binary in a static release to exec — but before this the member
200// was told "nothing is promoted for this instance's channel" while `carlos
201// channels` showed the promote plainly there. Two true-sounding statements
202// that contradict each other, and no route from either to the real problem.
203//
204// So the assertion is on the DIAGNOSIS, not merely on the failure: the error
205// must name the static head, and must not repeat the "nothing promoted" claim
206// that sent a real session hunting a bake window that did not exist.
207func TestMaterializeHostNamesAStaticHeadUnderAnInstance(t *testing.T) {
208 env := newMatFixture(t)
209 ctx := context.Background()
210
211 shipStatic(t, env.store, "bab", "matapp", "0.1.0", []byte("<h1>a site</h1>"))
212 if err := Promote(ctx, env.store, env.priv, "bab", "matapp", "canary/dev", "0.1.0"); err != nil {
213 t.Fatalf("Promote: %v", err)
214 }
215 addRoute(t, env.reg, "mat.test", "matapp", "canary/dev", false)
216
217 err := MaterializeHost(ctx, env.reg, env.store, env.pub, env.optDir, env.dataDir, "mat.test")
218 if err == nil {
219 t.Fatal("an instance route on a static head must fail the wake, not report success")
220 }
221 if !strings.Contains(err.Error(), StaticHeadUnderInstance) {
222 t.Errorf("error %q does not name the static head — the member is left with the\n"+
223 "contradiction between the wake and `carlos channels` that this diagnosis exists to end", err)
224 }
225 if strings.Contains(err.Error(), "nothing promoted for its channel") {
226 t.Errorf("error %q still claims nothing is promoted — 0.1.0 IS promoted on this channel", err)
227 }
228 // The way out has to be IN the message: a member reading this in
229 // `carlos logs` has no other source for it.
230 for _, want := range []string{"0.1.0", "carlos deploy", "--kind static"} {
231 if !strings.Contains(err.Error(), want) {
232 t.Errorf("error %q does not name %q", err, want)
233 }
234 }
235}
236
237// The generic reason must survive for every cause it is still right about: a
238// BINARY head that is simply not promoted stays "nothing promoted", and must
239// never be mislabelled as a static-shape problem.
240func TestMaterializeHostKeepsTheGenericReasonForABinaryApp(t *testing.T) {
241 env := newMatFixture(t)
242 ctx := context.Background()
243
244 shipRelease(t, env.store, "bab", "matapp", "abc1234", adoptPlatform, []byte("never promoted"))
245 addRoute(t, env.reg, "mat.test", "matapp", "canary/dev", false)
246
247 err := MaterializeHost(ctx, env.reg, env.store, env.pub, env.optDir, env.dataDir, "mat.test")
248 if err == nil {
249 t.Fatal("a channel with nothing promoted must error")
250 }
251 if !strings.Contains(err.Error(), noAdoptableHead) {
252 t.Errorf("error %q lost the generic reason", err)
253 }
254 if strings.Contains(err.Error(), StaticHeadUnderInstance) {
255 t.Errorf("error %q blames a static head on an app that has only ever shipped binaries", err)
256 }
257}
258