package release import ( "bytes" "context" "encoding/json" "errors" "fmt" "io" "os" "strings" "testing" "github.com/carlosframework/platform/internal/registry" ) // erroringStore is a Store whose every operation fails with a NON-not-found // error — the "bucket on fire" case a read must never swallow. type erroringStore struct{ err error } func (e *erroringStore) Get(context.Context, string) ([]byte, string, error) { return nil, "", e.err } func (e *erroringStore) Put(context.Context, string, []byte, string) (string, error) { return "", e.err } func (e *erroringStore) List(context.Context, string) ([]string, error) { return nil, e.err } func (e *erroringStore) ListDirs(context.Context, string) ([]string, error) { return nil, e.err } func (e *erroringStore) Delete(context.Context, string) error { return e.err } // legacyTripwire fails the test if anything asks the store for a key or // prefix under one app's LEGACY unqualified prefix. It is how "the runtime // does not read legacy keys" is pinned as a property rather than asserted one // call site at a time: a fallback that crept back into any read path would // trip it, whatever shape it took. type legacyTripwire struct { Store t *testing.T app string } func (l *legacyTripwire) tripped(what, key string) { l.t.Helper() if strings.HasPrefix(key, LegacyAppPrefix(l.app)) { l.t.Errorf("the runtime %s a legacy key: %s — the read-fallback is retired", what, key) } } func (l *legacyTripwire) Get(ctx context.Context, key string) ([]byte, string, error) { l.tripped("read", key) return l.Store.Get(ctx, key) } // Open is tripped too: staging streams the artifact (#269), and without this // a streamed read would reach the store under the tripwire unwatched. func (l *legacyTripwire) Open(ctx context.Context, key string) (io.ReadCloser, string, error) { l.tripped("read", key) return OpenObject(ctx, l.Store, key) } func (l *legacyTripwire) List(ctx context.Context, prefix string) ([]string, error) { l.tripped("listed", prefix) return l.Store.List(ctx, prefix) } func TestKeysAreAccountScoped(t *testing.T) { if got := ChannelKey("kqmv", "kass", "stable"); got != "apps/kqmv/kass/channels/stable.json" { t.Errorf("ChannelKey = %q", got) } if got := ManifestKey("bab", "console", "abc1234"); got != "apps/bab/console/releases/abc1234/manifest.json" { t.Errorf("ManifestKey = %q", got) } if got := ArtifactKey("kqmv", "kass", "v1", "kass"); got != "apps/kqmv/kass/releases/v1/kass" { t.Errorf("ArtifactKey = %q", got) } // The legacy shapes stay reachable BY NAME — no read path resolves // through them any more, but `accounts migrate -from legacy` still has to // be able to spell them, forever. if got := LegacyChannelKey("kass", "stable"); got != "apps/kass/channels/stable.json" { t.Errorf("LegacyChannelKey = %q", got) } if got := LegacyManifestKey("console", "abc1234"); got != "apps/console/releases/abc1234/manifest.json" { t.Errorf("LegacyManifestKey = %q", got) } if got := LegacyArtifactKey("kass", "v1", "kass"); got != "apps/kass/releases/v1/kass" { t.Errorf("LegacyArtifactKey = %q", got) } } // An empty account must never silently rebuild a legacy key: that would make // an unqualified write look like a successful qualified one, and the // migration would then never notice the app had not moved. func TestEmptyAccountDoesNotRebuildALegacyKey(t *testing.T) { if got := ChannelKey("", "kass", "stable"); got == LegacyChannelKey("kass", "stable") { t.Errorf("ChannelKey(\"\", ...) = %q — an empty account rebuilt the legacy key", got) } } // The retirement, at its smallest: a legacy object at the matching // unqualified key does not answer a qualified read. It used to. func TestGetQualifiedDoesNotFallBack(t *testing.T) { s := NewDirStore(t.TempDir()) ctx := context.Background() if _, err := s.Put(ctx, "apps/kass/channels/stable.json", []byte("legacy"), ""); err != nil { t.Fatal(err) } if _, _, err := GetQualified(ctx, s, "apps/kqmv/kass/channels/stable.json"); !errors.Is(err, os.ErrNotExist) { t.Fatalf("a legacy object answered a qualified read: err = %v", err) } if _, err := s.Put(ctx, "apps/kqmv/kass/channels/stable.json", []byte("qualified"), ""); err != nil { t.Fatal(err) } body, _, err := GetQualified(ctx, s, "apps/kqmv/kass/channels/stable.json") if err != nil || string(body) != "qualified" { t.Fatalf("qualified read: body=%q err=%v", body, err) } } // A miss is actionable: a deployment that upgraded from a pre-accounts binary // without migrating gets the exact command, not a mystery 404. It is still // os.ErrNotExist-wrapped, so every caller's "has this app ever shipped here" // check keeps working. func TestQualifiedMissNamesTheMigrationCommand(t *testing.T) { s := NewDirStore(t.TempDir()) _, _, err := GetQualified(context.Background(), s, ChannelKey("kqmv", "kass", "stable")) if !errors.Is(err, os.ErrNotExist) { t.Fatalf("miss = %v, want os.ErrNotExist-wrapped", err) } if !strings.Contains(err.Error(), "carlos accounts migrate --app kass --from legacy --to kqmv") { t.Errorf("miss does not name the fix:\n%s", err) } } // The hint is only for app keys. A miss on anything else (status snapshots, // account manifests) must not advertise a command that cannot apply to it. func TestNonAppKeyMissCarriesNoMigrationHint(t *testing.T) { s := NewDirStore(t.TempDir()) _, _, err := GetQualified(context.Background(), s, "status/fleet.json") if !errors.Is(err, os.ErrNotExist) { t.Fatalf("miss = %v, want os.ErrNotExist-wrapped", err) } if strings.Contains(err.Error(), "accounts migrate") { t.Errorf("status/fleet.json miss suggested a migration:\n%s", err) } } // A store error that is NOT not-found must surface untouched, and must never // acquire a migration hint: telling an operator to migrate because the bucket // is unreachable sends them at the wrong problem. func TestGetQualifiedPropagatesRealErrors(t *testing.T) { s := &erroringStore{err: errors.New("bucket on fire")} _, _, err := GetQualified(context.Background(), s, ChannelKey("kqmv", "kass", "stable")) if err == nil || !strings.Contains(err.Error(), "on fire") { t.Fatalf("want the real error, got %v", err) } if strings.Contains(err.Error(), "accounts migrate") { t.Errorf("a broken bucket was reported as an unmigrated app:\n%s", err) } } // THE test the retirement exists for: an app whose objects are ONLY at the // legacy keys is invisible to every runtime read path. Nothing resolves it, // nothing lists it, and — the tripwire — nothing so much as touches its // legacy prefix on the way to finding that out. // // Adopt SKIPS such an app rather than failing the pass: one app's missing // pointer must not stop every other app on the box from installing, which is // already how a channel with nothing promoted behaves. The loudness lives in // the error text (asserted here), which adopt prints per channel. func TestLegacyOnlyAppIsInvisibleToTheRuntime(t *testing.T) { dir := NewDirStore(t.TempDir()) ctx := context.Background() priv, pub := testKeys(t) s := &legacyTripwire{Store: dir, t: t, app: "old"} // A wholly legacy app: manifest, artifact and pointer all unqualified. m := Manifest{Schema: 1, App: "old", Version: "v1", Kind: "binary", Artifacts: map[string]Artifact{"linux-amd64": {File: "old", SHA256: etagOf([]byte("bin")), Size: 3}}} raw, err := json.MarshalIndent(m, "", " ") if err != nil { t.Fatal(err) } if _, err := dir.Put(ctx, LegacyArtifactKey("old", "v1", "old"), []byte("bin"), ""); err != nil { t.Fatal(err) } if _, err := dir.Put(ctx, LegacyManifestKey("old", "v1"), raw, ""); err != nil { t.Fatal(err) } ptr, err := signPointer(RootSigner(priv), &ChannelPointer{Account: "bab", App: "old", Channel: "stable", Version: "v1"}) if err != nil { t.Fatal(err) } if _, err := dir.Put(ctx, LegacyChannelKey("old", "stable"), ptr, ""); err != nil { t.Fatal(err) } if _, err := GetManifest(ctx, s, OpsAccount, "old", "v1"); !errors.Is(err, os.ErrNotExist) { t.Errorf("GetManifest resolved a legacy-only app: %v", err) } else if !strings.Contains(err.Error(), "--from legacy") { t.Errorf("GetManifest miss is not actionable:\n%s", err) } if _, err := Channel(ctx, s, pub, OpsAccount, "old", "stable"); !errors.Is(err, os.ErrNotExist) { t.Errorf("Channel resolved a legacy-only app: %v", err) } else if !strings.Contains(err.Error(), "carlos accounts migrate --app old --from legacy --to bab") { t.Errorf("Channel miss is not actionable:\n%s", err) } // Listing paths see nothing either, so the console and `carlos channels` // show an empty app rather than a row they cannot resolve. keys, err := s.List(ctx, ChannelsPrefix(OpsAccount, "old")) if err != nil || len(keys) != 0 { t.Errorf("channels listing = %v (err %v), want empty", keys, err) } rung, err := VersionRung(ctx, s, pub, OpsAccount, "old", "v1") if err != nil || rung != "" { t.Errorf("VersionRung = %q (err %v), want no rung", rung, err) } } // A tripwire only guards the paths a test routes through it, so the ARTIFACT // download — the read that puts bytes on a box — gets its own pass. Its // fixture is the half-migrated shape that makes the read observable: pointer // and manifest qualified (so the pass gets as far as staging), the artifact // itself only at the legacy key. A fallback reintroduced in ensureStaged // would find those bytes and stage them; qualified-only cannot, so the host // is left alone and the pass survives. // // Without this, a fallback in ensureStaged passes the whole suite. func TestAdoptNeverDownloadsALegacyArtifact(t *testing.T) { dir := NewDirStore(t.TempDir()) ctx := context.Background() priv, pub := testKeys(t) opt, reg := t.TempDir(), testRegistry(t) // Built in a scratch store, then placed key by key: the qualified pointer // and manifest go down as they are, the artifact only in its legacy shape. scratch := NewDirStore(t.TempDir()) shipRelease(t, scratch, OpsAccount, "half", "v1", adoptPlatform, []byte("build one")) if err := Promote(ctx, scratch, priv, OpsAccount, "half", "canary/x", "v1"); err != nil { t.Fatal(err) } art := "" keys, err := scratch.List(ctx, AppPrefix(OpsAccount, "half")) if err != nil { t.Fatal(err) } for _, k := range keys { body, _, err := scratch.Get(ctx, k) if err != nil { t.Fatal(err) } dst := k if strings.Contains(k, "/releases/") && !strings.HasSuffix(k, "manifest.json") { art = k dst = LegacyAppPrefix("half") + strings.TrimPrefix(k, AppPrefix(OpsAccount, "half")) } if _, err := dir.Put(ctx, dst, body, ""); err != nil { t.Fatal(err) } } if art == "" { t.Fatal("fixture: no artifact key found") } if _, _, err := dir.Get(ctx, art); !errors.Is(err, os.ErrNotExist) { t.Fatalf("fixture: the qualified artifact exists (%v) — the test would prove nothing", err) } s := &legacyTripwire{Store: dir, t: t, app: "half"} addRoute(t, reg, "half.example.com", "half", "canary/x", false) if err := Pass(ctx, reg, s, pub, opt, func(string) error { return nil }); err != nil { t.Fatalf("one app's missing artifact must not fail the pass: %v", err) } if _, err := os.Stat(VersionPath(opt, OpsAccount, "v1", "half")); err == nil { t.Error("adopt staged a build whose artifact only exists at the legacy key") } if v, _ := LiveVersion(opt, "half.example.com"); v != "" { t.Errorf("host repointed to %q on a build that was never staged", v) } } // The other half of the bargain: the migration verb's access to legacy keys // is EXPLICIT, and outlives the fallback. It is the upgrade path for any // deployment arriving from a pre-accounts binary, so it works forever. func TestGetMigrationSourceStillReadsLegacy(t *testing.T) { s := NewDirStore(t.TempDir()) ctx := context.Background() if _, err := s.Put(ctx, LegacyChannelKey("old", "stable"), []byte("legacy"), ""); err != nil { t.Fatal(err) } body, _, usedLegacy, err := GetMigrationSource(ctx, s, ChannelKey(OpsAccount, "old", "stable"), LegacyChannelKey("old", "stable")) if err != nil || string(body) != "legacy" || !usedLegacy { t.Fatalf("migration source: body=%q usedLegacy=%v err=%v", body, usedLegacy, err) } // Qualified still wins where both exist — that precedence is what stops // the verb copying a stale legacy pointer over a live qualified one. if _, err := s.Put(ctx, ChannelKey(OpsAccount, "old", "stable"), []byte("qualified"), ""); err != nil { t.Fatal(err) } body, _, usedLegacy, err = GetMigrationSource(ctx, s, ChannelKey(OpsAccount, "old", "stable"), LegacyChannelKey("old", "stable")) if err != nil || string(body) != "qualified" || usedLegacy { t.Fatalf("qualified must win: body=%q usedLegacy=%v err=%v", body, usedLegacy, err) } } // A MIGRATED app waiting on its first promotion to a channel is ordinary, // and must not be told to migrate. This is kass's live shape on the flagship: // fully migrated to its own account, with a route following stable whose // pointer does not exist until the bake window ends — so the hourly adopt // would otherwise nag about migrating an app that is already migrated, every // hour, forever. // // The distinction is the same one PromoteOnLadder makes, and it is computed // from the qualified prefix alone: nothing there at all means the deployment // never migrated; anything there means this channel simply has no pointer yet. func TestAdoptDoesNotSuggestMigrationForAMigratedApp(t *testing.T) { s := NewDirStore(t.TempDir()) ctx := context.Background() priv, pub := testKeys(t) opt, reg := t.TempDir(), testRegistry(t) // Migrated and live on its own account — just not on stable yet. shipRelease(t, s, "bad", "kass", "v1", adoptPlatform, []byte("build one")) if err := Promote(ctx, s, priv, "bad", "kass", "canary/x", "v1"); err != nil { t.Fatal(err) } if err := reg.Upsert(registry.Route{Host: "kass.bad.oncarlos.test", Kind: "instance", App: "kass", Account: "bad", Channel: "stable"}); err != nil { t.Fatal(err) } var out bytes.Buffer withCapturedStdout(t, &out, func() { if err := Pass(ctx, reg, s, pub, opt, func(string) error { return nil }); err != nil { t.Errorf("Pass: %v", err) } }) // The PLAIN line exactly — "channel has no release yet" followed by // nothing. A bare Contains on the prefix would also match the hint line // ("channel has no release yet (…migrate…)"), making the positive half of // this test vacuous (2026-08-03 review): only pinning the line ending // keeps it a real assertion about WHICH branch printed. if !strings.Contains(out.String(), "channel has no release yet\n") { t.Errorf("want the plain skip line and nothing appended to it, got:\n%s", out.String()) } if strings.Contains(out.String(), "accounts migrate") { t.Errorf("a migrated app was told to migrate:\n%s", out.String()) } } // countingStore counts listings of one exact prefix. type countingStore struct { Store prefix string n int } func (c *countingStore) List(ctx context.Context, prefix string) ([]string, error) { if prefix == c.prefix { c.n++ } return c.Store.List(ctx, prefix) } // The check that decides the line costs one listing per (account, app) per // pass, not one per channel. A box following several of an app's channels — // the ordinary shape for a canary fleet — must not pay for the distinction // once per route. func TestAdoptListsAnAppsPrefixOncePerPass(t *testing.T) { dir := NewDirStore(t.TempDir()) ctx := context.Background() priv, pub := testKeys(t) opt, reg := t.TempDir(), testRegistry(t) shipRelease(t, dir, "bad", "kass", "v1", adoptPlatform, []byte("build one")) if err := Promote(ctx, dir, priv, "bad", "kass", "canary/x", "v1"); err != nil { t.Fatal(err) } // Three routes, three channels with no pointer, one app. for i, ch := range []string{"stable", "beta", "edge"} { if err := reg.Upsert(registry.Route{Host: fmt.Sprintf("h%d.example.com", i), Kind: "instance", App: "kass", Account: "bad", Channel: ch}); err != nil { t.Fatal(err) } } s := &countingStore{Store: dir, prefix: AppPrefix("bad", "kass")} var out bytes.Buffer withCapturedStdout(t, &out, func() { if err := Pass(ctx, reg, s, pub, opt, func(string) error { return nil }); err != nil { t.Errorf("Pass: %v", err) } }) if s.n != 1 { t.Errorf("listed %s %d times in one pass, want 1", s.prefix, s.n) } if strings.Contains(out.String(), "accounts migrate") { t.Errorf("a migrated app was told to migrate:\n%s", out.String()) } } // The other side of the same line: an app with NOTHING under its qualified // prefix is the deployment that upgraded without migrating, and it gets the // verb by name. Reserving the advice is what makes it worth reading. func TestAdoptSuggestsMigrationForAnUnmigratedApp(t *testing.T) { dir := NewDirStore(t.TempDir()) ctx := context.Background() priv, pub := testKeys(t) opt, reg := t.TempDir(), testRegistry(t) scratch := NewDirStore(t.TempDir()) shipRelease(t, scratch, OpsAccount, "old", "v1", adoptPlatform, []byte("build one")) if err := Promote(ctx, scratch, priv, OpsAccount, "old", "stable", "v1"); err != nil { t.Fatal(err) } keys, err := scratch.List(ctx, AppPrefix(OpsAccount, "old")) if err != nil { t.Fatal(err) } for _, k := range keys { body, _, err := scratch.Get(ctx, k) if err != nil { t.Fatal(err) } legacy := LegacyAppPrefix("old") + strings.TrimPrefix(k, AppPrefix(OpsAccount, "old")) if _, err := dir.Put(ctx, legacy, body, ""); err != nil { t.Fatal(err) } } s := &legacyTripwire{Store: dir, t: t, app: "old"} addRoute(t, reg, "old.example.com", "old", "stable", false) var out bytes.Buffer withCapturedStdout(t, &out, func() { if err := Pass(ctx, reg, s, pub, opt, func(string) error { return nil }); err != nil { t.Errorf("Pass: %v", err) } }) if !strings.Contains(out.String(), "carlos accounts migrate --app old --from legacy --to bab") { t.Errorf("an unmigrated app was not told how to migrate:\n%s", out.String()) } } // Promoting an app on a deployment that never migrated must say SO. Left to // the ladder, the answer is "stable is entered from beta only, and this build // is on no rung yet" — true (nothing qualified means no rung) and entirely // beside the point. The operator's actual problem is that this deployment's // objects are all at the account-less keys. // // The test drives it through the tripwire: the distinction is computed from // the qualified prefix alone, never by peeking at legacy. func TestPromoteOnLadderNamesTheMigrationForAnUnmigratedApp(t *testing.T) { dir := NewDirStore(t.TempDir()) ctx := context.Background() priv, _ := testKeys(t) s := &legacyTripwire{Store: dir, t: t, app: "old"} // A wholly legacy app, built in a scratch store and copied down. scratch := NewDirStore(t.TempDir()) shipRelease(t, scratch, OpsAccount, "old", "v1", adoptPlatform, []byte("build one")) if err := Promote(ctx, scratch, priv, OpsAccount, "old", "stable", "v1"); err != nil { t.Fatal(err) } keys, err := scratch.List(ctx, AppPrefix(OpsAccount, "old")) if err != nil { t.Fatal(err) } for _, k := range keys { body, _, err := scratch.Get(ctx, k) if err != nil { t.Fatal(err) } legacy := LegacyAppPrefix("old") + strings.TrimPrefix(k, AppPrefix(OpsAccount, "old")) if _, err := dir.Put(ctx, legacy, body, ""); err != nil { t.Fatal(err) } } _, err = PromoteOnLadder(ctx, s, priv, OpsAccount, "old", "stable", "v1", PromoteOptions{}) if err == nil { t.Fatal("promoting an unmigrated app succeeded") } if !strings.Contains(err.Error(), "carlos accounts migrate --app old --from legacy --to bab") { t.Errorf("the refusal does not name the fix:\n%v", err) } if strings.Contains(err.Error(), "no rung yet") { t.Errorf("the ladder answered instead of the migration:\n%v", err) } } // The control for the test above, and the reason the condition is "nothing // qualified AT ALL" rather than "no qualified releases": an app that HAS // shipped under this account is not unmigrated, however new the target // channel is. It gets the ladder's answer, with no migration advice. func TestPromoteOnLadderKeepsTheLadderRefusalForAShippedApp(t *testing.T) { s := NewDirStore(t.TempDir()) ctx := context.Background() priv, _ := testKeys(t) shipRelease(t, s, OpsAccount, "fresh", "v1", adoptPlatform, []byte("build one")) _, err := PromoteOnLadder(ctx, s, priv, OpsAccount, "fresh", "stable", "v1", PromoteOptions{}) if err == nil { t.Fatal("a fresh build entered stable directly") } if !strings.Contains(err.Error(), "no rung yet") { t.Errorf("want the ladder's refusal, got:\n%v", err) } if strings.Contains(err.Error(), "accounts migrate") { t.Errorf("a shipped app was told to migrate:\n%v", err) } } // A store error that is not "not found" is never retried against legacy: // masking a broken bucket as a fallback miss would make it look like an // unmigrated app, and the migration would then "verify" an app it never read. func TestGetMigrationSourcePropagatesRealErrors(t *testing.T) { s := &erroringStore{err: errors.New("bucket on fire")} _, _, _, err := GetMigrationSource(context.Background(), s, "a", "b") if err == nil || !strings.Contains(err.Error(), "on fire") { t.Fatalf("want the real error, got %v", err) } } // Promote against an app whose pointer only exists at the legacy key does not // see it: the new pointer is a FIRST promote at the qualified key, and the // legacy history does not come forward. That is the retirement's real cost, // stated — carrying history across is what `accounts migrate` is for, and it // is why a miss names that verb. The legacy object is still never deleted. func TestPromoteDoesNotInheritALegacyPointer(t *testing.T) { s := NewDirStore(t.TempDir()) ctx := context.Background() priv, pub := testKeys(t) shipRelease(t, s, "kqmv", "myapp", "v1", "linux-amd64", []byte("v1")) shipRelease(t, s, "kqmv", "myapp", "v2", "linux-amd64", []byte("v2")) legacy, err := signPointer(RootSigner(priv), &ChannelPointer{Account: "bab", App: "myapp", Channel: "stable", Version: "v1"}) if err != nil { t.Fatal(err) } if _, err := s.Put(ctx, LegacyChannelKey("myapp", "stable"), legacy, ""); err != nil { t.Fatal(err) } if err := Promote(ctx, s, priv, "kqmv", "myapp", "stable", "v2"); err != nil { t.Fatalf("Promote: %v", err) } ptr, err := Channel(ctx, s, pub, "kqmv", "myapp", "stable") if err != nil { t.Fatal(err) } if ptr.Version != "v2" { t.Fatalf("pointer = %s, want v2", ptr.Version) } if len(ptr.History) != 0 { t.Errorf("history = %v, want empty — a legacy pointer must not be inherited", ptr.History) } if _, _, err := s.Get(ctx, LegacyChannelKey("myapp", "stable")); err != nil { t.Errorf("promote deleted the legacy pointer: %v", err) } }