-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathparser_test.go
68 lines (59 loc) · 1.52 KB
/
parser_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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package extpromql_test
import (
"fmt"
"testing"
"github.com/efficientgo/core/testutil"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql/parser"
"github.com/thanos-io/thanos/pkg/extpromql"
)
func TestParseMetricSelector(t *testing.T) {
testCases := []struct {
name string
input string
}{
{
name: "single selector",
input: `http_requests_total{method="GET"}`,
},
{
name: "empty selectors",
input: `process_cpu_seconds_total`,
},
{
name: "multiple selectors",
input: `http_requests_total{method="GET",code="200"}`,
},
{
name: "multiple selectors with different matchers",
input: `http_requests_total{method="GET",code!="200"}`,
},
{
name: "multiple selectors with regex",
input: `http_requests_total{method="GET",code=~"2.*"}`,
},
{
name: "selector with negative regex",
input: `{code!~"2.*"}`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
//lint:ignore faillint Testing against prometheus parser.
want, err := parser.ParseMetricSelector(tc.input)
if err != nil {
t.Fatalf("Prometheus ParseMetricSelector failed: %v", err)
}
got, err := extpromql.ParseMetricSelector(tc.input)
if err != nil {
t.Fatalf("ParseMetricSelector failed: %v", err)
}
testutil.Equals(t, stringFmt(want), stringFmt(got))
})
}
}
func stringFmt(got []*labels.Matcher) string {
return fmt.Sprintf("%v", got)
}