carlos / platform Public

console: HA refusal tests allow only named, exact bucket reads

refused now takes haAllow: the exact multiset of bucket operations a
refusal makes (any other, or a missing one, fails) and the keys a test's
own injection may change, and it checks the seeded policy survived. The
adoption refusal allows exactly the door's region-validation, plan-cap
and claim-prefix listings, traced to their callers. The binding refusal
and its retry now run through refused too; the binding's allowance adds
only the seam's own hostname-history mode write.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 76e963352d3eb88ba8540433696597fe192acdce parent b68b14d
1 file changed, +87 −34
  • internal/console/apicli_ha_test.go +87 −34

CI passed — run details

Intent

Human

<task-notification> <task-id>bghpevxsz</task-id> <tool-use-id>toolu_01XGouSRKSaCLzKTCrWRzjDV</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/1be65f1d-61c8-4308-afb1-2c756edd9c6e/tasks/bghpevxsz.output</output-file> <status>completed</status> <summary>Background command "Wait for pool Task 2 re-review" completed (exit code 0)</summary> </task-notification>

Attributed to this commit: 16.5 g CO₂e (2,076,627 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on g6-pool, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/internal/console/apicli_ha_test.go b/internal/console/apicli_ha_test.go
index 5dfd0b871..770c3d203 100644
--- a/internal/console/apicli_ha_test.go
+++ b/internal/console/apicli_ha_test.go
@@ -14,6 +14,7 @@ import (
"testing"
"github.com/carlosframework/platform/internal/apppolicy"
+ "github.com/carlosframework/platform/internal/provision"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/svcrole"
)
@@ -114,42 +115,82 @@ func haRegionFixture(t *testing.T, o haRegionOpts) *haRegionWorld {
return w
}
+// haAllow is what one refusal may do, each entry named by the test that
+// grants it. The zero value is nothing at all.
+type haAllow struct {
+ // Ops is the exact multiset of witnessed bucket operations ("op key",
+ // e.g. "list apps/bab/adopted/") the request must make: every one,
+ // each as often as listed, and no other. A refusal that by design
+ // follows the claim's own reads lists them here.
+ Ops []string
+ // Changed is the bucket keys whose bytes may differ afterwards: a
+ // write the test itself injects, never one of the request's own.
+ Changed []string
+}
+
+// The bucket reads a create makes before the claim can refuse it, each
+// exactly as the door makes it (traced, Task 2 fix round 2). A refusal
+// that by design comes after them names the ones it makes in haAllow.Ops.
+//
+// haRegionCheckOps is newAppRegionReq's explicit-region validation
+// (validateExplicitRegion's readExplicitGate): placements lists the edges
+// files, then liveEdges lists them again with the box reports.
+func haRegionCheckOps() []string {
+ return []string{"list control/edges/", "list control/edges/", "listmod status/boxes/"}
+}
+
+// haPlanCapOps is checkCreateApp's instance count for name (the plan cap).
+func haPlanCapOps(name string) []string {
+ return []string{"list apps/bab/" + name + "/instances/"}
+}
+
+// haClaimPrefixOps is claimNamePrefixes for name in the ops account bab:
+// the account's prefix, then the legacy unqualified one.
+func haClaimPrefixOps(name string) []string {
+ return []string{"list apps/bab/" + name + "/", "list apps/" + name + "/"}
+}
+
// refused runs req, a create of name the test expects refused, and proves
// it changed nothing: before it, name gets an earlier app's policy object
// (the object createClaimAppRow clears before its insert), and after it
-// the request must have made no bucket mutation (put or delete, whether or
-// not it succeeded) and, unless lists, no bucket listing; every bucket
-// object's bytes and every console database table must be as they were;
-// and name has no row. lists is for a refusal that by design follows the
-// claim's prefix checks (an adoption).
-func (w *haRegionWorld) refused(t *testing.T, name string, lists bool, req func() (int, map[string]any)) (int, map[string]any) {
+// the request's witnessed bucket operations (puts and deletes whether or
+// not they succeeded, and every listing) must be exactly allow.Ops; every
+// bucket object's bytes, but for allow.Changed, and every console database
+// table must be as they were; the seeded policy must still be there; and
+// name has no row.
+func (w *haRegionWorld) refused(t *testing.T, name string, allow haAllow, req func() (int, map[string]any)) (int, map[string]any) {
t.Helper()
w.seedPolicy(t, name)
bucket, db := w.bucketSnap(t), w.dbSnap(t)
var mu sync.Mutex
- var muts, listed []string
+ var ops []string
ds := w.rel.(*deletableStore)
ds.setOpWitness(func(op, key string) {
mu.Lock()
defer mu.Unlock()
- if op == "put" || op == "delete" {
- muts = append(muts, op+" "+key)
- } else {
- listed = append(listed, op+" "+key)
- }
+ ops = append(ops, op+" "+key)
})
status, res := req()
ds.setOpWitness(nil)
mu.Lock()
defer mu.Unlock()
- if len(muts) > 0 {
- t.Errorf("%s: the refused request mutated the bucket: %v", name, muts)
+ got, want := slices.Sorted(slices.Values(ops)), slices.Sorted(slices.Values(allow.Ops))
+ if !slices.Equal(got, want) {
+ t.Errorf("%s: the refused request's bucket operations = %v, want exactly %v", name, got, want)
+ }
+ after := w.bucketSnap(t)
+ for _, k := range allow.Changed {
+ delete(after, k)
+ }
+ before := maps.Clone(bucket)
+ for _, k := range allow.Changed {
+ delete(before, k)
}
- if len(listed) > 0 && !lists {
- t.Errorf("%s: the refused request listed the bucket: %v", name, listed)
+ if !maps.Equal(after, before) {
+ t.Errorf("%s: the bucket changed: %v", name, mapDiff(before, after))
}
- if got := w.bucketSnap(t); !maps.Equal(got, bucket) {
- t.Errorf("%s: the bucket changed: %v", name, mapDiff(bucket, got))
+ if !w.policyPresent(t, name) {
+ t.Errorf("%s: the refused request cleared the earlier policy", name)
}
if got := w.dbSnap(t); !maps.Equal(got, db) {
t.Errorf("%s: the console database changed: %v", name, mapDiff(db, got))
@@ -304,7 +345,7 @@ func TestAPICreateAppHA(t *testing.T) {
if code, _, out := rawReq(t, http.MethodGet, w.ts.URL+"/api/cli/apps", svc, ""); code != http.StatusOK {
t.Fatalf("service warm-up = %d %s", code, out)
}
- status, res := w.refused(t, "svcha", false, func() (int, map[string]any) {
+ status, res := w.refused(t, "svcha", haAllow{}, func() (int, map[string]any) {
return post(svc, map[string]any{"name": "svcha", "region": "us", "ha": true})
})
wantHARefusal(t, "service ha", status, res, http.StatusForbidden, haCopyNotAllowed, haPoolNotAllowedCode)
@@ -328,7 +369,7 @@ func TestAPICreateAppHA(t *testing.T) {
{"name": "noregion", "region": " ", "ha": true},
{"name": "noregion", "region": autoRegion, "ha": true},
} {
- status, res := w.refused(t, "noregion", false, func() (int, map[string]any) { return post(w.token, body) })
+ status, res := w.refused(t, "noregion", haAllow{}, func() (int, map[string]any) { return post(w.token, body) })
wantHARefusal(t, fmt.Sprintf("ha %v", body), status, res, http.StatusBadRequest, haCopyNeedsRegion, "")
}
// Control: the same create naming us is (a).
@@ -336,7 +377,7 @@ func TestAPICreateAppHA(t *testing.T) {
t.Run("a region that is not HA", func(t *testing.T) {
for _, c := range []struct{ region, code string }{{"ie", ""}, {"uk", "uk"}} {
- status, res := w.refused(t, "notha", false, func() (int, map[string]any) {
+ status, res := w.refused(t, "notha", haAllow{}, func() (int, map[string]any) {
return post(w.token, map[string]any{"name": "notha", "region": c.region, "ha": true})
})
wantHARefusal(t, "ha in "+c.region, status, res, http.StatusBadRequest, fmt.Sprintf(haCopyRegion, srv.offlinePlace(c.code)), "")
@@ -347,12 +388,12 @@ func TestAPICreateAppHA(t *testing.T) {
t.Run("a placement", func(t *testing.T) {
place := "fleet/" + w.ownFleet
- status, res := w.refused(t, "placed", false, func() (int, map[string]any) {
+ status, res := w.refused(t, "placed", haAllow{}, func() (int, map[string]any) {
return post(w.token, map[string]any{"name": "placed", "region": "us", "place": place, "ha": true})
})
wantHARefusal(t, "ha with place", status, res, http.StatusBadRequest, fmt.Sprintf(haCopyRegion, place), "")
// A foreign fleet keeps its byte-identical 404, first.
- w.refused(t, "placed", false, func() (int, map[string]any) {
+ w.refused(t, "placed", haAllow{}, func() (int, map[string]any) {
code, _, body := rawReq(t, http.MethodPost, newAppURL(w.consoleFixture), w.token,
`{"name":"placed","region":"us","place":"fleet/`+w.foreignFleet+`","ha":true}`)
if code != http.StatusNotFound {
@@ -371,9 +412,11 @@ func TestAPICreateAppHA(t *testing.T) {
if _, err := w.rel.Put(context.Background(), "apps/bab/adopted/releases/v1.json", []byte(`{}`), ""); err != nil {
t.Fatal(err)
}
- // This refusal follows the claim's prefix checks by design (it
- // needs their answer), so it may list; it may not write.
- status, res := w.refused(t, "adopted", true, func() (int, map[string]any) {
+ // This refusal follows the claim's prefix check by design (it needs
+ // its answer), so it makes exactly that check and the door's reads
+ // before it, and writes nothing.
+ allow := haAllow{Ops: slices.Concat(haRegionCheckOps(), haPlanCapOps("adopted"), haClaimPrefixOps("adopted"))}
+ status, res := w.refused(t, "adopted", allow, func() (int, map[string]any) {
return post(w.token, map[string]any{"name": "adopted", "region": "us", "ha": true})
})
if status != http.StatusConflict || errOf(res) != "that name is taken" {
@@ -394,7 +437,7 @@ func TestAPICreateAppHA(t *testing.T) {
// is refused with the machine code, and its plain creates are untouched.
func TestAPICreateAppHAUnlisted(t *testing.T) {
w := haRegionFixture(t, haRegionOpts{Accounts: []string{"cdef"}})
- status, res := w.refused(t, "shop", false, func() (int, map[string]any) {
+ status, res := w.refused(t, "shop", haAllow{}, func() (int, map[string]any) {
return postJSON(t, newAppURL(w.consoleFixture), map[string]any{"name": "shop", "region": "us", "ha": true}, w.token)
})
wantHARefusal(t, "unlisted ha", status, res, http.StatusForbidden, haCopyNotAllowed, haPoolNotAllowedCode)
@@ -419,7 +462,7 @@ func TestAPICreateAppHABrokenConfig(t *testing.T) {
if !w.srv.haPoolsBroken {
t.Fatal("fixture: the HA config is not broken")
}
- status, res := w.refused(t, "shop", false, func() (int, map[string]any) {
+ status, res := w.refused(t, "shop", haAllow{}, func() (int, map[string]any) {
return postJSON(t, newAppURL(w.consoleFixture), map[string]any{"name": "shop", "region": "us", "ha": true}, w.token)
})
wantHARefusal(t, "broken ha", status, res, http.StatusBadRequest, fmt.Sprintf(haCopyRegion, w.srv.offlinePlace("us")), "")
@@ -431,7 +474,7 @@ func TestAPICreateAppHABrokenConfig(t *testing.T) {
// home. The world's control (a plain create naming us) lands on ie.
func TestAPICreateAppHASwitchOff(t *testing.T) {
w := haRegionFixture(t, haRegionOpts{Accounts: []string{"bab"}, HistoryInactive: true})
- status, res := w.refused(t, "shop", false, func() (int, map[string]any) {
+ status, res := w.refused(t, "shop", haAllow{}, func() (int, map[string]any) {
return postJSON(t, newAppURL(w.consoleFixture), map[string]any{"name": "shop", "region": "us", "ha": true}, w.token)
})
wantHARefusal(t, "ha in us, switch off", status, res, http.StatusBadRequest, fmt.Sprintf(haCopyRegion, w.srv.offlinePlace("us")), "")
@@ -476,17 +519,27 @@ func TestAPICreateAppHARegionBinding(t *testing.T) {
t.Error("fixture: the region switch is still on after the invalidation")
}
}
- status, res := postJSON(t, newAppURL(w.consoleFixture), map[string]any{"name": "shop", "region": "us", "ha": true}, w.token)
+ post := func() (int, map[string]any) {
+ return postJSON(t, newAppURL(w.consoleFixture), map[string]any{"name": "shop", "region": "us", "ha": true}, w.token)
+ }
+ // The binding refusal comes after every read the claim makes before its
+ // insert, and the seam itself writes the invalidated mode: those, and
+ // nothing else. The seeded policy must survive (the clear comes after).
+ allow := haAllow{
+ Ops: slices.Concat(haRegionCheckOps(), haPlanCapOps("shop"), haClaimPrefixOps("shop"), []string{"put " + provision.HostsModeKey}),
+ Changed: []string{provision.HostsModeKey},
+ }
+ status, res := w.refused(t, "shop", allow, post)
if !fired {
t.Fatal("the seam did not run")
}
if status != http.StatusInternalServerError || errOf(res) != appCreateFailedMsg {
t.Errorf("HA create across an invalidation = %d %v, want 500 %q", status, res, appCreateFailedMsg)
}
- if has, err := w.st.HasAppIn(OpsAccountID, "shop"); err != nil || has {
- pool, region, _ := w.st.AppHAPool(context.Background(), OpsAccountID, "shop")
- t.Fatalf("HasAppIn = %v %v (pool %q region %q), want no row", has, err, pool, region)
+ if pool, region, err := w.st.AppHAPool(context.Background(), OpsAccountID, "shop"); err != nil || pool != "" || region != "" {
+ t.Errorf("AppHAPool = %q %q %v, want no live row", pool, region, err)
}
- status, res = postJSON(t, newAppURL(w.consoleFixture), map[string]any{"name": "shop", "region": "us", "ha": true}, w.token)
+ // The retry meets the door's own switch-off refusal, before any read.
+ status, res = w.refused(t, "shop", haAllow{}, post)
wantHARefusal(t, "retry, switch off", status, res, http.StatusBadRequest, fmt.Sprintf(haCopyRegion, w.srv.offlinePlace("us")), "")
}