main_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // TestMain_ConfigTestReleasesItsGoroutines pins the rule that Main only leaves goroutines running
  52. // when it hands back a Control to stop them with.
  53. //
  54. // A config test gets no Control, so anything it started had nothing to stop it: the lighthouse
  55. // query worker, and a hostname resolver per dns named static host, ran for the life of the
  56. // process. That matters to every embedder that validates a config in process rather than by
  57. // exec'ing, dnclient and the apple clients included, because they do it on each config load and
  58. // the leak accumulates.
  59. func TestMain_ConfigTestReleasesItsGoroutines(t *testing.T) {
  60. defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
  61. l := test.NewLogger()
  62. dir := t.TempDir()
  63. before := time.Now().Add(-time.Hour)
  64. after := time.Now().Add(time.Hour)
  65. ca, _, caKey, caPEM := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, before, after, nil, nil, nil)
  66. networks := []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")}
  67. _, _, keyPEM, certPEM := cert_test.NewTestCert(
  68. cert.Version2, cert.Curve_CURVE25519, ca, caKey, "config-test", before, after, networks, nil, nil)
  69. caPath := filepath.Join(dir, "ca.pem")
  70. certPath := filepath.Join(dir, "cert.pem")
  71. keyPath := filepath.Join(dir, "key.pem")
  72. require.NoError(t, os.WriteFile(caPath, caPEM, 0o600))
  73. require.NoError(t, os.WriteFile(certPath, certPEM, 0o600))
  74. require.NoError(t, os.WriteFile(keyPath, keyPEM, 0o600))
  75. // A static host by address, not by name: the query worker is the goroutine under test and a
  76. // hostname would drag a real dns lookup into a unit test.
  77. configBody := fmt.Sprintf(`
  78. pki:
  79. ca: %s
  80. cert: %s
  81. key: %s
  82. static_host_map:
  83. "10.0.0.2": ["192.0.2.1:4242"]
  84. lighthouse:
  85. hosts:
  86. - "10.0.0.2"
  87. listen:
  88. host: 127.0.0.1
  89. port: 0
  90. tun:
  91. disabled: true
  92. firewall:
  93. outbound:
  94. - port: any
  95. proto: any
  96. host: any
  97. inbound:
  98. - port: any
  99. proto: any
  100. host: any
  101. `, caPath, certPath, keyPath)
  102. require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yml"), []byte(configBody), 0o600))
  103. c := config.NewC(l)
  104. require.NoError(t, c.Load(dir))
  105. ctrl, err := Main(c, true, "config-test", l, nil)
  106. require.NoError(t, err)
  107. require.Nil(t, ctrl, "a config test hands back nothing to stop, so it must stop itself")
  108. }