influxdb/kapacitor/triggers_test.go

110 lines
2.3 KiB
Go
Raw Normal View History

2016-11-03 00:59:25 +00:00
package kapacitor
import (
"testing"
"github.com/influxdata/chronograf"
)
func TestTrigger(t *testing.T) {
tests := []struct {
name string
rule chronograf.AlertRule
want string
wantErr bool
}{
{
name: "Test Deadman",
rule: chronograf.AlertRule{
Trigger: "deadman",
2016-11-03 00:59:25 +00:00
},
want: `var trigger = data
|deadman(threshold, period)
2016-11-03 00:59:25 +00:00
.stateChangesOnly()
.message(message)
.id(idVar)
.idTag(idtag)
.levelTag(leveltag)
.messageField(messagefield)
.durationField(durationfield)
2016-11-03 00:59:25 +00:00
`,
wantErr: false,
},
{
name: "Test Relative",
rule: chronograf.AlertRule{
Trigger: "relative",
TriggerValues: chronograf.TriggerValues{
Operator: "greater than",
},
2016-11-03 00:59:25 +00:00
},
want: `var past = data
|shift(shift)
var current = data
var trigger = past
|join(current)
.as('past', 'current')
|eval(lambda: abs(float("current.value" - "past.value")) / float("past.value") * 100.0)
2016-11-03 00:59:25 +00:00
.keep()
.as('value')
|alert()
.stateChangesOnly()
.crit(lambda: "value" > crit)
.message(message)
.id(idVar)
.idTag(idtag)
.levelTag(leveltag)
.messageField(messagefield)
.durationField(durationfield)
2016-11-03 00:59:25 +00:00
`,
wantErr: false,
},
{
name: "Test Threshold",
rule: chronograf.AlertRule{
Trigger: "threshold",
TriggerValues: chronograf.TriggerValues{
Operator: "greater than",
},
2016-11-03 00:59:25 +00:00
},
want: `var trigger = data
|alert()
.stateChangesOnly()
.crit(lambda: "value" > crit)
.message(message)
.id(idVar)
.idTag(idtag)
.levelTag(leveltag)
.messageField(messagefield)
.durationField(durationfield)
2016-11-03 00:59:25 +00:00
`,
wantErr: false,
},
{
name: "Test Invalid",
rule: chronograf.AlertRule{
Trigger: "invalid",
2016-11-03 00:59:25 +00:00
},
want: ``,
wantErr: true,
},
}
for _, tt := range tests {
got, err := Trigger(tt.rule)
if (err != nil) != tt.wantErr {
t.Errorf("%q. Trigger() error = %v, wantErr %v", tt.name, err, tt.wantErr)
continue
}
formatted, err := formatTick(got)
if err != nil {
t.Errorf("%q. formatTick() error = %v", tt.name, err)
continue
}
if string(formatted) != tt.want {
t.Errorf("%q. Trigger() = \n%v\n want \n%v\n", tt.name, string(formatted), tt.want)
}
}
}