Skip to content

Commit 7560435

Browse files
committed
tstest/deptest: add test-only package to unify negative dep tests
Updates tailscale#8658 Signed-off-by: Brad Fitzpatrick <[email protected]>
1 parent 32d486e commit 7560435

File tree

4 files changed

+104
-55
lines changed

4 files changed

+104
-55
lines changed

cmd/tailscaled/tailscaled_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,34 @@
33

44
package main // import "tailscale.com/cmd/tailscaled"
55

6-
import "testing"
6+
import (
7+
"testing"
8+
9+
"tailscale.com/tstest/deptest"
10+
)
711

812
func TestNothing(t *testing.T) {
913
// This test does nothing on purpose, so we can run
1014
// GODEBUG=memprofilerate=1 go test -v -run=Nothing -memprofile=prof.mem
1115
// without any errors about no matching tests.
1216
}
17+
18+
func TestDeps(t *testing.T) {
19+
deptest.DepChecker{
20+
GOOS: "darwin",
21+
GOARCH: "arm64",
22+
BadDeps: map[string]string{
23+
"gvisor.dev/gvisor/pkg/hostarch": "will crash on non-4K page sizes",
24+
},
25+
}.Check(t)
26+
27+
deptest.DepChecker{
28+
GOOS: "linux",
29+
GOARCH: "arm64",
30+
BadDeps: map[string]string{
31+
// TODO: per https://github.com/tailscale/tailscale/issues/8658,
32+
// add this line too:
33+
// "gvisor.dev/gvisor/pkg/hostarch": "will crash on non-4K page sizes",
34+
},
35+
}.Check(t)
36+
}

tstest/deptest/deptest.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) Tailscale Inc & AUTHORS
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
// The deptest package contains a shared implementation of negative
5+
// dependency tests for other packages, making sure we don't start
6+
// depending on certain packages.
7+
package deptest
8+
9+
import (
10+
"encoding/json"
11+
"os"
12+
"os/exec"
13+
"runtime"
14+
"testing"
15+
)
16+
17+
type DepChecker struct {
18+
GOOS string
19+
GOARCH string
20+
BadDeps map[string]string // package => why
21+
}
22+
23+
func (c DepChecker) Check(t *testing.T) {
24+
if runtime.GOOS == "windows" {
25+
// Slow and avoid caring about "go.exe" etc.
26+
t.Skip("skipping dep tests on windows hosts")
27+
}
28+
t.Helper()
29+
cmd := exec.Command("go", "list", "-json", ".")
30+
var extraEnv []string
31+
if c.GOOS != "" {
32+
extraEnv = append(extraEnv, "GOOS="+c.GOOS)
33+
}
34+
if c.GOARCH != "" {
35+
extraEnv = append(extraEnv, "GOARCH="+c.GOARCH)
36+
}
37+
cmd.Env = append(os.Environ(), extraEnv...)
38+
out, err := cmd.Output()
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
var res struct {
43+
Deps []string
44+
}
45+
if err := json.Unmarshal(out, &res); err != nil {
46+
t.Fatal(err)
47+
}
48+
49+
for _, dep := range res.Deps {
50+
if why, ok := c.BadDeps[dep]; ok {
51+
t.Errorf("package %q is not allowed as a dependency (env: %q); reason: %s", dep, extraEnv, why)
52+
}
53+
}
54+
t.Logf("got %d dependencies", len(res.Deps))
55+
56+
}

tstest/iosdeps/iosdeps_test.go

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,21 @@
11
// Copyright (c) Tailscale Inc & AUTHORS
22
// SPDX-License-Identifier: BSD-3-Clause
33

4-
// No need to run this on Windows where CI's slow enough. Then we don't need to
5-
// worry about "go.exe" etc.
6-
7-
//go:build !windows
8-
94
package iosdeps
105

116
import (
12-
"encoding/json"
13-
"os"
14-
"os/exec"
157
"testing"
8+
9+
"tailscale.com/tstest/deptest"
1610
)
1711

1812
func TestDeps(t *testing.T) {
19-
cmd := exec.Command("go", "list", "-json", ".")
20-
cmd.Env = append(os.Environ(), "GOOS=ios", "GOARCH=arm64")
21-
out, err := cmd.Output()
22-
if err != nil {
23-
t.Fatal(err)
24-
}
25-
var res struct {
26-
Deps []string
27-
}
28-
if err := json.Unmarshal(out, &res); err != nil {
29-
t.Fatal(err)
30-
}
31-
for _, dep := range res.Deps {
32-
switch dep {
33-
case "text/template", "html/template":
34-
t.Errorf("package %q is not allowed as a dependency on iOS", dep)
35-
}
36-
}
37-
t.Logf("got %d dependencies", len(res.Deps))
13+
deptest.DepChecker{
14+
GOOS: "ios",
15+
GOARCH: "arm64",
16+
BadDeps: map[string]string{
17+
"text/template": "linker bloat (MethodByName)",
18+
"html/template": "linker bloat (MethodByName)",
19+
},
20+
}.Check(t)
3821
}

tstest/jsdeps/jsdeps_test.go

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
11
// Copyright (c) Tailscale Inc & AUTHORS
22
// SPDX-License-Identifier: BSD-3-Clause
33

4-
// No need to run this on Windows where CI's slow enough. Then we don't need to
5-
// worry about "go.exe" etc.
6-
7-
//go:build !windows
8-
94
package jsdeps
105

116
import (
12-
"encoding/json"
13-
"os"
14-
"os/exec"
157
"testing"
8+
9+
"tailscale.com/tstest/deptest"
1610
)
1711

1812
func TestDeps(t *testing.T) {
19-
cmd := exec.Command("go", "list", "-json", ".")
20-
cmd.Env = append(os.Environ(), "GOOS=js", "GOARCH=wasm")
21-
out, err := cmd.Output()
22-
if err != nil {
23-
t.Fatal(err)
24-
}
25-
var res struct {
26-
Deps []string
27-
}
28-
if err := json.Unmarshal(out, &res); err != nil {
29-
t.Fatal(err)
30-
}
31-
for _, dep := range res.Deps {
32-
switch dep {
33-
case "runtime/pprof", "golang.org/x/net/http2/h2c", "net/http/pprof", "golang.org/x/net/proxy", "github.com/tailscale/goupnp":
34-
t.Errorf("package %q is not allowed as a dependency on JS", dep)
35-
}
36-
}
37-
t.Logf("got %d dependencies", len(res.Deps))
13+
deptest.DepChecker{
14+
GOOS: "js",
15+
GOARCH: "wasm",
16+
BadDeps: map[string]string{
17+
"runtime/pprof": "bloat",
18+
"golang.org/x/net/http2/h2c": "bloat",
19+
"net/http/pprof": "bloat",
20+
"golang.org/x/net/proxy": "bloat",
21+
"github.com/tailscale/goupnp": "bloat, which can't work anyway in wasm",
22+
},
23+
}.Check(t)
3824
}

0 commit comments

Comments
 (0)