punchy_test.go 1.9 KB

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