Something went wrong. Try again.
Go port of clojure riemann
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226package riemann
import ( "testing")
func TestParseAndEvaluate(t *testing.T) { tests := []struct { query string event *Event expected bool }{ // Basic equality { query: `service = "cpu"`, event: &Event{Service: "cpu"}, expected: true, }, { query: `service = "cpu"`, event: &Event{Service: "mem"}, expected: false, }, // Case insensitivity of logic keywords { query: `service = "cpu" AND host = "web1"`, event: &Event{Service: "cpu", Host: "web1"}, expected: true, }, { query: `service = "cpu" and host = "web2"`, event: &Event{Service: "cpu", Host: "web1"}, expected: false, }, // Logical OR { query: `service = "cpu" or service = "mem"`, event: &Event{Service: "mem"}, expected: true, }, // Logical NOT { query: `not service = "cpu"`, event: &Event{Service: "mem"}, expected: true, }, // Parenthesized expressions { query: `(service = "cpu" or service = "mem") and state = "ok"`, event: &Event{Service: "mem", State: "ok"}, expected: true, }, { query: `(service = "cpu" or service = "mem") and state = "ok"`, event: &Event{Service: "mem", State: "warn"}, expected: false, }, // Metric comparisons { query: `metric > 10`, event: &Event{Metric: 15.0}, expected: true, }, { query: `metric >= 10`, event: &Event{Metric: 10.0}, expected: true, }, { query: `metric < 10.5`, event: &Event{Metric: 10.4}, expected: true, }, { query: `metric <= 10`, event: &Event{Metric: 11.0}, expected: false, }, // Negative and decimal numbers { query: `metric = -1.5`, event: &Event{Metric: -1.5}, expected: true, }, // Like operator { query: `service =~ "http%"`, event: &Event{Service: "http-get"}, expected: true, }, { query: `service =~ "http%"`, event: &Event{Service: "my-http"}, expected: false, }, // Regex match operator { query: `service ~= ".*http.*"`, event: &Event{Service: "my-http-service"}, expected: true, }, // Tagged evaluator { query: `tagged "production"`, event: &Event{Tags: []string{"web", "production"}}, expected: true, }, { query: `tagged "staging"`, event: &Event{Tags: []string{"web", "production"}}, expected: false, }, // Custom attributes { query: `owner = "santos"`, event: &Event{Attributes: map[string]string{"owner": "santos"}}, expected: true, }, { query: `owner = "santos"`, event: &Event{Attributes: map[string]string{"owner": "john"}}, expected: false, }, // Truthy single fields { query: `service`, event: &Event{Service: "cpu"}, expected: true, }, { query: `service`, event: &Event{}, expected: false, }, // Nil checks { query: `service = nil`, event: &Event{}, expected: true, }, { query: `geoip != null`, event: &Event{Extra: map[string]any{"geoip": map[string]any{"city": "Toronto"}}}, expected: true, }, { query: `geoip = null`, event: &Event{}, expected: true, }, { query: `geoip = null`, event: &Event{Extra: map[string]any{"geoip": map[string]any{"city": "Toronto"}}}, expected: false, }, // User regex query test cases { query: `alias = "@foo" and plugin = "syslog" and not description ~= "(^\t*at |^Caused by |Connection reset by peer|Exception from client)"`, event: &Event{Description: "\tat org.example.Foo.bar(Foo.java:123)", Attributes: map[string]string{"alias": "@foo", "plugin": "syslog"}}, expected: false, }, { query: `alias = "@foo" and plugin = "syslog" and not description ~= "(^\t*at |^Caused by |Connection reset by peer|Exception from client)"`, event: &Event{Description: "Connection reset by peer", Attributes: map[string]string{"alias": "@foo", "plugin": "syslog"}}, expected: false, }, { query: `alias = "@foo" and plugin = "syslog" and not description ~= "(^\t*at |^Caused by |Connection reset by peer|Exception from client)"`, event: &Event{Description: "Exception from client", Attributes: map[string]string{"alias": "@foo", "plugin": "syslog"}}, expected: false, }, }
for i, tt := range tests { eval, err := ParseQuery(tt.query) if err != nil { t.Errorf("test %d (%q) failed to parse: %v", i, tt.query, err) continue } got := eval.Evaluate(tt.event) if got != tt.expected { t.Errorf("test %d (%q) evaluated to %v, expected %v", i, tt.query, got, tt.expected) } }}
func TestOperatorPrecedence(t *testing.T) { // and should bind tighter than or // host = "web1" or host = "web2" and state = "critical" // evaluates as: host = "web1" or (host = "web2" and state = "critical") query := `host = "web1" or host = "web2" and state = "critical"` eval, err := ParseQuery(query) if err != nil { t.Fatalf("failed to parse precedence query: %v", err) }
// Case 1: host is web1, state is ok (should match because web1 matches first part of OR) e1 := &Event{Host: "web1", State: "ok"} if !eval.Evaluate(e1) { t.Errorf("expected e1 to match due to operator precedence") }
// Case 2: host is web2, state is ok (should not match because "and state = critical" binds to web2) e2 := &Event{Host: "web2", State: "ok"} if eval.Evaluate(e2) { t.Errorf("expected e2 to not match because state is not critical") }
// Case 3: host is web2, state is critical (should match) e3 := &Event{Host: "web2", State: "critical"} if !eval.Evaluate(e3) { t.Errorf("expected e3 to match because host is web2 and state is critical") }}
func TestEmptyQuery(t *testing.T) { eval, err := ParseQuery("") if err != nil { t.Fatalf("expected empty query to parse, got error: %v", err) } e := &Event{Host: "web1"} if !eval.Evaluate(e) { t.Errorf("expected empty query to match everything") }}