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 {