tstest: add method to Replace values for tests

We have many function pointers that we replace for the duration of test and
restore it on test completion, add method to do that.

Signed-off-by: Maisem Ali <maisem@tailscale.com>
This commit is contained in:
Maisem Ali
2023-03-03 16:18:59 -08:00
committed by Maisem Ali
parent 12100320d2
commit b9ebf7cf14
5 changed files with 48 additions and 12 deletions

View File

@@ -13,6 +13,8 @@ import (
"reflect"
"testing"
"time"
"tailscale.com/tstest"
)
var dialTest = flag.String("dial-test", "", "if non-empty, addr:port to test dial")
@@ -142,9 +144,7 @@ func TestResolverAllHostStaticResult(t *testing.T) {
}
func TestShouldTryBootstrap(t *testing.T) {
oldDebug := debug
t.Cleanup(func() { debug = oldDebug })
debug = func() bool { return true }
tstest.Replace(t, &debug, func() bool { return true })
type step struct {
ip netip.Addr // IP we pretended to dial

View File

@@ -22,6 +22,7 @@ import (
"tailscale.com/net/stun"
"tailscale.com/net/stun/stuntest"
"tailscale.com/tailcfg"
"tailscale.com/tstest"
)
func TestHairpinSTUN(t *testing.T) {
@@ -679,9 +680,7 @@ func TestNoCaptivePortalWhenUDP(t *testing.T) {
}
})
oldTransport := noRedirectClient.Transport
t.Cleanup(func() { noRedirectClient.Transport = oldTransport })
noRedirectClient.Transport = tr
tstest.Replace(t, &noRedirectClient.Transport, http.RoundTripper(tr))
stunAddr, cleanup := stuntest.Serve(t)
defer cleanup()