punchy_test.go 1.7 KB

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