all: update tests to use tstest.MemLogger

And give MemLogger a mutex, as one caller had, which does match the logf
contract better.

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/2817/head
Brad Fitzpatrick 3 years ago committed by Brad Fitzpatrick
parent 48bdffd395
commit 640134421e

@ -17,6 +17,7 @@ import (
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/ipn" "tailscale.com/ipn"
"tailscale.com/ipn/ipnstate" "tailscale.com/ipn/ipnstate"
"tailscale.com/tstest"
"tailscale.com/types/persist" "tailscale.com/types/persist"
"tailscale.com/types/preftype" "tailscale.com/types/preftype"
) )
@ -607,10 +608,7 @@ func TestPrefsFromUpArgs(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
var warnBuf bytes.Buffer var warnBuf tstest.MemLogger
warnf := func(format string, a ...interface{}) {
fmt.Fprintf(&warnBuf, format, a...)
}
goos := tt.goos goos := tt.goos
if goos == "" { if goos == "" {
goos = "linux" goos = "linux"
@ -619,7 +617,7 @@ func TestPrefsFromUpArgs(t *testing.T) {
if st == nil { if st == nil {
st = new(ipnstate.Status) st = new(ipnstate.Status)
} }
got, err := prefsFromUpArgs(tt.args, warnf, st, goos) got, err := prefsFromUpArgs(tt.args, warnBuf.Logf, st, goos)
gotErr := fmt.Sprint(err) gotErr := fmt.Sprint(err)
if tt.wantErr != "" { if tt.wantErr != "" {
if tt.wantErr != gotErr { if tt.wantErr != gotErr {

@ -20,16 +20,13 @@ import (
"testing" "testing"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/tstest"
) )
type peerAPITestEnv struct { type peerAPITestEnv struct {
ph *peerAPIHandler ph *peerAPIHandler
rr *httptest.ResponseRecorder rr *httptest.ResponseRecorder
logBuf bytes.Buffer logBuf tstest.MemLogger
}
func (e *peerAPITestEnv) logf(format string, a ...interface{}) {
fmt.Fprintf(&e.logBuf, format, a...)
} }
type check func(*testing.T, *peerAPITestEnv) type check func(*testing.T, *peerAPITestEnv)
@ -403,7 +400,7 @@ func TestHandlePeerAPI(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
var e peerAPITestEnv var e peerAPITestEnv
lb := &LocalBackend{ lb := &LocalBackend{
logf: e.logf, logf: e.logBuf.Logf,
capFileSharing: tt.capSharing, capFileSharing: tt.capSharing,
} }
e.ph = &peerAPIHandler{ e.ph = &peerAPIHandler{

@ -5,14 +5,13 @@
package dns package dns
import ( import (
"bytes"
"errors" "errors"
"fmt"
"io/fs" "io/fs"
"os" "os"
"strings" "strings"
"testing" "testing"
"tailscale.com/tstest"
"tailscale.com/util/cmpver" "tailscale.com/util/cmpver"
) )
@ -146,12 +145,8 @@ func TestLinuxDNSMode(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
var logBuf bytes.Buffer var logBuf tstest.MemLogger
logf := func(format string, a ...interface{}) { got, err := dnsMode(logBuf.Logf, tt.env)
fmt.Fprintf(&logBuf, format, a...)
logBuf.WriteByte('\n')
}
got, err := dnsMode(logf, tt.env)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

@ -5,7 +5,6 @@
package portmapper package portmapper
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"io" "io"
@ -17,6 +16,7 @@ import (
"testing" "testing"
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/tstest"
) )
// Google Wifi // Google Wifi
@ -97,12 +97,8 @@ func TestGetUPnPClient(t *testing.T) {
})) }))
defer ts.Close() defer ts.Close()
gw, _ := netaddr.FromStdIP(ts.Listener.Addr().(*net.TCPAddr).IP) gw, _ := netaddr.FromStdIP(ts.Listener.Addr().(*net.TCPAddr).IP)
var logBuf bytes.Buffer var logBuf tstest.MemLogger
logf := func(format string, a ...interface{}) { c, err := getUPnPClient(context.Background(), logBuf.Logf, gw, uPnPDiscoResponse{
fmt.Fprintf(&logBuf, format, a...)
logBuf.WriteByte('\n')
}
c, err := getUPnPClient(context.Background(), logf, gw, uPnPDiscoResponse{
Location: ts.URL + "/rootDesc.xml", Location: ts.URL + "/rootDesc.xml",
}) })
if err != nil { if err != nil {

@ -127,10 +127,13 @@ func (lt *LogLineTracker) Close() {
// MemLogger is a bytes.Buffer with a Logf method for tests that want // MemLogger is a bytes.Buffer with a Logf method for tests that want
// to log to a buffer. // to log to a buffer.
type MemLogger struct { type MemLogger struct {
sync.Mutex
bytes.Buffer bytes.Buffer
} }
func (ml *MemLogger) Logf(format string, args ...interface{}) { func (ml *MemLogger) Logf(format string, args ...interface{}) {
ml.Lock()
defer ml.Unlock()
fmt.Fprintf(&ml.Buffer, format, args...) fmt.Fprintf(&ml.Buffer, format, args...)
if !mem.HasSuffix(mem.B(ml.Buffer.Bytes()), mem.S("\n")) { if !mem.HasSuffix(mem.B(ml.Buffer.Bytes()), mem.S("\n")) {
ml.Buffer.WriteByte('\n') ml.Buffer.WriteByte('\n')

@ -1315,26 +1315,6 @@ func BenchmarkReceiveFrom_Native(b *testing.B) {
} }
} }
type bufLog struct {
sync.Mutex
buf bytes.Buffer
}
func (b *bufLog) Logf(format string, args ...interface{}) {
b.Lock()
defer b.Unlock()
fmt.Fprintf(&b.buf, format, args...)
if !bytes.HasPrefix(b.buf.Bytes(), []byte("\n")) {
b.buf.WriteByte('\n')
}
}
func (b *bufLog) String() string {
b.Lock()
defer b.Unlock()
return b.buf.String()
}
// Test that a netmap update where node changes its node key but // Test that a netmap update where node changes its node key but
// doesn't change its disco key doesn't result in a broken state. // doesn't change its disco key doesn't result in a broken state.
// //
@ -1342,7 +1322,7 @@ func (b *bufLog) String() string {
func TestSetNetworkMapChangingNodeKey(t *testing.T) { func TestSetNetworkMapChangingNodeKey(t *testing.T) {
conn := newTestConn(t) conn := newTestConn(t)
t.Cleanup(func() { conn.Close() }) t.Cleanup(func() { conn.Close() })
var buf bufLog var buf tstest.MemLogger
conn.logf = buf.Logf conn.logf = buf.Logf
conn.SetPrivateKey(wgkey.Private{0: 1}) conn.SetPrivateKey(wgkey.Private{0: 1})
@ -1401,7 +1381,7 @@ func TestSetNetworkMapChangingNodeKey(t *testing.T) {
func TestRebindStress(t *testing.T) { func TestRebindStress(t *testing.T) {
conn := newTestConn(t) conn := newTestConn(t)
var buf bufLog var buf tstest.MemLogger
conn.logf = buf.Logf conn.logf = buf.Logf
closed := false closed := false

Loading…
Cancel
Save