| 1 |
package nebula |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"log/slog" |
| 6 |
"net/netip" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/gaissmai/bart" |
| 10 |
"github.com/slackhq/nebula/test" |
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
) |
| 13 |
|
| 14 |
// TestStartRelaysLogDedupe verifies that repeated attempts with the same relay set drop the log |
| 15 |
// chatter to Debug, mirroring how the normal handshake retry loop quiets down once it's already |
| 16 |
// announced its targets. |
| 17 |
func TestStartRelaysLogDedupe(t *testing.T) { |
| 18 |
vpnIp := netip.MustParseAddr("100.64.99.4") |
| 19 |
otherRelay := netip.MustParseAddr("100.64.99.5") |
| 20 |
|
| 21 |
newHH := func() *HandshakeHostInfo { |
| 22 |
// Use the target's own vpnIp as the "relay" so the loop body skips it without |
| 23 |
// touching any sender-side state. That isolates the test to the level-selection |
| 24 |
// behavior of the top-level "Attempt to relay through hosts" log. |
| 25 |
hostinfo := &HostInfo{ |
| 26 |
vpnAddrs: []netip.Addr{vpnIp}, |
| 27 |
localIndexId: 1, |
| 28 |
remotes: NewRemoteList([]netip.Addr{vpnIp}, nil), |
| 29 |
} |
| 30 |
hostinfo.remotes.relays = []netip.Addr{vpnIp} |
| 31 |
return &HandshakeHostInfo{hostinfo: hostinfo} |
| 32 |
} |
| 33 |
|
| 34 |
// Park any extra relay addresses we'll introduce mid-test in myVpnAddrsTable so the loop |
| 35 |
// body always skips before touching f.Handshake (which would need a real handshakeManager). |
| 36 |
addrTable := new(bart.Lite) |
| 37 |
addrTable.Insert(netip.PrefixFrom(otherRelay, otherRelay.BitLen())) |
| 38 |
f := &Interface{myVpnAddrsTable: addrTable} |
| 39 |
|
| 40 |
newRM := func(buf *bytes.Buffer) *relayManager { |
| 41 |
l := test.NewLoggerWithOutputAndLevel(buf, slog.LevelDebug) |
| 42 |
rm := &relayManager{l: l, hostmap: newHostMap(l)} |
| 43 |
rm.useRelays.Store(true) |
| 44 |
return rm |
| 45 |
} |
| 46 |
|
| 47 |
const msg = `msg="Attempt to relay through hosts"` |
| 48 |
|
| 49 |
t.Run("first attempt logs at Info", func(t *testing.T) { |
| 50 |
var buf bytes.Buffer |
| 51 |
rm := newRM(&buf) |
| 52 |
hh := newHH() |
| 53 |
rm.StartRelays(f, vpnIp, hh, nil) |
| 54 |
assert.Equal(t, []netip.Addr{vpnIp}, hh.lastRelays, "lastRelays should record the relay set we just attempted") |
| 55 |
assert.Contains(t, buf.String(), "level=INFO "+msg, "expected Info level on first attempt") |
| 56 |
}) |
| 57 |
|
| 58 |
t.Run("repeat attempt with same relays drops to Debug", func(t *testing.T) { |
| 59 |
var buf bytes.Buffer |
| 60 |
rm := newRM(&buf) |
| 61 |
hh := newHH() |
| 62 |
rm.StartRelays(f, vpnIp, hh, nil) |
| 63 |
first := append([]netip.Addr(nil), hh.lastRelays...) |
| 64 |
buf.Reset() |
| 65 |
rm.StartRelays(f, vpnIp, hh, nil) |
| 66 |
assert.Equal(t, first, hh.lastRelays) |
| 67 |
assert.Contains(t, buf.String(), "level=DEBUG "+msg, "expected Debug level on identical retry") |
| 68 |
assert.NotContains(t, buf.String(), "level=INFO "+msg, "Info should not fire on identical retry") |
| 69 |
}) |
| 70 |
|
| 71 |
t.Run("changed relay list bumps back to Info", func(t *testing.T) { |
| 72 |
var buf bytes.Buffer |
| 73 |
rm := newRM(&buf) |
| 74 |
hh := newHH() |
| 75 |
rm.StartRelays(f, vpnIp, hh, nil) |
| 76 |
buf.Reset() |
| 77 |
|
| 78 |
// The lighthouse handed us a new set this round. |
| 79 |
hh.hostinfo.remotes.relays = []netip.Addr{vpnIp, otherRelay} |
| 80 |
|
| 81 |
rm.StartRelays(f, vpnIp, hh, nil) |
| 82 |
assert.Equal(t, []netip.Addr{vpnIp, otherRelay}, hh.lastRelays) |
| 83 |
assert.Contains(t, buf.String(), "level=INFO "+msg, "expected Info when the relay list changes") |
| 84 |
}) |
| 85 |
|
| 86 |
t.Run("disabled relays clears lastRelays and emits no Attempt log", func(t *testing.T) { |
| 87 |
var buf bytes.Buffer |
| 88 |
rm := newRM(&buf) |
| 89 |
rm.useRelays.Store(false) |
| 90 |
hh := newHH() |
| 91 |
hh.lastRelays = []netip.Addr{vpnIp} |
| 92 |
|
| 93 |
rm.StartRelays(f, vpnIp, hh, nil) |
| 94 |
assert.Nil(t, hh.lastRelays, "with relays disabled lastRelays should be cleared") |
| 95 |
assert.NotContains(t, buf.String(), msg, "should not log when we shortcut out") |
| 96 |
}) |
| 97 |
} |