Files
tailscale/net/netmon/netmon_static_test.go
Brad Fitzpatrick 6e16dac9e9 net/netmon: make static monitors work without netlink
Userspace-only programs like tailcat don't need to watch the OS
network state, so in sandboxes without netlink access (as in ChatGPT
Work's) they'd like to use netmon.NewStatic instead of netmon.New,
which fails there. But two things broke with a static Monitor:

If reading the interface state failed, NewStatic left it nil, and
callers of InterfaceState don't expect nil.

LinkChangeLogLimiter, used by magicsock, subscribed to the static
Monitor's nil event bus client and panicked. Static monitors never
publish link changes, so skip the subscription.

Updates #22412

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ifa826c8cbac6a82b8a15f57bc8178b364fc30e54
2026-11-15 18:14:23 -08:00

39 lines
899 B
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package netmon
import (
"errors"
"reflect"
"testing"
"tailscale.com/tstest"
)
func TestNewStaticInterfaceDiscoveryFailure(t *testing.T) {
tstest.Replace(t, &altNetInterfaces, func() ([]Interface, error) {
return nil, errors.New("interface enumeration unavailable")
})
mon := NewStatic()
if got := mon.InterfaceState(); !reflect.DeepEqual(got, &State{}) {
t.Fatalf("InterfaceState = %#v, want non-nil empty state", got)
}
mon.Start()
if err := mon.Close(); err != nil {
t.Fatal(err)
}
}
func TestLinkChangeLogLimiterStatic(t *testing.T) {
mon := NewStatic()
calls := 0
logf := LinkChangeLogLimiter(t.Context(), func(string, ...any) { calls++ }, mon)
logf("first format")
logf("first format")
logf("second format")
if calls != 2 {
t.Fatalf("logged %d messages, want 2", calls)
}
}