Skip to content

Commit 189e03e

Browse files
committed
net/portmapper: fix test flakes from logging after test done
Fixes tailscale#15794 Change-Id: Ic22aa99acb10fdb6dc5f0b6482e722e48237703c Signed-off-by: Brad Fitzpatrick <[email protected]>
1 parent 66371f3 commit 189e03e

File tree

5 files changed

+17
-13
lines changed

5 files changed

+17
-13
lines changed

net/portmapper/igd_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import (
1818
"tailscale.com/net/netaddr"
1919
"tailscale.com/net/netmon"
2020
"tailscale.com/syncs"
21+
"tailscale.com/tstest"
2122
"tailscale.com/types/logger"
2223
"tailscale.com/util/eventbus"
24+
"tailscale.com/util/testenv"
2325
)
2426

2527
// TestIGD is an IGD (Internet Gateway Device) for testing. It supports fake
@@ -64,7 +66,8 @@ type igdCounters struct {
6466
invalidPCPMapPkt int32
6567
}
6668

67-
func NewTestIGD(logf logger.Logf, t TestIGDOptions) (*TestIGD, error) {
69+
func NewTestIGD(tb testenv.TB, t TestIGDOptions) (*TestIGD, error) {
70+
logf := tstest.WhileTestRunningLogger(tb)
6871
d := &TestIGD{
6972
doPMP: t.PMP,
7073
doPCP: t.PCP,
@@ -265,7 +268,7 @@ func (d *TestIGD) handlePCPQuery(pkt []byte, src netip.AddrPort) {
265268
func newTestClient(t *testing.T, igd *TestIGD, bus *eventbus.Bus) *Client {
266269
var c *Client
267270
c = NewClient(Config{
268-
Logf: t.Logf,
271+
Logf: tstest.WhileTestRunningLogger(t),
269272
NetMon: netmon.NewStatic(),
270273
ControlKnobs: new(controlknobs.Knobs),
271274
EventBus: bus,

net/portmapper/portmapper_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestClientProbeThenMap(t *testing.T) {
6161
}
6262

6363
func TestProbeIntegration(t *testing.T) {
64-
igd, err := NewTestIGD(t.Logf, TestIGDOptions{PMP: true, PCP: true, UPnP: true})
64+
igd, err := NewTestIGD(t, TestIGDOptions{PMP: true, PCP: true, UPnP: true})
6565
if err != nil {
6666
t.Fatal(err)
6767
}
@@ -95,7 +95,7 @@ func TestProbeIntegration(t *testing.T) {
9595
}
9696

9797
func TestPCPIntegration(t *testing.T) {
98-
igd, err := NewTestIGD(t.Logf, TestIGDOptions{PMP: false, PCP: true, UPnP: false})
98+
igd, err := NewTestIGD(t, TestIGDOptions{PMP: false, PCP: true, UPnP: false})
9999
if err != nil {
100100
t.Fatal(err)
101101
}
@@ -137,7 +137,7 @@ func TestGetUPnPErrorsMetric(t *testing.T) {
137137
}
138138

139139
func TestUpdateEvent(t *testing.T) {
140-
igd, err := NewTestIGD(t.Logf, TestIGDOptions{PCP: true})
140+
igd, err := NewTestIGD(t, TestIGDOptions{PCP: true})
141141
if err != nil {
142142
t.Fatalf("Create test gateway: %v", err)
143143
}

net/portmapper/select_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestSelectBestService(t *testing.T) {
2828
}
2929

3030
// Run a fake IGD server to respond to UPnP requests.
31-
igd, err := NewTestIGD(t.Logf, TestIGDOptions{UPnP: true})
31+
igd, err := NewTestIGD(t, TestIGDOptions{UPnP: true})
3232
if err != nil {
3333
t.Fatal(err)
3434
}

net/portmapper/upnp_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ func TestGetUPnPClient(t *testing.T) {
533533
}
534534

535535
func TestGetUPnPPortMapping(t *testing.T) {
536-
igd, err := NewTestIGD(t.Logf, TestIGDOptions{UPnP: true})
536+
igd, err := NewTestIGD(t, TestIGDOptions{UPnP: true})
537537
if err != nil {
538538
t.Fatal(err)
539539
}
@@ -672,7 +672,7 @@ func TestGetUPnPPortMapping_LeaseDuration(t *testing.T) {
672672
"DeletePortMapping": "", // Do nothing for test
673673
}
674674

675-
igd, err := NewTestIGD(t.Logf, TestIGDOptions{UPnP: true})
675+
igd, err := NewTestIGD(t, TestIGDOptions{UPnP: true})
676676
if err != nil {
677677
t.Fatal(err)
678678
}
@@ -722,7 +722,7 @@ func TestGetUPnPPortMapping_LeaseDuration(t *testing.T) {
722722
//
723723
// See https://github.com/tailscale/tailscale/issues/10911
724724
func TestGetUPnPPortMapping_NoValidServices(t *testing.T) {
725-
igd, err := NewTestIGD(t.Logf, TestIGDOptions{UPnP: true})
725+
igd, err := NewTestIGD(t, TestIGDOptions{UPnP: true})
726726
if err != nil {
727727
t.Fatal(err)
728728
}
@@ -753,7 +753,7 @@ func TestGetUPnPPortMapping_NoValidServices(t *testing.T) {
753753

754754
// Tests the legacy behaviour with the pre-UPnP standard portmapping service.
755755
func TestGetUPnPPortMapping_Legacy(t *testing.T) {
756-
igd, err := NewTestIGD(t.Logf, TestIGDOptions{UPnP: true})
756+
igd, err := NewTestIGD(t, TestIGDOptions{UPnP: true})
757757
if err != nil {
758758
t.Fatal(err)
759759
}
@@ -796,7 +796,7 @@ func TestGetUPnPPortMapping_Legacy(t *testing.T) {
796796
}
797797

798798
func TestGetUPnPPortMappingNoResponses(t *testing.T) {
799-
igd, err := NewTestIGD(t.Logf, TestIGDOptions{UPnP: true})
799+
igd, err := NewTestIGD(t, TestIGDOptions{UPnP: true})
800800
if err != nil {
801801
t.Fatal(err)
802802
}
@@ -912,7 +912,7 @@ func TestGetUPnPPortMapping_Invalid(t *testing.T) {
912912
"127.0.0.1",
913913
} {
914914
t.Run(responseAddr, func(t *testing.T) {
915-
igd, err := NewTestIGD(t.Logf, TestIGDOptions{UPnP: true})
915+
igd, err := NewTestIGD(t, TestIGDOptions{UPnP: true})
916916
if err != nil {
917917
t.Fatal(err)
918918
}

tstest/log.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"go4.org/mem"
1515
"tailscale.com/types/logger"
16+
"tailscale.com/util/testenv"
1617
)
1718

1819
type testLogWriter struct {
@@ -149,7 +150,7 @@ func (ml *MemLogger) String() string {
149150

150151
// WhileTestRunningLogger returns a logger.Logf that logs to t.Logf until the
151152
// test finishes, at which point it no longer logs anything.
152-
func WhileTestRunningLogger(t testing.TB) logger.Logf {
153+
func WhileTestRunningLogger(t testenv.TB) logger.Logf {
153154
var (
154155
mu sync.RWMutex
155156
done bool

0 commit comments

Comments
 (0)