Files
tailscale/tstest/natlab/vmtest/qmp_test.go
Claus Lensbøl 1e3097c9f2 tstest/natlab/{vnet,vmtest},cmd/tta: add roaming test to natlab (#21695)
To faciliate roaming a few things had to happen as tta communicated over
the same links as tailscaled communicates over, meaning that the
connection to tta gets cut when cutting the interface.

 - Whenever we have connected to the client after waiting for it to come
   up, clean up idle connections.
 - Have tta use keepalives to reconnect.
 - Delete default route on just via, not the interface.
 - Add capabilities to QMP for setting the interface state.
 - Implement the test.

Fixes tailscale/corp#49639

Signed-off-by: Claus Lensbøl <[email protected]>
2026-10-09 13:05:45 -04:00

186 lines
4.8 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package vmtest
import (
jsonv1 "encoding/json"
"net"
"strings"
"sync"
"testing"
)
// newTestQMPSock builds a qmpSock backed by an in-memory net.Pipe connection,
// returning both the client-side qmpSock and the server-side Conn for the
// test to script responses on.
func newTestQMPSock(t *testing.T) (*qmpSock, net.Conn) {
t.Helper()
server, client := net.Pipe()
t.Cleanup(func() { server.Close(); client.Close() })
return &qmpSock{
conn: client,
dec: jsonv1.NewDecoder(client),
enc: jsonv1.NewEncoder(client),
}, server
}
// TestQMPExecute sets up a "server" and injects various responses in to test
// how execute handles these error states.
func TestQMPExecute(t *testing.T) {
tests := []struct {
name string
serverMsgs []any
wantErr string
}{
{
name: "success",
serverMsgs: []any{
jsonv1.RawMessage(`{"return": {}, "id": 1}`),
},
},
{
name: "error_same_id",
serverMsgs: []any{
jsonv1.RawMessage(`{"error": {"class": "SomeError", "desc": "very sad"}, "id": 1}`),
},
wantErr: "SomeError: very sad",
},
{
name: "event_then_success",
serverMsgs: []any{
jsonv1.RawMessage(`{"event": "something", "data": 12345}`),
jsonv1.RawMessage(`{"return": {}, "id": 1}`),
},
},
{
name: "wrong_id_then_correct_id",
serverMsgs: []any{
jsonv1.RawMessage(`{"return": {}, "id": 99}`),
jsonv1.RawMessage(`{"return": {}, "id": 1}`),
},
},
{
name: "error_for_wrong_id_then_success",
serverMsgs: []any{
jsonv1.RawMessage(`{"error": {"class": "SomeError", "desc": "very sad"}, "id": 99}`),
jsonv1.RawMessage(`{"return": {}, "id": 1}`),
},
},
{
name: "unexpected_greeting_mid_session",
serverMsgs: []any{
map[string]any{"QMP": map[string]any{"version": "fake"}},
},
wantErr: "unexpected QMP greeting mid-session",
},
{
name: "shutdown",
serverMsgs: []any{
jsonv1.RawMessage(`{"event": "SHUTDOWN"}`),
},
wantErr: "got closing QMP event: SHUTDOWN",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
q, server := newTestQMPSock(t)
var wg sync.WaitGroup
wg.Go(func() {
var discard qmpCommand
jsonv1.NewDecoder(server).Decode(&discard)
enc := jsonv1.NewEncoder(server)
for _, m := range tt.serverMsgs {
enc.Encode(m)
}
})
defer wg.Wait()
_, err := q.execute[struct{}](&qmpCommand{Execute: "test-cmd"})
if tt.wantErr == "" && err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tt.wantErr != "" && (err == nil || !strings.Contains(err.Error(), tt.wantErr)) {
t.Fatalf("error = %v, want substring %q", err, tt.wantErr)
}
})
}
}
// TestQMPExecuteSendsID ensures that an ID is sent on the wire for a command.
func TestQMPExecuteSendsID(t *testing.T) {
qmpSock, server := newTestQMPSock(t)
var received qmpCommand
var wg sync.WaitGroup
wg.Go(func() {
dec := jsonv1.NewDecoder(server)
if err := dec.Decode(&received); err != nil {
return
}
// Echo back a success response with the same ID.
jsonv1.NewEncoder(server).Encode(
map[string]any{"return": map[string]any{}, "id": received.ID})
})
if _, err := qmpSock.execute[struct{}](&qmpCommand{Execute: "set_link"}); err != nil {
t.Fatalf("execute: %v", err)
}
wg.Wait()
if received.ID == 0 {
t.Errorf("sent command has ID 0, want non-zero")
}
if received.Execute != "set_link" {
t.Errorf("Execute = %q, want %q", received.Execute, "set_link")
}
}
// TestHostFwdRe tests responses generated from the QMP command "human-monitor-command".
// Effectively the rules below can be generated by setting up the "-monitor" directive
// on a local qemu VM, and driving the interface by hand with "info usernet",
// or by printing the output generated in qmp.go from the command run against
// the VMs being tested.
func TestHostFwdRe(t *testing.T) {
tests := []struct {
name string
response string
wantPort string
}{
{
name: "standard",
response: "TCP[HOST_FORWARD] 12 127.0.0.1 35323 10.0.2.15 22\n",
wantPort: "35323",
},
{
name: "multiple",
response: "TCP[HOST_FORWARD] 12 127.0.0.1 35323 10.0.2.15 22\n" +
"TCP[HOST_FORWARD] 13 127.0.0.1 44444 10.0.2.15 80\n",
wantPort: "35323",
},
{
name: "random",
response: "BICYCLE=42\n",
},
{
name: "empty",
response: "\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
match := hostFwdRe.FindStringSubmatch(tt.response)
if tt.wantPort != "" && match == nil {
t.Fatalf("no match in %q", tt.response)
}
if tt.wantPort == "" && match != nil {
t.Fatalf("unexpected match %v in %q", match, tt.response)
}
if tt.wantPort != "" && match[1] != tt.wantPort {
t.Errorf("port = %q, want %q", match[1], tt.wantPort)
}
})
}
}