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" "sync"
"testing" "testing"
"go4.org/mem"
"tailscale.com/types/logger" "tailscale.com/types/logger"
) )
@ -122,3 +123,16 @@ func (lt *LogLineTracker) Close() {
defer lt.mu.Unlock() defer lt.mu.Unlock()
lt.closed = true 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 package router
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand"
@ -18,6 +17,7 @@ import (
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/tun"
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/tstest"
"tailscale.com/types/logger" "tailscale.com/types/logger"
"tailscale.com/wgengine/monitor" "tailscale.com/wgengine/monitor"
) )
@ -660,13 +660,8 @@ func TestDelRouteIdempotent(t *testing.T) {
tun := createTestTUN(t) tun := createTestTUN(t)
defer tun.Close() defer tun.Close()
var logOutput bytes.Buffer var logOutput tstest.MemLogger
logf := func(format string, args ...interface{}) { logf := logOutput.Logf
fmt.Fprintf(&logOutput, format, args...)
if !bytes.HasSuffix(logOutput.Bytes(), []byte("\n")) {
logOutput.WriteByte('\n')
}
}
mon, err := monitor.New(logger.Discard) mon, err := monitor.New(logger.Discard)
if err != nil { if err != nil {

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

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

Loading…
Cancel
Save