path_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //go:build !windows
  2. package localapi
  3. import (
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. type fakeConfig map[string]string
  10. func (f fakeConfig) GetString(k, d string) string {
  11. if v, ok := f[k]; ok {
  12. return v
  13. }
  14. return d
  15. }
  16. // TestSocketPathFromConfigUsesTheDefault is what makes the endpoint work with no
  17. // configuration: the daemon and the CLI both land on the one fixed path.
  18. func TestSocketPathFromConfigUsesTheDefault(t *testing.T) {
  19. p, err := SocketPathFromConfig(fakeConfig{})
  20. require.NoError(t, err)
  21. assert.Equal(t, "/var/run/nebula/ctl.sock", p)
  22. assert.Equal(t, DefaultSocketPath(), p)
  23. }
  24. func TestSocketPathFromConfigHonoursOverride(t *testing.T) {
  25. p, err := SocketPathFromConfig(fakeConfig{CtlSocketKey: "/tmp/custom.sock"})
  26. require.NoError(t, err)
  27. assert.Equal(t, "/tmp/custom.sock", p, "the path is used as given")
  28. }
  29. // TestSocketPathFromConfigRefusesARelativeOverride: the daemon and the CLI are started
  30. // from different directories, so a relative path would name two sockets from one config.
  31. // It is refused rather than resolved against anything.
  32. func TestSocketPathFromConfigRefusesARelativeOverride(t *testing.T) {
  33. for _, rel := range []string{"run/ctl.sock", "./ctl.sock", "ctl.sock", "../ctl.sock"} {
  34. _, err := SocketPathFromConfig(fakeConfig{CtlSocketKey: rel})
  35. require.Error(t, err, rel)
  36. require.ErrorContains(t, err, "must be an absolute path", rel)
  37. assert.ErrorContains(t, err, CtlSocketKey, rel)
  38. }
  39. }
  40. func TestSocketPathFromConfigRejectsOverlongOverride(t *testing.T) {
  41. _, err := SocketPathFromConfig(fakeConfig{CtlSocketKey: "/" + strings.Repeat("x", sunPathMax)})
  42. assert.ErrorContains(t, err, "too long for this platform")
  43. }