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{
|
2016-11-03 22:27:58 +00:00
|
|
|
Trigger: "deadman",
|
2016-11-03 00:59:25 +00:00
|
|
|
},
|
|
|
|
want: `var trigger = data
|
2016-11-04 01:56:42 +00:00
|
|
|
|deadman(threshold, period)
|
2016-11-03 00:59:25 +00:00
|
|
|
.stateChangesOnly()
|
|
|
|
.message(message)
|
2016-11-03 18:41:34 +00:00
|
|
|
.id(idVar)
|
|
|
|
.idTag(idtag)
|
2016-11-03 22:27:58 +00:00
|
|
|
.levelTag(leveltag)
|
2016-11-03 18:41:34 +00:00
|
|
|
.messageField(messagefield)
|
|
|
|
.durationField(durationfield)
|
2016-11-03 00:59:25 +00:00
|
|
|
`,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test Relative",
|
|
|
|
rule: chronograf.AlertRule{
|
2016-11-03 22:27:58 +00:00
|
|
|
Trigger: "relative",
|
|
|
|
TriggerValues: chronograf.TriggerValues{
|
2016-11-04 00:44:28 +00:00
|
|
|
Operator: "greater than",
|
2016-11-04 22:25:57 +00:00
|
|
|
Change: "% change",
|
2016-11-03 22:27:58 +00:00
|
|
|
},
|
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')
|
2016-11-04 22:05:18 +00:00
|
|
|
|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)
|
2016-11-03 18:41:34 +00:00
|
|
|
.message(message)
|
|
|
|
.id(idVar)
|
|
|
|
.idTag(idtag)
|
2016-11-03 22:27:58 +00:00
|
|
|
.levelTag(leveltag)
|
2016-11-03 18:41:34 +00:00
|
|
|
.messageField(messagefield)
|
|
|
|
.durationField(durationfield)
|
2016-11-04 22:25:57 +00:00
|
|
|
`,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test Relative percent change",
|
|
|
|
rule: chronograf.AlertRule{
|
|
|
|
Trigger: "relative",
|
|
|
|
TriggerValues: chronograf.TriggerValues{
|
|
|
|
Operator: "greater than",
|
|
|
|
Change: "change",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: `var past = data
|
|
|
|
|shift(shift)
|
|
|
|
|
|
|
|
var current = data
|
|
|
|
|
|
|
|
var trigger = past
|
|
|
|
|join(current)
|
|
|
|
.as('past', 'current')
|
|
|
|
|eval(lambda: float("current.value" - "past.value"))
|
|
|
|
.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{
|
2016-11-03 22:27:58 +00:00
|
|
|
Trigger: "threshold",
|
|
|
|
TriggerValues: chronograf.TriggerValues{
|
2016-11-04 00:44:28 +00:00
|
|
|
Operator: "greater than",
|
2016-11-03 22:27:58 +00:00
|
|
|
},
|
2016-11-03 00:59:25 +00:00
|
|
|
},
|
|
|
|
want: `var trigger = data
|
|
|
|
|alert()
|
|
|
|
.stateChangesOnly()
|
|
|
|
.crit(lambda: "value" > crit)
|
2016-11-03 18:41:34 +00:00
|
|
|
.message(message)
|
|
|
|
.id(idVar)
|
|
|
|
.idTag(idtag)
|
2016-11-03 22:27:58 +00:00
|
|
|
.levelTag(leveltag)
|
2016-11-03 18:41:34 +00:00
|
|
|
.messageField(messagefield)
|
|
|
|
.durationField(durationfield)
|
2016-11-03 00:59:25 +00:00
|
|
|
`,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test Invalid",
|
|
|
|
rule: chronograf.AlertRule{
|
2016-11-03 22:27:58 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|