From 6b6f7250709b194520550449befef0fdad513019 Mon Sep 17 00:00:00 2001 From: Chris Goller Date: Fri, 17 Nov 2017 15:56:32 -0600 Subject: [PATCH] Add test for deadman json pipeline conversion --- kapacitor/pipeline_test.go | 139 +++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/kapacitor/pipeline_test.go b/kapacitor/pipeline_test.go index 61095a99b5..bb07a3cab5 100644 --- a/kapacitor/pipeline_test.go +++ b/kapacitor/pipeline_test.go @@ -147,3 +147,142 @@ alert4 t.Errorf("%s", diff.DiffPrettyText(delta)) } } +func TestPipelineJSONDeadman(t *testing.T) { + script := `var db = 'telegraf' + + var rp = 'autogen' + + var measurement = 'cpu' + + var groupBy = ['host', 'cluster_id'] + + var whereFilter = lambda: ("cpu" == 'cpu_total') AND ("host" == 'acc-0eabc309-eu-west-1-data-3' OR "host" == 'prod') + + var period = 10m + + var name = 'name' + + var idVar = name + ':{{.Group}}' + + var message = 'message' + + var idTag = 'alertID' + + var levelTag = 'level' + + var messageField = 'message' + + var durationField = 'duration' + + var outputDB = 'chronograf' + + var outputRP = 'autogen' + + var outputMeasurement = 'alerts' + + var triggerType = 'deadman' + + var threshold = 0.0 + + var data = stream + |from() + .database(db) + .retentionPolicy(rp) + .measurement(measurement) + .groupBy(groupBy) + .where(whereFilter) + + var trigger = data + |deadman(threshold, period) + .stateChangesOnly() + .message(message) + .id(idVar) + .idTag(idTag) + .levelTag(levelTag) + .messageField(messageField) + .durationField(durationField) + .slack() + .victorOps() + .email() + + trigger + |eval(lambda: "emitted") + .as('value') + .keep('value', messageField, durationField) + |influxDBOut() + .create() + .database(outputDB) + .retentionPolicy(outputRP) + .measurement(outputMeasurement) + .tag('alertName', name) + .tag('triggerType', triggerType) + + trigger + |httpOut('output') +` + + want := `var from1 = stream + |from() + .database('telegraf') + .retentionPolicy('autogen') + .measurement('cpu') + .where(lambda: "cpu" == 'cpu_total' AND "host" == 'acc-0eabc309-eu-west-1-data-3' OR "host" == 'prod') + .groupBy('host', 'cluster_id') + +var alert5 = from1 + |stats(10m) + .align() + |derivative('emitted') + .as('emitted') + .unit(10m) + .nonNegative() + |alert() + .id('name:{{.Group}}') + .message('message') + .details('{{ json . }}') + .crit(lambda: "emitted" <= 0.0) + .history(21) + .levelTag('level') + .messageField('message') + .durationField('duration') + .idTag('alertID') + .stateChangesOnly() + .email() + .victorOps() + .slack() + +alert5 + |httpOut('output') + +alert5 + |eval(lambda: "emitted") + .as('value') + .tags() + .keep('value', 'message', 'duration') + |influxDBOut() + .database('chronograf') + .retentionPolicy('autogen') + .measurement('alerts') + .buffer(1000) + .flushInterval(10s) + .create() + .tag('alertName', 'name') + .tag('triggerType', 'deadman') +` + + octets, err := MarshalTICK(script) + if err != nil { + t.Fatalf("MarshalTICK unexpected error %v", err) + } + got, err := UnmarshalTICK(octets) + if err != nil { + t.Fatalf("UnmarshalTICK unexpected error %v", err) + } + + if got != want { + fmt.Println(got) + diff := diffmatchpatch.New() + delta := diff.DiffMain(want, got, true) + t.Errorf("%s", diff.DiffPrettyText(delta)) + } +}