wgengine/magicsock: assume network up for tests

Without this, any test relying on underlying use of magicsock will fail
without network connectivity, even when the test logic has no need for a
network connection. Tests currently in this bucket include many in
tstest/integration and in tsnet.

Further explanation:

ipn only becomes Running when it sees at least one live peer or DERP
connection:
https://github.com/tailscale/tailscale/blob/0cc1b2ff76560ee4675909272fa37ba6b397744c/ipn/ipnlocal/local.go#L5861-L5866

When tests only use a single node, they will never see a peer, so the
node has to wait to see a DERP server.

magicsock sets the preferred DERP server in updateNetInfo(), but this
function returns early if the network is down.
https://github.com/tailscale/tailscale/blob/0cc1b2ff76560ee4675909272fa37ba6b397744c/wgengine/magicsock/magicsock.go#L1053-L1106

Because we're checking the real network, this prevents ipn from entering
"Running" and causes the test to fail or hang.

In tests, we can assume the network is up unless we're explicitly testing
the behaviour of tailscaled when the network is down. We do something similar
in magicsock/derp.go, where we assume we're connected to control unless
explicitly testing otherwise:
https://github.com/tailscale/tailscale/blob/7d2101f3520f16b86f2ed5e15f23c44d720534e6/wgengine/magicsock/derp.go#L166-L177

This is the template for the changes to `networkDown()`.

Fixes #17122

Co-authored-by: Alex Chan <alexc@tailscale.com>
Signed-off-by: Harry Harpham <harry@tailscale.com>
This commit is contained in:
Harry Harpham
2026-03-27 21:13:39 -06:00
parent 87388ceea9
commit 61ac021c5d
4 changed files with 18 additions and 4 deletions
+12 -1
View File
@@ -1438,7 +1438,18 @@ func (c *Conn) LocalPort() uint16 {
var errNetworkDown = errors.New("magicsock: network down")
func (c *Conn) networkDown() bool { return !c.networkUp.Load() }
// This allows tests to pass when the user's machine is offline, but allows us
// to still test network-down behaviour when desired.
var checkNetworkDownDuringTests = false
func (c *Conn) networkDown() bool {
// For tests, always assume the network is up unless we're explicitly
// testing this behaviour.
if envknob.AssumeNetworkUp() || (testenv.InTest() && !checkNetworkDownDuringTests) {
return false
}
return !c.networkUp.Load()
}
// Send implements conn.Bind.
//
+2
View File
@@ -3241,6 +3241,8 @@ func TestNetworkSendErrors(t *testing.T) {
t.Skipf("skipping on %s", runtime.GOOS)
}
tstest.Replace(t, &checkNetworkDownDuringTests, true)
conn, reg, close := newTestConnAndRegistry(t)
defer close()