You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.9 KiB

package main
import (
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func init() {
rand.Seed(time.Now().Unix())
}
func randString() string {
retval := ""
count := 'z' - 'a'
x := rand.Intn(10) + 3
for i := 0; i < x; i++ {
retval = string(append([]byte(retval), byte(int('a')+rand.Intn(int(count)))))
}
return retval
}
func TestPingMonitorFromConfig(t *testing.T) {
// name and type will be filled in automatically
failing := map[string]monitorConfig{
"blank": {},
"missing_interval": {baseConfig: baseConfig{
Params: map[string]interface{}{
"host": "127.0.0.1",
"count": 1,
"timeout": "1s",
},
}},
"missing_host": {baseConfig: baseConfig{
Params: map[string]interface{}{
"count": 1,
"timeout": "1s",
},
},
Interval: 30 * time.Second,
},
"missing_count": {baseConfig: baseConfig{
Params: map[string]interface{}{
"host": "127.0.0.1",
"timeout": "1s",
},
},
Interval: 30 * time.Second,
},
"missing_timeout": {baseConfig: baseConfig{
Params: map[string]interface{}{
"host": "127.0.0.1",
"count": 1,
},
},
Interval: 30 * time.Second,
},
}
for name, target := range failing {
target.Name = randString()
target.Type = "ping"
pm := &pingMonitor{}
assert.NotNil(t, pm.FromConfig(target), name)
}
passing := monitorConfig{baseConfig: baseConfig{
Name: "passing",
Type: "ping",
Params: map[string]interface{}{
"host": "127.0.0.1",
"count": 100,
"timeout": "1s",
},
},
Interval: 30 * time.Second,
}
pm := &pingMonitor{}
assert.Nil(t, pm.FromConfig(passing), "passing")
assert.Equal(t, pm.name, passing.Name)
assert.Equal(t, pm.host, passing.Params["host"])
assert.Equal(t, pm.count, uint(passing.Params["count"].(int)))
assert.Equal(t, pm.timeout, time.Second)
assert.Equal(t, pm.pollInterval, 30*time.Second)
assert.Equal(t, pm.Type(), "ping")
}