all: use tstest.Replace more

Signed-off-by: Maisem Ali <maisem@tailscale.com>
This commit is contained in:
Maisem Ali
2023-03-04 12:04:55 -08:00
committed by Maisem Ali
parent 57a44846ae
commit 1a30b2d73f
12 changed files with 35 additions and 58 deletions

View File

@@ -997,11 +997,8 @@ func TestMarshalResponseFormatError(t *testing.T) {
}
func TestForwardLinkSelection(t *testing.T) {
old := initListenConfig
defer func() { initListenConfig = old }()
configCall := make(chan string, 1)
initListenConfig = func(nc *net.ListenConfig, mon *monitor.Mon, tunName string) error {
tstest.Replace(t, &initListenConfig, func(nc *net.ListenConfig, mon *monitor.Mon, tunName string) error {
select {
case configCall <- tunName:
return nil
@@ -1009,7 +1006,7 @@ func TestForwardLinkSelection(t *testing.T) {
t.Error("buffer full")
return errors.New("buffer full")
}
}
})
// specialIP is some IP we pretend that our link selector
// routes differently.

View File

@@ -10,14 +10,14 @@ import (
"os"
"path/filepath"
"testing"
"tailscale.com/tstest"
)
// test the specific /proc/net/route path as found on Google Cloud Run instances
func TestGoogleCloudRunDefaultRouteInterface(t *testing.T) {
dir := t.TempDir()
savedProcNetRoutePath := procNetRoutePath
defer func() { procNetRoutePath = savedProcNetRoutePath }()
procNetRoutePath = filepath.Join(dir, "CloudRun")
tstest.Replace(t, &procNetRoutePath, filepath.Join(dir, "CloudRun"))
buf := []byte("Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\tMTU\tWindow\tIRTT\n" +
"eth0\t8008FEA9\t00000000\t0001\t0\t0\t0\t01FFFFFF\t0\t0\t0\n" +
"eth1\t00000000\t00000000\t0001\t0\t0\t0\t00000000\t0\t0\t0\n")
@@ -39,9 +39,7 @@ func TestGoogleCloudRunDefaultRouteInterface(t *testing.T) {
// size can be handled.
func TestExtremelyLongProcNetRoute(t *testing.T) {
dir := t.TempDir()
savedProcNetRoutePath := procNetRoutePath
defer func() { procNetRoutePath = savedProcNetRoutePath }()
procNetRoutePath = filepath.Join(dir, "VeryLong")
tstest.Replace(t, &procNetRoutePath, filepath.Join(dir, "VeryLong"))
f, err := os.Create(procNetRoutePath)
if err != nil {
t.Fatal(err)
@@ -76,9 +74,7 @@ func TestExtremelyLongProcNetRoute(t *testing.T) {
// test the specific /proc/net/route path as found on AWS App Runner instances
func TestAwsAppRunnerDefaultRouteInterface(t *testing.T) {
dir := t.TempDir()
savedProcNetRoutePath := procNetRoutePath
defer func() { procNetRoutePath = savedProcNetRoutePath }()
procNetRoutePath = filepath.Join(dir, "CloudRun")
tstest.Replace(t, &procNetRoutePath, filepath.Join(dir, "CloudRun"))
buf := []byte("Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\tMTU\tWindow\tIRTT\n" +
"eth0\t00000000\tF9AFFEA9\t0003\t0\t0\t0\t00000000\t0\t0\t0\n" +
"*\tFEA9FEA9\t00000000\t0005\t0\t0\t0\tFFFFFFFF\t0\t0\t0\n" +

View File

@@ -16,6 +16,8 @@ import (
"strings"
"testing"
"time"
"tailscale.com/tstest"
)
func TestSynologyProxyFromConfigCached(t *testing.T) {
@@ -24,9 +26,7 @@ func TestSynologyProxyFromConfigCached(t *testing.T) {
t.Fatal(err)
}
var orig string
orig, synologyProxyConfigPath = synologyProxyConfigPath, filepath.Join(t.TempDir(), "proxy.conf")
defer func() { synologyProxyConfigPath = orig }()
tstest.Replace(t, &synologyProxyConfigPath, filepath.Join(t.TempDir(), "proxy.conf"))
t.Run("no config file", func(t *testing.T) {
if _, err := os.Stat(synologyProxyConfigPath); err == nil {
@@ -160,11 +160,9 @@ func TestSynologyProxiesFromConfig(t *testing.T) {
openReader io.ReadCloser
openErr error
)
var origOpen func() (io.ReadCloser, error)
origOpen, openSynologyProxyConf = openSynologyProxyConf, func() (io.ReadCloser, error) {
tstest.Replace(t, &openSynologyProxyConf, func() (io.ReadCloser, error) {
return openReader, openErr
}
defer func() { openSynologyProxyConf = origOpen }()
})
t.Run("with config", func(t *testing.T) {
mc := &mustCloser{Reader: strings.NewReader(`