| new file mode 100644 |
| index 000000000..a4a8f0402 |
| --- /dev/null |
| +++ b/internal/provision/reconcile_rootfs_test.go |
| @@ -0,0 +1,377 @@ |
| +package provision |
| + |
| +import ( |
| + "bytes" |
| + "encoding/json" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// Rootfs apps Plan 3 Task 4: reconcile materializes a record's runtime. A |
| +// rootfs record mints a row with its runtime and hibernation, at the nested |
| +// paths and with no seed, and no later pass converges either column. |
| + |
| +const rootfsHost = "bab.shop.example.com" |
| + |
| +// rootfsSetup writes the shop app's manifest, with a seed, so a test can |
| +// show a rootfs row never takes it (D5). |
| +func rootfsSetup(t *testing.T) (*release.DirStore, string) { |
| + t.Helper() |
| + s := dirStore(t) |
| + sqid := writeAccountManifest(t, s, acctID, "Shop") |
| + if err := PutManifest(ctx, s, sqid, "shop", Manifest{ |
| + Schema: 1, App: "shop", Domain: "shop.example.com", Seed: []string{"seed"}, |
| + }); err != nil { |
| + t.Fatal(err) |
| + } |
| + return s, sqid |
| +} |
| + |
| +func rootfsRecord(sqid, host string, hibernate bool) Record { |
| + h := hibernate |
| + return Record{Schema: 1, Host: host, Slug: strings.SplitN(host, ".", 2)[0], App: "shop", |
| + Account: sqid, Region: "ie", Channel: "stable", Runtime: RuntimeRootfsV1, |
| + Hibernate: &h, CreatedAt: time.Now()} |
| +} |
| + |
| +func rootfsCfg(rep *ReconcileReport) ReconcileConfig { |
| + return ReconcileConfig{Region: "ie", DataDir: "/data", Report: rep} |
| +} |
| + |
| +// rewriteRecord rewrites a stored record's body with a raw Put, around |
| +// WriteRecord's guard: what an older console's re-marshal, or anything else |
| +// with bucket write access, can leave in the bucket. |
| +func rewriteRecord(t *testing.T, s release.Store, key string, mut func(map[string]any)) { |
| + t.Helper() |
| + body, _, err := s.Get(ctx, key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var m map[string]any |
| + if err := json.Unmarshal(body, &m); err != nil { |
| + t.Fatal(err) |
| + } |
| + mut(m) |
| + putReplace(t, s, key, mustJSON(t, m)) |
| +} |
| + |
| +func reconcileOnce(t *testing.T, s release.Store, reg *registry.Registry, cfg ReconcileConfig) { |
| + t.Helper() |
| + if err := Reconcile(ctx, s, reg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func TestReconcileMintsRootfsNested(t *testing.T) { |
| + s, sqid := rootfsSetup(t) |
| + if err := CreateRecord(ctx, s, rootfsRecord(sqid, rootfsHost, false)); err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := regWith(t) |
| + // cfg.Layout is nil: a flat-mode box. A rootfs row is never flat. |
| + reconcileOnce(t, s, reg, rootfsCfg(nil)) |
| + want := registry.Route{ |
| + Host: rootfsHost, Slug: "bab", Kind: "instance", |
| + Socket: NestedSocket(rootfsHost), DBPath: NestedDBPath("/data", rootfsHost), |
| + App: "shop", Account: sqid, Region: "ie", Channel: "stable", |
| + Provisioned: true, Status: "ready", Runtime: registry.RuntimeRootfsV1, |
| + } |
| + got := *mustGet(t, reg, rootfsHost) |
| + got.CreatedAt = 0 |
| + if got != want { |
| + t.Fatalf("minted route = %+v\nwant %+v", got, want) |
| + } |
| + // The app's instances.json seed is ignored at mint and on every later |
| + // pass: the owned arm never converges it onto a rootfs row. |
| + for pass := 2; pass <= 3; pass++ { |
| + reconcileOnce(t, s, reg, rootfsCfg(nil)) |
| + got := *mustGet(t, reg, rootfsHost) |
| + if got.Seed != "" || got.SeedSource != "" { |
| + t.Fatalf("pass %d: rootfs row took a seed: seed %q source %q", pass, got.Seed, got.SeedSource) |
| + } |
| + } |
| +} |
| + |
| +func TestReconcileMintsHibernatingRootfs(t *testing.T) { |
| + s, sqid := rootfsSetup(t) |
| + if err := CreateRecord(ctx, s, rootfsRecord(sqid, rootfsHost, true)); err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := regWith(t) |
| + reconcileOnce(t, s, reg, rootfsCfg(nil)) |
| + got := mustGet(t, reg, rootfsHost) |
| + if !got.Hibernate || got.Runtime != registry.RuntimeRootfsV1 { |
| + t.Fatalf("row hibernate %v runtime %q, want true %q", got.Hibernate, got.Runtime, registry.RuntimeRootfsV1) |
| + } |
| + if got.Socket != NestedSocket(rootfsHost) || got.DBPath != NestedDBPath("/data", rootfsHost) { |
| + t.Fatalf("row paths socket %q db %q, want nested", got.Socket, got.DBPath) |
| + } |
| + if got.Seed != "" || got.SeedSource != "" { |
| + t.Fatalf("rootfs row took a seed: %q %q", got.Seed, got.SeedSource) |
| + } |
| +} |
| + |
| +// A converge would go through registry.SetHibernate, which refuses a row |
| +// with a runtime (ErrRuntimeFixed): the call's failure is logged as |
| +// "converge hibernate on" and leaves the record unapplied. So "never |
| +// called" is witnessed by both: no such line, and the app's report |
| +// complete on every pass. |
| +func TestReconcileNeverConvergesRootfsHibernation(t *testing.T) { |
| + cases := []struct { |
| + name string |
| + minted bool // the record's hibernation at mint |
| + flipped bool // what the raw rewrite stores |
| + logWord string |
| + }{ |
| + {"record asks to hibernate", false, true, "record hibernate true, row false"}, |
| + // The reverse is the case today's nested-row guard would leave |
| + // unapplied (a nested row keeps its flag): it must not reach it. |
| + {"record asks to stop hibernating", true, false, "record hibernate false, row true"}, |
| + } |
| + for _, c := range cases { |
| + t.Run(c.name, func(t *testing.T) { |
| + s, sqid := rootfsSetup(t) |
| + rec := rootfsRecord(sqid, rootfsHost, c.minted) |
| + if err := CreateRecord(ctx, s, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := regWith(t) |
| + reconcileOnce(t, s, reg, rootfsCfg(nil)) |
| + before := *mustGet(t, reg, rootfsHost) |
| + rewriteRecord(t, s, RecordKey(sqid, "shop", rootfsHost), func(m map[string]any) { |
| + m["hibernate"] = c.flipped |
| + }) |
| + logged := captureLog(t) |
| + var rep ReconcileReport |
| + reconcileOnce(t, s, reg, rootfsCfg(&rep)) |
| + after := *mustGet(t, reg, rootfsHost) |
| + if after != before { |
| + t.Fatalf("row changed:\n before %+v\n after %+v", before, after) |
| + } |
| + out := logged.String() |
| + if strings.Contains(out, "converge hibernate") || strings.Contains(out, "converged "+rootfsHost+" hibernate") { |
| + t.Fatalf("the pass tried to converge hibernation:\n%s", out) |
| + } |
| + if n := strings.Count(out, c.logWord); n != 1 { |
| + t.Fatalf("want one line naming %q, got %d:\n%s", c.logWord, n, out) |
| + } |
| + if !rep.Complete[sqid+"/shop"] { |
| + t.Fatalf("the disagreement left the app's report incomplete (resident retention would wait forever):\n%s", out) |
| + } |
| + }) |
| + } |
| + // An unknown runtime is fixed and inert like rootfs-v1: the skip keys |
| + // on any runtime, not on Rootfs(). The record here is binary (a |
| + // record with the unknown runtime never passes ValidateShape). |
| + t.Run("unknown runtime row", func(t *testing.T) { |
| + s, sqid := rootfsSetup(t) |
| + off := false |
| + rec := Record{Schema: 1, Host: rootfsHost, Slug: "bab", App: "shop", Account: sqid, |
| + Region: "ie", Channel: "stable", Hibernate: &off, CreatedAt: time.Now()} |
| + if err := CreateRecord(ctx, s, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := regWith(t, registry.Route{Host: rootfsHost, Slug: "bab", Kind: "instance", |
| + Socket: NestedSocket(rootfsHost), DBPath: NestedDBPath("/data", rootfsHost), |
| + App: "shop", Account: sqid, Region: "ie", Channel: "stable", Hibernate: true, |
| + Provisioned: true, Runtime: "rootfs-v2"}) |
| + logged := captureLog(t) |
| + var rep ReconcileReport |
| + reconcileOnce(t, s, reg, rootfsCfg(&rep)) |
| + got := mustGet(t, reg, rootfsHost) |
| + if got.Runtime != "rootfs-v2" || !got.Hibernate { |
| + t.Fatalf("row runtime %q hibernate %v, want rootfs-v2 true", got.Runtime, got.Hibernate) |
| + } |
| + out := logged.String() |
| + if strings.Contains(out, "converge hibernate") { |
| + t.Fatalf("the pass tried to converge hibernation:\n%s", out) |
| + } |
| + if !strings.Contains(out, `record runtime "", row "rootfs-v2"`) { |
| + t.Fatalf("no runtime line:\n%s", out) |
| + } |
| + if !rep.Complete[sqid+"/shop"] { |
| + t.Fatalf("report incomplete:\n%s", out) |
| + } |
| + }) |
| +} |
| + |
| +// Review Focus 1: an older console's UpdateRecord re-marshals its own |
| +// struct and drops "runtime". The box keeps the row's runtime and |
| +// hibernation, logs the mismatch, converges every other column, and |
| +// retires nothing. |
| +func TestStrippedRecordLeavesRootfsRowAlone(t *testing.T) { |
| + cases := []struct { |
| + name string |
| + mut func(map[string]any) |
| + }{ |
| + {"runtime dropped", func(m map[string]any) { |
| + delete(m, "runtime") |
| + m["channel"] = "beta" |
| + }}, |
| + {"runtime dropped and hibernate flipped", func(m map[string]any) { |
| + delete(m, "runtime") |
| + m["hibernate"] = true |
| + m["channel"] = "beta" |
| + }}, |
| + } |
| + for _, c := range cases { |
| + t.Run(c.name, func(t *testing.T) { |
| + s, sqid := rootfsSetup(t) |
| + if err := CreateRecord(ctx, s, rootfsRecord(sqid, rootfsHost, false)); err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := regWith(t) |
| + reconcileOnce(t, s, reg, rootfsCfg(nil)) |
| + rewriteRecord(t, s, RecordKey(sqid, "shop", rootfsHost), c.mut) |
| + for pass := 1; pass <= 3; pass++ { |
| + logged := captureLog(t) |
| + var rep ReconcileReport |
| + reconcileOnce(t, s, reg, rootfsCfg(&rep)) |
| + got, err := reg.Get(rootfsHost) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if got == nil { |
| + t.Fatalf("pass %d: the host was retired", pass) |
| + } |
| + if got.Runtime != registry.RuntimeRootfsV1 || got.Hibernate { |
| + t.Fatalf("pass %d: row runtime %q hibernate %v, want %q false", pass, got.Runtime, got.Hibernate, registry.RuntimeRootfsV1) |
| + } |
| + if got.Socket != NestedSocket(rootfsHost) || got.DBPath != NestedDBPath("/data", rootfsHost) { |
| + t.Fatalf("pass %d: row paths moved: socket %q db %q", pass, got.Socket, got.DBPath) |
| + } |
| + if got.Seed != "" || got.SeedSource != "" { |
| + t.Fatalf("pass %d: row took a seed: %q %q", pass, got.Seed, got.SeedSource) |
| + } |
| + if got.Channel != "beta" { |
| + t.Fatalf("pass %d: channel %q, want the record's beta converged", pass, got.Channel) |
| + } |
| + out := logged.String() |
| + line := `provision: ` + rootfsHost + `: record runtime "", row "rootfs-v1": the runtime is fixed at mint; leaving the row's runtime and hibernation alone` |
| + if n := strings.Count(out, line); n != 1 { |
| + t.Fatalf("pass %d: want the runtime line once, got %d:\n%s", pass, n, out) |
| + } |
| + if strings.Contains(out, "converge hibernate") { |
| + t.Fatalf("pass %d: the pass tried to converge hibernation:\n%s", pass, out) |
| + } |
| + if !rep.Complete[sqid+"/shop"] { |
| + t.Fatalf("pass %d: report incomplete:\n%s", pass, out) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestReconcileRefusesUnknownRuntime(t *testing.T) { |
| + s, sqid := rootfsSetup(t) |
| + const fresh, held = "new.shop.example.com", "old.shop.example.com" |
| + off := false |
| + for _, host := range []string{fresh, held} { |
| + if err := CreateRecord(ctx, s, Record{Schema: 1, Host: host, Slug: strings.SplitN(host, ".", 2)[0], |
| + App: "shop", Account: sqid, Region: "ie", Channel: "stable", Hibernate: &off, |
| + CreatedAt: time.Now()}); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + reg := regWith(t) |
| + reconcileOnce(t, s, reg, rootfsCfg(nil)) |
| + if err := reg.Delete(fresh); err != nil { |
| + t.Fatal(err) |
| + } |
| + before := *mustGet(t, reg, held) |
| + for _, host := range []string{fresh, held} { |
| + rewriteRecord(t, s, RecordKey(sqid, "shop", host), func(m map[string]any) { |
| + m["runtime"] = "rootfs-v2" |
| + m["channel"] = "beta" |
| + }) |
| + } |
| + logged := captureLog(t) |
| + var rep ReconcileReport |
| + reconcileOnce(t, s, reg, rootfsCfg(&rep)) |
| + if rt, err := reg.Get(fresh); err != nil || rt != nil { |
| + t.Fatalf("an unknown-runtime record was minted: %+v %v", rt, err) |
| + } |
| + after, err := reg.Get(held) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if after == nil { |
| + t.Fatal("an unknown-runtime record retired its host's row") |
| + } |
| + if *after != before { |
| + t.Fatalf("an unknown-runtime record converged its row:\n before %+v\n after %+v", before, *after) |
| + } |
| + if !strings.Contains(logged.String(), `runtime "rootfs-v2"`) { |
| + t.Fatalf("no refusal names the runtime:\n%s", logged.String()) |
| + } |
| + if rep.Complete[sqid+"/shop"] { |
| + t.Fatal("an unusable record left its app complete") |
| + } |
| +} |
| + |
| +func TestAdoptionRefusesAcrossRuntimes(t *testing.T) { |
| + s, sqid := rootfsSetup(t) |
| + if err := CreateRecord(ctx, s, rootfsRecord(sqid, rootfsHost, false)); err != nil { |
| + t.Fatal(err) |
| + } |
| + // A hand-added binary row at exactly the paths the rootfs record |
| + // derives: only the runtime tells them apart. |
| + hand := registry.Route{Host: rootfsHost, Slug: "bab", Kind: "instance", |
| + Socket: NestedSocket(rootfsHost), DBPath: NestedDBPath("/data", rootfsHost), |
| + App: "shop", Account: sqid, Region: "ie", Channel: "stable", Hibernate: true} |
| + reg := regWith(t, hand) |
| + before := *mustGet(t, reg, rootfsHost) |
| + logged := captureLog(t) |
| + reconcileOnce(t, s, reg, rootfsCfg(nil)) |
| + after := *mustGet(t, reg, rootfsHost) |
| + if after != before { |
| + t.Fatalf("the hand-added row was touched:\n before %+v\n after %+v", before, after) |
| + } |
| + if !bytes.Contains(logged.Bytes(), []byte(`runtime ""/"rootfs-v1"`)) { |
| + t.Fatalf("the refusal does not name the runtime:\n%s", logged.String()) |
| + } |
| +} |
| + |
| +// A rootfs record or row is home-boxed whatever else it says, and the two |
| +// gates agree on it (TestEverywhereClassAgreesAcrossRecordAndRow's rule). |
| +// The fixtures are shapes that would otherwise be everywhere-class, so only |
| +// the runtime answers. |
| +func TestRootfsRecordIsHomeBoxed(t *testing.T) { |
| + for _, runtime := range []string{RuntimeRootfsV1, "rootfs-v2"} { |
| + for _, rec := range []Record{ |
| + {Host: "site.shop.example.com", App: "shop", Account: "bab", Channel: "stable", Kind: "static", Runtime: runtime}, |
| + {Host: "pub.shop.example.com", App: "shop", Account: "bab", Channel: "stable", Kind: "service", Addr: "203.0.113.10:443", Runtime: runtime}, |
| + } { |
| + base := rec |
| + base.Runtime = "" |
| + if !everywhereClass(base) || !everywhereRoute(RouteFor(base, "/data", "", false, nil)) { |
| + t.Fatalf("fixture %s is not everywhere-class without a runtime", rec.Host) |
| + } |
| + if everywhereClass(rec) { |
| + t.Errorf("everywhereClass(%s, runtime %q) = true, want home-boxed", rec.Host, runtime) |
| + } |
| + row := RouteFor(rec, "/data", "", false, nil) |
| + if everywhereRoute(row) { |
| + t.Errorf("everywhereRoute(%s, runtime %q) = true, want home-boxed", rec.Host, runtime) |
| + } |
| + row.Runtime = runtime |
| + if everywhereRoute(row) { |
| + t.Errorf("everywhereRoute(row %s, runtime %q) = true, want home-boxed", rec.Host, runtime) |
| + } |
| + } |
| + } |
| + // The real shape: a valid rootfs record and the row it mints. |
| + h := false |
| + rec := Record{Schema: 1, Host: rootfsHost, Slug: "bab", App: "shop", Account: "bab", |
| + Region: "ie", Channel: "stable", Runtime: RuntimeRootfsV1, Hibernate: &h} |
| + if err := ValidateShape(rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + if everywhereClass(rec) || everywhereRoute(RouteFor(rec, "/data", "", true, nil)) { |
| + t.Fatal("a rootfs record is not home-boxed") |
| + } |
| +} |