tstest/natlab/{vmtest,vnet}: bound "tailscale up", dump tailscaled logs on failure

When a natlab node's "tailscale up" hung (TestEasyEasy on CI, twice on
main), the only bound was the 10 minute test context, so the failure
arrived as go test's timeout panic. The VM console logs weren't dumped
(t.Cleanup doesn't run on a panic), and they wouldn't have helped
anyway: on gokrazy the console holds only kernel and init output, while
tailscaled's stdout/stderr goes to a remote syslog that vnet discards
unless the node has VerboseSyslog set. There was no way to see what the
stuck node was doing.

Bound each node's "tailscale up" in Env.Start to 90 seconds (it takes
about a second against the in-process control server), so a stuck node
becomes a normal test failure. On failure, dump the tail of each node's
tailscaled logs from vnet's fake log.tailscale.com log catcher, which
already buffered them per node but exposed them to nothing; add
Server.NodeLogs for that. Also add VMTEST_VERBOSE_SYSLOG=1 to stream
the guests' syslog into the test output live, the vmtest equivalent of
tstest/integration/nat's --log-tailscaled flag.

With this, the CI hang reproduced locally under CPU pressure (2 CPUs
shared with busy loops) in 1 of 22 runs, and the dumped logs showed
tailscaled's control client stuck at "awaiting unpause", which is fixed
separately.

Updates #deflake

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Icfd3a2cdc66d924388a722f3a3cd19f86e116921
This commit is contained in:
Brad Fitzpatrick authored and Brad Fitzpatrick committed 2026-09-20 11:02:04 -07:00
1 parent 7d96cf5a62
commit 3014ad828e
3 files changed
+71 -7

No files matched your search

+11 -4
View File
@@ -383,16 +383,23 @@ func dumpLogTail(t testing.TB, name, kind, path string) {
t.Logf("=== %s %s log unavailable: %v ===", name, kind, err)
return
}
dumpTail(t, name, kind, data, 50)
}
// dumpTail prints the last maxLines lines of data to the test log, prefixed
// with the VM name and kind. It prints only a short note if data is empty.
func dumpTail(t testing.TB, name, kind string, data []byte, maxLines int) {
t.Helper()
if len(data) == 0 {
t.Logf("=== %s %s log is empty ===", name, kind)
return
}
lines := bytes.Split(data, []byte("\n"))
lines := bytes.Split(bytes.TrimRight(data, "\n"), []byte("\n"))
start := 0
if len(lines) > 50 {
start = len(lines) - 50
if len(lines) > maxLines {
start = len(lines) - maxLines
}
t.Logf("=== last 50 lines of %s %s log ===", name, kind)
t.Logf("=== last %d lines of %s %s log ===", len(lines)-start, name, kind)
for _, line := range lines[start:] {
t.Logf("[%s] %s", name, line)
}
+46 -2
View File
@@ -344,6 +344,9 @@ func New(t testing.TB, opts ...EnvOption) *Env {
o.applyTo(e)
}
t.Cleanup(func() {
if t.Failed() {
e.dumpNodeLogs()
}
e.testStatus.finish(t.Failed())
e.eventBus.Publish(VMEvent{
Type: EventTestStatus,
@@ -354,6 +357,24 @@ func New(t testing.TB, opts ...EnvOption) *Env {
return e
}
// dumpNodeLogs writes the tail of each node's tailscaled logs, as uploaded
// to the fake log catcher, to the test log. It runs on test failure. The
// VM console log dumped by [dumpLogTail] holds only kernel and init output;
// on gokrazy the processes' own output goes to a remote syslog that the
// virtual network discards, so this is the only view of what tailscaled
// was doing.
func (e *Env) dumpNodeLogs() {
if e.server == nil {
return
}
// Nodes run tailscaled and upload its logs whether or not they joined
// the tailnet or have an agent, so dump them all. A node that uploaded
// nothing gets a one-line note.
for _, n := range e.nodes {
dumpTail(e.t, n.name, "tailscaled (via logcatcher)", []byte(e.server.NodeLogs(n.vnetNode)), 100)
}
}
// EnvOption configures an [Env] in [New].
type EnvOption interface {
applyTo(*Env)
@@ -525,6 +546,15 @@ func (e *Env) AddNode(name string, opts ...any) *Node {
n.vnetNode = e.cfg.AddNode(vnetOpts...)
n.num = n.vnetNode.Num()
// VMTEST_VERBOSE_SYSLOG=1 logs each node's remote syslog (the stdout
// and stderr of tailscaled and the other guest processes) into the
// test output as it arrives. It is the natlab equivalent of
// tstest/integration/nat's --log-tailscaled flag and is the way to
// watch a guest live; on failure the tail of tailscaled's logs is
// dumped regardless, from the fake log catcher.
if os.Getenv("VMTEST_VERBOSE_SYSLOG") == "1" {
n.vnetNode.SetVerboseSyslog(true)
}
return n
}
@@ -750,8 +780,17 @@ func (e *Env) Start() {
if n.joinTailnet {
tsStep := e.Step("Tailscale up: " + n.name)
tsStep.Begin()
if err := e.tailscaleUp(ctx, n); err != nil {
return fmt.Errorf("[%s] tailscale up: %w", n.name, err)
// Bound "tailscale up" more tightly than the overall
// test context. It normally completes in about a second
// against the in-process control server, so a node that
// is stuck here should fail promptly, with its logs
// dumped, rather than hang until go test's timeout panic,
// which dumps nothing useful about the node.
upCtx, upCancel := context.WithTimeout(ctx, tailscaleUpTimeout)
err := e.tailscaleUp(upCtx, n)
upCancel()
if err != nil {
return fmt.Errorf("[%s] tailscale up (limit %v): %w", n.name, tailscaleUpTimeout, err)
}
st2, err := n.agent.Status(ctx)
if err != nil {
@@ -808,6 +847,11 @@ func (e *Env) Start() {
}
}
// tailscaleUpTimeout bounds one node's "tailscale up" in [Env.Start]. It
// is far above the roughly one second the command takes against the
// in-process control server, and far below the test's overall context.
const tailscaleUpTimeout = 90 * time.Second
// tailscaleUp runs "tailscale up" on the node via TTA.
func (e *Env) tailscaleUp(ctx context.Context, n *Node) error {
url := "http://unused/up?accept-routes=true"
+14 -1
View File
@@ -594,7 +594,7 @@ func (n *network) serveLogCatcherConn(clientRemoteIP netip.Addr, c net.Conn) {
node.logCatcherWrites++
for _, lg := range logs {
tStr := lg.Logtail.Client_Time.Round(time.Millisecond).Format(time.RFC3339Nano)
fmt.Fprintf(&node.logBuf, "[%v] %s\n", tStr, lg.Text)
fmt.Fprintf(&node.logBuf, "[%v] %s\n", tStr, strings.TrimSuffix(lg.Text, "\n"))
}
}
})
@@ -3001,6 +3001,19 @@ func (s *Server) NodeAgentDialer(n *Node) netx.DialFunc {
return d
}
// NodeLogs returns the tailscaled log lines that node n has uploaded so far
// to the fake log.tailscale.com log catcher, one line per entry, each
// prefixed with the client's timestamp. It returns the empty string if the
// node has not been started under this server or has uploaded nothing.
func (s *Server) NodeLogs(n *Node) string {
if n == nil || n.n == nil {
return ""
}
n.n.logMu.Lock()
defer n.n.logMu.Unlock()
return n.n.logBuf.String()
}
func (s *Server) NodeAgentClient(n *Node) *NodeAgentClient {
d := s.NodeAgentDialer(n)
return &NodeAgentClient{