carlos / platform Public

fix: managed gate tests under the CI sandbox

The package test seam answered the host's own "/" with the production
identity loader. InstallNamedUnits publishes under the real root, so on
the CI runner (bubblewrap, unprivileged uid, /etc owned by the overflow
uid) the strict loader read the runner as a managed box with an error and
refused to publish. Tests now see "/" as BYO and load fake roots with the
test uid from the fake root down; production stays strict.

TestFixedAdmissionNoHibernateEntryPoints follows RunAgent's new startup
decision: RunAgent must call decideAgentStartup on os.Getenv and the
production marker, the decision's BYO branch must run the same startup
check, and the BYO subtests now also drive decideAgentStartup.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 7eda50c1edf3a3e9a45b7c54d9f3d70a430de76b parent 1046b87
2 files changed, +90 −4
  • internal/host/group_fixed_entry_test.go +78 −0
  • internal/host/managedidentity_fake_test.go +12 −4
diff --git a/internal/host/group_fixed_entry_test.go b/internal/host/group_fixed_entry_test.go
index e2d5011e7..14a3b70bd 100644
--- a/internal/host/group_fixed_entry_test.go
+++ b/internal/host/group_fixed_entry_test.go
@@ -43,6 +43,14 @@ func TestFixedAdmissionNoHibernateEntryPoints(t *testing.T) {
return true
})
}
+ // Managed fleets 2b-2: RunAgent makes its managed/BYO decision
+ // first (decideAgentStartup, before any fatal group preflight).
+ // For a BYO box that decision runs this same startup check on the
+ // same environment and the production marker, which the subtests
+ // below exercise through decideAgentStartup itself.
+ if entry.name == "RunAgent" && !wired {
+ wired = callsDecideAgentStartup(t, fn(t, file, entry.name)) && byoDecisionRunsStartupCheck(t)
+ }
if !wired {
t.Fatal("entry no longer uses the tested startup check")
}
@@ -63,6 +71,14 @@ func TestFixedAdmissionNoHibernateEntryPoints(t *testing.T) {
if a := newFixedAdmission(get, admissionHealth()); a == nil || a.group {
t.Fatal("fixed admission changed")
}
+ if entry.name == "RunAgent" {
+ // The entry's decision on a BYO box: the same fixed
+ // startup, no managed join, no refusal.
+ st, err := decideAgentStartup(t.TempDir(), filepath.Join(t.TempDir(), "absent.json"), get, nil, t.Logf)
+ if err != nil || st.groupBoot != nil || st.join != nil {
+ t.Fatalf("BYO entry decision changed: %+v %v", st, err)
+ }
+ }
env["CARLOS_BOX_REFERENCE"] = "group"
if validateGroupSwitches(get) == nil {
t.Fatal("group accepted missing hibernation configuration")
@@ -76,3 +92,65 @@ func TestFixedAdmissionNoHibernateEntryPoints(t *testing.T) {
})
}
}
+
+// fn is the named function declaration in file.
+func fn(t *testing.T, file *ast.File, name string) *ast.FuncDecl {
+ t.Helper()
+ for _, decl := range file.Decls {
+ if f, ok := decl.(*ast.FuncDecl); ok && f.Name.Name == name {
+ return f
+ }
+ }
+ t.Fatalf("no func %s", name)
+ return nil
+}
+
+// callsDecideAgentStartup: the entry decides on the production marker and
+// the process environment.
+func callsDecideAgentStartup(t *testing.T, f *ast.FuncDecl) bool {
+ t.Helper()
+ found := false
+ ast.Inspect(f.Body, func(node ast.Node) bool {
+ call, ok := node.(*ast.CallExpr)
+ if !ok {
+ return true
+ }
+ if name, ok := call.Fun.(*ast.Ident); !ok || name.Name != "decideAgentStartup" || len(call.Args) != 5 {
+ return true
+ }
+ marker, ok1 := call.Args[1].(*ast.SelectorExpr)
+ getenv, ok2 := call.Args[2].(*ast.SelectorExpr)
+ if ok1 && ok2 && marker.Sel.Name == "GroupConfigPath" && getenv.Sel.Name == "Getenv" {
+ if pkg, ok := getenv.X.(*ast.Ident); ok && pkg.Name == "os" {
+ found = true
+ }
+ }
+ return true
+ })
+ return found
+}
+
+// byoDecisionRunsStartupCheck: decideAgentStartup's BYO branch runs the
+// tested startup check on its own environment and marker, and returns its
+// error to the entry (which is fatal there, as before).
+func byoDecisionRunsStartupCheck(t *testing.T) bool {
+ t.Helper()
+ file, err := parser.ParseFile(token.NewFileSet(), "managedstartup.go", nil, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ found := false
+ ast.Inspect(fn(t, file, "decideAgentStartup").Body, func(node ast.Node) bool {
+ call, ok := node.(*ast.CallExpr)
+ if !ok {
+ return true
+ }
+ if name, ok := call.Fun.(*ast.Ident); ok && name.Name == "loadGroupConfigurationAt" && len(call.Args) == 2 {
+ a, ok1 := call.Args[0].(*ast.Ident)
+ b, ok2 := call.Args[1].(*ast.Ident)
+ found = found || ok1 && ok2 && a.Name == "getenv" && b.Name == "marker"
+ }
+ return true
+ })
+ return found
+}
diff --git a/internal/host/managedidentity_fake_test.go b/internal/host/managedidentity_fake_test.go
index 36f048da9..861b4fce8 100644
--- a/internal/host/managedidentity_fake_test.go
+++ b/internal/host/managedidentity_fake_test.go
@@ -13,14 +13,22 @@ import (
// the real loader (expecting root) would read as an unsafe /etc: a managed
// box with an error, refused (ruling T3-Q2). For the package's tests a fake
// root is a root-owned tree: identity is loaded with the test's uid as the
-// expected owner, so a fake root without the identity file is BYO, as a
-// real root-owned BYO box is. "/" keeps the production loader. Tests of a
-// managed fake root stub managedIdentityAt themselves.
+// expected owner, from the fake root down, so a fake root without the
+// identity file is BYO, as a real root-owned BYO box is.
+//
+// The host's own "/" is never a test's box. Production paths that publish
+// under the real root (InstallNamedUnits via namedUnitRoot, the root pass
+// gate) would otherwise read the machine running the tests, and on the CI
+// runner, a bubblewrap sandbox with an unprivileged uid, "/etc" is owned by
+// the overflow uid: the strict loader rightly calls that a managed box with
+// an error and refuses to publish. Tests therefore see "/" as BYO; a test
+// of a managed box stubs managedIdentityAt with its own fake root. The
+// production loader and its strictness are unchanged (managedboot tests).
func init() {
uid := os.Getuid()
managedIdentityAt = func(root string) (managedboot.Identity, bool, error) {
if root == "" || filepath.Clean(root) == "/" {
- return managedboot.LoadIdentity(root)
+ return managedboot.Identity{}, false, nil
}
return managedboot.LoadIdentityAs(root, managedboot.RootOwner{UID: uid})
}