tstest: add MemLogger bytes.Buffer wrapper with Logf method

We use it tons of places. Updated three at least in this PR.

Another use in next commit.

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/2808/head
Brad Fitzpatrick 3 years ago committed by Brad Fitzpatrick
parent 30a614f9b9
commit 4c68b7df7c

@ -12,6 +12,7 @@ import (
"sync"
"testing"
"go4.org/mem"
"tailscale.com/types/logger"
)
@ -122,3 +123,16 @@ func (lt *LogLineTracker) Close() {
defer lt.mu.Unlock()
lt.closed = true
}
// MemLogger is a bytes.Buffer with a Logf method for tests that want
// to log to a buffer.
type MemLogger struct {
bytes.Buffer
}
func (ml *MemLogger) Logf(format string, args ...interface{}) {
fmt.Fprintf(&ml.Buffer, format, args...)
if !mem.HasSuffix(mem.B(ml.Buffer.Bytes()), mem.S("\n")) {
ml.Buffer.WriteByte('\n')
}
}

@ -5,7 +5,6 @@
package router
import (
"bytes"
"errors"
"fmt"
"math/rand"
@ -18,6 +17,7 @@ import (
"github.com/google/go-cmp/cmp"
"golang.zx2c4.com/wireguard/tun"
"inet.af/netaddr"
"tailscale.com/tstest"
"tailscale.com/types/logger"
"tailscale.com/wgengine/monitor"
)
@ -660,13 +660,8 @@ func TestDelRouteIdempotent(t *testing.T) {
tun := createTestTUN(t)
defer tun.Close()
var logOutput bytes.Buffer
logf := func(format string, args ...interface{}) {
fmt.Fprintf(&logOutput, format, args...)
if !bytes.HasSuffix(logOutput.Bytes(), []byte("\n")) {
logOutput.WriteByte('\n')
}
}
var logOutput tstest.MemLogger
logf := logOutput.Logf
mon, err := monitor.New(logger.Discard)
if err != nil {

@ -5,7 +5,6 @@
package wgengine
import (
"bytes"
"fmt"
"reflect"
"testing"
@ -15,6 +14,7 @@ import (
"tailscale.com/net/dns"
"tailscale.com/net/tstun"
"tailscale.com/tailcfg"
"tailscale.com/tstest"
"tailscale.com/tstime/mono"
"tailscale.com/types/key"
"tailscale.com/types/netmap"
@ -25,7 +25,7 @@ import (
func TestNoteReceiveActivity(t *testing.T) {
now := mono.Time(123456)
var logBuf bytes.Buffer
var logBuf tstest.MemLogger
confc := make(chan bool, 1)
gotConf := func() bool {
@ -37,11 +37,9 @@ func TestNoteReceiveActivity(t *testing.T) {
}
}
e := &userspaceEngine{
timeNow: func() mono.Time { return now },
recvActivityAt: map[tailcfg.NodeKey]mono.Time{},
logf: func(format string, a ...interface{}) {
fmt.Fprintf(&logBuf, format, a...)
},
timeNow: func() mono.Time { return now },
recvActivityAt: map[tailcfg.NodeKey]mono.Time{},
logf: logBuf.Logf,
tundev: new(tstun.Wrapper),
testMaybeReconfigHook: func() { confc <- true },
trimmedNodes: map[tailcfg.NodeKey]bool{},

@ -5,12 +5,13 @@
package wgengine
import (
"bytes"
"fmt"
"runtime"
"strings"
"testing"
"time"
"tailscale.com/tstest"
)
func TestWatchdog(t *testing.T) {
@ -52,11 +53,9 @@ func TestWatchdog(t *testing.T) {
wdEngine := e.(*watchdogEngine)
wdEngine.maxWait = maxWaitMultiple * 100 * time.Millisecond
logBuf := new(bytes.Buffer)
logBuf := new(tstest.MemLogger)
fatalCalled := make(chan struct{})
wdEngine.logf = func(format string, args ...interface{}) {
fmt.Fprintf(logBuf, format+"\n", args...)
}
wdEngine.logf = logBuf.Logf
wdEngine.fatalf = func(format string, args ...interface{}) {
t.Logf("FATAL: %s", fmt.Sprintf(format, args...))
fatalCalled <- struct{}{}

Loading…
Cancel
Save