tstest/integration: fail tests when tailscaled logs an [unexpected] line

tailscaled uses the [unexpected] log prefix for conditions that should
never happen. Record such lines as the log catcher receives them and
report each one as a test error when the test environment shuts down.
Run tailscaled with IN_TS_TEST=1 so logs are uploaded as they are
written, rather than batched every two seconds and partly lost when the
process is killed at the end of the test.

Updates #22307

Signed-off-by: Nick Khyl <nickk@tailscale.com>
This commit is contained in:
Nick Khyl authored and Nick Khyl committed 2026-12-02 07:45:55 -08:00
1 parent d55ba7218e
commit e3ddebe50c
1 file changed
+17
+17
View File
@@ -410,6 +410,7 @@ type LogCatcher struct {
raw bool // indicates whether to store the raw JSON logs uploaded, instead of just the text
nodeWriters map[string]io.Writer // by /node/<key> upload path, for nodes with no stderr to read
unexpected []string // log lines containing "[unexpected]"
}
// UseLogf makes the logcatcher implementation use a given logf function
@@ -433,6 +434,15 @@ func (lc *LogCatcher) logsContains(sub mem.RO) bool {
return mem.Contains(mem.B(lc.buf.Bytes()), sub)
}
// unexpectedLines returns the captured log lines that contain
// "[unexpected]", which tailscaled uses to flag conditions that
// should never happen.
func (lc *LogCatcher) unexpectedLines() []string {
lc.mu.Lock()
defer lc.mu.Unlock()
return lc.unexpected
}
func (lc *LogCatcher) numRequests() int {
lc.mu.Lock()
defer lc.mu.Unlock()
@@ -553,6 +563,9 @@ func (lc *LogCatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else {
id := privID.Public().String()[:3] // good enough for integration tests
for _, ent := range jreq {
if strings.Contains(ent.Text, "[unexpected]") {
lc.unexpected = append(lc.unexpected, strings.TrimSpace(ent.Text))
}
if lc.raw {
lc.buf.Write(bodyBytes)
continue
@@ -641,6 +654,9 @@ func NewTestEnv(t testing.TB, opts ...TestEnvOpt) *TestEnv {
e.t.Errorf("traffic trap: %v", err)
e.t.Logf("logs: %s", e.LogCatcher.logsString())
}
for _, line := range e.LogCatcher.unexpectedLines() {
e.t.Errorf("tailscaled logged: %s", line)
}
e.LogCatcherServer.Close()
e.TrafficTrapServer.Close()
e.ControlServer.Close()
@@ -1027,6 +1043,7 @@ func (n *TestNode) daemonEnv(ipnGOOS string) []string {
"TS_PANIC_IF_HIT_MAIN_CONTROL=1",
"TS_DISABLE_PORTMAPPER=1", // shouldn't be needed; test is all localhost
"TS_DEBUG_LOG_RATE=all",
"IN_TS_TEST=1", // upload logs immediately, so the LogCatcher sees them before the test ends; skip external network probes
}
if n.allowUpdates {
env = append(env, "TS_TEST_ALLOW_AUTO_UPDATE=1")