Debian Codegraph @

udp/netchange_test.go

raw
1 package udp
2
3 import (
4 "context"
5 "testing"
6
7 "github.com/slackhq/nebula/config"
8 "github.com/slackhq/nebula/test"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11 )
12
13 func newMonitor(t *testing.T, ctx context.Context, cfg string) *NetworkChangeMonitor {
14 t.Helper()
15 l := test.NewLogger()
16 c := config.NewC(l)
17 require.NoError(t, c.LoadString(cfg))
18 return NewNetworkChangeMonitor(ctx, l, c)
19 }
20
21 func TestNetworkChangeMonitorDefaultsOn(t *testing.T) {
22 // Says nothing about rebinding, so this covers the default.
23 m := newMonitor(t, context.Background(), "listen:\n host: 0.0.0.0\n")
24 assert.True(t, m.enabled, "should default to on")
25 }
26
27 func TestNetworkChangeMonitorDisabledIsANoOp(t *testing.T) {
28 m := newMonitor(t, context.Background(), "listen:\n rebind_on_network_change: false\n")
29 require.False(t, m.enabled)
30
31 // Must return without opening a socket. If it watched anything this would block.
32 m.Start(func() {})
33 }
34
35 func TestNetworkChangeMonitorNilRebindIsANoOp(t *testing.T) {
36 // Nothing to rebind, so there is no point watching, on any platform.
37 m := newMonitor(t, context.Background(), "listen:\n rebind_on_network_change: true\n")
38 m.Start(nil)
39 }