punchy_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package nebula
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/slackhq/nebula/config"
  6. "github.com/slackhq/nebula/test"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestNewPunchyFromConfig(t *testing.T) {
  11. l := test.NewLogger()
  12. c := config.NewC(l)
  13. // Test defaults
  14. p := NewPunchyFromConfig(l, c)
  15. assert.False(t, p.GetPunch())
  16. assert.False(t, p.GetRespond())
  17. assert.Equal(t, time.Second, p.GetDelay())
  18. assert.Equal(t, 5*time.Second, p.GetRespondDelay())
  19. // punchy deprecation
  20. c.Settings["punchy"] = true
  21. p = NewPunchyFromConfig(l, c)
  22. assert.True(t, p.GetPunch())
  23. // punchy.punch
  24. c.Settings["punchy"] = map[string]any{"punch": true}
  25. p = NewPunchyFromConfig(l, c)
  26. assert.True(t, p.GetPunch())
  27. // punch_back deprecation
  28. c.Settings["punch_back"] = true
  29. p = NewPunchyFromConfig(l, c)
  30. assert.True(t, p.GetRespond())
  31. // punchy.respond
  32. c.Settings["punchy"] = map[string]any{"respond": true}
  33. c.Settings["punch_back"] = false
  34. p = NewPunchyFromConfig(l, c)
  35. assert.True(t, p.GetRespond())
  36. // punchy.delay
  37. c.Settings["punchy"] = map[string]any{"delay": "1m"}
  38. p = NewPunchyFromConfig(l, c)
  39. assert.Equal(t, time.Minute, p.GetDelay())
  40. // punchy.respond_delay
  41. c.Settings["punchy"] = map[string]any{"respond_delay": "1m"}
  42. p = NewPunchyFromConfig(l, c)
  43. assert.Equal(t, time.Minute, p.GetRespondDelay())
  44. }
  45. func TestPunchy_reload(t *testing.T) {
  46. l := test.NewLogger()
  47. c := config.NewC(l)
  48. delay, _ := time.ParseDuration("1m")
  49. require.NoError(t, c.LoadString(`
  50. punchy:
  51. delay: 1m
  52. respond: false
  53. `))
  54. p := NewPunchyFromConfig(l, c)
  55. assert.Equal(t, delay, p.GetDelay())
  56. assert.False(t, p.GetRespond())
  57. newDelay, _ := time.ParseDuration("10m")
  58. require.NoError(t, c.ReloadConfigString(`
  59. punchy:
  60. delay: 10m
  61. respond: true
  62. `))
  63. p.reload(c, false)
  64. assert.Equal(t, newDelay, p.GetDelay())
  65. assert.True(t, p.GetRespond())
  66. }