2020-02-05 22:16:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-04-29 02:56:11 +00:00
|
|
|
package tstest
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
2020-08-25 18:42:54 +00:00
|
|
|
"sync"
|
2020-02-05 22:16:58 +00:00
|
|
|
"testing"
|
2020-08-25 18:42:54 +00:00
|
|
|
|
|
|
|
"tailscale.com/types/logger"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type testLogWriter struct {
|
|
|
|
t *testing.T
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *testLogWriter) Write(b []byte) (int, error) {
|
|
|
|
w.t.Helper()
|
|
|
|
w.t.Logf("%s", b)
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func FixLogs(t *testing.T) {
|
|
|
|
log.SetFlags(log.Ltime | log.Lshortfile)
|
|
|
|
log.SetOutput(&testLogWriter{t})
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnfixLogs(t *testing.T) {
|
|
|
|
defer log.SetOutput(os.Stderr)
|
|
|
|
}
|
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>
2020-05-14 02:59:54 +00:00
|
|
|
|
2020-05-14 17:05:32 +00:00
|
|
|
type panicLogWriter struct{}
|
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>
2020-05-14 02:59:54 +00:00
|
|
|
|
2020-05-14 17:05:32 +00:00
|
|
|
func (panicLogWriter) Write(b []byte) (int, error) {
|
|
|
|
panic("please use tailscale.com/logger.Logf instead of the log package")
|
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>
2020-05-14 02:59:54 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 17:05:32 +00:00
|
|
|
// PanicOnLog modifies the standard library log package's default output to
|
|
|
|
// an io.Writer that panics, to root out code that's not plumbing their logging
|
|
|
|
// through explicit tailscale.com/logger.Logf paths.
|
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>
2020-05-14 02:59:54 +00:00
|
|
|
func PanicOnLog() {
|
2020-05-14 17:05:32 +00:00
|
|
|
log.SetOutput(panicLogWriter{})
|
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>
2020-05-14 02:59:54 +00:00
|
|
|
}
|
2020-08-25 18:42:54 +00:00
|
|
|
|
|
|
|
// ListenFor produces a LogListener wrapping a given logf with the given logStrings
|
|
|
|
func ListenFor(logf logger.Logf, logStrings []string) *LogListener {
|
|
|
|
ret := LogListener{
|
|
|
|
logf: logf,
|
|
|
|
listenFor: logStrings,
|
|
|
|
seen: make(map[string]bool),
|
|
|
|
}
|
|
|
|
for _, line := range logStrings {
|
|
|
|
ret.seen[line] = false
|
|
|
|
}
|
|
|
|
return &ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogListener takes a list of log lines to listen for
|
|
|
|
type LogListener struct {
|
|
|
|
logf logger.Logf
|
|
|
|
listenFor []string
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
seen map[string]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logf records and logs a given line
|
|
|
|
func (ll *LogListener) Logf(format string, args ...interface{}) {
|
|
|
|
ll.mu.Lock()
|
|
|
|
if _, ok := ll.seen[format]; ok {
|
|
|
|
ll.seen[format] = true
|
|
|
|
}
|
|
|
|
ll.mu.Unlock()
|
|
|
|
ll.logf(format, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check returns which lines haven't been logged yet
|
|
|
|
func (ll *LogListener) Check() []string {
|
|
|
|
ll.mu.Lock()
|
|
|
|
defer ll.mu.Unlock()
|
|
|
|
var notSeen []string
|
|
|
|
for _, line := range ll.listenFor {
|
|
|
|
if !ll.seen[line] {
|
|
|
|
notSeen = append(notSeen, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return notSeen
|
|
|
|
}
|