diff --git a/tstest/natlab/vmtest/qemu.go b/tstest/natlab/vmtest/qemu.go index 93a69e137..b9d12187f 100644 --- a/tstest/natlab/vmtest/qemu.go +++ b/tstest/natlab/vmtest/qemu.go @@ -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) } diff --git a/tstest/natlab/vmtest/vmtest.go b/tstest/natlab/vmtest/vmtest.go index 70a4b9412..b2b30953a 100644 --- a/tstest/natlab/vmtest/vmtest.go +++ b/tstest/natlab/vmtest/vmtest.go @@ -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" diff --git a/tstest/natlab/vnet/vnet.go b/tstest/natlab/vnet/vnet.go index 19bb17eec..b722e3f4e 100644 --- a/tstest/natlab/vnet/vnet.go +++ b/tstest/natlab/vnet/vnet.go @@ -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{