forked from gepaplexx/multena-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenforcer_promql_test.go
68 lines (66 loc) · 1.54 KB
/
enforcer_promql_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
package main
import (
"strings"
"testing"
)
func Test_promqlEnforcer(t *testing.T) {
type args struct {
query string
tenantLabels map[string]bool
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "case 1",
args: args{
query: "up",
tenantLabels: map[string]bool{"namespace1": true},
},
want: "up{namespace=\"namespace1\"}",
wantErr: false,
},
{
name: "case 2",
args: args{
query: "{__name__=\"up\",namespace=\"namespace2\"}",
tenantLabels: map[string]bool{"namespace1": true},
},
want: "",
wantErr: true,
},
{
name: "case 3",
args: args{
query: "up{namespace=\"namespace1\"}",
tenantLabels: map[string]bool{"namespace1": true, "namespace2": true},
},
want: "up{namespace=\"namespace1\"}",
wantErr: false,
},
{
name: "case 4",
args: args{
query: "up",
tenantLabels: map[string]bool{"namespace": true, "grrr": true},
},
want: "up{namespace=~\"namespace|grrr\"}|s|up{namespace=~\"grrr|namespace\"}",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := PromQLEnforcer{}.Enforce(tt.args.query, tt.args.tenantLabels, "namespace")
if (err != nil) != tt.wantErr {
t.Errorf("promqlEnforcer() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != strings.Split(tt.want, "|s|")[0] && got != strings.Split(tt.want, "|s|")[1] {
t.Errorf("promqlEnforcer() = %v, want %v", got, tt.want)
}
})
}
}