main_test.go 4.5 KB

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