Pulse/scripts/installtests/native_installer_fixture_tools_test.go
pulse-triage[bot] 89390faff2 Supply GNU sed to the existing macOS Linux-installer fixtures
Native jobs 129147427240 and 129147427594 fail the no-jq updater controls because BSD sed rejects the Linux in-place edit. Supply and verify the actual GNU editor only in the existing fixture PATH, keeping native agent tools unchanged. Exercise missing/non-GNU refusal and actual two-field edits alongside the current tool-boundary controls.

Contract-Neutral: Repairs the native workflow fixture prerequisite without changing product behaviour, native agent tools, gating coverage or service authority.
Change-source: pulse-maintainer
2026-11-09 12:46:27 +00:00

449 lines
19 KiB
Go

//go:build !windows
package installtests
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"gopkg.in/yaml.v3"
)
func nativeInstallerPhaseRun(phase string) string {
return `set -euo pipefail
if [[ -n "${PULSE_INSTALLER_FIXTURE_BIN:-}" ]]; then
export PATH="$PULSE_INSTALLER_FIXTURE_BIN:$PATH"
fi
python3 scripts/installtests/run_unix_contracts.py --phase ` + phase
}
func validateNativeInstallerPhaseOrder(steps []nativeGuidanceStep) error {
indices := map[string]int{}
names := []string{
"Set up Go",
"Supply GNU timeout for macOS guidance checks",
"Verify native Unix timeout prerequisite",
"Supply Linux server installer fixture tools on macOS",
"Test NAS removal contracts before frontend preparation",
"Set up Node.js for native runtime test assets",
"Test native agent runtime",
"Test native Unix installer contracts",
}
for i, step := range steps {
for _, name := range names {
if step.Name == name {
if _, exists := indices[name]; exists {
return fmt.Errorf("native installer phase step must run once: %s", name)
}
indices[name] = i
}
}
}
previous := -1
for _, name := range names {
index, exists := indices[name]
if !exists || index <= previous {
return fmt.Errorf("native installer preparation and both phases must stay in order: %s", name)
}
previous = index
}
supply := steps[indices[names[3]]]
if supply.If != "runner.os == 'macOS'" || supply.Shell != "bash" || supply.ContinueOnError ||
supply.WorkingDirectory != "" || strings.Contains(supply.Run, "GITHUB_PATH") {
return fmt.Errorf("Linux fixture tools must not replace the native runtime tools or hide a failure")
}
for name, phase := range map[string]string{names[4]: "preflight", names[7]: "remaining"} {
step := steps[indices[name]]
if step.If != "matrix.unix" || step.Shell != "bash" || step.ContinueOnError || step.WorkingDirectory != "" ||
strings.TrimSpace(step.Run) != nativeInstallerPhaseRun(phase) {
return fmt.Errorf("both native installer phases must execute the exact fail-closed runner with step-local tools: %s", name)
}
}
return nil
}
func nativeInstallerFixtureSteps(t *testing.T) (nativeGuidanceStep, 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
if err := validateNativeInstallerPhaseOrder(steps); err != nil {
t.Fatal(err)
}
var supply, preflight, installer nativeGuidanceStep
for _, step := range steps {
switch step.Name {
case "Supply Linux server installer fixture tools on macOS":
supply = step
case "Test NAS removal contracts before frontend preparation":
preflight = step
case "Test native Unix installer contracts":
installer = step
}
}
return supply, preflight, installer
}
func TestNativeInstallerPhasesKeepNASBeforeFrontendAndAllChecksGating(t *testing.T) {
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
if err := validateNativeInstallerPhaseOrder(steps); err != nil {
t.Fatal(err)
}
indices := map[string]int{}
for i, step := range steps {
indices[step.Name] = i
}
early := indices["Test NAS removal contracts before frontend preparation"]
remaining := indices["Test native Unix installer contracts"]
for _, scenario := range []string{"missing preflight", "missing remaining", "duplicate preflight", "after Node", "before tools", "Linux only", "hidden early failure", "hidden remaining failure", "all checks repeated", "no remaining checks", "nonlocal tools", "wrong directory"} {
t.Run(scenario, func(t *testing.T) {
changed := append([]nativeGuidanceStep(nil), steps...)
switch scenario {
case "missing preflight":
changed = append(changed[:early], changed[early+1:]...)
case "missing remaining":
changed = append(changed[:remaining], changed[remaining+1:]...)
case "duplicate preflight":
changed = append(changed, changed[early])
case "after Node":
i := indices["Set up Node.js for native runtime test assets"]
changed[early], changed[i] = changed[i], changed[early]
case "before tools":
i := indices["Supply Linux server installer fixture tools on macOS"]
changed[early], changed[i] = changed[i], changed[early]
case "Linux only":
changed[early].If = "runner.os == 'Linux'"
case "hidden early failure":
changed[early].ContinueOnError = true
case "hidden remaining failure":
changed[remaining].ContinueOnError = true
case "all checks repeated":
changed[remaining].Run = nativeInstallerPhaseRun("all")
case "no remaining checks":
changed[remaining].Run = nativeInstallerPhaseRun("preflight")
case "nonlocal tools":
i := indices["Supply Linux server installer fixture tools on macOS"]
changed[i].Run += "\nprintf '%s\\n' \"$fixture_bin\" >> \"$GITHUB_PATH\"\n"
case "wrong directory":
changed[early].WorkingDirectory = "frontend-modern"
}
if err := validateNativeInstallerPhaseOrder(changed); err == nil {
t.Fatalf("accepted %s native installer phase", scenario)
}
})
}
}
// nativeVersionStreamTool preserves real tool behavior while making its version
// producer outlive an early matching grep. A matched but failed producer must
// still fail the prerequisite under pipefail.
func nativeVersionStreamTool(t *testing.T, path, realTool string, exitCode int) {
t.Helper()
python, err := exec.LookPath("python3")
if err != nil {
t.Fatal(err)
}
quote := func(value string) string {
return "'" + strings.ReplaceAll(value, "'", "'\"'\"'") + "'"
}
script := fmt.Sprintf(`#!/bin/sh
set -e
if [ "${1:-}" = --version ]; then
%s "$@"
%s -c 'import os, signal; signal.signal(signal.SIGPIPE, signal.SIG_DFL); data = b"version continuation\n" * 256; [os.write(1, data) for _ in range(64)]'
exit %d
fi
exec %s "$@"
`, quote(realTool), quote(python), exitCode, quote(realTool))
if err := os.WriteFile(path, []byte(script), 0700); err != nil {
t.Fatal(err)
}
}
// Give each command its own budget. In particular, the complete Python
// transfer suite must not consume the later shell probes' time on a slower
// native runner. A deadline is a fixture failure, never the expected exit 23.
func runNativeInstallerFixtureCommand(t *testing.T, limit time.Duration, env []string, program string, args ...string) ([]byte, error) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), limit)
defer cancel()
cmd := exec.CommandContext(ctx, program, args...)
cmd.Env = env
output, err := cmd.CombinedOutput()
if ctx.Err() != nil {
t.Fatalf("%s fixture command exceeded its %s budget: %v\n%s", program, limit, ctx.Err(), output)
}
return output, err
}
func TestNativeInstallerFixtureToolsAreRealAndStepLocal(t *testing.T) {
supply, preflight, installer := nativeInstallerFixtureSteps(t)
for _, scenario := range []string{"real tools", "symlinked fixture root", "streamed version output", "failed version producer", "install failure", "missing bash", "legacy bash", "missing stat", "non-GNU chmod", "missing cp", "non-GNU cp", "missing mv", "non-GNU mv", "missing ln", "non-GNU ln", "missing readlink", "non-GNU readlink", "missing sha256sum", "non-GNU sha256sum", "missing tar", "non-GNU tar", "missing awk", "non-GNU awk", "missing flock", "missing sed", "non-GNU sed"} {
t.Run(scenario, func(t *testing.T) {
root := t.TempDir()
if scenario == "symlinked fixture root" {
// macOS temporary roots can be reached through /var -> /private/var.
// Reproduce that legitimate alias without requiring a macOS host.
alias := filepath.Join(t.TempDir(), "alias with spaces")
if err := os.Symlink(root, alias); err != nil {
t.Fatal(err)
}
root = alias
}
bin, prefix, tarPrefix, runnerTemp := filepath.Join(root, "bin"), filepath.Join(root, "brew with spaces"), filepath.Join(root, "gnu tar with spaces"), filepath.Join(root, "runner temp")
for _, path := range []string{bin, filepath.Join(prefix, "bin"), filepath.Join(tarPrefix, "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 gnu-tar gawk flock gnu-sed') exit "${BREW_EXIT:-0}" ;;
'--prefix bash'|'--prefix coreutils') printf '%s\n' "$TOOLS_PREFIX" ;;
'--prefix gnu-tar') printf '%s\n' "$TAR_PREFIX" ;;
'--prefix gawk'|'--prefix flock'|'--prefix gnu-sed') printf '%s\n' "$TOOLS_PREFIX" ;;
*) exit 64 ;;
esac
`
if err := os.WriteFile(filepath.Join(bin, "brew"), []byte(brew), 0700); err != nil {
t.Fatal(err)
}
tools := []string{"bash", "stat", "chmod", "chown", "cp", "mv", "ln", "readlink", "sha256sum", "tar", "awk", "flock", "sed"}
installed := make(map[string]string)
for _, name := range tools {
leaf, toolPrefix := name, prefix
if name != "bash" && name != "flock" {
leaf = "g" + name
}
if name == "tar" {
toolPrefix = tarPrefix
}
path := filepath.Join(toolPrefix, "bin", leaf)
installed[name] = path
if scenario == "missing "+name {
continue
}
if (scenario == "legacy bash" && name == "bash") || scenario == "non-GNU "+name {
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 {
realName := name
if name == "awk" {
realName = "gawk"
}
realTool, err := exec.LookPath(realName)
if err != nil {
t.Fatal(err)
}
if scenario == "streamed version output" && name != "bash" {
nativeVersionStreamTool(t, path, realTool, 0)
} else if scenario == "failed version producer" && name == "stat" {
nativeVersionStreamTool(t, path, realTool, 23)
} else if err := os.Symlink(realTool, path); err != nil {
t.Fatal(err)
}
}
}
// Native macOS defaults must not be the fallback for Linux-only
// archive extraction, canonicalization or atomic replacement. Real
// GNU tools must win even when the native names are unsuitable.
for _, name := range []string{"cp", "mv", "readlink", "sha256sum", "tar", "awk", "flock", "sed"} {
if err := os.WriteFile(filepath.Join(bin, name), []byte("#!/bin/sh\nprintf 'native BSD utility reached\\n' >&2\nexit 64\n"), 0700); 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,
"TAR_PREFIX="+tarPrefix, "BREW_CALLS="+calls, "BREW_EXIT="+installExit, "RUNNER_TEMP="+runnerTemp, "GITHUB_ENV="+envFile)
output, runErr := runNativeInstallerFixtureCommand(t, 10*time.Second, env, "bash", "-c", supply.Run)
if scenario != "real tools" && scenario != "symlinked fixture root" && scenario != "streamed version output" {
want := 1
if scenario == "install failure" || scenario == "failed version producer" {
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) != len(tools) {
t.Fatal("fixture supply must select only Bash/stat/chmod/chown/cp/mv/ln/readlink/sha256sum/tar/awk/flock/sed")
}
for _, name := range tools {
if target, err := os.Readlink(filepath.Join(fixtureBin, name)); err != nil || target != installed[name] {
t.Fatalf("%s did not bind the verified installed executable", name)
}
}
if data, err := os.ReadFile(calls); err != nil || string(data) != "install bash gnu-tar gawk flock gnu-sed\n--prefix bash\n--prefix coreutils\n--prefix gnu-tar\n--prefix gawk\n--prefix flock\n--prefix gnu-sed\n" {
t.Fatal("fixture supply changed its fixed package/resource selection")
}
if scenario == "real tools" {
// Execute the consuming Linux CI shells, not just --version.
// The unusable native sha256sum remains behind the staged tools.
output, err := runNativeInstallerFixtureCommand(t, time.Minute,
append(env, "PATH="+fixtureBin+":"+bin+":"+os.Getenv("PATH")),
"python3", repoFile("scripts", "tests", "test_frontend_real_embed.py"), "FrontendEmbedTransfer", "-v")
if err != nil {
t.Fatalf("staged GNU tools did not execute frontend transfer controls: %v\n%s", err, output)
}
t.Logf("staged-tool frontend transfer controls:\n%s", output)
}
// Exercise the actual workflow step's local PATH boundary, replacing
// only Python's containing suite to avoid recursively starting Go.
probe := `#!/usr/bin/env bash
set -eu
test "$*" = "scripts/installtests/run_unix_contracts.py --phase $EXPECTED_PHASE" || exit 91
test "$(command -v bash)" = "$PULSE_INSTALLER_FIXTURE_BIN/bash" || exit 92
for tool in stat chmod chown cp mv ln readlink sha256sum tar awk flock sed; do
test "$(command -v "$tool")" = "$PULSE_INSTALLER_FIXTURE_BIN/$tool" || exit 93
done
# The expected readlink result must use a physical root too. A temporary
# directory reached through a legitimate parent symlink is not itself the
# canonical spelling (notably /var on macOS).
PROBE_ROOT="$(cd "$PROBE_ROOT" && pwd -P)"
# The real standalone updater takes a nonblocking exclusive directory lock.
# A second open of that same directory must fail until the holder closes it.
exec {fixture_lock}< "$PROBE_ROOT"
flock --exclusive --nonblock "$fixture_lock"
bash -c 'exec {contender}< "$1"; if flock --exclusive --nonblock "$contender"; then exit 99; fi' _ "$PROBE_ROOT"
exec {fixture_lock}<&-
exec {fixture_lock}< "$PROBE_ROOT"
flock --exclusive --nonblock "$fixture_lock"
exec {fixture_lock}<&-
# Exercise the actual Linux-only flags with real executables and file bytes.
# Neither a filename check nor a --version double can establish these semantics.
printf '{"autoUpdateEnabled":false,"updateChannel":"rc","marker":"preserved"}\n' > "$PROBE_ROOT/preferences"
sed -i 's/"autoUpdateEnabled":[^,}]*/"autoUpdateEnabled":true/' "$PROBE_ROOT/preferences"
sed -i 's/"updateChannel":[^,}]*/"updateChannel":"stable"/' "$PROBE_ROOT/preferences"
printf '{"autoUpdateEnabled":true,"updateChannel":"stable","marker":"preserved"}\n' | cmp - "$PROBE_ROOT/preferences"
printf 'archive bytes\n' > "$PROBE_ROOT/source"
# Linux CI checks the complete packet with GNU-only long options. Native
# macOS sha256sum is not a substitute, and corruption must remain terminal.
sha256sum "$PROBE_ROOT/source" > "$PROBE_ROOT/source.sha256"
sha256sum --check --strict "$PROBE_ROOT/source.sha256"
printf 'changed bytes\n' >> "$PROBE_ROOT/source"
if sha256sum --check --strict "$PROBE_ROOT/source.sha256"; then exit 96; fi
if printf 'not a checksum\n' | sha256sum --check --strict; then exit 97; fi
printf 'archive bytes\n' > "$PROBE_ROOT/source"
tar -czf "$PROBE_ROOT/source.tgz" -C "$PROBE_ROOT" source
mkdir -p "$PROBE_ROOT/extracted"
tar --no-same-owner --no-overwrite-dir -xzf "$PROBE_ROOT/source.tgz" -C "$PROBE_ROOT/extracted"
cmp "$PROBE_ROOT/source" "$PROBE_ROOT/extracted/source"
# The server's bundle publisher replaces a staged symlink without changing
# its target. BSD cp cannot execute this production flag. Verify bytes and
# link identity too, not just that the selected tool reports GNU provenance.
printf 'unrelated target\n' > "$PROBE_ROOT/copy-target"
ln -s -- "$PROBE_ROOT/copy-target" "$PROBE_ROOT/staged"
cp --remove-destination -- "$PROBE_ROOT/extracted/source" "$PROBE_ROOT/staged"
test ! -L "$PROBE_ROOT/staged"
test "$(cat "$PROBE_ROOT/copy-target")" = 'unrelated target'
mv -T "$PROBE_ROOT/staged" "$PROBE_ROOT/published"
cmp "$PROBE_ROOT/source" "$PROBE_ROOT/published"
test ! -e "$PROBE_ROOT/staged"
# DSM Upstart removal links its manual override and restores retired config
# with GNU ln -T: a hard link that never replaces or descends into a target.
links="$PROBE_ROOT/links"
rm -rf "$links"
mkdir "$links" "$links/occupied-directory"
ln -T -- "$PROBE_ROOT/published" "$links/linked"
cmp "$PROBE_ROOT/source" "$links/linked"
printf 'concurrent\n' > "$links/occupied"
if ln -T -- "$PROBE_ROOT/published" "$links/occupied" 2>/dev/null; then exit 94; fi
test "$(cat "$links/occupied")" = concurrent
if ln -T -- "$PROBE_ROOT/published" "$links/occupied-directory" 2>/dev/null; then exit 95; fi
test ! -e "$links/occupied-directory/published"
# Removal validates the entire runtime/data layout before deleting a file.
# Its Linux readlink -m must resolve symlinked parents and missing leaves;
# BSD readlink cannot stand in for that safety boundary on a macOS runner.
mkdir "$links/config"
ln -s -- "$links/config" "$links/config-link"
test "$(readlink -m -- "$links/config-link/future/../settings")" = "$links/config/settings"
test "$(readlink -m -- "$links/config-link/../missing")" = "$links/missing"
# Exercise the NAS filter's actual octal byte range, not a --version double.
# Safe text must pass; NUL and other disallowed controls must fail. Neither
# false-positive empty matching nor dropping binary bytes is acceptable.
printf 'safe boot text\n' > "$PROBE_ROOT/boot-safe"
printf 'unsafe\000boot\n' > "$PROBE_ROOT/boot-nul"
printf 'unsafe\001boot\n' > "$PROBE_ROOT/boot-control"
awk 'BEGIN { bad = 0 } /[\000-\010\013-\037\177]/ { bad = 1 } END { exit bad }' "$PROBE_ROOT/boot-safe"
for file in boot-nul boot-control; do
if awk 'BEGIN { bad = 0 } /[\000-\010\013-\037\177]/ { bad = 1 } END { exit bad }' "$PROBE_ROOT/$file"; then
exit 94
fi
done
exit "${SUITE_EXIT:-0}"
`
if err := os.WriteFile(filepath.Join(bin, "python3"), []byte(probe), 0700); err != nil {
t.Fatal(err)
}
// Supply uses native ln, while installer phases must use the verified GNU ln.
if err := os.WriteFile(filepath.Join(bin, "ln"), []byte("#!/bin/sh\nprintf 'native BSD utility reached\\n' >&2\nexit 64\n"), 0700); err != nil {
t.Fatal(err)
}
for phase, step := range map[string]nativeGuidanceStep{"preflight": preflight, "remaining": installer} {
for _, code := range []string{"0", "23"} {
output, err := runNativeInstallerFixtureCommand(t, 10*time.Second,
append(env, "PULSE_INSTALLER_FIXTURE_BIN="+fixtureBin, "PROBE_ROOT="+root, "SUITE_EXIT="+code, "EXPECTED_PHASE="+phase),
"bash", "-c", step.Run)
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)
}
}
}
}
})
}
}