-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathquery_test.go
87 lines (71 loc) · 1.72 KB
/
query_test.go
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
//: Copyright Verizon Media
//: Licensed under the terms of the Apache 2.0 License. See LICENSE file in the project root for terms.
package vssh
import (
"testing"
)
func TestQueryExprEval(t *testing.T) {
labels := map[string]string{"POP": "LAX", "OS": "JUNOS"}
exprTests := []struct {
expr string
expected bool
}{
{"POP==LAX", true},
{"POP!=LAX", false},
{"POP==LAX && OS==JUNOS", true},
{"POP==LAX && OS!=JUNOS", false},
{"(POP==LAX || POP==BUR) && OS==JUNOS", true},
{"OS==JUNOS && (POP==LAX || POP==BUR)", true},
{"OS!=JUNOS && (POP==LAX || POP==BUR)", false},
{"(OS==JUNOS) && (POP==LAX || POP==BUR)", true},
{"((OS==JUNOS) && (POP==LAX || POP==BUR))", true},
}
for _, x := range exprTests {
v, err := parseExpr(x.expr)
if err != nil {
t.Fatal(err)
}
ok, err := exprEval(v, labels)
if err != nil {
t.Fatal(err)
}
if ok != x.expected {
t.Fatalf("expect %t, got %t", x.expected, ok)
}
}
_, err := parseExpr("OS=JUNOS")
if err == nil {
t.Fatal("expect error but got nil")
}
v, err := parseExpr("OS")
if err != nil {
t.Fatal("expect error but got nil")
}
_, err = exprEval(v, labels)
if err == nil {
t.Fatal("expect error but got nil")
}
// not support operator
ops := []string{"&", "+", "<=", "<"}
for _, op := range ops {
v, _ := parseExpr("OS == JUNOS " + op + " POP == LAX")
_, err = exprEval(v, labels)
if err == nil {
t.Fatal("expect error but got nil")
}
}
}
func BenchmarkQueryExprEval(b *testing.B) {
labels := map[string]string{"POP": "LAX", "OS": "JUNOS"}
expr := "POP==LAX"
for i := 0; i < b.N; i++ {
v, err := parseExpr(expr)
if err != nil {
b.Fatal(err)
}
_, err = exprEval(v, labels)
if err != nil {
b.Fatal(err)
}
}
}