tstest/deptest: add test-only package to unify negative dep tests

Updates #8658

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2023-07-20 18:26:52 -07:00 committed by Brad Fitzpatrick
parent 32d486e2bf
commit 7560435eb5
4 changed files with 104 additions and 55 deletions

View File

@ -3,10 +3,34 @@
package main // import "tailscale.com/cmd/tailscaled"
import "testing"
import (
"testing"
"tailscale.com/tstest/deptest"
)
func TestNothing(t *testing.T) {
// This test does nothing on purpose, so we can run
// GODEBUG=memprofilerate=1 go test -v -run=Nothing -memprofile=prof.mem
// without any errors about no matching tests.
}
func TestDeps(t *testing.T) {
deptest.DepChecker{
GOOS: "darwin",
GOARCH: "arm64",
BadDeps: map[string]string{
"gvisor.dev/gvisor/pkg/hostarch": "will crash on non-4K page sizes",
},
}.Check(t)
deptest.DepChecker{
GOOS: "linux",
GOARCH: "arm64",
BadDeps: map[string]string{
// TODO: per https://github.com/tailscale/tailscale/issues/8658,
// add this line too:
// "gvisor.dev/gvisor/pkg/hostarch": "will crash on non-4K page sizes",
},
}.Check(t)
}

56
tstest/deptest/deptest.go Normal file
View File

@ -0,0 +1,56 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
// The deptest package contains a shared implementation of negative
// dependency tests for other packages, making sure we don't start
// depending on certain packages.
package deptest
import (
"encoding/json"
"os"
"os/exec"
"runtime"
"testing"
)
type DepChecker struct {
GOOS string
GOARCH string
BadDeps map[string]string // package => why
}
func (c DepChecker) Check(t *testing.T) {
if runtime.GOOS == "windows" {
// Slow and avoid caring about "go.exe" etc.
t.Skip("skipping dep tests on windows hosts")
}
t.Helper()
cmd := exec.Command("go", "list", "-json", ".")
var extraEnv []string
if c.GOOS != "" {
extraEnv = append(extraEnv, "GOOS="+c.GOOS)
}
if c.GOARCH != "" {
extraEnv = append(extraEnv, "GOARCH="+c.GOARCH)
}
cmd.Env = append(os.Environ(), extraEnv...)
out, err := cmd.Output()
if err != nil {
t.Fatal(err)
}
var res struct {
Deps []string
}
if err := json.Unmarshal(out, &res); err != nil {
t.Fatal(err)
}
for _, dep := range res.Deps {
if why, ok := c.BadDeps[dep]; ok {
t.Errorf("package %q is not allowed as a dependency (env: %q); reason: %s", dep, extraEnv, why)
}
}
t.Logf("got %d dependencies", len(res.Deps))
}

View File

@ -1,38 +1,21 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
// No need to run this on Windows where CI's slow enough. Then we don't need to
// worry about "go.exe" etc.
//go:build !windows
package iosdeps
import (
"encoding/json"
"os"
"os/exec"
"testing"
"tailscale.com/tstest/deptest"
)
func TestDeps(t *testing.T) {
cmd := exec.Command("go", "list", "-json", ".")
cmd.Env = append(os.Environ(), "GOOS=ios", "GOARCH=arm64")
out, err := cmd.Output()
if err != nil {
t.Fatal(err)
}
var res struct {
Deps []string
}
if err := json.Unmarshal(out, &res); err != nil {
t.Fatal(err)
}
for _, dep := range res.Deps {
switch dep {
case "text/template", "html/template":
t.Errorf("package %q is not allowed as a dependency on iOS", dep)
}
}
t.Logf("got %d dependencies", len(res.Deps))
deptest.DepChecker{
GOOS: "ios",
GOARCH: "arm64",
BadDeps: map[string]string{
"text/template": "linker bloat (MethodByName)",
"html/template": "linker bloat (MethodByName)",
},
}.Check(t)
}

View File

@ -1,38 +1,24 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
// No need to run this on Windows where CI's slow enough. Then we don't need to
// worry about "go.exe" etc.
//go:build !windows
package jsdeps
import (
"encoding/json"
"os"
"os/exec"
"testing"
"tailscale.com/tstest/deptest"
)
func TestDeps(t *testing.T) {
cmd := exec.Command("go", "list", "-json", ".")
cmd.Env = append(os.Environ(), "GOOS=js", "GOARCH=wasm")
out, err := cmd.Output()
if err != nil {
t.Fatal(err)
}
var res struct {
Deps []string
}
if err := json.Unmarshal(out, &res); err != nil {
t.Fatal(err)
}
for _, dep := range res.Deps {
switch dep {
case "runtime/pprof", "golang.org/x/net/http2/h2c", "net/http/pprof", "golang.org/x/net/proxy", "github.com/tailscale/goupnp":
t.Errorf("package %q is not allowed as a dependency on JS", dep)
}
}
t.Logf("got %d dependencies", len(res.Deps))
deptest.DepChecker{
GOOS: "js",
GOARCH: "wasm",
BadDeps: map[string]string{
"runtime/pprof": "bloat",
"golang.org/x/net/http2/h2c": "bloat",
"net/http/pprof": "bloat",
"golang.org/x/net/proxy": "bloat",
"github.com/tailscale/goupnp": "bloat, which can't work anyway in wasm",
},
}.Check(t)
}