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

Updates #8658

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/8607/head
Brad Fitzpatrick 10 months ago committed by Brad Fitzpatrick
parent 32d486e2bf
commit 7560435eb5

@ -3,10 +3,34 @@
package main // import "tailscale.com/cmd/tailscaled" package main // import "tailscale.com/cmd/tailscaled"
import "testing" import (
"testing"
"tailscale.com/tstest/deptest"
)
func TestNothing(t *testing.T) { func TestNothing(t *testing.T) {
// This test does nothing on purpose, so we can run // This test does nothing on purpose, so we can run
// GODEBUG=memprofilerate=1 go test -v -run=Nothing -memprofile=prof.mem // GODEBUG=memprofilerate=1 go test -v -run=Nothing -memprofile=prof.mem
// without any errors about no matching tests. // 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)
}

@ -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))
}

@ -1,38 +1,21 @@
// Copyright (c) Tailscale Inc & AUTHORS // Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause // 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 package iosdeps
import ( import (
"encoding/json"
"os"
"os/exec"
"testing" "testing"
"tailscale.com/tstest/deptest"
) )
func TestDeps(t *testing.T) { func TestDeps(t *testing.T) {
cmd := exec.Command("go", "list", "-json", ".") deptest.DepChecker{
cmd.Env = append(os.Environ(), "GOOS=ios", "GOARCH=arm64") GOOS: "ios",
out, err := cmd.Output() GOARCH: "arm64",
if err != nil { BadDeps: map[string]string{
t.Fatal(err) "text/template": "linker bloat (MethodByName)",
} "html/template": "linker bloat (MethodByName)",
var res struct { },
Deps []string }.Check(t)
}
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))
} }

@ -1,38 +1,24 @@
// Copyright (c) Tailscale Inc & AUTHORS // Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause // 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 package jsdeps
import ( import (
"encoding/json"
"os"
"os/exec"
"testing" "testing"
"tailscale.com/tstest/deptest"
) )
func TestDeps(t *testing.T) { func TestDeps(t *testing.T) {
cmd := exec.Command("go", "list", "-json", ".") deptest.DepChecker{
cmd.Env = append(os.Environ(), "GOOS=js", "GOARCH=wasm") GOOS: "js",
out, err := cmd.Output() GOARCH: "wasm",
if err != nil { BadDeps: map[string]string{
t.Fatal(err) "runtime/pprof": "bloat",
} "golang.org/x/net/http2/h2c": "bloat",
var res struct { "net/http/pprof": "bloat",
Deps []string "golang.org/x/net/proxy": "bloat",
} "github.com/tailscale/goupnp": "bloat, which can't work anyway in wasm",
if err := json.Unmarshal(out, &res); err != nil { },
t.Fatal(err) }.Check(t)
}
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))
} }

Loading…
Cancel
Save