chronograf/influx/templates_test.go

298 lines
7.2 KiB
Go
Raw Normal View History

2017-04-19 16:03:53 +00:00
package influx
import (
"encoding/json"
"reflect"
2017-04-19 16:03:53 +00:00
"testing"
"time"
"github.com/influxdata/chronograf"
2017-04-19 16:03:53 +00:00
)
func TestTemplateReplace(t *testing.T) {
tests := []struct {
name string
query string
vars chronograf.TemplateVars
2017-04-19 16:03:53 +00:00
want string
}{
{
name: "select with parameters",
query: "$METHOD field1, $field FROM $measurement WHERE temperature > $temperature",
vars: chronograf.TemplateVars{
chronograf.BasicTemplateVar{
Var: "$temperature",
Values: []chronograf.BasicTemplateValue{
{
Type: "csv",
Value: "10",
2017-04-19 16:03:53 +00:00
},
},
},
chronograf.BasicTemplateVar{
Var: "$field",
Values: []chronograf.BasicTemplateValue{
{
Type: "fieldKey",
Value: "field2",
2017-04-19 16:03:53 +00:00
},
},
},
chronograf.BasicTemplateVar{
Var: "$METHOD",
Values: []chronograf.BasicTemplateValue{
{
Type: "csv",
Value: "SELECT",
2017-04-19 16:03:53 +00:00
},
},
},
chronograf.BasicTemplateVar{
Var: "$measurement",
Values: []chronograf.BasicTemplateValue{
{
Type: "csv",
Value: `"cpu"`,
2017-04-19 16:03:53 +00:00
},
},
},
},
want: `SELECT field1, "field2" FROM "cpu" WHERE temperature > 10`,
},
{
name: "select with parameters and aggregates",
query: `SELECT mean($field) FROM "cpu" WHERE $tag = $value GROUP BY $tag`,
vars: chronograf.TemplateVars{
chronograf.BasicTemplateVar{
Var: "$value",
Values: []chronograf.BasicTemplateValue{
{
Type: "tagValue",
Value: "howdy.com",
2017-04-19 16:03:53 +00:00
},
},
},
chronograf.BasicTemplateVar{
Var: "$tag",
Values: []chronograf.BasicTemplateValue{
{
Type: "tagKey",
Value: "host",
2017-04-19 16:03:53 +00:00
},
},
},
chronograf.BasicTemplateVar{
Var: "$field",
Values: []chronograf.BasicTemplateValue{
{
Type: "fieldKey",
Value: "field",
2017-04-19 16:03:53 +00:00
},
},
},
},
want: `SELECT mean("field") FROM "cpu" WHERE "host" = 'howdy.com' GROUP BY "host"`,
},
{
name: "Non-existant parameters",
query: `SELECT $field FROM "cpu"`,
want: `SELECT $field FROM "cpu"`,
},
{
name: "var without a value",
query: `SELECT $field FROM "cpu"`,
vars: chronograf.TemplateVars{
chronograf.BasicTemplateVar{
Var: "$field",
2017-04-19 16:03:53 +00:00
},
},
want: `SELECT $field FROM "cpu"`,
},
{
name: "var with unknown type",
query: `SELECT $field FROM "cpu"`,
vars: chronograf.TemplateVars{
chronograf.BasicTemplateVar{
Var: "$field",
Values: []chronograf.BasicTemplateValue{
{
Type: "who knows?",
Value: "field",
2017-04-19 16:03:53 +00:00
},
},
},
},
want: `SELECT $field FROM "cpu"`,
},
{
name: "auto group by",
query: `SELECT mean(usage_idle) from "cpu" where time > now() - 4320h group by :interval:`,
vars: chronograf.TemplateVars{
&chronograf.GroupByVar{
Var: ":interval:",
Duration: 180 * 24 * time.Hour,
Resolution: 1000,
ReportingInterval: 10 * time.Second,
},
},
want: `SELECT mean(usage_idle) from "cpu" where time > now() - 4320h group by time(46656s)`,
},
{
name: "auto group by without duration",
query: `SELECT mean(usage_idle) from "cpu" WHERE time > now() - 4320h group by :interval:`,
vars: chronograf.TemplateVars{
&chronograf.GroupByVar{
Var: ":interval:",
Duration: 0 * time.Minute,
Resolution: 1000,
ReportingInterval: 10 * time.Second,
},
},
want: `SELECT mean(usage_idle) from "cpu" WHERE time > now() - 4320h group by time(46656s)`,
},
{
name: "auto group by with :dashboardTime:",
query: `SELECT mean(usage_idle) from "cpu" WHERE time > :dashboardTime: group by :interval:`,
vars: chronograf.TemplateVars{
&chronograf.GroupByVar{
Var: ":interval:",
Duration: 0 * time.Minute,
Resolution: 1000,
ReportingInterval: 10 * time.Second,
},
&chronograf.BasicTemplateVar{
Var: ":dashboardTime:",
Values: []chronograf.BasicTemplateValue{
{
Type: "constant",
Value: "now() - 4320h",
},
},
},
},
want: `SELECT mean(usage_idle) from "cpu" WHERE time > now() - 4320h group by time(46656s)`,
},
{
name: "auto group by failing condition",
query: `SELECT mean(usage_idle) FROM "cpu" WHERE time > :dashboardTime: GROUP BY :interval:`,
vars: []chronograf.TemplateVariable{
&chronograf.GroupByVar{
Var: ":interval:",
Resolution: 115,
ReportingInterval: 10 * time.Second,
},
chronograf.BasicTemplateVar{
Var: ":dashboardTime:",
Values: []chronograf.BasicTemplateValue{
{
Value: "now() - 1h",
Type: "constant",
Selected: true,
},
},
},
},
want: `SELECT mean(usage_idle) FROM "cpu" WHERE time > now() - 1h GROUP BY time(93s)`,
},
2017-04-19 16:03:53 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := TemplateReplace(tt.query, tt.vars)
if got != tt.want {
t.Errorf("TestParse %s =\n%s\nwant\n%s", tt.name, got, tt.want)
}
})
}
}
func Test_TemplateVarsUnmarshalling(t *testing.T) {
req := `[
{
"tempVar": ":interval:",
"resolution": 1000,
"reportingInterval": 10
},
{
"tempVar": ":cpu:",
"values": [
{
"type": "tagValue",
2017-06-07 15:16:09 +00:00
"value": "cpu-total",
"selected": false
}
]
}
]`
expected := []chronograf.TemplateVariable{
&chronograf.GroupByVar{
Var: ":interval:",
Resolution: 1000,
ReportingInterval: 10 * time.Nanosecond,
},
chronograf.BasicTemplateVar{
Var: ":cpu:",
Values: []chronograf.BasicTemplateValue{
{
Value: "cpu-total",
Type: "tagValue",
Selected: false,
},
},
},
}
var tvars chronograf.TemplateVars
err := json.Unmarshal([]byte(req), &tvars)
if err != nil {
t.Fatal("Err unmarshaling:", err)
}
2017-06-07 15:16:09 +00:00
if len(tvars) != len(expected) {
t.Fatal("Expected", len(expected), "vars but found", len(tvars))
}
if !reflect.DeepEqual(*(tvars[0].(*chronograf.GroupByVar)), *(expected[0].(*chronograf.GroupByVar))) {
t.Errorf("UnmarshalJSON() = \n%#v\n want \n%#v\n", *(tvars[0].(*chronograf.GroupByVar)), *(expected[0].(*chronograf.GroupByVar)))
}
if !reflect.DeepEqual(tvars[1].(chronograf.BasicTemplateVar), expected[1].(chronograf.BasicTemplateVar)) {
t.Errorf("UnmarshalJSON() = \n%#v\n want \n%#v\n", tvars[1].(chronograf.BasicTemplateVar), expected[1].(chronograf.BasicTemplateVar))
}
}
func TestGroupByVarString(t *testing.T) {
tests := []struct {
name string
tvar *chronograf.GroupByVar
want string
}{
{
name: "String() calculates the GROUP BY interval",
tvar: &chronograf.GroupByVar{
Resolution: 700,
ReportingInterval: 10 * time.Second,
Duration: 24 * time.Hour,
},
2017-10-11 20:14:44 +00:00
want: "time(370s)",
},
{
2017-10-16 21:15:12 +00:00
name: "String() milliseconds if less than one second intervals",
tvar: &chronograf.GroupByVar{
Resolution: 100000,
ReportingInterval: 10 * time.Second,
Duration: time.Hour,
},
2017-10-11 20:14:44 +00:00
want: "time(107ms)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.tvar.String()
if got != tt.want {
t.Errorf("TestGroupByVarString %s =\n%s\nwant\n%s", tt.name, got, tt.want)
}
})
}
}