Files
Pulse/scripts/installtests/native_installer_fixture_tools_test.go
pulse-triage[bot] d81401f29b Repair native installer CI portability without weakening safety
Load the real service-status enum in the mocked Windows SCM controls and check lifecycle statements independently of checkout line endings. Keep Linux-only server installer fixtures on supported Bash and GNU metadata tools within their macOS test step, after native agent runtime testing. Partition the discovered installer inventory so the expanded reset matrix retains its own existing package deadline; preserve every check, failure exit and per-fixture deadline.

Change-source: pulse-maintainer
Contract-Neutral: Native CI fixture environment and inventory repair only; product installers, native lifecycle, safety contracts and fixture deadlines are unchanged.
2026-10-09 15:55:04 +01:00

186 lines
6.5 KiB
Go

//go:build !windows
package installtests
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"gopkg.in/yaml.v3"
)
func nativeInstallerFixtureSteps(t *testing.T) (nativeGuidanceStep, nativeGuidanceStep) {
t.Helper()
content, err := os.ReadFile(repoFile(".github", "workflows", "unified-agent-native.yml"))
if err != nil {
t.Fatal(err)
}
var workflow struct {
Jobs map[string]struct{ Steps []nativeGuidanceStep }
}
if err := yaml.Unmarshal(content, &workflow); err != nil {
t.Fatal(err)
}
steps := workflow.Jobs["native-agent"].Steps
runtimeAt, supplyAt, installerAt := -1, -1, -1
for i, step := range steps {
switch step.Name {
case "Test native agent runtime":
runtimeAt = i
case "Supply Linux server installer fixture tools on macOS":
supplyAt = i
case "Test native Unix installer contracts":
installerAt = i
}
}
if runtimeAt < 0 || supplyAt <= runtimeAt || installerAt <= supplyAt {
t.Fatal("Linux fixture tool supply must follow native runtime testing and precede Unix installer checks")
}
supply, installer := steps[supplyAt], steps[installerAt]
if supply.If != "runner.os == 'macOS'" || supply.Shell != "bash" || installer.If != "matrix.unix" || installer.Shell != "bash" ||
strings.Contains(supply.Run, "GITHUB_PATH") || !strings.Contains(installer.Run, `export PATH="$PULSE_INSTALLER_FIXTURE_BIN:$PATH"`) ||
!strings.Contains(installer.Run, "python3 scripts/installtests/run_unix_contracts.py") {
t.Fatal("Linux tools must stay local to complete Unix installer qualification")
}
return supply, installer
}
func TestNativeInstallerFixtureToolsAreRealAndStepLocal(t *testing.T) {
supply, installer := nativeInstallerFixtureSteps(t)
for _, scenario := range []string{"real tools", "install failure", "missing bash", "legacy bash", "missing stat", "non-GNU chmod"} {
t.Run(scenario, func(t *testing.T) {
root := t.TempDir()
bin, prefix, runnerTemp := filepath.Join(root, "bin"), filepath.Join(root, "brew with spaces"), filepath.Join(root, "runner temp")
for _, path := range []string{bin, filepath.Join(prefix, "bin"), runnerTemp} {
if err := os.MkdirAll(path, 0700); err != nil {
t.Fatal(err)
}
}
brew := `#!/bin/sh
printf '%s\n' "$*" >> "$BREW_CALLS"
case "$*" in
'install bash') exit "${BREW_EXIT:-0}" ;;
'--prefix bash'|'--prefix coreutils') printf '%s\n' "$TOOLS_PREFIX" ;;
*) exit 64 ;;
esac
`
if err := os.WriteFile(filepath.Join(bin, "brew"), []byte(brew), 0700); err != nil {
t.Fatal(err)
}
for _, name := range []string{"bash", "stat", "chmod", "chown"} {
if scenario == "missing "+name {
continue
}
leaf := name
if name != "bash" {
leaf = "g" + name
}
path := filepath.Join(prefix, "bin", leaf)
if (scenario == "legacy bash" && name == "bash") || (scenario == "non-GNU chmod" && name == "chmod") {
fake := "#!/bin/sh\nprintf 'BSD utility\\n'\n"
if name == "bash" {
fake = "#!/bin/sh\nexit 1\n"
}
if err := os.WriteFile(path, []byte(fake), 0700); err != nil {
t.Fatal(err)
}
} else {
realTool, err := exec.LookPath(name)
if err != nil {
t.Fatal(err)
}
if err := os.Symlink(realTool, path); err != nil {
t.Fatal(err)
}
}
}
envFile, calls := filepath.Join(root, "github-env"), filepath.Join(root, "brew-calls")
installExit := "0"
if scenario == "install failure" {
installExit = "23"
}
env := append(os.Environ(), "PATH="+bin+":"+os.Getenv("PATH"), "TOOLS_PREFIX="+prefix,
"BREW_CALLS="+calls, "BREW_EXIT="+installExit, "RUNNER_TEMP="+runnerTemp, "GITHUB_ENV="+envFile)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "bash", "-c", supply.Run)
cmd.Env = env
output, runErr := cmd.CombinedOutput()
if ctx.Err() != nil {
t.Fatalf("fixture prerequisite deadline: %v", ctx.Err())
}
if scenario != "real tools" {
want := 1
if scenario == "install failure" {
want = 23
}
exit, ok := runErr.(*exec.ExitError)
if !ok || exit.ExitCode() != want {
t.Fatalf("prerequisite exit %v, want %d: %s", runErr, want, output)
}
if _, err := os.Stat(envFile); !os.IsNotExist(err) {
t.Fatal("failed prerequisite exported an executable path")
}
return
}
if runErr != nil {
t.Fatalf("real tool supply: %v: %s", runErr, output)
}
data, err := os.ReadFile(envFile)
if err != nil {
t.Fatal(err)
}
fixtureBin := strings.TrimSuffix(strings.TrimPrefix(string(data), "PULSE_INSTALLER_FIXTURE_BIN="), "\n")
if filepath.Dir(fixtureBin) != runnerTemp {
t.Fatal("fixture tools escaped the owned runner directory")
}
entries, err := os.ReadDir(fixtureBin)
if err != nil || len(entries) != 4 {
t.Fatal("fixture supply must select only Bash/stat/chmod/chown")
}
for _, name := range []string{"bash", "stat", "chmod", "chown"} {
leaf := name
if name != "bash" {
leaf = "g" + name
}
if target, err := os.Readlink(filepath.Join(fixtureBin, name)); err != nil || target != filepath.Join(prefix, "bin", leaf) {
t.Fatalf("%s did not bind the verified installed executable", name)
}
}
if data, err := os.ReadFile(calls); err != nil || string(data) != "install bash\n--prefix bash\n--prefix coreutils\n" {
t.Fatal("fixture supply changed its fixed package/resource selection")
}
// Exercise the actual workflow step's local PATH boundary, replacing
// only Python's containing suite to avoid recursively starting Go.
probe := `#!/bin/sh
test "$*" = 'scripts/installtests/run_unix_contracts.py' || exit 91
test "$(command -v bash)" = "$PULSE_INSTALLER_FIXTURE_BIN/bash" || exit 92
for tool in stat chmod chown; do
test "$(command -v "$tool")" = "$PULSE_INSTALLER_FIXTURE_BIN/$tool" || exit 93
done
exit "${SUITE_EXIT:-0}"
`
if err := os.WriteFile(filepath.Join(bin, "python3"), []byte(probe), 0700); err != nil {
t.Fatal(err)
}
for _, code := range []string{"0", "23"} {
cmd := exec.CommandContext(ctx, "bash", "-c", installer.Run)
cmd.Env = append(env, "PULSE_INSTALLER_FIXTURE_BIN="+fixtureBin, "SUITE_EXIT="+code)
output, err := cmd.CombinedOutput()
if code == "0" && err != nil {
t.Fatalf("fixture-local path did not reach the suite: %v: %s", err, output)
} else if code == "23" {
if exit, ok := err.(*exec.ExitError); !ok || exit.ExitCode() != 23 {
t.Fatalf("containing suite failure hidden: %v: %s", err, output)
}
}
}
})
}
}