carlos / platform Public

tests: hold link-row writers to their own link's rows

000e10a9 held PutLinkChannel's prune, RevokeLinkChannel and
DeleteLinkForward only to kind = 'forward', so a writer broadened to every
link-placed row would have passed.

links_writers_test.go runs each writer against one link in a registry that
also holds another link's rows (same and other account), a fleet row and
ordinary rows (one with the link's placement in its region column), and
asserts through Get that exactly the intended rows changed and every other
row is byte-identical.

TestRouteWriterInventoryIsComplete now also requires each link-row writer
statement to be constant, hold no OR, and carry a region term bound to the
writer's own link ID ('link/' || id, or LinkPlacementPrefix + id), read from
the call's arguments.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 183b5c10f4d2894db43089f7840c0d9d850d0dd2 parent 000e10a
2 files changed, +280 −16
  • internal/registry/forward_inventory_test.go +68 −16
  • internal/registry/links_writers_test.go +212 −0

CI failed — run details

diff --git a/internal/registry/forward_inventory_test.go b/internal/registry/forward_inventory_test.go
index c8010ddea..bd0907356 100644
--- a/internal/registry/forward_inventory_test.go
+++ b/internal/registry/forward_inventory_test.go
@@ -1,6 +1,7 @@
package registry
import (
+ "fmt"
"go/ast"
"go/constant"
"go/importer"
@@ -26,15 +27,58 @@ var forwardRowWriters = map[string]bool{
// linkRowWriters are linked deployments' writers of link-placed forward
// rows (Region "link/<id>"), beside the five exceptions: they delete forward
// rows by design, so the writer table (which asks a writer to leave a forward
-// row alone) cannot hold them. Every routes statement in one of them must
-// carry kind = 'forward', so it never touches an ordinary row; the link class
-// (only rows placed on that link, never a fleet row) is held by
-// TestForwardClassesDisjoint, TestPutLinkChannelOneWriter and
-// TestLinkForwardNeedsActiveChannel in links_test.go.
-var linkRowWriters = map[string]string{
- "PutLinkChannel": "prunes the link's forward rows whose account no longer holds the active channel, in the transaction that applied a newer generation (pruneLinkForwardsSQL)",
- "RevokeLinkChannel": "deletes every forward row placed on the link, in the transaction that marks its channel revoked",
- "DeleteLinkForward": "deletes one host's row while it is a forward row placed exactly on the link (unpublish); DeleteForward refuses link rows",
+// row alone) cannot hold them. Each routes statement in one of them must be a
+// constant (so the scan reads it), one statement with no OR, carrying
+// kind = 'forward' and a WHERE or AND term region = 'link/' || ? bound to
+// the writer's link ID expression (id), or region = ? bound to
+// LinkPlacementPrefix + id: so it never touches an ordinary row nor a row
+// placed anywhere but its own link. links_writers_test.go holds the rest by
+// behaviour: run against one link, each writer leaves every other link's
+// rows, fleet rows and ordinary rows byte-identical.
+var linkRowWriters = map[string]linkRowWriter{
+ "PutLinkChannel": {"c.LinkID", "prunes the link's forward rows whose account no longer holds the active channel, in the transaction that applied a newer generation (pruneLinkForwardsSQL)"},
+ "RevokeLinkChannel": {"linkID", "deletes every forward row placed on the link, in the transaction that marks its channel revoked"},
+ "DeleteLinkForward": {"linkID", "deletes one host's row while it is a forward row placed exactly on the link (unpublish); DeleteForward refuses link rows"},
+}
+
+type linkRowWriter struct {
+ id string // the writer's link ID, as source (types.ExprString)
+ why string
+}
+
+// linkRegionTerm is a link-row writer's region constraint: a WHERE or AND
+// term region = 'link/' || ? (group 2 set) or region = ? on the outer table.
+var linkRegionTerm = regexp.MustCompile(`(?i)\b(where|and)\s+region\s*=\s*('link/'\s*\|\|\s*)?\?`)
+
+// checkLinkRowWrite is "" when s, a link-row writer's routes statement,
+// carries kind = 'forward' and a region term bound to w.id, and otherwise
+// says what it lacks.
+func checkLinkRowWrite(s routeSQLSite, w linkRowWriter) string {
+ text := strings.TrimSpace(s.text)
+ switch {
+ case !regexp.MustCompile(`(?i)\bkind\s*=\s*'forward'`).MatchString(text):
+ return "does not carry kind = 'forward'"
+ case regexp.MustCompile(`(?i)\bor\b`).MatchString(text):
+ return "holds an OR, so its kind and region terms may not bind every row it writes"
+ case strings.Contains(strings.TrimSuffix(text, ";"), ";"):
+ return "holds more than one statement"
+ }
+ m := linkRegionTerm.FindStringSubmatchIndex(text)
+ if m == nil {
+ return "carries no WHERE or AND term region = 'link/' || ? or region = ?"
+ }
+ arg := strings.Count(text[:m[1]], "?") - 1
+ if arg >= len(s.args) {
+ return fmt.Sprintf("binds region to placeholder %d of %d arguments", arg+1, len(s.args))
+ }
+ want := "LinkPlacementPrefix + " + w.id
+ if m[4] >= 0 {
+ want = w.id
+ }
+ if s.args[arg] != want {
+ return fmt.Sprintf("binds region to %s; want %s, the writer's own link", s.args[arg], want)
+ }
+ return ""
}
// unresolvedSQL classifies one SQL argument with no constant value.
@@ -85,6 +129,7 @@ var sqlMethods = map[string]int{
type routeSQLSite struct {
file, fn, text string
+ args []string // the SQL's bound arguments, as source
pos token.Position
}
@@ -169,7 +214,11 @@ func routeSQLSites(t *testing.T) (sites []routeSQLSite, unresolved map[string][]
pos := fset.Position(arg.Pos())
switch {
case routeWriteSQL.MatchString(text):
- sites = append(sites, routeSQLSite{file: file, fn: fn, text: text, pos: pos})
+ var args []string
+ for _, a := range call.Args[idx+1:] {
+ args = append(args, types.ExprString(a))
+ }
+ sites = append(sites, routeSQLSite{file: file, fn: fn, text: text, args: args, pos: pos})
case !whole:
key := file + ":" + fn
unresolved[key] = append(unresolved[key], pos)
@@ -185,7 +234,7 @@ func routeSQLSites(t *testing.T) (sites []routeSQLSite, unresolved map[string][]
// table whole: every statement in internal/registry that writes routes sits in
// a function that some entry of the table reaches (its encloses list), or in
// one of the five exceptions, or in a link-row writer whose statements carry
-// kind = 'forward'. Every other UPDATE, DELETE and upsert on routes carries
+// kind = 'forward' and a region term bound to its own link (checkLinkRowWrite). Every other UPDATE, DELETE and upsert on routes carries
// kind <> 'forward' in its text. A SQL argument with no constant value is
// classified in unresolvedRouteSQL.
func TestRouteWriterInventoryIsComplete(t *testing.T) {
@@ -213,20 +262,23 @@ func TestRouteWriterInventoryIsComplete(t *testing.T) {
}
fn := k[strings.Index(k, ":")+1:]
seen[fn] = true
- if !forwardRowWriters[fn] && linkRowWriters[fn] == "" && len(reached[fn]) == 0 {
+ if _, ok := linkRowWriters[fn]; ok {
+ t.Errorf("%s (%v): link-row writer %s holds a routes write the scan cannot read; it must be a constant so its region term is checked", k, unresolved[k], fn)
+ continue
+ }
+ if !forwardRowWriters[fn] && len(reached[fn]) == 0 {
t.Errorf("%s (%v): a routes write classified in unresolvedRouteSQL, and no entry of the writer table reaches %s (nor is it one of the five exceptions)", k, unresolved[k], fn)
}
}
guard := regexp.MustCompile(`(?i)\bkind\s*<>\s*'forward'`)
- linkGuard := regexp.MustCompile(`(?i)\bkind\s*=\s*'forward'`)
for _, s := range sites {
seen[s.fn] = true
if forwardRowWriters[s.fn] {
continue
}
- if linkRowWriters[s.fn] != "" {
- if !linkGuard.MatchString(s.text) {
- t.Errorf("%s: %s is a link-row writer and its routes statement does not carry kind = 'forward':\n %q", s.pos, s.fn, s.text)
+ if w, ok := linkRowWriters[s.fn]; ok {
+ if why := checkLinkRowWrite(s, w); why != "" {
+ t.Errorf("%s: %s is a link-row writer and its routes statement %s:\n %q", s.pos, s.fn, why, s.text)
}
continue
}
diff --git a/internal/registry/links_writers_test.go b/internal/registry/links_writers_test.go
new file mode 100644
index 000000000..5a347510b
--- /dev/null
+++ b/internal/registry/links_writers_test.go
@@ -0,0 +1,212 @@
+package registry
+
+import (
+ "reflect"
+ "slices"
+ "testing"
+)
+
+// The three writers of link-placed forward rows (PutLinkChannel's prune,
+// RevokeLinkChannel, DeleteLinkForward) delete forward rows by design, so
+// TestRouteWriterInventoryIsComplete holds them only to kind = 'forward' and
+// a region bound to their own link. These tests hold the rest: run against
+// link L1, a writer changes exactly the L1 rows it means to, and every other
+// row (another link's of the same account or another, a fleet row, an
+// ordinary row, even one whose region column reads "link/<L1>") reads back
+// byte-identical through Get.
+
+// testLinkC is a third link, owned by another account than testLinkA's.
+const testLinkC = "c0ffee000001"
+
+const (
+ hostL1Shop = "shop.acct1.apps.example.com"
+ hostL1Blog = "blog.acct1.apps.example.com"
+ hostL2 = "docs.acct1.apps.example.com"
+ hostL3 = "shop.acct2.apps.example.com"
+ hostFleet = "shop.example.com"
+ hostPlain = "blog.example.com"
+ hostPlaced = "api.example.com"
+ hostL1Acct2 = "api.acct2.apps.example.com"
+)
+
+// newLinkWriterRegistry holds link L1 (testLinkA, acct1) with two forward
+// rows; link L2 (testLinkB) of the same account with one; link L3
+// (testLinkC, acct2) with one; a fleet forward row of acct1; an ordinary
+// row; and an ordinary row whose region column carries L1's placement.
+func newLinkWriterRegistry(t *testing.T) *Registry {
+ t.Helper()
+ reg := openTestRegistry(t)
+ putLink(t, reg, activeLink(testLinkA, "acct1", 1))
+ putLink(t, reg, activeLink(testLinkB, "acct1", 1))
+ l3 := activeLink(testLinkC, "acct2", 1)
+ l3.TokenHash = testHash('b')
+ putLink(t, reg, l3)
+ for _, rt := range []Route{
+ linkForward(hostL1Shop, "acct1", testLinkA),
+ linkForward(hostL1Blog, "acct1", testLinkA),
+ linkForward(hostL2, "acct1", testLinkB),
+ linkForward(hostL3, "acct2", testLinkC),
+ } {
+ if err := reg.PutForward(rt); err != nil {
+ t.Fatalf("PutForward(%s) = %v", rt.Host, err)
+ }
+ }
+ fleet := validForward(hostFleet, "home")
+ fleet.Account = "acct1"
+ if err := reg.PutForward(fleet); err != nil {
+ t.Fatal(err)
+ }
+ for _, rt := range []Route{
+ {Host: hostPlain, Kind: "instance", App: "blog", Account: "acct1", Socket: "/s/blog"},
+ {Host: hostPlaced, Kind: "instance", App: "shop", Account: "acct1", Socket: "/s/shop", Region: LinkPlacementPrefix + testLinkA},
+ } {
+ if err := reg.Upsert(rt); err != nil {
+ t.Fatalf("Upsert(%s) = %v", rt.Host, err)
+ }
+ }
+ if got, err := reg.Get(hostPlaced); err != nil || got == nil || got.Region != LinkPlacementPrefix+testLinkA || got.IsForward() {
+ t.Fatalf("fixture: ordinary row on L1's region = %+v, %v", got, err)
+ }
+ return reg
+}
+
+// routeSnapshot is every row's Get, keyed by host.
+func routeSnapshot(t *testing.T, reg *Registry) map[string]*Route {
+ t.Helper()
+ all, err := reg.List()
+ if err != nil {
+ t.Fatal(err)
+ }
+ out := map[string]*Route{}
+ for _, rt := range all {
+ got, err := reg.Get(rt.Host)
+ if err != nil {
+ t.Fatal(err)
+ }
+ out[rt.Host] = got
+ }
+ return out
+}
+
+// assertOnlyChanged fails unless the hosts whose Get differs between
+// before and reg now (added, deleted or changed) are exactly want, and
+// every other host is byte-identical.
+func assertOnlyChanged(t *testing.T, step string, before map[string]*Route, reg *Registry, want ...string) map[string]*Route {
+ t.Helper()
+ after := routeSnapshot(t, reg)
+ var changed []string
+ for h, b := range before {
+ if !reflect.DeepEqual(b, after[h]) {
+ changed = append(changed, h)
+ }
+ }
+ for h := range after {
+ if _, ok := before[h]; !ok {
+ changed = append(changed, h)
+ }
+ }
+ slices.Sort(changed)
+ slices.Sort(want)
+ if !slices.Equal(changed, want) {
+ t.Errorf("%s: changed rows %v; want exactly %v", step, changed, want)
+ for _, h := range changed {
+ t.Logf(" %s: before %+v\n after %+v", h, before[h], after[h])
+ }
+ }
+ return after
+}
+
+func assertGone(t *testing.T, reg *Registry, hosts ...string) {
+ t.Helper()
+ for _, h := range hosts {
+ if got, err := reg.Get(h); err != nil || got != nil {
+ t.Errorf("%s still present: %+v, %v", h, got, err)
+ }
+ }
+}
+
+func TestPutLinkChannelPrunesOnlyItsLink(t *testing.T) {
+ reg := newLinkWriterRegistry(t)
+ snap := routeSnapshot(t, reg)
+ if len(snap) != 7 {
+ t.Fatalf("fixture holds %d rows; want 7", len(snap))
+ }
+
+ // A token rotation keeps the account: nothing is pruned.
+ rotated := activeLink(testLinkA, "acct1", 2)
+ rotated.TokenHash = testHash('c')
+ putLink(t, reg, rotated)
+ snap = assertOnlyChanged(t, "rotation", snap, reg)
+
+ // A stale write does not apply and prunes nothing.
+ if applied, err := reg.PutLinkChannel(activeLink(testLinkA, "acct2", 1)); err != nil || applied {
+ t.Fatalf("stale write = %v, %v; want not applied", applied, err)
+ }
+ snap = assertOnlyChanged(t, "stale write", snap, reg)
+
+ // L1 moves to acct2: its acct1 rows go; L2's acct1 row, L3's acct2 row,
+ // the acct1 fleet row and both ordinary rows stay.
+ putLink(t, reg, activeLink(testLinkA, "acct2", 3))
+ snap = assertOnlyChanged(t, "account change", snap, reg, hostL1Shop, hostL1Blog)
+ assertGone(t, reg, hostL1Shop, hostL1Blog)
+
+ // An acct2 row on L1, then a write that revokes L1: only that row goes.
+ if err := reg.PutForward(linkForward(hostL1Acct2, "acct2", testLinkA)); err != nil {
+ t.Fatal(err)
+ }
+ snap = routeSnapshot(t, reg)
+ revoked := activeLink(testLinkA, "acct2", 4)
+ revoked.Status = "revoked"
+ putLink(t, reg, revoked)
+ assertOnlyChanged(t, "revoking write", snap, reg, hostL1Acct2)
+ assertGone(t, reg, hostL1Acct2)
+}
+
+func TestRevokeLinkChannelDeletesOnlyItsLink(t *testing.T) {
+ reg := newLinkWriterRegistry(t)
+ snap := routeSnapshot(t, reg)
+
+ // A link ID with no channel and no rows: recorded revoked, no row moves.
+ if _, err := reg.RevokeLinkChannel("dddddddddddd"); err != nil {
+ t.Fatal(err)
+ }
+ snap = assertOnlyChanged(t, "revoke an unknown link", snap, reg)
+
+ if revoked, err := reg.RevokeLinkChannel(testLinkA); err != nil || !revoked {
+ t.Fatalf("RevokeLinkChannel(L1) = %v, %v", revoked, err)
+ }
+ snap = assertOnlyChanged(t, "revoke L1", snap, reg, hostL1Shop, hostL1Blog)
+ assertGone(t, reg, hostL1Shop, hostL1Blog)
+
+ if revoked, err := reg.RevokeLinkChannel(testLinkA); err != nil || revoked {
+ t.Fatalf("second RevokeLinkChannel(L1) = %v, %v; want false", revoked, err)
+ }
+ assertOnlyChanged(t, "revoke L1 again", snap, reg)
+}
+
+func TestDeleteLinkForwardDeletesOnlyItsRow(t *testing.T) {
+ reg := newLinkWriterRegistry(t)
+ snap := routeSnapshot(t, reg)
+
+ // Against L1, no host that is not one of L1's forward rows is deleted:
+ // another link's row, a fleet row, an ordinary row (one of them on L1's
+ // region), an absent host.
+ for _, h := range []string{hostL2, hostL3, hostFleet, hostPlain, hostPlaced, "absent.example.com"} {
+ if deleted, err := reg.DeleteLinkForward(h, testLinkA); err != nil || deleted {
+ t.Errorf("DeleteLinkForward(%s, L1) = %v, %v; want false", h, deleted, err)
+ }
+ snap = assertOnlyChanged(t, "DeleteLinkForward("+h+", L1)", snap, reg)
+ }
+ // Nor is one of L1's rows deleted against another link.
+ if deleted, err := reg.DeleteLinkForward(hostL1Shop, testLinkB); err != nil || deleted {
+ t.Errorf("DeleteLinkForward(L1 row, L2) = %v, %v; want false", deleted, err)
+ }
+ snap = assertOnlyChanged(t, "DeleteLinkForward(L1 row, L2)", snap, reg)
+
+ // Its own row goes, and only that row: L1's other row stays.
+ if deleted, err := reg.DeleteLinkForward(hostL1Shop, testLinkA); err != nil || !deleted {
+ t.Fatalf("DeleteLinkForward(L1 row, L1) = %v, %v; want true", deleted, err)
+ }
+ assertOnlyChanged(t, "DeleteLinkForward(L1 row, L1)", snap, reg, hostL1Shop)
+ assertGone(t, reg, hostL1Shop)
+}