kubelatte-ce
Форк от sbertech/kubelatte-ce
240 строк · 4.4 Кб
1package match
2
3import (
4"github.com/stretchr/testify/assert"
5"gitverse.ru/synapse/kubelatte/pkg/api/v1alpha1"
6"testing"
7)
8
9func Test_doesMatchExpressionsMatch(t *testing.T) {
10type args struct {
11searchData map[string]string
12matchExp []v1alpha1.MatchExpression
13}
14tests := []struct {
15name string
16args args
17want bool
18}{
19{
20name: "In true",
21args: args{
22searchData: map[string]string{
23"name": "test",
24},
25matchExp: []v1alpha1.MatchExpression{
26{
27Key: "name",
28Operator: "In",
29Values: []string{"test"},
30},
31},
32},
33want: true,
34},
35{
36name: "In false",
37args: args{
38searchData: map[string]string{
39"name": "name",
40},
41matchExp: []v1alpha1.MatchExpression{
42{
43Key: "name",
44Operator: "In",
45Values: []string{"test"},
46},
47},
48},
49want: false,
50},
51{
52name: "NotIn true",
53args: args{
54searchData: map[string]string{
55"name": "name",
56},
57matchExp: []v1alpha1.MatchExpression{
58{
59Key: "name",
60Operator: "NotIn",
61Values: []string{"test"},
62},
63},
64},
65want: true,
66},
67{
68name: "NotIn false",
69args: args{
70searchData: map[string]string{
71"name": "test",
72},
73matchExp: []v1alpha1.MatchExpression{
74{
75Key: "name",
76Operator: "NotIn",
77Values: []string{"test"},
78},
79},
80},
81want: false,
82},
83{
84name: "Exists true",
85args: args{
86searchData: map[string]string{
87"name": "test",
88},
89matchExp: []v1alpha1.MatchExpression{
90{
91Key: "name",
92Operator: "Exists",
93},
94},
95},
96want: true,
97},
98{
99name: "Exists false",
100args: args{
101searchData: map[string]string{
102"test": "name",
103},
104matchExp: []v1alpha1.MatchExpression{
105{
106Key: "name",
107Operator: "Exists",
108},
109},
110},
111want: false,
112},
113{
114name: "DoesNotExist true",
115args: args{
116searchData: map[string]string{
117"test": "name",
118},
119matchExp: []v1alpha1.MatchExpression{
120{
121Key: "name",
122Operator: "DoesNotExist",
123},
124},
125},
126want: true,
127},
128{
129name: "DoesNotExist false",
130args: args{
131searchData: map[string]string{
132"name": "test",
133},
134matchExp: []v1alpha1.MatchExpression{
135{
136Key: "name",
137Operator: "DoesNotExist",
138},
139},
140},
141want: false,
142},
143{
144name: "NotIn true",
145args: args{
146searchData: map[string]string{
147"test": "test",
148},
149matchExp: []v1alpha1.MatchExpression{
150{
151Key: "name",
152Operator: "NotIn",
153},
154},
155},
156want: true,
157},
158}
159for _, tt := range tests {
160t.Run(tt.name, func(t *testing.T) {
161assert.Equalf(t, tt.want, doesMatchExpressionsMatch(tt.args.searchData, tt.args.matchExp), "doesMatchExpressionsMatch(%v, %v)", tt.args.searchData, tt.args.matchExp)
162})
163}
164}
165
166func Test_checkStringsMatchingWithSliceRegexp(t *testing.T) {
167type args struct {
168rule []string
169comparedVal string
170}
171tests := []struct {
172name string
173args args
174want bool
175}{
176{
177name: "value",
178args: args{
179rule: []string{"eetets", "test"},
180comparedVal: "test",
181},
182want: true,
183},
184{
185name: "star",
186args: args{
187rule: []string{"*"},
188comparedVal: "test",
189},
190want: true,
191},
192{
193name: "regex wrong",
194args: args{
195rule: []string{"a(b`"},
196comparedVal: "test",
197},
198want: false,
199},
200}
201for _, tt := range tests {
202t.Run(tt.name, func(t *testing.T) {
203assert.Equalf(t, tt.want, checkStringsMatchingWithSliceRegexp(tt.args.rule, tt.args.comparedVal), "checkStringsMatchingWithSliceRegexp(%v, %v)", tt.args.rule, tt.args.comparedVal)
204})
205}
206}
207
208func Test_existsStringInSlice(t *testing.T) {
209type args struct {
210rule []string
211comparedVal string
212}
213tests := []struct {
214name string
215args args
216want bool
217}{
218{
219name: "ok",
220args: args{
221rule: []string{"Deployment"},
222comparedVal: "DeploymentConfig",
223},
224want: false,
225},
226{
227name: "ok",
228args: args{
229rule: []string{"*"},
230comparedVal: "DeploymentConfig",
231},
232want: true,
233},
234}
235for _, tt := range tests {
236t.Run(tt.name, func(t *testing.T) {
237assert.Equalf(t, tt.want, existsStringInSlice(tt.args.rule, tt.args.comparedVal), "checkStringsMatchingWithSliceRegexp(%v, %v)", tt.args.rule, tt.args.comparedVal)
238})
239}
240}
241