From 60009b5ddc60532f61d171bf6f9961dc1d19dd87 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sat, 5 Dec 2026 14:22:34 +0000 Subject: [PATCH] tstest/integration: reuse built binaries across test processes on build VMs GetBinaries builds cmd/tailscale and cmd/tailscaled once per process, into the first test's temp dir. The go command caches compiled packages but not linked binaries, so that build costs a few seconds (2.8s on an idle 14-CPU VM, much more under load) even when every package is in the build cache. Test runners that run each test in its own process, such as gotst, pay it for every integration test. On ephemeral build VMs, which have ~/.is-ephemeral-build-vm, now install the binaries into a directory under the user cache directory that later test processes reuse, so that "go install" skips relinking binaries that are already up to date. Each GOOS, GOARCH, race mode, and checkout gets its own subdirectory, so they don't keep replacing each other's binaries. A file lock serializes the build and the opening of the binaries across processes, so concurrent test processes build once and each opens complete binaries. Tests still get their own copies in their temp dirs, as before. $TS_INTEGRATION_BIN_CACHE names a directory to use anywhere, as for local use; otherwise, as when employees run go test on their own machines, nothing changes and nothing is left behind. Windows keeps building into the temp dir for now, lacking a file lock here. With the cache, TestOneNodeUpNoAuth in its own process went from 2.0s to 0.6s. Updates tailscale/corp#28679 Updates tailscale/corp#46045 Signed-off-by: Brad Fitzpatrick Change-Id: I01dec43ae80dafac2a05561999d6090f2139fcfd --- tstest/integration/bincache_notwindows.go | 25 +++++++ tstest/integration/bincache_windows.go | 12 ++++ tstest/integration/integration.go | 86 +++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 tstest/integration/bincache_notwindows.go create mode 100644 tstest/integration/bincache_windows.go diff --git a/tstest/integration/bincache_notwindows.go b/tstest/integration/bincache_notwindows.go new file mode 100644 index 000000000..9574c41fe --- /dev/null +++ b/tstest/integration/bincache_notwindows.go @@ -0,0 +1,25 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build !windows + +package integration + +import ( + "os" + "syscall" +) + +// lockFile takes an exclusive lock on the file at path, creating it if +// needed, and returns the function that releases it. +func lockFile(path string) (unlock func(), err error) { + f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o600) + if err != nil { + return nil, err + } + if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil { + f.Close() + return nil, err + } + return func() { f.Close() }, nil // closing releases the lock +} diff --git a/tstest/integration/bincache_windows.go b/tstest/integration/bincache_windows.go new file mode 100644 index 000000000..6bba24cd6 --- /dev/null +++ b/tstest/integration/bincache_windows.go @@ -0,0 +1,12 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package integration + +import "errors" + +// lockFile is not implemented on Windows, where binaries are built into +// the first test's temp dir instead of a shared cache directory. +func lockFile(path string) (unlock func(), err error) { + return nil, errors.New("not implemented on Windows") +} diff --git a/tstest/integration/integration.go b/tstest/integration/integration.go index db6ba2fa1..c760fa05c 100644 --- a/tstest/integration/integration.go +++ b/tstest/integration/integration.go @@ -11,7 +11,9 @@ import ( "bytes" "cmp" "context" + "crypto/sha256" "crypto/tls" + "encoding/hex" "encoding/json" "flag" "fmt" @@ -184,6 +186,12 @@ func GetBinaries(tb testing.TB) *Binaries { } }) buildOnce.Do(func() { + if cache := binCacheDir(); cache != "" { + if buildErr = buildCachedTestBinaries(cache); buildErr == nil { + return + } + tb.Logf("not reusing binaries in %s: %v", cache, buildErr) + } buildErr = buildTestBinaries(dir) }) if buildErr != nil { @@ -213,6 +221,84 @@ var ( binariesCache *Binaries ) +// binCacheDir returns a directory in which to build cmd/tailscale and +// cmd/tailscaled for reuse by later test processes, or "" to build them +// into the first test's temp dir instead. +// +// Building costs seconds even when every package is in the build cache, as +// the go command doesn't cache linked binaries, and test runners that run +// each test in its own process (such as gotst) pay that for every test. +// Installing into the same directory as before lets "go install" skip +// relinking binaries that are up to date. That leaves files behind, so +// it's only done on ephemeral build VMs, which have a marker file in their +// home directory, or with $TS_INTEGRATION_BIN_CACHE naming the directory to +// use. Binaries for each GOOS, GOARCH, race mode, and checkout get their own +// subdirectory, so they don't keep replacing each other. +func binCacheDir() string { + if runtime.GOOS == "windows" { + return "" // see lockFile + } + base := os.Getenv("TS_INTEGRATION_BIN_CACHE") + if base == "" { + home, err := os.UserHomeDir() + if err != nil { + return "" + } + if _, err := os.Stat(filepath.Join(home, ".is-ephemeral-build-vm")); err != nil { + return "" + } + cache, err := os.UserCacheDir() + if err != nil { + return "" + } + base = filepath.Join(cache, "tailscale-integration-bins") + } + key := runtime.GOOS + "_" + runtime.GOARCH + if version.IsRace() { + key += "_race" + } + if root := moduleRoot(); root != "" { + sum := sha256.Sum256([]byte(root)) + key += "_" + hex.EncodeToString(sum[:6]) + } + return filepath.Join(base, key) +} + +// moduleRoot returns the directory of the go.mod file enclosing the +// current directory, or "" if there isn't one. +func moduleRoot() string { + dir, err := os.Getwd() + if err != nil { + return "" + } + for { + if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { + return dir + } + parent := filepath.Dir(dir) + if parent == dir { + return "" + } + dir = parent + } +} + +// buildCachedTestBinaries is buildTestBinaries for the shared directory +// dir, from binCacheDir. It holds a lock on dir while building and opening +// the binaries, so that concurrent test processes build them only once and +// each opens complete binaries. +func buildCachedTestBinaries(dir string) error { + if err := os.MkdirAll(dir, 0o755); err != nil { + return err + } + unlock, err := lockFile(filepath.Join(dir, ".lock")) + if err != nil { + return err + } + defer unlock() + return buildTestBinaries(dir) +} + // buildTestBinaries builds tailscale and tailscaled. // On success, it initializes [binariesCache]. func buildTestBinaries(dir string) error {