Files
felhom-controller/controller/cmd/controller/after_install_wiring_test.go
T

62 lines
1.8 KiB
Go

package main
import (
"go/ast"
"go/parser"
"go/token"
"testing"
)
// v0.279.0 (decision 45): the deploy-done hook — the ONE place a fresh install ends — runs after_install, and
// the hook is set on every box, not only inside `if notifier != nil` (a box with no hub still installs apps).
// COMPANION RED-PROOF: move SetDeployDoneHook back inside `if notifier != nil` → the second assertion fails;
// drop the RunAfterInstall call → the first fails.
func TestAfterInstall_IsWiredToEveryFreshInstall(t *testing.T) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "main.go", nil, 0)
if err != nil {
t.Fatal(err)
}
var hook *ast.CallExpr
var insideNotifierIf bool
var walk func(n ast.Node, underNotifier bool)
walk = func(n ast.Node, underNotifier bool) {
ast.Inspect(n, func(x ast.Node) bool {
if ifs, ok := x.(*ast.IfStmt); ok {
if be, ok := ifs.Cond.(*ast.BinaryExpr); ok {
if id, ok := be.X.(*ast.Ident); ok && id.Name == "notifier" && be.Op == token.NEQ {
walk(ifs.Body, true)
if ifs.Else != nil {
walk(ifs.Else, underNotifier)
}
return false
}
}
}
if c, ok := x.(*ast.CallExpr); ok {
if s, ok := c.Fun.(*ast.SelectorExpr); ok && s.Sel.Name == "SetDeployDoneHook" && hook == nil {
hook, insideNotifierIf = c, underNotifier
}
}
return true
})
}
walk(f, false)
if hook == nil {
t.Fatal("SetDeployDoneHook is never called")
}
calls := false
ast.Inspect(hook, func(x ast.Node) bool {
if s, ok := x.(*ast.SelectorExpr); ok && s.Sel.Name == "RunAfterInstall" {
calls = true
}
return true
})
if !calls {
t.Fatal("the deploy-done hook does not run RunAfterInstall")
}
if insideNotifierIf {
t.Fatal("SetDeployDoneHook is set only when a notifier exists — a box with no hub would never run after_install")
}
}