bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/expr/influx_test.go (about) 1 package expr 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "bosun.org/opentsdb" 9 "github.com/influxdata/influxdb/client/v2" 10 ) 11 12 const influxTimeFmt = time.RFC3339Nano 13 14 func TestInfluxQueryDuration(t *testing.T) { 15 type influxTest struct { 16 query string 17 gbi string // group by interval 18 expect string // empty for error 19 } 20 date := time.Date(2000, time.January, 1, 2, 0, 0, 0, time.UTC) 21 dur := time.Hour 22 end := date.Format(influxTimeFmt) 23 start := date.Add(-dur).Format(influxTimeFmt) 24 tests := []influxTest{ 25 { 26 "select * from a", "", 27 fmt.Sprintf("SELECT * FROM a WHERE time >= '%s' AND time <= '%s' fill(none)", start, end), 28 }, 29 { 30 "select * from a WHERE value > 0", "", 31 fmt.Sprintf("SELECT * FROM a WHERE value > 0 AND time >= '%s' AND time <= '%s' fill(none)", start, end), 32 }, 33 { 34 "select * from a WHERE value > 0", "15m", 35 fmt.Sprintf("SELECT * FROM a WHERE value > 0 AND time >= '%s' AND time <= '%s' GROUP BY time(15m) fill(none)", start, end), 36 }, 37 { 38 "select NON_NEGATIVE_DERIVATIVE(SUM(value)) from a WHERE value > 0", "15m", 39 fmt.Sprintf("SELECT non_negative_derivative(sum(value)) FROM a WHERE value > 0 AND time >= '%s' AND time <= '%s' GROUP BY time(15m) fill(none)", start, end), 40 }, 41 { 42 "select * from a WHERE time > 0 fill(none)", "", 43 "", 44 }, 45 } 46 for _, test := range tests { 47 q, err := influxQueryDuration(date, test.query, dur.String(), "", test.gbi) 48 if err != nil && test.expect != "" { 49 t.Errorf("%v: unexpected error: %v", test.query, err) 50 } else if q != test.expect { 51 t.Errorf("%v: \n\texpected: %v\n\tgot: %v", test.query, test.expect, q) 52 } 53 } 54 } 55 56 func TestInfluxQuery(t *testing.T) { 57 e := State{ 58 now: time.Date(2015, time.February, 25, 0, 0, 0, 0, time.UTC), 59 Backends: &Backends{ 60 InfluxConfig: client.HTTPConfig{}, 61 }, 62 BosunProviders: &BosunProviders{ 63 Squelched: func(tags opentsdb.TagSet) bool { 64 return false 65 }, 66 }, 67 } 68 _, err := InfluxQuery(&e, "db", "select * from alh limit 10", "1n", "", "") 69 if err == nil { 70 t.Fatal("Should have received an error from InfluxQuery") 71 } 72 }