From 07fad02078202269f8d0f25dd12a2fedcf6097e9 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 6 Dec 2026 01:11:31 +0000 Subject: [PATCH] all: avoid test variant recompile amplification, add check Package feature has in-package tests, and an external test there blank-imported feature/condregister, so ~100 packages that depend on feature were recompiled as [feature.test] variants that never hit the build cache: about 175s of CPU on each build of feature's tests, even with everything else cached. Move that test to feature/condregister, which nothing imports, where it costs a ~3s link. Also move tailscaled's feature/ssh import into condregister so the test covers ssh without a second copy of its build tags. Likewise check PeerStatus.IsRouter agreement from ipnstate's tests rather than tailcfg's, which recompiled 5 packages. Then add TestTestVariantAmplification to fail if any test binary recompiles more than 5 packages this way; the max is now 4. Updates tailscale/corp#28679 Change-Id: I4490d9b7cb8ddd71d3888a89195327313834e652 Signed-off-by: Brad Fitzpatrick --- cmd/tailscaled/depaware.txt | 2 +- cmd/tailscaled/ssh.go | 9 --- .../disable_feature_test.go} | 12 ++-- feature/condregister/maybe_ssh.go | 10 +++ ipn/ipnstate/ipnstate_test.go | 18 +++++ tailcfg/tailcfg_test.go | 23 ------- test_variants_test.go | 68 +++++++++++++++++++ .../tailscaled_deps_test_darwin.go | 1 - .../tailscaled_deps_test_freebsd.go | 1 - .../integration/tailscaled_deps_test_linux.go | 1 - .../tailscaled_deps_test_openbsd.go | 1 - 11 files changed, 104 insertions(+), 42 deletions(-) delete mode 100644 cmd/tailscaled/ssh.go rename feature/{register_disable_test.go => condregister/disable_feature_test.go} (91%) create mode 100644 feature/condregister/maybe_ssh.go create mode 100644 test_variants_test.go diff --git a/cmd/tailscaled/depaware.txt b/cmd/tailscaled/depaware.txt index 576afe4b2..3fd6a55bc 100644 --- a/cmd/tailscaled/depaware.txt +++ b/cmd/tailscaled/depaware.txt @@ -331,7 +331,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de L tailscale.com/feature/sdnotify from tailscale.com/feature/condregister tailscale.com/feature/serviceclientprefs from tailscale.com/feature/condregister tailscale.com/feature/serviceclientprefs/serviceclient from tailscale.com/client/local+ - LD tailscale.com/feature/ssh from tailscale.com/cmd/tailscaled + LD tailscale.com/feature/ssh from tailscale.com/feature/condregister L tailscale.com/feature/syslog from tailscale.com/feature/condregister tailscale.com/feature/syspolicy from tailscale.com/feature/condregister tailscale.com/feature/taildrop from tailscale.com/feature/condregister diff --git a/cmd/tailscaled/ssh.go b/cmd/tailscaled/ssh.go deleted file mode 100644 index 8de311794..000000000 --- a/cmd/tailscaled/ssh.go +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright (c) Tailscale Inc & contributors -// SPDX-License-Identifier: BSD-3-Clause - -//go:build (linux || darwin || freebsd || openbsd || plan9) && !ts_omit_ssh - -package main - -// Register implementations of various SSH hooks. -import _ "tailscale.com/feature/ssh" diff --git a/feature/register_disable_test.go b/feature/condregister/disable_feature_test.go similarity index 91% rename from feature/register_disable_test.go rename to feature/condregister/disable_feature_test.go index dd6390a84..40f326f71 100644 --- a/feature/register_disable_test.go +++ b/feature/condregister/disable_feature_test.go @@ -1,7 +1,7 @@ // Copyright (c) Tailscale Inc & contributors // SPDX-License-Identifier: BSD-3-Clause -package feature_test +package condregister_test import ( "encoding/json" @@ -11,10 +11,6 @@ import ( "strings" "testing" - // Link all the features that tailscaled links, so that this test - // binary registers the same set that the daemon does. - _ "tailscale.com/feature/condregister" - "tailscale.com/feature" "tailscale.com/net/netcheck" ) @@ -75,6 +71,12 @@ func childReportForEnv(t *testing.T, disableEnv string) childReport { return rep } +// TestDisableFeature checks TS_DISABLE_FEATURE against the features +// that tailscaled links, which are those linked by condregister. +// +// It lives here rather than in package feature because package feature +// has in-package tests, so linking condregister into its test binary +// recompiles every dependent package as a [feature.test] variant. func TestDisableFeature(t *testing.T) { // Baseline: with the env var unset, everything this binary links // registers itself. diff --git a/feature/condregister/maybe_ssh.go b/feature/condregister/maybe_ssh.go new file mode 100644 index 000000000..32e46008f --- /dev/null +++ b/feature/condregister/maybe_ssh.go @@ -0,0 +1,10 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +// This build constraint must match feature/ssh's own. + +//go:build ((linux && !android) || (darwin && !ios) || freebsd || openbsd || plan9) && !ts_omit_ssh + +package condregister + +import _ "tailscale.com/feature/ssh" diff --git a/ipn/ipnstate/ipnstate_test.go b/ipn/ipnstate/ipnstate_test.go index 28dd433dd..655d11dde 100644 --- a/ipn/ipnstate/ipnstate_test.go +++ b/ipn/ipnstate/ipnstate_test.go @@ -161,10 +161,28 @@ func TestPeerStatusIsRouter(t *testing.T) { if got := tc.status.IsRouter(); got != tc.want { t.Errorf("got %t, want %t", got, tc.want) } + + // Check that [tailcfg.Node.IsRouter] agrees. + if got := nodeFromPeerStatus(&tc.status).IsRouter(); got != tc.want { + t.Errorf("tailcfg.Node: got %t, want %t", got, tc.want) + } }) } } +// nodeFromPeerStatus returns the [tailcfg.Node] that ps would be +// derived from, for the fields that IsRouter looks at. +func nodeFromPeerStatus(ps *ipnstate.PeerStatus) *tailcfg.Node { + n := &tailcfg.Node{} + for _, ip := range ps.TailscaleIPs { + n.Addresses = append(n.Addresses, netip.PrefixFrom(ip, ip.BitLen())) + } + if ps.AllowedIPs != nil { + n.AllowedIPs = ps.AllowedIPs.AsSlice() + } + return n +} + func TestIsConn25Connector(t *testing.T) { for _, tc := range []struct { name string diff --git a/tailcfg/tailcfg_test.go b/tailcfg/tailcfg_test.go index ddb502b7e..05e1f6567 100644 --- a/tailcfg/tailcfg_test.go +++ b/tailcfg/tailcfg_test.go @@ -13,7 +13,6 @@ import ( "testing" "time" - "tailscale.com/ipn/ipnstate" . "tailscale.com/tailcfg" "tailscale.com/tailcfg/peercap" "tailscale.com/tstest/deptest" @@ -801,32 +800,10 @@ func TestNodeIsRouter(t *testing.T) { if got := nv.IsRouter(); got != tc.want { t.Errorf("view: got %t, want %t", got, tc.want) } - - // Check that [ipnstate.PeerStatus.IsRouter] matches. - ps := peerStatusFromNode(nv) - if got := ps.IsRouter(); got != tc.want { - t.Errorf("peer status: got %t, want %t", got, tc.want) - } }) } } -func peerStatusFromNode(n NodeView) *ipnstate.PeerStatus { - ps := &ipnstate.PeerStatus{ - ID: n.StableID(), - NodeID: n.ID(), - PublicKey: n.Key(), - DNSName: n.Name(), - } - for _, p := range n.Addresses().All() { - if p.IsSingleIP() { - ps.TailscaleIPs = append(ps.TailscaleIPs, p.Addr()) - } - } - ps.AllowedIPs = new(n.AllowedIPs()) - return ps -} - func TestNetInfoFields(t *testing.T) { handled := []string{ "MappingVariesByDestIP", diff --git a/test_variants_test.go b/test_variants_test.go new file mode 100644 index 000000000..c067c4f42 --- /dev/null +++ b/test_variants_test.go @@ -0,0 +1,68 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package tailscaleroot + +import ( + "cmp" + "maps" + "os" + "os/exec" + "slices" + "strings" + "testing" +) + +// maxTestVariants is the most packages that any one test binary may +// recompile as test variants. The largest in the tree was 4 when this +// limit was set. +const maxTestVariants = 5 + +// TestTestVariantAmplification checks that no package's test binary +// recompiles many other packages as test variants. +// +// When package P has in-package _test.go files, "go test P" compiles a +// modified copy of P, and every package in the test binary that depends +// on P (directly or not) is recompiled against that copy. These show up +// in "go list -test -deps" as "Q [P.test]". They share no build cache +// entries with normal compiles, so a test that imports something large +// that depends on P (such as feature/condregister from package feature) +// can add minutes of compile CPU. The fix is usually to move that test +// into an external test package of a package nothing else imports, or +// into a test-only leaf package. +func TestTestVariantAmplification(t *testing.T) { + for _, goos := range []string{"linux", "darwin", "windows"} { + t.Run(goos, func(t *testing.T) { + t.Parallel() + cmd := exec.Command("go", "list", "-e", "-test", "-deps", "-f", "{{.ImportPath}}", "./...") + cmd.Env = append(os.Environ(), "GOOS="+goos) + cmd.Stderr = os.Stderr + out, err := cmd.Output() + if err != nil { + t.Fatalf("go list: %v", err) + } + variants := map[string][]string{} // test package => recompiled deps + for line := range strings.Lines(string(out)) { + pkg, testPkg, ok := strings.Cut(strings.TrimSpace(line), " [") + if !ok { + continue + } + testPkg, ok = strings.CutSuffix(testPkg, ".test]") + if !ok || pkg == testPkg || pkg == testPkg+"_test" { + continue + } + variants[testPkg] = append(variants[testPkg], pkg) + } + for _, testPkg := range slices.SortedFunc(maps.Keys(variants), func(a, b string) int { + return cmp.Or(cmp.Compare(len(variants[b]), len(variants[a])), cmp.Compare(a, b)) + }) { + deps := variants[testPkg] + if len(deps) <= maxTestVariants { + break + } + t.Errorf("%s test binary recompiles %d packages as [%s.test] variants (max %d), including %s", + testPkg, len(deps), testPkg, maxTestVariants, strings.Join(deps[:5], ", ")) + } + }) + } +} diff --git a/tstest/integration/tailscaled_deps_test_darwin.go b/tstest/integration/tailscaled_deps_test_darwin.go index f8c1a62b0..d6c2454eb 100644 --- a/tstest/integration/tailscaled_deps_test_darwin.go +++ b/tstest/integration/tailscaled_deps_test_darwin.go @@ -19,7 +19,6 @@ import ( _ "tailscale.com/feature" _ "tailscale.com/feature/buildfeatures" _ "tailscale.com/feature/condregister" - _ "tailscale.com/feature/ssh" _ "tailscale.com/health" _ "tailscale.com/hostinfo" _ "tailscale.com/ipn" diff --git a/tstest/integration/tailscaled_deps_test_freebsd.go b/tstest/integration/tailscaled_deps_test_freebsd.go index f8c1a62b0..d6c2454eb 100644 --- a/tstest/integration/tailscaled_deps_test_freebsd.go +++ b/tstest/integration/tailscaled_deps_test_freebsd.go @@ -19,7 +19,6 @@ import ( _ "tailscale.com/feature" _ "tailscale.com/feature/buildfeatures" _ "tailscale.com/feature/condregister" - _ "tailscale.com/feature/ssh" _ "tailscale.com/health" _ "tailscale.com/hostinfo" _ "tailscale.com/ipn" diff --git a/tstest/integration/tailscaled_deps_test_linux.go b/tstest/integration/tailscaled_deps_test_linux.go index f8c1a62b0..d6c2454eb 100644 --- a/tstest/integration/tailscaled_deps_test_linux.go +++ b/tstest/integration/tailscaled_deps_test_linux.go @@ -19,7 +19,6 @@ import ( _ "tailscale.com/feature" _ "tailscale.com/feature/buildfeatures" _ "tailscale.com/feature/condregister" - _ "tailscale.com/feature/ssh" _ "tailscale.com/health" _ "tailscale.com/hostinfo" _ "tailscale.com/ipn" diff --git a/tstest/integration/tailscaled_deps_test_openbsd.go b/tstest/integration/tailscaled_deps_test_openbsd.go index f8c1a62b0..d6c2454eb 100644 --- a/tstest/integration/tailscaled_deps_test_openbsd.go +++ b/tstest/integration/tailscaled_deps_test_openbsd.go @@ -19,7 +19,6 @@ import ( _ "tailscale.com/feature" _ "tailscale.com/feature/buildfeatures" _ "tailscale.com/feature/condregister" - _ "tailscale.com/feature/ssh" _ "tailscale.com/health" _ "tailscale.com/hostinfo" _ "tailscale.com/ipn"