netchange_test.go 1.2 KB

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