2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-04-29 03:56:11 +01:00
|
|
|
package tstest
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-28 15:23:12 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2020-02-05 22:16:58 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2020-08-25 19:42:54 +01:00
|
|
|
"sync"
|
2020-02-05 22:16:58 +00:00
|
|
|
"testing"
|
2020-08-25 19:42:54 +01:00
|
|
|
|
2021-09-07 23:17:39 +01:00
|
|
|
"go4.org/mem"
|
2020-08-25 19:42:54 +01: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 03:59:54 +01:00
|
|
|
|
2020-05-14 18:05:32 +01: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 03:59:54 +01:00
|
|
|
|
2020-05-14 18:05:32 +01:00
|
|
|
func (panicLogWriter) Write(b []byte) (int, error) {
|
2020-10-28 15:23:12 +00:00
|
|
|
// Allow certain phrases for now, in the interest of getting
|
|
|
|
// CI working on Windows and not having to refactor all the
|
|
|
|
// interfaces.GetState & tshttpproxy code to allow pushing
|
|
|
|
// down a Logger yet. TODO(bradfitz): do that refactoring once
|
|
|
|
// 1.2.0 is out.
|
2024-04-18 05:32:18 +01:00
|
|
|
if bytes.Contains(b, []byte("tshttpproxy: ")) ||
|
|
|
|
bytes.Contains(b, []byte("runtime/panic.go:")) ||
|
|
|
|
bytes.Contains(b, []byte("XXX")) {
|
2020-10-28 15:23:12 +00:00
|
|
|
os.Stderr.Write(b)
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("please use tailscale.com/logger.Logf instead of the log package (tried to log: %q)", b))
|
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 03:59:54 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 18:05:32 +01: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 03:59:54 +01:00
|
|
|
func PanicOnLog() {
|
2020-05-14 18:05:32 +01: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 03:59:54 +01:00
|
|
|
}
|
2020-08-25 19:42:54 +01:00
|
|
|
|
2020-09-04 16:09:56 +01:00
|
|
|
// NewLogLineTracker produces a LogLineTracker wrapping a given logf that tracks whether expectedFormatStrings were seen.
|
|
|
|
func NewLogLineTracker(logf logger.Logf, expectedFormatStrings []string) *LogLineTracker {
|
|
|
|
ret := &LogLineTracker{
|
2020-08-25 19:42:54 +01:00
|
|
|
logf: logf,
|
2020-09-04 16:09:56 +01:00
|
|
|
listenFor: expectedFormatStrings,
|
2020-08-25 19:42:54 +01:00
|
|
|
seen: make(map[string]bool),
|
|
|
|
}
|
2020-09-04 16:09:56 +01:00
|
|
|
for _, line := range expectedFormatStrings {
|
2020-08-25 19:42:54 +01:00
|
|
|
ret.seen[line] = false
|
|
|
|
}
|
2020-09-04 16:09:56 +01:00
|
|
|
return ret
|
2020-08-25 19:42:54 +01:00
|
|
|
}
|
|
|
|
|
2020-09-04 16:09:56 +01:00
|
|
|
// LogLineTracker is a logger that tracks which log format patterns it's
|
|
|
|
// seen and can report which expected ones were not seen later.
|
|
|
|
type LogLineTracker struct {
|
2020-08-25 19:42:54 +01:00
|
|
|
logf logger.Logf
|
|
|
|
listenFor []string
|
|
|
|
|
2021-01-09 00:50:03 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
closed bool
|
|
|
|
seen map[string]bool // format string => false (if not yet seen but wanted) or true (once seen)
|
2020-08-25 19:42:54 +01:00
|
|
|
}
|
|
|
|
|
2020-09-04 16:09:56 +01:00
|
|
|
// Logf logs to its underlying logger and also tracks that the given format pattern has been seen.
|
2022-03-16 23:27:57 +00:00
|
|
|
func (lt *LogLineTracker) Logf(format string, args ...any) {
|
2020-09-04 16:09:56 +01:00
|
|
|
lt.mu.Lock()
|
2021-01-09 00:50:03 +00:00
|
|
|
if lt.closed {
|
|
|
|
lt.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2020-09-04 16:09:56 +01:00
|
|
|
if v, ok := lt.seen[format]; ok && !v {
|
|
|
|
lt.seen[format] = true
|
2020-08-25 19:42:54 +01:00
|
|
|
}
|
2020-09-04 16:09:56 +01:00
|
|
|
lt.mu.Unlock()
|
2020-09-04 16:31:43 +01:00
|
|
|
lt.logf(format, args...)
|
2020-08-25 19:42:54 +01:00
|
|
|
}
|
|
|
|
|
2020-09-04 16:09:56 +01:00
|
|
|
// Check returns which format strings haven't been logged yet.
|
|
|
|
func (lt *LogLineTracker) Check() []string {
|
|
|
|
lt.mu.Lock()
|
|
|
|
defer lt.mu.Unlock()
|
2020-08-25 19:42:54 +01:00
|
|
|
var notSeen []string
|
2020-09-04 16:09:56 +01:00
|
|
|
for _, format := range lt.listenFor {
|
|
|
|
if !lt.seen[format] {
|
|
|
|
notSeen = append(notSeen, format)
|
2020-08-25 19:42:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return notSeen
|
|
|
|
}
|
2021-01-09 00:50:03 +00:00
|
|
|
|
2021-05-05 04:36:10 +01:00
|
|
|
// Reset forgets everything that it's seen.
|
|
|
|
func (lt *LogLineTracker) Reset() {
|
|
|
|
lt.mu.Lock()
|
|
|
|
defer lt.mu.Unlock()
|
|
|
|
for _, line := range lt.listenFor {
|
|
|
|
lt.seen[line] = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 00:50:03 +00:00
|
|
|
// Close closes lt. After calling Close, calls to Logf become no-ops.
|
|
|
|
func (lt *LogLineTracker) Close() {
|
|
|
|
lt.mu.Lock()
|
|
|
|
defer lt.mu.Unlock()
|
|
|
|
lt.closed = true
|
|
|
|
}
|
2021-09-07 23:17:39 +01:00
|
|
|
|
|
|
|
// MemLogger is a bytes.Buffer with a Logf method for tests that want
|
|
|
|
// to log to a buffer.
|
|
|
|
type MemLogger struct {
|
2021-09-08 03:27:19 +01:00
|
|
|
sync.Mutex
|
2021-09-07 23:17:39 +01:00
|
|
|
bytes.Buffer
|
|
|
|
}
|
|
|
|
|
2022-03-16 23:27:57 +00:00
|
|
|
func (ml *MemLogger) Logf(format string, args ...any) {
|
2021-09-08 03:27:19 +01:00
|
|
|
ml.Lock()
|
|
|
|
defer ml.Unlock()
|
2021-09-07 23:17:39 +01:00
|
|
|
fmt.Fprintf(&ml.Buffer, format, args...)
|
|
|
|
if !mem.HasSuffix(mem.B(ml.Buffer.Bytes()), mem.S("\n")) {
|
|
|
|
ml.Buffer.WriteByte('\n')
|
|
|
|
}
|
|
|
|
}
|
2021-09-09 19:35:04 +01:00
|
|
|
|
|
|
|
func (ml *MemLogger) String() string {
|
|
|
|
ml.Lock()
|
|
|
|
defer ml.Unlock()
|
|
|
|
return ml.Buffer.String()
|
|
|
|
}
|
2022-11-02 19:45:20 +00:00
|
|
|
|
|
|
|
// WhileTestRunningLogger returns a logger.Logf that logs to t.Logf until the
|
|
|
|
// test finishes, at which point it no longer logs anything.
|
|
|
|
func WhileTestRunningLogger(t testing.TB) logger.Logf {
|
|
|
|
var (
|
|
|
|
mu sync.RWMutex
|
|
|
|
done bool
|
|
|
|
)
|
2023-08-31 02:33:58 +01:00
|
|
|
tlogf := logger.TestLogger(t)
|
2022-11-02 19:45:20 +00:00
|
|
|
logger := func(format string, args ...any) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
mu.RLock()
|
|
|
|
defer mu.RUnlock()
|
|
|
|
|
|
|
|
if done {
|
|
|
|
return
|
|
|
|
}
|
2023-08-31 02:33:58 +01:00
|
|
|
tlogf(format, args...)
|
2022-11-02 19:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// t.Cleanup is run before the test is marked as done, so by acquiring
|
|
|
|
// the mutex and then disabling logs, we know that all existing log
|
|
|
|
// functions have completed, and that no future calls to the logger
|
|
|
|
// will log something.
|
|
|
|
//
|
|
|
|
// We can't do this with an atomic bool, since it's possible to
|
|
|
|
// observe the following race:
|
|
|
|
//
|
|
|
|
// test goroutine goroutine 1
|
|
|
|
// -------------- -----------
|
|
|
|
// check atomic, testFinished = no
|
|
|
|
// test finishes
|
|
|
|
// run t.Cleanups
|
|
|
|
// set testFinished = true
|
|
|
|
// call t.Logf
|
|
|
|
// panic
|
|
|
|
//
|
|
|
|
// Using a mutex ensures that all actions in goroutine 1 in the
|
|
|
|
// sequence above occur atomically, and thus should not panic.
|
|
|
|
t.Cleanup(func() {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
done = true
|
|
|
|
})
|
|
|
|
return logger
|
|
|
|
}
|