| new file mode 100644 |
| index 000000000..8ae3aee72 |
| --- /dev/null |
| +++ b/internal/console/rootfs_test.go |
| @@ -0,0 +1,301 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "fmt" |
| + "net/http" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/geo" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/status" |
| +) |
| + |
| +// The three questions every rootfs door asks (rootfs apps Plan 4 Task 2): |
| +// what the creating release says, whether a placement is the account's own |
| +// boxes, and whether a box there has passed its rootfs check. App shop in |
| +// account bab, fleets home and away, hosts under example.com. |
| + |
| +// shipRootfsRelease ships a rootfs release of app (Plan 1's shape). |
| +func shipRootfsRelease(t *testing.T, fx *consoleFixture, app, version string, hibernate bool) { |
| + t.Helper() |
| + m := release.Manifest{ |
| + Schema: 1, App: app, Version: version, Kind: release.KindRootfs, BuiltAt: "2026-10-24T10:00:00Z", |
| + Artifacts: map[string]release.Artifact{release.RootfsArtifactKey: {File: release.RootfsArtifactFile}}, |
| + Processes: map[string]release.Process{release.ProcessWeb: {Command: []string{"/rails/bin/web"}}}, |
| + Hibernate: hibernate, |
| + } |
| + if err := release.Ship(context.Background(), fx.rel, fx.sqid, m, |
| + map[string][]byte{release.RootfsArtifactKey: []byte("hsqs" + version)}); err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.forgetFrame(app) |
| +} |
| + |
| +// writeFleetBox writes one heartbeat for a box of fleet (fleet == "" is a |
| +// platform box in region). |
| +func writeFleetBox(t *testing.T, fx *consoleFixture, fleet, label, region string, ready bool, at time.Time) { |
| + t.Helper() |
| + var s release.Store = fx.rel |
| + if fleet != "" { |
| + s = release.NewPrefixStore(fx.rel, "fleets/"+fleet+"/") |
| + } |
| + b := status.Box{GeneratedAt: at, Label: label, Region: region, |
| + Metrics: &status.BoxMetrics{Layout: &status.LayoutReport{Mode: "nested"}, RootfsReady: ready}} |
| + if err := status.WriteBox(context.Background(), s, b); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// registerFleetBox adds label to fleet's box list in the registry, active. |
| +func registerFleetBox(t *testing.T, reg *registry.Registry, fleet, label string) { |
| + t.Helper() |
| + if err := reg.AddFleetBox(registry.FleetBox{Fleet: fleet, Label: label, |
| + TokenHash: strings.Repeat("a", 64), Status: "active", AddedAt: time.Now().Unix()}); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func refStatus(ref *doorRefusal) int { |
| + if ref == nil { |
| + return 0 |
| + } |
| + return ref.Status |
| +} |
| + |
| +// rootfsDoorWorld is a console with the app shop in bab, a token, the |
| +// registry, the owned fleets home and away (through the real door), and a |
| +// fleet theirs that belongs to another account. |
| +func rootfsDoorWorld(t *testing.T, code string) (*consoleFixture, *registry.Registry) { |
| + t.Helper() |
| + fx, token := fleetFixture(t, code) |
| + if _, err := fx.st.CreateAppIn(OpsAccountID, "shop", "paul@keymail.dev", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := openTestReg(t, fx) |
| + t.Cleanup(func() { reg.Close() }) |
| + for _, f := range []string{"home", "away"} { |
| + if st, body := postJSON(t, fleetsURL(fx, fx.sqid)+"/new", |
| + map[string]string{"name": f, "bucket": f + "-fleet-data"}, token); st != http.StatusOK { |
| + t.Fatalf("create fleet %s = %d %v", f, st, body) |
| + } |
| + } |
| + if _, err := reg.CreateFleet(registry.Fleet{Name: "theirs", Account: "zzz", Bucket: "theirs-data"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + return fx, reg |
| +} |
| + |
| +// fleetFailReg is the registry with GetFleet and ListFleetBoxes failing. |
| +type fleetFailReg struct{ consoleRegistry } |
| + |
| +func (fleetFailReg) GetFleet(string) (*registry.Fleet, error) { |
| + return nil, errors.New("injected fleet read failure") |
| +} |
| + |
| +func (fleetFailReg) ListFleetBoxes(string) ([]registry.FleetBox, error) { |
| + return nil, errors.New("injected fleet box list failure") |
| +} |
| + |
| +func TestRootfsHeadReadsTheChannelHead(t *testing.T) { |
| + fx, _ := rootfsDoorWorld(t, "code-rfs-head") |
| + shipRootfsRelease(t, fx, "shop", "r1", true) |
| + seedChannel(t, fx, "shop", "edge", "r1") |
| + head, ref := fx.srv.rootfsHeadFor(context.Background(), fx.sqid, "shop", "edge") |
| + if ref != nil || head != (rootfsHead{Rootfs: true, Hibernate: true, Version: "r1"}) { |
| + t.Fatalf("head %+v, %v", head, ref) |
| + } |
| +} |
| + |
| +// Review Focus 1: a channel pointer the console cannot read or verify never |
| +// decides binary for an app whose history holds rootfs releases. |
| +func TestRootfsHeadFallsBackToHistory(t *testing.T) { |
| + fx, _ := rootfsDoorWorld(t, "code-rfs-history") |
| + shipRootfsRelease(t, fx, "shop", "r1", false) |
| + // No pointer at all, then a pointer the console cannot verify. |
| + for _, ch := range []string{"edge", "broken"} { |
| + if ch == "broken" { |
| + if _, err := fx.rel.Put(context.Background(), release.ChannelKey(fx.sqid, "shop", "broken"), []byte(`{"payload":"x","sig":"y"}`), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + head, ref := fx.srv.rootfsHeadFor(context.Background(), fx.sqid, "shop", ch) |
| + if ref != nil || head != (rootfsHead{Rootfs: true, Version: "r1"}) { |
| + t.Fatalf("%s: head %+v, %v; want rootfs from history", ch, head, ref) |
| + } |
| + } |
| + // A pointer whose manifest cannot be read falls back too, and the |
| + // history read then fails closed: never binary. |
| + seedChannel(t, fx, "shop", "edge", "r1") |
| + fx.srv.cfg.Releases = &failGetStore{Store: fx.rel, key: release.ManifestKey(fx.sqid, "shop", "r1")} |
| + if head, ref := fx.srv.rootfsHeadFor(context.Background(), fx.sqid, "shop", "edge"); refStatus(ref) != http.StatusBadGateway || head != (rootfsHead{}) { |
| + t.Fatalf("unreadable head manifest: head %+v, %v; want 502", head, ref) |
| + } |
| +} |
| + |
| +func TestRootfsHeadIsBinaryForBinaryAndNewApps(t *testing.T) { |
| + fx, _ := rootfsDoorWorld(t, "code-rfs-binary") |
| + // kass is binary: its readable head, and its history with no pointer. |
| + for _, ch := range []string{"stable", "nosuch"} { |
| + if head, ref := fx.srv.rootfsHeadFor(context.Background(), fx.sqid, "kass", ch); ref != nil || head != (rootfsHead{}) { |
| + t.Errorf("kass %s: head %+v, %v; want binary", ch, head, ref) |
| + } |
| + } |
| + // shop has no release and no marker: no first ship has fixed its kind. |
| + if head, ref := fx.srv.rootfsHeadFor(context.Background(), fx.sqid, "shop", "edge"); ref != nil || head != (rootfsHead{Unfixed: true}) { |
| + t.Errorf("shop: head %+v, %v; want unfixed", head, ref) |
| + } |
| +} |
| + |
| +// Astra R2 finding 2: a first ship that has written its kind marker but not |
| +// its manifest already decides. |
| +func TestRootfsHeadReadsAnUnpublishedFirstShip(t *testing.T) { |
| + fx, _ := rootfsDoorWorld(t, "code-rfs-marker") |
| + m := release.Manifest{Schema: 1, App: "shop", Version: "r1", Kind: release.KindRootfs, Hibernate: true, |
| + Artifacts: map[string]release.Artifact{release.RootfsArtifactKey: {File: release.RootfsArtifactFile, SHA256: strings.Repeat("a", 64), Size: 1}}, |
| + Processes: map[string]release.Process{release.ProcessWeb: {Command: []string{"/rails/bin/web"}}}} |
| + if err := release.EstablishFirstKind(context.Background(), fx.rel, fx.sqid, &m); err != nil { |
| + t.Fatal(err) |
| + } |
| + head, ref := fx.srv.rootfsHeadFor(context.Background(), fx.sqid, "shop", "edge") |
| + if ref != nil || head != (rootfsHead{Rootfs: true, Hibernate: true, Version: "r1"}) { |
| + t.Fatalf("head %+v, %v", head, ref) |
| + } |
| +} |
| + |
| +func TestRootfsHeadRefusesWhenHistoryIsUnreadable(t *testing.T) { |
| + fx, _ := rootfsDoorWorld(t, "code-rfs-unreadable") |
| + fx.srv.cfg.Releases = listFailStore{fx.rel} |
| + head, ref := fx.srv.rootfsHeadFor(context.Background(), fx.sqid, "shop", "edge") |
| + if refStatus(ref) != http.StatusBadGateway || ref.Msg != rootfsReleasesUnreadableMsg || head != (rootfsHead{}) { |
| + t.Fatalf("head %+v, %v; want 502 C08", head, ref) |
| + } |
| +} |
| + |
| +func TestRootfsPlacementRefusal(t *testing.T) { |
| + fx, reg := rootfsDoorWorld(t, "code-rfs-place") |
| + acct := fixtureAccount(fx) |
| + notOwn := fmt.Sprintf(rootfsNotOwnBoxesMsg, "shop") |
| + pooled := fmt.Sprintf(rootfsPoolMsg, "shop") |
| + for _, tc := range []struct { |
| + single bool |
| + placed, pool string |
| + status int |
| + msg string |
| + }{ |
| + {false, "fleet/home", "", 0, ""}, |
| + {false, "fleet/away", "", 0, ""}, |
| + {false, "", "", http.StatusConflict, notOwn}, |
| + {false, "fleet/theirs", "", http.StatusConflict, notOwn}, // another account's fleet |
| + {false, "fleet/nosuch", "", http.StatusConflict, notOwn}, |
| + {false, "uk", "", http.StatusConflict, notOwn}, |
| + {false, "fleet/home", "gold", http.StatusConflict, pooled}, // pools never in v1 |
| + {true, "", "", 0, ""}, |
| + {true, "fleet/theirs", "", 0, ""}, |
| + {true, "", "gold", http.StatusConflict, pooled}, // Plan 3 D6 refuses a pooled rootfs record on every deployment (D9) |
| + } { |
| + fx.srv.cfg.DeploymentSingleTenant = tc.single |
| + ref := fx.srv.rootfsPlacementRefusal(reg, acct, "shop", tc.placed, tc.pool) |
| + if got := refStatus(ref); got != tc.status || (ref != nil && ref.Msg != tc.msg) { |
| + t.Errorf("%+v: got %v", tc, ref) |
| + } |
| + } |
| + // A fleet row that cannot be read answers as the place door does. |
| + fx.srv.cfg.DeploymentSingleTenant = false |
| + ref := fx.srv.rootfsPlacementRefusal(fleetFailReg{reg}, acct, "shop", "fleet/home", "") |
| + if refStatus(ref) != http.StatusInternalServerError || ref.Msg != "could not check the fleet" { |
| + t.Errorf("GetFleet error: %v", ref) |
| + } |
| +} |
| + |
| +// Review Focus 3: a ready box that is stale, detached, on another fleet of |
| +// the same account, or never registered to the fleet does not count. |
| +func TestRootfsReadyCountsOnlyFreshBoxesOfThePlacement(t *testing.T) { |
| + fx, reg := rootfsDoorWorld(t, "code-rfs-ready") |
| + for _, l := range []string{"pi-1", "pi-2", "pi-4"} { |
| + registerFleetBox(t, reg, "home", l) |
| + } |
| + registerFleetBox(t, reg, "away", "pi-3") |
| + if err := reg.SetFleetBoxStatus("home", "pi-4", "detached"); err != nil { |
| + t.Fatal(err) |
| + } |
| + now := time.Now() |
| + writeFleetBox(t, fx, "home", "pi-1", "", false, now) // fresh, not ready |
| + writeFleetBox(t, fx, "home", "pi-2", "", true, now.Add(-2*time.Hour)) // ready, stale |
| + writeFleetBox(t, fx, "away", "pi-3", "", true, now) // ready, another fleet |
| + writeFleetBox(t, fx, "home", "pi-4", "", true, now) // ready, detached |
| + writeFleetBox(t, fx, "home", "pi-5", "", true, now) // ready, never registered |
| + writeFleetBox(t, fx, "", "pi-6", "", true, now) // ready, a platform box |
| + want := fmt.Sprintf(rootfsFleetNotReadyMsg, "home") |
| + if ref := fx.srv.rootfsReadyRefusal(context.Background(), reg, "fleet/home", ""); refStatus(ref) != http.StatusConflict || ref.Msg != want { |
| + t.Fatalf("not ready: %v", ref) |
| + } |
| + writeFleetBox(t, fx, "home", "pi-1", "", true, now) |
| + if ref := fx.srv.rootfsReadyRefusal(context.Background(), reg, "fleet/home", ""); ref != nil { |
| + t.Fatalf("ready: %v", ref) |
| + } |
| + // A heartbeat with no metrics is not ready. |
| + if boxRootfsReady(status.Box{Label: "pi-1"}) { |
| + t.Error("a box with no metrics read as ready") |
| + } |
| +} |
| + |
| +func TestRootfsReadyOnASingleTenantRegion(t *testing.T) { |
| + fx, reg := rootfsDoorWorld(t, "code-rfs-region") |
| + fx.srv.cfg.DeploymentSingleTenant = true |
| + fx.srv.cfg.HomeRegion = "ie" |
| + now := time.Now() |
| + writeFleetBox(t, fx, "", "box-1", "ie", false, now) // home, fresh, not ready |
| + writeFleetBox(t, fx, "", "box-2", "", true, now.Add(-2*time.Hour)) // home, ready, stale |
| + writeFleetBox(t, fx, "", "box-3", "uk", true, now) // uk, ready |
| + writeFleetBox(t, fx, "", "box-4", "us", false, now) // us, not ready |
| + writeFleetBox(t, fx, "home", "pi-1", "", true, now) // a fleet box: not the platform's |
| + registerFleetBox(t, reg, "home", "pi-1") |
| + // Row 2.8: C06 names the region by its place, as the create door's |
| + // other region refusals do, never by its code. |
| + ref := fx.srv.rootfsReadyRefusal(context.Background(), reg, "", "") |
| + if refStatus(ref) != http.StatusConflict || ref.Msg != fmt.Sprintf(rootfsRegionNotReadyMsg, "ie") { |
| + t.Fatalf("home with no edges: %v", ref) |
| + } |
| + // With no home name and no edges the sentence still names a place. |
| + fx.srv.cfg.HomeRegion = "" |
| + if ref := fx.srv.rootfsReadyRefusal(context.Background(), reg, "", ""); refStatus(ref) != http.StatusConflict || |
| + ref.Msg != fmt.Sprintf(rootfsRegionNotReadyMsg, "home") { |
| + t.Fatalf("unnamed home: %v", ref) |
| + } |
| + fx.srv.cfg.HomeRegion = "ie" |
| + setEdges(t, fx, map[string]string{"edge-ie": edgeJSON("edge-ie", "5.6.7.8", "eu-west-1", "", eligibleBox)}) |
| + place := geo.PlaceName("eu-west-1", "") |
| + if place == "" || place == "ie" { |
| + t.Fatalf("no place for eu-west-1: %q", place) |
| + } |
| + ref = fx.srv.rootfsReadyRefusal(context.Background(), reg, "", "") |
| + if want := fmt.Sprintf(rootfsRegionNotReadyMsg, place); refStatus(ref) != http.StatusConflict || ref.Msg != want { |
| + t.Fatalf("home: %v, want %q", ref, want) |
| + } |
| + if ref := fx.srv.rootfsReadyRefusal(context.Background(), reg, "", "uk"); ref != nil { |
| + t.Fatalf("uk: %v", ref) |
| + } |
| + if ref := fx.srv.rootfsReadyRefusal(context.Background(), reg, "", "us"); refStatus(ref) != http.StatusConflict { |
| + t.Fatalf("us: %v", ref) |
| + } |
| +} |
| + |
| +func TestRootfsReadyRefusesWhenItCannotLook(t *testing.T) { |
| + fx, reg := rootfsDoorWorld(t, "code-rfs-ready-err") |
| + registerFleetBox(t, reg, "home", "pi-1") |
| + writeFleetBox(t, fx, "home", "pi-1", "", true, time.Now()) |
| + if ref := fx.srv.rootfsReadyRefusal(context.Background(), fleetFailReg{reg}, "fleet/home", ""); refStatus(ref) != http.StatusServiceUnavailable || ref.Msg != rootfsReadyUnreadableMsg { |
| + t.Errorf("box list error: %v", ref) |
| + } |
| + fx.srv.cfg.Releases = listFailStore{fx.rel} |
| + for _, placed := range []string{"fleet/home", ""} { |
| + if ref := fx.srv.rootfsReadyRefusal(context.Background(), reg, placed, ""); refStatus(ref) != http.StatusServiceUnavailable || ref.Msg != rootfsReadyUnreadableMsg { |
| + t.Errorf("%q heartbeat error: %v", placed, ref) |
| + } |
| + } |
| +} |