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 <[email protected]>
This commit is contained in:
Brad Fitzpatrick authored and Brad Fitzpatrick committed 2026-12-05 21:21:37 -08:00
1 parent 5bab2a0052
commit 07fad02078
11 files changed
+104 -42

No files matched your search

+1 -1
View File
@@ -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
-9
View File
@@ -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"
@@ -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.
+10
View File
@@ -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"
+18
View File
@@ -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
-23
View File
@@ -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",
+68
View File
@@ -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], ", "))
}
})
}
}
@@ -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"
@@ -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"
@@ -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"
@@ -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"