Fix cold-placement root fixture bootstrap and HA assertions
Create the trusted guard identity namespace before arming the fresh runner. Require HA writers, share accounting checks, preserve the observation deadline, and isolate start journaling to the competing-wake fixture.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+56
−36
internal/host/haplacement_budget_test.go+1 −1internal/host/haplacement_root_linux_test.go+27 −20internal/host/haplacement_scenarios_root_linux_test.go+26 −13internal/host/haplacement_wiring_test.go+2 −2
diff --git a/internal/host/haplacement_budget_test.go b/internal/host/haplacement_budget_test.go| index 8650350a7..2a489effa 100644 |
| --- a/internal/host/haplacement_budget_test.go |
| +++ b/internal/host/haplacement_budget_test.go |
| @@ -3,6 +3,7 @@ package host |
| import ( |
| "context" |
| "encoding/json" |
| + "strings" |
| "testing" |
| "time" |
| @@ -10,7 +11,6 @@ import ( |
| "github.com/carlosframework/platform/internal/hafresh" |
| "github.com/carlosframework/platform/internal/lease/locktest" |
| "github.com/carlosframework/platform/internal/release" |
| - "strings" |
| ) |
| func (f *coldPlacementProcesses) marker(ref string) { |
diff --git a/internal/host/haplacement_root_linux_test.go b/internal/host/haplacement_root_linux_test.go| index 60c0d6a13..994368f44 100644 |
| --- a/internal/host/haplacement_root_linux_test.go |
| +++ b/internal/host/haplacement_root_linux_test.go |
| @@ -61,14 +61,18 @@ import ( |
| func init() { |
| if lifecyclePayloadArgs(os.Args) { |
| - f, err := os.OpenFile(filepath.Join(filepath.Dir(os.Args[4]), "cold-starts"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) |
| - if err != nil { |
| + f, err := os.OpenFile(filepath.Join(filepath.Dir(os.Args[4]), "cold-starts"), os.O_APPEND|os.O_WRONLY, 0600) |
| + if err != nil && !os.IsNotExist(err) { |
| panic(err) |
| } |
| - if _, err = f.WriteString("start\n"); err != nil { |
| - panic(err) |
| + // Only the competing-wake fixture pre-creates this journal. Other |
| + // lifecycle payloads must not acquire a cold-placement side effect. |
| + if f != nil { |
| + if _, err = f.WriteString("start\n"); err != nil { |
| + panic(err) |
| + } |
| + f.Close() |
| } |
| - f.Close() |
| } |
| coldPlacementProductionLaunch = coldRootProductionWiring |
| if os.Getenv("COLD_ROOT_BOX") != "" { |
| @@ -200,7 +204,7 @@ func TestColdPlacementRootAgent(t *testing.T) { |
| case "/publish": |
| var last []byte |
| ident := coldRootIdentity(ref, 800) |
| - ident.Headroom.Apps = haAwakeApps() |
| + ident.Headroom = headroomFor(ident.Capabilities, data, reg) |
| if err := edgesFileWrite(r.Context(), store, reg, ident, coldRootIP(ref), &last, true); err != nil { |
| http.Error(w, err.Error(), 500) |
| return |
| @@ -253,24 +257,13 @@ func TestColdPlacementRootRunner(t *testing.T) { |
| if !selectedStats.Owned || ingressStats.Owned || selectedStats.Apps["bab/lifecycle"] != 1 { |
| t.Fatalf("ownership selected=%+v ingress=%+v", selectedStats, ingressStats) |
| } |
| - selectedRow := selectedStats.Meter.Rows[lifecycleHost] |
| - ingressRow := ingressStats.Meter.Rows[lifecycleHost] |
| - selectedChecks, ingressChecks := int64(0), int64(0) |
| - for _, g := range selectedStats.Meter.Guard { |
| - selectedChecks += g.Checked |
| - } |
| - for _, g := range ingressStats.Meter.Guard { |
| - ingressChecks += g.Checked |
| - } |
| - if selectedRow.Requests != 1 || selectedRow.HopRequests != 1 || ingressRow.Requests != 0 || selectedChecks != 1 || ingressChecks != 0 || selectedStats.Analytics != 1 || ingressStats.Analytics != 0 { |
| - t.Fatalf("accounting owner selected=%+v ingress=%+v", selectedStats, ingressStats) |
| - } |
| + coldRootAssertAccounting(t, selectedStats, ingressStats) |
| if owner, ok := hafresh.ParseOwner(func() string { |
| v, e := lease.ReadLock(t.Context(), "data", "instances/"+lifecycleHost, "eu-west-1") |
| lifecycleCheck(t, "lock owner", e) |
| return v.Lock.Owner |
| - }()); !ok || owner.Ref != "leo" { |
| + }()); !ok || !owner.HA || owner.Ref != "leo" { |
| t.Fatalf("wrong writer: %+v", owner) |
| } |
| untilReplicated := time.Now().Add(30 * time.Second) |
| @@ -476,6 +469,18 @@ func newColdRootHarness(t *testing.T, mode string) *coldRootHarness { |
| lifecycleCheck(t, "exclusive root "+path, os.Mkdir(path, 0755)) |
| t.Cleanup(func() { lifecycleCheck(t, "remove owned root "+path, os.RemoveAll(path)) }) |
| } |
| + t.Cleanup(func() { |
| + if !t.Failed() { |
| + return |
| + } |
| + for _, path := range []string{"/data/cold-sophie/edge.log", "/data/cold-sophie/restart.log"} { |
| + if raw, err := os.ReadFile(path); err == nil { |
| + t.Logf("%s:\n%s", path, raw) |
| + } |
| + } |
| + out, _ := exec.Command("journalctl", "--no-pager", "-n", "60", "-u", "carlos-edge.service", "-u", "carlos-instance-control.service", "-u", "carlos-system-recover.service").CombinedOutput() |
| + t.Logf("root fixture journal:\n%s", out) |
| + }) |
| lifecycleCheck(t, "private root authority", os.Chmod("/var/lib/carlos-claims", 0700)) |
| for _, path := range []string{lifecycleHome + "/deployment", "/data/.instances", "/data/instances", "/run/carlos/.instances", "/run/carlos/instances", "/run/carlos/.logs", "/run/carlos/logs", "/opt/carlos/versions"} { |
| lifecycleCheck(t, "tree", os.MkdirAll(path, 0755)) |
| @@ -512,7 +517,9 @@ func newColdRootHarness(t *testing.T, mode string) *coldRootHarness { |
| pointer := release.SystemPointer{Schema: 1, Component: "router", Channel: "stable", Spread: "0s", PromotedAt: time.Now(), Current: release.SystemRelease{Version: "v2.0.0", ReleaseTag: "v2.0.0", MovesProtocol: 1, Artifacts: map[string]release.SystemArtifact{platform: {Key: release.SystemArtifactKey("v2.0.0", platform), SHA256: hex.EncodeToString(sum[:])}}}} |
| identityBytes, err := release.SignRootEnvelope(release.RootSigner(key), pointer) |
| lifecycleCheck(t, "executor identity", err) |
| - identities, err := rootstate.Open("/opt/carlos/system/guard/identities", false, true) |
| + // Bootstrap the public root-owned identity namespace before arming guard, |
| + // as provisioning does; a fresh designated runner has no guard tree. |
| + identities, err := rootstate.Open("/opt/carlos/system/guard/identities", true, true) |
| lifecycleCheck(t, "executor identities", err) |
| lifecycleCheck(t, "executor identity persistence", identities.Write("v2.0.0.json", identityBytes)) |
| identities.Close() |
diff --git a/internal/host/haplacement_scenarios_root_linux_test.go b/internal/host/haplacement_scenarios_root_linux_test.go| index 08ab9e848..2dd5816b3 100644 |
| --- a/internal/host/haplacement_scenarios_root_linux_test.go |
| +++ b/internal/host/haplacement_scenarios_root_linux_test.go |
| @@ -7,9 +7,9 @@ import ( |
| "context" |
| "crypto/tls" |
| "crypto/x509" |
| + "encoding/base64" |
| "encoding/json" |
| "fmt" |
| - "github.com/carlosframework/platform/internal/edge" |
| "io" |
| "log" |
| "net" |
| @@ -27,11 +27,11 @@ import ( |
| "time" |
| "unsafe" |
| - "encoding/base64" |
| "github.com/aws/aws-sdk-go-v2/aws" |
| "github.com/aws/aws-sdk-go-v2/service/ec2" |
| "github.com/aws/aws-sdk-go-v2/service/ec2/types" |
| "github.com/carlosframework/platform/internal/appconfig" |
| + "github.com/carlosframework/platform/internal/edge" |
| "github.com/carlosframework/platform/internal/hafresh" |
| "github.com/carlosframework/platform/internal/hostfiles" |
| "github.com/carlosframework/platform/internal/instancectl" |
| @@ -196,7 +196,7 @@ func (f *coldRootHarness) owner() string { |
| v, err := lease.ReadLock(f.t.Context(), "data", "instances/"+lifecycleHost, "eu-west-1") |
| lifecycleCheck(f.t, "owner read", err) |
| o, ok := hafresh.ParseOwner(v.Lock.Owner) |
| - if !ok { |
| + if !ok || !o.HA { |
| f.t.Fatalf("not an HA writer: %+v", v) |
| } |
| return o.Ref |
| @@ -219,6 +219,20 @@ func (f *coldRootHarness) admin(ref, path string) { |
| f.t.Fatalf("admin %s: %d %s", path, response.StatusCode, body) |
| } |
| } |
| +func coldRootAssertAccounting(t *testing.T, selected, ingress coldRootStats) { |
| + t.Helper() |
| + selectedChecks, ingressChecks := int64(0), int64(0) |
| + for _, g := range selected.Meter.Guard { |
| + selectedChecks += g.Checked |
| + } |
| + for _, g := range ingress.Meter.Guard { |
| + ingressChecks += g.Checked |
| + } |
| + if row := selected.Meter.Rows[lifecycleHost]; row.Requests != 1 || row.HopRequests != 1 || ingress.Meter.Rows[lifecycleHost].Requests != 0 || selectedChecks != 1 || ingressChecks != 0 || selected.Analytics != 1 || ingress.Analytics != 0 { |
| + t.Fatalf("accounting selected=%+v ingress=%+v", selected, ingress) |
| + } |
| +} |
| + |
| func coldRootProductionWiring(t *testing.T) { |
| f := newColdRootHarness(t, "production") |
| f.served("sophie") |
| @@ -232,16 +246,7 @@ func coldRootProductionWiring(t *testing.T) { |
| if !selected.Owned || ingress.Owned || selected.Apps["bab/lifecycle"] != 1 { |
| t.Fatalf("writer/arming selected=%+v ingress=%+v", selected, ingress) |
| } |
| - selectedChecks, ingressChecks := int64(0), int64(0) |
| - for _, g := range selected.Meter.Guard { |
| - selectedChecks += g.Checked |
| - } |
| - for _, g := range ingress.Meter.Guard { |
| - ingressChecks += g.Checked |
| - } |
| - if row := selected.Meter.Rows[lifecycleHost]; row.Requests != 1 || row.HopRequests != 1 || ingress.Meter.Rows[lifecycleHost].Requests != 0 || selectedChecks != 1 || ingressChecks != 0 || selected.Analytics != 1 || ingress.Analytics != 0 { |
| - t.Fatalf("accounting selected=%+v ingress=%+v", selected, ingress) |
| - } |
| + coldRootAssertAccounting(t, selected, ingress) |
| deadline := time.Now().Add(5 * time.Second) |
| for len(ingress.Pages) == 0 && time.Now().Before(deadline) { |
| time.Sleep(20 * time.Millisecond) |
| @@ -271,6 +276,12 @@ func coldRootProductionWiring(t *testing.T) { |
| } |
| func coldRootCompetingViews(t *testing.T) { |
| f := newColdRootHarness(t, "competing") |
| + journal := filepath.Join(provision.InstanceDir("/data", lifecycleHost), "cold-starts") |
| + uid, gid := generationRunnerRegistryUser(t) |
| + lifecycleCheck(t, "journal directory", os.MkdirAll(filepath.Dir(journal), 0755)) |
| + lifecycleCheck(t, "journal directory owner", os.Chown(filepath.Dir(journal), uid, gid)) |
| + lifecycleCheck(t, "start journal", os.WriteFile(journal, nil, 0600)) |
| + lifecycleCheck(t, "start journal owner", os.Chown(journal, uid, gid)) |
| start := make(chan struct{}) |
| var wg sync.WaitGroup |
| codes := make(chan int, 2) |
| @@ -879,6 +890,8 @@ func (f *coldRootHarness) servedHost(ref, host string) { |
| // after leo's listener/process is gone. It starts no tenant and publishes no |
| // placement evidence. This preserves the production unit rather than replacing |
| // its BindsTo dependency with a stand-in. |
| +// Like restartSophie, this agent runs a real startup sweep over shared /data. |
| +// Callers must stop existing writers before starting it. |
| func (f *coldRootHarness) keepControlHost() { |
| f.t.Helper() |
| dir := "/data/cold-control-host" |
diff --git a/internal/host/haplacement_wiring_test.go b/internal/host/haplacement_wiring_test.go| index 682e72561..181467344 100644 |
| --- a/internal/host/haplacement_wiring_test.go |
| +++ b/internal/host/haplacement_wiring_test.go |
| @@ -144,8 +144,8 @@ func testColdPlacementRequestBudget(t *testing.T, holder string) { |
| f := newColdPlacementProcesses(t) |
| a, b := f.box("leo"), f.box("sophie") |
| s := f.snapshot("leo", "sophie") |
| - started := time.Now() |
| d := a.call(coldCommand{Op: "forward", Snapshot: s}).Decision |
| + observed := time.Now() |
| if d.Ref != "sophie" || d.Kind != edge.Placement || a.reads() != 1 { |
| t.Fatalf("cold choice %+v, ingress reads %d", d, a.reads()) |
| } |
| @@ -166,7 +166,7 @@ func testColdPlacementRequestBudget(t *testing.T, holder string) { |
| t.Fatalf("refusal budget: ingress %d GET, receiver %d GET, %d PUT", a.reads(), b.reads(), f.writes()) |
| } |
| - time.Sleep(time.Until(started.Add(10*time.Second + 100*time.Millisecond))) |
| + time.Sleep(time.Until(observed.Add(10*time.Second + 100*time.Millisecond))) |
| a.call(coldCommand{Op: "forward", Snapshot: s}) |
| if a.reads() != 4 { |
| t.Fatalf("original observation deadline did not cause exactly one re-read: %d", a.reads()) |