| 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 |
| +} |