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