diff --git a/scripts/installtests/install_sh_test.go b/scripts/installtests/install_sh_test.go index 6a5e74b1d1..0047c252e9 100644 --- a/scripts/installtests/install_sh_test.go +++ b/scripts/installtests/install_sh_test.go @@ -8044,16 +8044,11 @@ write_action_runner_config } func TestInstallSHActionRunnerRecoveredCollectorTrust(t *testing.T) { - // The real runner, TLS server and recovered state belong to this test; + // The runner copy, TLS server and recovered state belong to this test; // only child environments change. Share Go's existing parallel budget. t.Parallel() root := t.TempDir() - runnerBinary := filepath.Join(root, "pulse-agent-runner") - build := exec.Command("go", "build", "-o", runnerBinary, "./cmd/pulse-agent-runner") - build.Dir = filepath.Dir(repoFile("go.mod")) - if output, err := build.CombinedOutput(); err != nil { - t.Fatalf("build action runner: %v\n%s", err, output) - } + runnerBinary := buildPulseAgentRunnerLifecycleBinary(t, root) const token = "fixture-runner-credential" var requests atomic.Int32 server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -8618,12 +8613,7 @@ provision_action_runner func TestInstallSHActionRunnerAtomicCancelUsesPrivateGoTransportAndRequires204(t *testing.T) { const token = "runner-cancel-secret" root := t.TempDir() - runnerBinary := filepath.Join(root, "pulse-agent-runner") - build := exec.Command("go", "build", "-o", runnerBinary, "./cmd/pulse-agent-runner") - build.Dir = filepath.Dir(repoFile("go.mod")) - if output, err := build.CombinedOutput(); err != nil { - t.Fatalf("build action runner: %v\n%s", err, output) - } + runnerBinary := buildPulseAgentRunnerLifecycleBinary(t, root) caSource := httptest.NewTLSServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) defer caSource.Close() caFile := filepath.Join(root, "runner-ca.pem") @@ -8705,12 +8695,7 @@ func TestInstallSHActionRunnerSelfRevokeUsesPrivateCredential(t *testing.T) { defer server.Close() root := t.TempDir() - runnerBinary := filepath.Join(root, "pulse-agent-runner") - build := exec.Command("go", "build", "-o", runnerBinary, "./cmd/pulse-agent-runner") - build.Dir = filepath.Dir(repoFile("go.mod")) - if output, err := build.CombinedOutput(); err != nil { - t.Fatalf("build action runner: %v\n%s", err, output) - } + runnerBinary := buildPulseAgentRunnerLifecycleBinary(t, root) envFile := filepath.Join(root, "runner.env") tokenFile := filepath.Join(root, "token") mustWrite(t, tokenFile, token+"\n") diff --git a/scripts/installtests/lifecycle_binary_fixture_test.go b/scripts/installtests/lifecycle_binary_fixture_test.go index 5699dfcc6d..28141c2541 100644 --- a/scripts/installtests/lifecycle_binary_fixture_test.go +++ b/scripts/installtests/lifecycle_binary_fixture_test.go @@ -14,12 +14,11 @@ import ( "time" ) -// Only the build is shared, and only within this test process. Each caller -// receives its own executable, so replacing or removing it cannot affect -// another lifecycle test. Keep the real source build, the URL/removal tests' -// three-minute build deadline and any compiler failure. The Go build cache -// alone still links the same agent once for every caller. -var lifecycleAgentFixture = newLifecycleBinaryFixture(func(binary string) error { +// Each fixture shares only its real source build, and only within this test +// process. A caller can replace or remove its executable without affecting +// another test. Reusing Go build objects still leaves a link per invocation. +var lifecycleAgentFixture = newLifecycleBinaryFixture("pulse-agent", func(binary string) error { + // Keep the URL and removal checks' existing three-minute build deadline. ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) defer cancel() build := exec.CommandContext(ctx, "go", "build", "-o", binary, "./cmd/pulse-agent") @@ -30,18 +29,36 @@ var lifecycleAgentFixture = newLifecycleBinaryFixture(func(binary string) error return nil }) -func newLifecycleBinaryFixture(build func(string) error) func(string) (string, error) { +var lifecycleRunnerFixture = newLifecycleBinaryFixture("pulse-agent-runner", func(binary string) error { + build := exec.Command("go", "build", "-o", binary, "./cmd/pulse-agent-runner") + build.Dir = repoFile() + if output, err := build.CombinedOutput(); err != nil { + return fmt.Errorf("build action runner: %w\n%s", err, output) + } + return nil +}) + +func buildPulseAgentRunnerLifecycleBinary(t *testing.T, dir string) string { + t.Helper() + binary, err := lifecycleRunnerFixture(dir) + if err != nil { + t.Fatal(err) + } + return binary +} + +func newLifecycleBinaryFixture(name string, build func(string) error) func(string) (string, error) { load := sync.OnceValues(func() (payload []byte, resultErr error) { - dir, err := os.MkdirTemp("", "pulse-installtest-agent-") + dir, err := os.MkdirTemp("", "pulse-installtest-"+name+"-") if err != nil { return nil, err } defer func() { if err := os.RemoveAll(dir); err != nil { - resultErr = errors.Join(resultErr, fmt.Errorf("clean agent build directory: %w", err)) + resultErr = errors.Join(resultErr, fmt.Errorf("clean %s build directory: %w", name, err)) } }() - binary := filepath.Join(dir, "pulse-agent") + binary := filepath.Join(dir, name) if err := build(binary); err != nil { return nil, err } @@ -50,7 +67,7 @@ func newLifecycleBinaryFixture(build func(string) error) func(string) (string, e return nil, err } if len(payload) == 0 { - return nil, errors.New("pulse-agent build produced an empty executable") + return nil, fmt.Errorf("%s build produced an empty executable", name) } return payload, nil }) @@ -59,7 +76,7 @@ func newLifecycleBinaryFixture(build func(string) error) func(string) (string, e if err != nil { return "", err } - binary := filepath.Join(dir, "pulse-agent") + binary := filepath.Join(dir, name) if err := os.WriteFile(binary, payload, 0755); err != nil { return "", err } @@ -69,10 +86,17 @@ func newLifecycleBinaryFixture(build func(string) error) func(string) (string, e func TestLifecycleBinaryFixtureBuildsOnceWithIndependentCopies(t *testing.T) { t.Parallel() + for _, name := range []string{"pulse-agent", "pulse-agent-runner"} { + t.Run(name, func(t *testing.T) { checkLifecycleBinaryCopies(t, name) }) + } +} + +func checkLifecycleBinaryCopies(t *testing.T, name string) { + t.Helper() var builds atomic.Int32 var buildDir string payload := []byte("fixture executable bytes") - fixture := newLifecycleBinaryFixture(func(binary string) error { + fixture := newLifecycleBinaryFixture(name, func(binary string) error { builds.Add(1) buildDir = filepath.Dir(binary) return os.WriteFile(binary, payload, 0755) @@ -100,7 +124,7 @@ func TestLifecycleBinaryFixtureBuildsOnceWithIndependentCopies(t *testing.T) { t.Fatalf("temporary build directory still exists: %v", err) } for i := range paths { - if errs[i] != nil || paths[i] != filepath.Join(dirs[i], "pulse-agent") { + if errs[i] != nil || paths[i] != filepath.Join(dirs[i], name) { t.Fatalf("copy %d: path=%q err=%v", i, paths[i], errs[i]) } if info, err := os.Stat(paths[i]); err != nil || info.Mode().Perm()&0100 == 0 { @@ -134,7 +158,7 @@ func TestLifecycleBinaryFixtureKeepsBuildFailures(t *testing.T) { wantErr := errors.New("compiler failed after partial output") builds := 0 var buildDir string - fixture := newLifecycleBinaryFixture(func(binary string) error { + fixture := newLifecycleBinaryFixture("pulse-agent", func(binary string) error { builds++ buildDir = filepath.Dir(binary) if err := os.WriteFile(binary, []byte("partial"), 0755); err != nil { @@ -163,7 +187,7 @@ func TestLifecycleBinaryFixtureKeepsBuildFailures(t *testing.T) { func TestLifecycleBinaryFixtureRejectsMissingOrEmptyOutput(t *testing.T) { t.Parallel() for _, empty := range []bool{false, true} { - fixture := newLifecycleBinaryFixture(func(binary string) error { + fixture := newLifecycleBinaryFixture("pulse-agent", func(binary string) error { if empty { return os.WriteFile(binary, nil, 0755) } @@ -178,7 +202,7 @@ func TestLifecycleBinaryFixtureRejectsMissingOrEmptyOutput(t *testing.T) { func TestLifecycleBinaryFixtureKeepsCopyFailuresLocal(t *testing.T) { t.Parallel() builds := 0 - fixture := newLifecycleBinaryFixture(func(binary string) error { + fixture := newLifecycleBinaryFixture("pulse-agent", func(binary string) error { builds++ return os.WriteFile(binary, []byte("complete"), 0755) })