-
Notifications
You must be signed in to change notification settings - Fork 7
/
enforcer_logql_test.go
98 lines (91 loc) · 2.27 KB
/
enforcer_logql_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
package main
import (
"testing"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/assert"
)
func TestLogqlEnforcer(t *testing.T) {
tests := []struct {
name string
query string
tenantLabels map[string]bool
expectedResult string
expectErr bool
}{
{
name: "Valid query and tenant labels",
query: "{kubernetes_namespace_name=\"test\"}",
tenantLabels: map[string]bool{"test": true},
expectedResult: "{kubernetes_namespace_name=\"test\"}",
expectErr: false,
},
{
name: "Empty query and valid tenant labels",
query: "",
tenantLabels: map[string]bool{"test": true},
expectedResult: "{kubernetes_namespace_name=\"test\"}",
expectErr: false,
},
{
name: "Valid query and invalid tenant labels",
query: "{kubernetes_namespace_name=\"test\"}",
tenantLabels: map[string]bool{"invalid": true},
expectErr: true,
},
}
enforcer := LogQLEnforcer{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := enforcer.Enforce(tt.query, tt.tenantLabels, "kubernetes_namespace_name")
if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectedResult, result)
}
})
}
}
func TestMatchNamespaceMatchers(t *testing.T) {
tests := []struct {
name string
matchers []*labels.Matcher
tenantLabels map[string]bool
expectErr bool
}{
{
name: "Valid matchers and tenant labels",
matchers: []*labels.Matcher{
{
Type: labels.MatchEqual,
Name: "kubernetes_namespace_name",
Value: "test",
},
},
tenantLabels: map[string]bool{"test": true},
expectErr: false,
},
{
name: "Invalid matchers and valid tenant labels",
matchers: []*labels.Matcher{
{
Type: labels.MatchEqual,
Name: "kubernetes_namespace_name",
Value: "invalid",
},
},
tenantLabels: map[string]bool{"test": true},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := matchNamespaceMatchers(tt.matchers, tt.tenantLabels, "kubernetes_namespace_name")
if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}