forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 15
/
rule_test.go
112 lines (106 loc) · 2.59 KB
/
rule_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
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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package main
import (
"testing"
"github.com/efficientgo/core/testutil"
)
func Test_parseFlagLabels(t *testing.T) {
var tData = []struct {
s []string
expectErr bool
}{
{
s: []string{`labelName="LabelVal"`, `_label_Name="LabelVal"`, `label_name="LabelVal"`, `LAb_el_Name="LabelValue"`, `lab3l_Nam3="LabelValue"`},
expectErr: false,
},
{
s: []string{`label-Name="LabelVal"`}, // Unsupported labelname.
expectErr: true,
},
{
s: []string{`label:Name="LabelVal"`}, // Unsupported labelname.
expectErr: true,
},
{
s: []string{`1abelName="LabelVal"`}, // Unsupported labelname.
expectErr: true,
},
{
s: []string{`label_Name"LabelVal"`}, // Missing "=" seprator.
expectErr: true,
},
{
s: []string{`label_Name= "LabelVal"`}, // Whitespace invalid syntax.
expectErr: true,
},
{
s: []string{`label_name=LabelVal`}, // Missing quotes invalid syntax.
expectErr: true,
},
}
for _, td := range tData {
_, err := parseFlagLabels(td.s)
testutil.Equals(t, err != nil, td.expectErr)
}
}
func Test_validateTemplate(t *testing.T) {
tData := []struct {
template string
expectErr bool
}{
{
template: `/graph?g0.expr={{.Expr}}&g0.tab=1`,
expectErr: false,
},
{
template: `/graph?g0.expr={{.Expression}}&g0.tab=1`,
expectErr: true,
},
{
template: `another template includes {{.Expr}}`,
expectErr: false,
},
}
for _, td := range tData {
err := validateTemplate(td.template)
testutil.Equals(t, err != nil, td.expectErr)
}
}
func Test_tableLinkForExpression(t *testing.T) {
tData := []struct {
template string
expr string
expectStr string
expectErr bool
}{
{
template: `/graph?g0.expr={{.Expr}}&g0.tab=1`,
expr: `up{app="foo"}`,
expectStr: `/graph?g0.expr=up%7Bapp%3D%22foo%22%7D&g0.tab=1`,
expectErr: false,
},
{
template: `/graph?g0.expr={{.Expr}}&g0.tab=1`,
expr: `up{app="foo yoo"}`,
expectStr: `/graph?g0.expr=up%7Bapp%3D%22foo+yoo%22%7D&g0.tab=1`,
expectErr: false,
},
{
template: `/graph?g0.expr={{.Expression}}&g0.tab=1`,
expr: "test_expr",
expectErr: true,
},
{
template: `another template includes {{.Expr}}`,
expr: "test_expr",
expectStr: `another template includes test_expr`,
expectErr: false,
},
}
for _, td := range tData {
resStr, err := tableLinkForExpression(td.template, td.expr)
testutil.Equals(t, err != nil, td.expectErr)
testutil.Equals(t, resStr, td.expectStr)
}
}