-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathutils_test.go
More file actions
186 lines (162 loc) · 4.5 KB
/
utils_test.go
File metadata and controls
186 lines (162 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package metricsql
import (
"testing"
)
func TestExpandWithExprsSuccess(t *testing.T) {
f := func(q, qExpected string) {
t.Helper()
for i := 0; i < 3; i++ {
qExpanded, err := ExpandWithExprs(q)
if err != nil {
t.Fatalf("unexpected error when expanding %q: %s", q, err)
}
if qExpanded != qExpected {
t.Fatalf("unexpected expanded expression for %q;\ngot\n%q\nwant\n%q", q, qExpanded, qExpected)
}
}
}
f(`1`, `1`)
f(`foobar`, `foobar`)
f(`with (x = 1) x+x`, `2`)
f(`with (f(x) = x*x) 3+f(2)+2`, `9`)
}
func TestExpandWithExprsError(t *testing.T) {
f := func(q string) {
t.Helper()
for i := 0; i < 3; i++ {
qExpanded, err := ExpandWithExprs(q)
if err == nil {
t.Fatalf("expecting non-nil error when expanding %q", q)
}
if qExpanded != "" {
t.Fatalf("unexpected non-empty qExpanded=%q", qExpanded)
}
}
}
f(``)
f(` with (`)
}
func TestVisitAll(t *testing.T) {
f := func(q, sExpected string) {
t.Helper()
expr, err := Parse(q)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
var buf []byte
VisitAll(expr, func(e Expr) {
buf = e.AppendString(buf)
buf = append(buf, ',')
})
if string(buf) != sExpected {
t.Fatalf("unexpected result; got\n%q\nwant\n%q", buf, sExpected)
}
}
f("123", "123,")
f("1+2", "3,")
f("1+a", "1,a,(),(),1 + a,")
f("avg(a<b+1, sum(x) by (y))", "a,b,1,(),(),b + 1,(),(),a < (b + 1),x,by(y),sum(x) by(y),(),avg(a < (b + 1), sum(x) by(y)),")
f("x[1s]", "x,1s,x[1s],")
f("x[1h:5m] offset 5s @ 10s", "x,1h,5m,5s,10s,x[1h:5m] offset 5s @ 10s,")
}
func TestVisitAllBreakable(t *testing.T) {
f := func(q, sExpected string, finishedVisitExpected bool) {
t.Helper()
expr, err := Parse(q)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
var buf []byte
visits := 0
finishedVisit := VisitAllBreakable(expr, func(e Expr) bool {
visits += 1
buf = e.AppendString(buf)
buf = append(buf, ',')
return visits < 3
})
if string(buf) != sExpected {
t.Fatalf("unexpected result; got\n%q\nwant\n%q", buf, sExpected)
}
if finishedVisit != finishedVisitExpected {
t.Fatalf("unexpected 'finishedVisit' result; got\n%t\nwant\n%t", finishedVisit, finishedVisitExpected)
}
}
f("123", "123,", true)
f("1+2", "3,", true)
f("1+a", "1,a,(),", false)
f("avg(a<b+1, sum(x) by (y))", "a,b,1,", false)
f("x[1s]", "x,1s,x[1s],", false)
f("x[1h:5m] offset 5s @ 10s", "x,1h,5m,", false)
}
func TestIsLikelyInvalid(t *testing.T) {
f := func(q string, resultExpected bool) {
t.Helper()
expr, err := Parse(q)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
result := IsLikelyInvalid(expr)
if result != resultExpected {
t.Fatalf("unexpected result for IsLikelyInvalid(%q); got %v; want %v", q, result, resultExpected)
}
}
f("1", false)
f(`foo{bar="baz"}`, false)
// This should be OK, since it is easy to reason about
f(`rate(foo)`, false)
f(`timestamp(foo)`, false)
f(`foo[5m]`, false)
f(`1 + foo[5m]`, false)
f(`rate(foo[5s])`, false)
f(`rate(foo{bar=~"baz"}[5s])`, false)
f(`rate(foo{bar=~"baz"}[5s] offset 1h)`, false)
// Explicit subqueries are allowed
f(`sum_over_time((up > 0)[5m:1s])`, false)
f(`rate(sum(foo)[5m])`, false)
f(`rate(sum(foo)[5m:3s])`, false)
// Implicit step in the subquery is OK
f(`sum_over_time((up > 0)[5m])`, false)
// This is OK, since it is supported by Prometheus
f(`rate(foo{bar=~"baz"}[5m:1s])`, false)
f(`rate(foo{bar=~"baz"}[5m:1s] offset 1h)`, false)
f(`timestamp(sum(foo))`, false)
f(`sum(foo)`, false)
f(`sum(rate(foo))`, false)
f(`abs(foo)`, false)
f(`sum(abs(foo))`, false)
// This isn't OK, since these queries work unexpectedly most of the time
f(`rate(sum(foo))`, true)
f(`rate(abs(foo))`, true)
f(`rate(1)`, true)
f(`rate(foo + bar)`, true)
f(`rate(rate(foo))`, true)
f(`1 + rate(label_set(foo, "bar", "baz"))`, true)
f(`rate(sum(foo) offset 5m)`, true)
// invalid number of args
f(`quantile_over_time(foo)`, false)
}
func TestIsSupportedFunction(t *testing.T) {
f := func(s string, expectedResult bool) {
t.Helper()
result := IsSupportedFunction(s)
if result != expectedResult {
t.Fatalf("unexpected result for IsSupportedFunction(%q); got %v; want %v", s, result, expectedResult)
}
}
// empty function name is a synonim to union()
f("", true)
f("union", true)
// rollup function
f("rate", true)
f("RATE", true)
f("Increase", true)
// transform function
f("ceil", true)
f("histogram_QUANTILe", true)
// aggregate function
f("sum", true)
f("aVG", true)
// Unknown function
f("foo", false)
f("BAR", false)
}