Add tstest.PanicOnLog(), and fix various problems detected by this.

If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.

This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.

To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.

Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
This commit is contained in:
Avery Pennarun
2020-05-13 22:59:54 -04:00
parent e0b666c5d2
commit 08acb502e5
18 changed files with 206 additions and 108 deletions
+33 -15
View File
@@ -39,8 +39,15 @@ func init() {
}
func TestIPN(t *testing.T) {
tstest.FixLogs(t)
defer tstest.UnfixLogs(t)
tstest.PanicOnLog()
// This gets reassigned inside every test, so that the connections
// all log using the "current" t.Logf function. Sigh.
current_t := t
logf := func(s string, args ...interface{}) {
current_t.Helper()
current_t.Logf(s, args...)
}
// Turn off STUN for the test to make it hermetic.
// TODO(crawshaw): add a test that runs against a local STUN server.
@@ -67,7 +74,7 @@ func TestIPN(t *testing.T) {
if err != nil {
t.Fatalf("create tempdir: %v\n", err)
}
ctl, err = control.New(tmpdir, tmpdir, tmpdir, serverURL, true, t.Logf)
ctl, err = control.New(tmpdir, tmpdir, tmpdir, serverURL, true, logf)
if err != nil {
t.Fatalf("create control server: %v\n", ctl)
}
@@ -75,18 +82,20 @@ func TestIPN(t *testing.T) {
t.Fatal(err)
}
n1 := newNode(t, "n1", https, false)
n1 := newNode(t, logf, "n1", https, false)
defer n1.Backend.Shutdown()
n1.Backend.StartLoginInteractive()
n2 := newNode(t, "n2", https, true)
n2 := newNode(t, logf, "n2", https, true)
defer n2.Backend.Shutdown()
n2.Backend.StartLoginInteractive()
t.Run("login", func(t *testing.T) {
current_t = t
var s1, s2 State
for {
t.Logf("\n\nn1.state=%v n2.state=%v\n\n", s1, s2)
logf("\n\nn1.state=%v n2.state=%v\n\n", s1, s2)
// TODO(crawshaw): switch from || to &&. To do this we need to
// transmit some data so that the handshake completes on both
@@ -102,7 +111,7 @@ func TestIPN(t *testing.T) {
select {
case n := <-n1.NotifyCh:
t.Logf("n1n: %v\n", n)
logf("n1n: %v\n", n)
if n.State != nil {
s1 = *n.State
if s1 == NeedsMachineAuth {
@@ -110,7 +119,7 @@ func TestIPN(t *testing.T) {
}
}
case n := <-n2.NotifyCh:
t.Logf("n2n: %v\n", n)
logf("n2n: %v\n", n)
if n.State != nil {
s2 = *n.State
if s2 == NeedsMachineAuth {
@@ -122,10 +131,13 @@ func TestIPN(t *testing.T) {
}
}
})
current_t = t
n1addr := n1.Backend.NetMap().Addresses[0].IP
n2addr := n2.Backend.NetMap().Addresses[0].IP
t.Run("ping n2", func(t *testing.T) {
current_t = t
t.Skip("TODO(crawshaw): skipping ping test, it is flaky")
msg := tuntest.Ping(n2addr.IP(), n1addr.IP())
n1.ChannelTUN.Outbound <- msg
@@ -138,7 +150,10 @@ func TestIPN(t *testing.T) {
t.Error("no ping seen")
}
})
current_t = t
t.Run("ping n1", func(t *testing.T) {
current_t = t
t.Skip("TODO(crawshaw): skipping ping test, it is flaky")
msg := tuntest.Ping(n1addr.IP(), n2addr.IP())
n2.ChannelTUN.Outbound <- msg
@@ -151,6 +166,7 @@ func TestIPN(t *testing.T) {
t.Error("no ping seen")
}
})
current_t = t
drain:
for {
@@ -165,6 +181,8 @@ drain:
n1.Backend.Logout()
t.Run("logout", func(t *testing.T) {
current_t = t
var s State
for {
select {
@@ -173,7 +191,7 @@ drain:
continue
}
s = *n.State
t.Logf("n.State=%v", s)
logf("n.State=%v", s)
if s == NeedsLogin {
return
}
@@ -182,6 +200,7 @@ drain:
}
}
})
current_t = t
}
type testNode struct {
@@ -191,12 +210,11 @@ type testNode struct {
}
// Create a new IPN node.
func newNode(t *testing.T, prefix string, https *httptest.Server, weirdPrefs bool) testNode {
func newNode(t *testing.T, logfx logger.Logf, prefix string, https *httptest.Server, weirdPrefs bool) testNode {
t.Helper()
logfe := logger.WithPrefix(t.Logf, prefix+"e: ")
logf := logger.WithPrefix(t.Logf, prefix+": ")
logfe := logger.WithPrefix(logfx, prefix+"e: ")
logf := logger.WithPrefix(logfx, prefix+": ")
var err error
httpc := https.Client()
@@ -241,7 +259,7 @@ func newNode(t *testing.T, prefix string, https *httptest.Server, weirdPrefs boo
Notify: func(n Notify) {
// Automatically visit auth URLs
if n.BrowseToURL != nil {
t.Logf("BrowseToURL: %v", *n.BrowseToURL)
logf("BrowseToURL: %v", *n.BrowseToURL)
authURL := *n.BrowseToURL
i := strings.Index(authURL, "/a/")
@@ -258,7 +276,7 @@ func newNode(t *testing.T, prefix string, https *httptest.Server, weirdPrefs boo
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
if _, err := httpc.Do(req); err != nil {
t.Logf("BrowseToURL: %v\n", err)
logf("BrowseToURL: %v\n", err)
}
}
nch <- n
+2 -2
View File
@@ -164,7 +164,7 @@ func Run(rctx context.Context, logf logger.Logf, logid string, opts Options, e w
}
}
bo := backoff.Backoff{Name: "ipnserver"}
bo := backoff.NewBackoff("ipnserver", logf)
for i := 1; rctx.Err() == nil; i++ {
s, err = listen.Accept()
@@ -229,7 +229,7 @@ func BabysitProc(ctx context.Context, args []string, logf logger.Logf) {
proc.mu.Unlock()
}()
bo := backoff.Backoff{Name: "BabysitProc"}
bo := backoff.NewBackoff("BabysitProc", logf)
for {
startTime := time.Now()
+4 -4
View File
@@ -73,7 +73,7 @@ func NewLocalBackend(logf logger.Logf, logid string, store StateStore, e wgengin
}
// Default filter blocks everything, until Start() is called.
e.SetFilter(filter.NewAllowNone())
e.SetFilter(filter.NewAllowNone(logf))
ctx, cancel := context.WithCancel(context.Background())
portpoll, err := portlist.NewPoller()
@@ -360,11 +360,11 @@ func (b *LocalBackend) updateFilter(netMap *controlclient.NetworkMap) {
if netMap == nil {
// Not configured yet, block everything
b.logf("netmap packet filter: (not ready yet)")
b.e.SetFilter(filter.NewAllowNone())
b.e.SetFilter(filter.NewAllowNone(b.logf))
} else if b.Prefs().ShieldsUp {
// Shields up, block everything
b.logf("netmap packet filter: (shields up)")
b.e.SetFilter(filter.NewAllowNone())
b.e.SetFilter(filter.NewAllowNone(b.logf))
} else {
now := time.Now()
if now.Sub(b.lastFilterPrint) > 1*time.Minute {
@@ -373,7 +373,7 @@ func (b *LocalBackend) updateFilter(netMap *controlclient.NetworkMap) {
} else {
b.logf("netmap packet filter: (length %d)", len(netMap.PacketFilter))
}
b.e.SetFilter(filter.New(netMap.PacketFilter, b.e.GetFilter()))
b.e.SetFilter(filter.New(netMap.PacketFilter, b.e.GetFilter(), b.logf))
}
}
+2 -4
View File
@@ -13,8 +13,7 @@ import (
)
func TestReadWrite(t *testing.T) {
tstest.FixLogs(t)
defer tstest.UnfixLogs(t)
tstest.PanicOnLog()
rc := tstest.NewResourceCheck()
defer rc.Assert(t)
@@ -62,8 +61,7 @@ func TestReadWrite(t *testing.T) {
}
func TestClientServer(t *testing.T) {
tstest.FixLogs(t)
defer tstest.UnfixLogs(t)
tstest.PanicOnLog()
rc := tstest.NewResourceCheck()
defer rc.Assert(t)
+7
View File
@@ -10,6 +10,7 @@ import (
"github.com/tailscale/wireguard-go/wgcfg"
"tailscale.com/control/controlclient"
"tailscale.com/tstest"
)
func fieldsOf(t reflect.Type) (fields []string) {
@@ -20,6 +21,8 @@ func fieldsOf(t reflect.Type) (fields []string) {
}
func TestPrefsEqual(t *testing.T) {
tstest.PanicOnLog()
prefsHandles := []string{"ControlURL", "RouteAll", "AllowSingleHosts", "CorpDNS", "WantRunning", "ShieldsUp", "AdvertiseRoutes", "AdvertiseTags", "NoSNAT", "NotepadURLs", "DisableDERP", "Persist"}
if have := fieldsOf(reflect.TypeOf(Prefs{})); !reflect.DeepEqual(have, prefsHandles) {
t.Errorf("Prefs.Equal check might be out of sync\nfields: %q\nhandled: %q\n",
@@ -231,6 +234,8 @@ func checkPrefs(t *testing.T, p Prefs) {
}
func TestBasicPrefs(t *testing.T) {
tstest.PanicOnLog()
p := Prefs{
ControlURL: "https://login.tailscale.com",
}
@@ -238,6 +243,8 @@ func TestBasicPrefs(t *testing.T) {
}
func TestPrefsPersist(t *testing.T) {
tstest.PanicOnLog()
c := controlclient.Persist{
LoginName: "test@example.com",
}
+6
View File
@@ -8,6 +8,8 @@ import (
"io/ioutil"
"os"
"testing"
"tailscale.com/tstest"
)
func testStoreSemantics(t *testing.T, store StateStore) {
@@ -76,11 +78,15 @@ func testStoreSemantics(t *testing.T, store StateStore) {
}
func TestMemoryStore(t *testing.T) {
tstest.PanicOnLog()
store := &MemoryStore{}
testStoreSemantics(t, store)
}
func TestFileStore(t *testing.T) {
tstest.PanicOnLog()
f, err := ioutil.TempFile("", "test_ipn_store")
if err != nil {
t.Fatal(err)