2017-02-10 14:47:08 +00:00
|
|
|
package kapacitor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/chronograf"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVarsCritStringEqual(t *testing.T) {
|
|
|
|
alert := chronograf.AlertRule{
|
|
|
|
Name: "name",
|
|
|
|
Trigger: "threshold",
|
|
|
|
TriggerValues: chronograf.TriggerValues{
|
|
|
|
Operator: "equal to",
|
|
|
|
Value: "DOWN",
|
|
|
|
},
|
|
|
|
Every: "30s",
|
2017-05-05 19:30:20 +00:00
|
|
|
Query: &chronograf.QueryConfig{
|
2017-02-10 14:47:08 +00:00
|
|
|
Database: "telegraf",
|
|
|
|
Measurement: "haproxy",
|
|
|
|
RetentionPolicy: "autogen",
|
|
|
|
Fields: []chronograf.Field{
|
|
|
|
{
|
2017-10-16 15:56:12 +00:00
|
|
|
Value: "status",
|
|
|
|
Type: "field",
|
2017-02-10 14:47:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
GroupBy: chronograf.GroupBy{
|
|
|
|
Time: "10m",
|
|
|
|
Tags: []string{"pxname"},
|
|
|
|
},
|
|
|
|
AreTagsAccepted: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := Vars(alert)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%s", raw)
|
|
|
|
t.Fatalf("Error generating alert: %v %s", err, raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
tick, err := formatTick(raw)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error formatting alert: %v %s", err, raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateTick(tick); err != nil {
|
|
|
|
t.Errorf("Error validating alert: %v %s", err, tick)
|
|
|
|
}
|
|
|
|
}
|
2017-12-01 14:11:04 +00:00
|
|
|
|
|
|
|
func Test_formatValue(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "parses floats",
|
|
|
|
value: "3.14",
|
|
|
|
want: "3.14",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "parses booleans",
|
|
|
|
value: "TRUE",
|
|
|
|
want: "TRUE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single quotes for strings",
|
|
|
|
value: "up",
|
|
|
|
want: "'up'",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "handles escaping of single quotes",
|
|
|
|
value: "down's",
|
|
|
|
want: "'down\\'s'",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := formatValue(tt.value); got != tt.want {
|
|
|
|
t.Errorf("formatValue() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|