package release import ( "context" "crypto/ed25519" "os" "strings" "testing" "github.com/carlosframework/platform/internal/registry" ) // matFixture is the shape every test below needs: a dir store with a shipped // release, a signed channel pointer, a registry holding the route(s), a fresh // optDir with NOTHING staged, and a dataDir for the bake clock. Built inline // the way adopt_test.go builds its fixtures (testKeys/shipRelease/addRoute/ // testRegistry) — this package has no shared fixture builder and inventing a // parallel harness for three tests would be the drift, not the reuse. type matFixture struct { store Store priv ed25519.PrivateKey pub ed25519.PublicKey reg *registry.Registry optDir string dataDir string } func newMatFixture(t *testing.T) *matFixture { t.Helper() priv, pub := testKeys(t) return &matFixture{ store: NewDirStore(t.TempDir()), priv: priv, pub: pub, reg: testRegistry(t), optDir: t.TempDir(), dataDir: t.TempDir(), } } // TestMaterializeHostStagesChannelHead is the cold-wake case the activator // hits: an exec-backed route with nothing staged at all. MaterializeHost must // leave the live symlink pointing at the channel head — and must touch ONLY // the named host, since the wake it serves is that host's alone. func TestMaterializeHostStagesChannelHead(t *testing.T) { env := newMatFixture(t) ctx := context.Background() // canary/* has no bake window, so the head is immediately adoptable. shipRelease(t, env.store, "bab", "matapp", "abc1234", adoptPlatform, []byte("cold wake build")) if err := Promote(ctx, env.store, env.priv, "bab", "matapp", "canary/dev", "abc1234"); err != nil { t.Fatalf("Promote: %v", err) } addRoute(t, env.reg, "mat.test", "matapp", "canary/dev", false) // A sibling on the same channel, equally unstaged: waking one host must not // stage the fleet. addRoute(t, env.reg, "other.test", "matapp", "canary/dev", false) if err := MaterializeHost(ctx, env.reg, env.store, env.pub, env.optDir, env.dataDir, "mat.test"); err != nil { t.Fatalf("MaterializeHost: %v", err) } v, err := LiveVersion(env.optDir, "mat.test") if err != nil || v != "abc1234" { t.Fatalf("live = %q, %v; want abc1234", v, err) } if v, _ := LiveVersion(env.optDir, "other.test"); v != "" { t.Fatalf("sibling host was materialized too (%q) — a wake stages one host", v) } } // TestMaterializeHostRefusesHeldHead: a cold wake must NOT become a bake // bypass (spec §5). The app is declared production and the route follows beta // (24h hold) with a head promoted moments ago, so the pass correctly stages // nothing — and MaterializeHost must report that as an error, because the // caller's next move is exec'ing a binary that isn't there. func TestMaterializeHostRefusesHeldHead(t *testing.T) { env := newMatFixture(t) ctx := context.Background() shipRelease(t, env.store, "bab", "matapp", "abc1234", adoptPlatform, []byte("held build")) if err := Promote(ctx, env.store, env.priv, "bab", "matapp", "beta", "abc1234"); err != nil { t.Fatalf("Promote: %v", err) } if err := SetProduction(ctx, env.store, env.priv, "bab", "matapp", true, "test"); err != nil { t.Fatalf("SetProduction: %v", err) } addRoute(t, env.reg, "mat.test", "matapp", "beta", false) err := MaterializeHost(ctx, env.reg, env.store, env.pub, env.optDir, env.dataDir, "mat.test") if err == nil { t.Fatal("materialize of a bake-held head must refuse") } if !strings.Contains(err.Error(), "mat.test") { t.Errorf("error %q does not name the host", err) } if v, _ := LiveVersion(env.optDir, "mat.test"); v != "" { t.Fatalf("held head was staged anyway: %q", v) } } // TestMaterializeHostUnknownHost: a host the registry has never heard of // matches no route, so the pass is a no-op and the symlink stays absent. That // must surface as an error rather than a silent success the activator would // read as "go ahead and exec". func TestMaterializeHostUnknownHost(t *testing.T) { env := newMatFixture(t) if err := MaterializeHost(context.Background(), env.reg, env.store, env.pub, env.optDir, env.dataDir, "nope.test"); err == nil { t.Fatal("unknown host must error") } } // TestMaterializeHostDanglingLiveLink is the state this hook is ACTUALLY // called in as often as "no link at all": ExecBackend.ensureLiveLink invokes // Materialize when os.Stat of the link fails, and os.Stat FOLLOWS symlinks — // so "link present, staged version deleted" (an operator's /opt cleanup, a // partially restored volume) arrives here indistinguishably from "link // absent". // // The pass cannot repair it while the channel head is unchanged: adoptHost // reads the version off the dangling link with os.Readlink, sees cur == ver, // and returns having staged nothing. So the only honest answer is an error // naming the target — a nil here would be answered by the activator exec'ing // a path that does not resolve, which is the confusing failure this whole // function exists to remove. func TestMaterializeHostDanglingLiveLink(t *testing.T) { env := newMatFixture(t) ctx := context.Background() shipRelease(t, env.store, "bab", "matapp", "abc1234", adoptPlatform, []byte("cold wake build")) if err := Promote(ctx, env.store, env.priv, "bab", "matapp", "canary/dev", "abc1234"); err != nil { t.Fatalf("Promote: %v", err) } addRoute(t, env.reg, "mat.test", "matapp", "canary/dev", false) // The box staged and pointed the head, then lost the version store under // it — the link survives and still reads "abc1234". if err := StageArtifact(env.optDir, "bab", "abc1234", "matapp", []byte("cold wake build")); err != nil { t.Fatalf("StageArtifact: %v", err) } if err := PointLive(env.optDir, "mat.test", "bab", "abc1234", "matapp"); err != nil { t.Fatalf("PointLive: %v", err) } if err := os.RemoveAll(VersionPath(env.optDir, "bab", "abc1234", "matapp")); err != nil { t.Fatalf("remove staged version: %v", err) } if v, _ := LiveVersion(env.optDir, "mat.test"); v != "abc1234" { t.Fatalf("fixture is wrong: LiveVersion = %q, want the dangling link to still read abc1234", v) } err := MaterializeHost(ctx, env.reg, env.store, env.pub, env.optDir, env.dataDir, "mat.test") if err == nil { // If a future change makes the pass restage here, that is also a // correct outcome — but only if the binary is genuinely back. if _, statErr := os.Stat(LiveLink(env.optDir, "mat.test")); statErr != nil { t.Fatalf("materialize reported success with a dangling live link (%v) — the activator would exec a path that does not resolve", statErr) } return } if !strings.Contains(err.Error(), "abc1234") { t.Errorf("error %q does not name the missing target", err) } } // TestMaterializeHostNothingPromoted: the route exists and the app has // shipped, but its channel has no pointer — nothing to stage, and the same // error contract as a held head (the link is what is judged, not the pass). func TestMaterializeHostNothingPromoted(t *testing.T) { env := newMatFixture(t) ctx := context.Background() shipRelease(t, env.store, "bab", "matapp", "abc1234", adoptPlatform, []byte("never promoted")) addRoute(t, env.reg, "mat.test", "matapp", "canary/dev", false) if err := MaterializeHost(ctx, env.reg, env.store, env.pub, env.optDir, env.dataDir, "mat.test"); err == nil { t.Fatal("a channel with nothing promoted must error, not report success") } if v, _ := LiveVersion(env.optDir, "mat.test"); v != "" { t.Fatalf("live = %q, want absent", v) } } // shipStatic ships a STATIC release: one "site" artifact and no build for any // box's platform, which is the whole shape of the bug below. func shipStatic(t *testing.T, s Store, acct, app, version string, body []byte) { t.Helper() m := Manifest{ App: app, Version: version, Kind: "static", Artifacts: map[string]Artifact{"site": {}}, } if err := Ship(context.Background(), s, acct, m, map[string][]byte{"site": body}); err != nil { t.Fatalf("Ship static %s/%s: %v", app, version, err) } } // TestMaterializeHostNamesAStaticHeadUnderAnInstance is skills#29, as it // happened: a static site shipped and promoted, and an instance record // created for it (the CLI's --kind default). The wake can never succeed — // there is no binary in a static release to exec — but before this the member // was told "nothing is promoted for this instance's channel" while `carlos // channels` showed the promote plainly there. Two true-sounding statements // that contradict each other, and no route from either to the real problem. // // So the assertion is on the DIAGNOSIS, not merely on the failure: the error // must name the static head, and must not repeat the "nothing promoted" claim // that sent a real session hunting a bake window that did not exist. func TestMaterializeHostNamesAStaticHeadUnderAnInstance(t *testing.T) { env := newMatFixture(t) ctx := context.Background() shipStatic(t, env.store, "bab", "matapp", "0.1.0", []byte("