main_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package nebula
  2. import (
  3. "fmt"
  4. "net/netip"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "testing"
  9. "time"
  10. "github.com/slackhq/nebula/cert"
  11. cert_test "github.com/slackhq/nebula/cert_test"
  12. "github.com/slackhq/nebula/config"
  13. "github.com/slackhq/nebula/noiseutil"
  14. "github.com/slackhq/nebula/test"
  15. "github.com/slackhq/nebula/util"
  16. "github.com/stretchr/testify/assert"
  17. "github.com/stretchr/testify/require"
  18. "go.uber.org/goleak"
  19. )
  20. func TestParseCpuAffinity(t *testing.T) {
  21. l := test.NewLogger()
  22. // newConfig returns a config.C with tun.cpu_affinity set to v. A nil v
  23. // leaves the key unset.
  24. newConfig := func(v any) *config.C {
  25. c := config.NewC(l)
  26. if v != nil {
  27. c.Settings["tun"] = map[string]any{"cpu_affinity": v}
  28. }
  29. return c
  30. }
  31. // unset -> nil (listenIn falls back to spreading across the allowed set)
  32. assert.Nil(t, parseCpuAffinity(newConfig(nil), l, 1))
  33. // Pick a CPU we're actually allowed to run on so a valid list survives
  34. // validation regardless of the host's affinity mask.
  35. allowed, _ := util.AllowedCPUs()
  36. validCPU := 0
  37. if len(allowed) > 0 {
  38. validCPU = allowed[0]
  39. }
  40. // valid list -> parsed through unchanged
  41. assert.Equal(t, []int{validCPU, validCPU}, parseCpuAffinity(newConfig([]any{validCPU, validCPU}), l, 2))
  42. // a negative entry is out of range on every platform -> disables the override
  43. assert.Nil(t, parseCpuAffinity(newConfig([]any{validCPU, -1}), l, 2))
  44. // a non-integer entry -> disables the override
  45. assert.Nil(t, parseCpuAffinity(newConfig([]any{validCPU, "not-a-cpu"}), l, 2))
  46. // a CPU id outside the allowed set -> disables the override. Only assertable
  47. // where we can enumerate the allowed set (e.g. linux); 1<<20 is far beyond
  48. // any representable CPU id so it can never be in the mask.
  49. if len(allowed) > 0 {
  50. assert.Nil(t, parseCpuAffinity(newConfig([]any{1 << 20}), l, 1))
  51. }
  52. }
  53. // TestMain_ConfigTestReleasesItsGoroutines pins the rule that Main only leaves goroutines running
  54. // when it hands back a Control to stop them with.
  55. //
  56. // A config test gets no Control, so anything it started had nothing to stop it: the lighthouse
  57. // query worker, and a hostname resolver per dns named static host, ran for the life of the
  58. // process. That matters to every embedder that validates a config in process rather than by
  59. // exec'ing, dnclient and the apple clients included, because they do it on each config load and
  60. // the leak accumulates.
  61. func TestMain_ConfigTestReleasesItsGoroutines(t *testing.T) {
  62. defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
  63. l := test.NewLogger()
  64. dir := t.TempDir()
  65. before := time.Now().Add(-time.Hour)
  66. after := time.Now().Add(time.Hour)
  67. ca, _, caKey, caPEM := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, before, after, nil, nil, nil)
  68. networks := []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")}
  69. _, _, keyPEM, certPEM := cert_test.NewTestCert(
  70. cert.Version2, cert.Curve_CURVE25519, ca, caKey, "config-test", before, after, networks, nil, nil)
  71. caPath := filepath.Join(dir, "ca.pem")
  72. certPath := filepath.Join(dir, "cert.pem")
  73. keyPath := filepath.Join(dir, "key.pem")
  74. require.NoError(t, os.WriteFile(caPath, caPEM, 0o600))
  75. require.NoError(t, os.WriteFile(certPath, certPEM, 0o600))
  76. require.NoError(t, os.WriteFile(keyPath, keyPEM, 0o600))
  77. // A static host by address, not by name: the query worker is the goroutine under test and a
  78. // hostname would drag a real dns lookup into a unit test.
  79. configBody := fmt.Sprintf(`
  80. pki:
  81. ca: %s
  82. cert: %s
  83. key: %s
  84. static_host_map:
  85. "10.0.0.2": ["192.0.2.1:4242"]
  86. lighthouse:
  87. hosts:
  88. - "10.0.0.2"
  89. listen:
  90. host: 127.0.0.1
  91. port: 0
  92. tun:
  93. disabled: true
  94. firewall:
  95. outbound:
  96. - port: any
  97. proto: any
  98. host: any
  99. inbound:
  100. - port: any
  101. proto: any
  102. host: any
  103. `, caPath, certPath, keyPath)
  104. require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yml"), []byte(configBody), 0o600))
  105. c := config.NewC(l)
  106. require.NoError(t, c.Load(dir))
  107. ctrl, err := Main(c, true, "config-test", l, nil)
  108. require.NoError(t, err)
  109. require.Nil(t, ctrl, "a config test hands back nothing to stop, so it must stop itself")
  110. }
  111. func TestParseStagedConfig(t *testing.T) {
  112. l := test.NewLogger()
  113. staged := func(workers any) stagedConfig {
  114. c := config.NewC(l)
  115. if workers != nil {
  116. c.Settings["tun"] = map[string]any{"staged": map[string]any{"workers": workers}}
  117. }
  118. return parseStagedConfig(c, l)
  119. }
  120. for _, workers := range []any{nil, -1, 0} {
  121. require.Equal(t, stagedConfig{}, staged(workers))
  122. }
  123. if runtime.GOOS != "linux" {
  124. require.Equal(t, stagedConfig{}, staged(2))
  125. return
  126. }
  127. want := stagedConfig{workers: 2, tx: !noiseutil.EncryptLockNeeded, rx: true}
  128. require.Equal(t, want, staged(2))
  129. }