main_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package nebula
  2. import (
  3. "testing"
  4. "github.com/slackhq/nebula/config"
  5. "github.com/slackhq/nebula/test"
  6. "github.com/slackhq/nebula/util"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestParseCpuAffinity(t *testing.T) {
  10. l := test.NewLogger()
  11. // newConfig returns a config.C with tun.cpu_affinity set to v. A nil v
  12. // leaves the key unset.
  13. newConfig := func(v any) *config.C {
  14. c := config.NewC(l)
  15. if v != nil {
  16. c.Settings["tun"] = map[string]any{"cpu_affinity": v}
  17. }
  18. return c
  19. }
  20. // unset -> nil (listenIn falls back to spreading across the allowed set)
  21. assert.Nil(t, parseCpuAffinity(newConfig(nil), l, 1))
  22. // Pick a CPU we're actually allowed to run on so a valid list survives
  23. // validation regardless of the host's affinity mask.
  24. allowed, _ := util.AllowedCPUs()
  25. validCPU := 0
  26. if len(allowed) > 0 {
  27. validCPU = allowed[0]
  28. }
  29. // valid list -> parsed through unchanged
  30. assert.Equal(t, []int{validCPU, validCPU}, parseCpuAffinity(newConfig([]any{validCPU, validCPU}), l, 2))
  31. // a negative entry is out of range on every platform -> disables the override
  32. assert.Nil(t, parseCpuAffinity(newConfig([]any{validCPU, -1}), l, 2))
  33. // a non-integer entry -> disables the override
  34. assert.Nil(t, parseCpuAffinity(newConfig([]any{validCPU, "not-a-cpu"}), l, 2))
  35. // a CPU id outside the allowed set -> disables the override. Only assertable
  36. // where we can enumerate the allowed set (e.g. linux); 1<<20 is far beyond
  37. // any representable CPU id so it can never be in the mask.
  38. if len(allowed) > 0 {
  39. assert.Nil(t, parseCpuAffinity(newConfig([]any{1 << 20}), l, 1))
  40. }
  41. }