kubelatte-ce

Форк
2
Форк от sbertech/kubelatte-ce
/
helpers_test.go 
240 строк · 4.4 Кб
1
package match
2

3
import (
4
	"github.com/stretchr/testify/assert"
5
	"gitverse.ru/synapse/kubelatte/pkg/api/v1alpha1"
6
	"testing"
7
)
8

9
func Test_doesMatchExpressionsMatch(t *testing.T) {
10
	type args struct {
11
		searchData map[string]string
12
		matchExp   []v1alpha1.MatchExpression
13
	}
14
	tests := []struct {
15
		name string
16
		args args
17
		want bool
18
	}{
19
		{
20
			name: "In true",
21
			args: args{
22
				searchData: map[string]string{
23
					"name": "test",
24
				},
25
				matchExp: []v1alpha1.MatchExpression{
26
					{
27
						Key:      "name",
28
						Operator: "In",
29
						Values:   []string{"test"},
30
					},
31
				},
32
			},
33
			want: true,
34
		},
35
		{
36
			name: "In false",
37
			args: args{
38
				searchData: map[string]string{
39
					"name": "name",
40
				},
41
				matchExp: []v1alpha1.MatchExpression{
42
					{
43
						Key:      "name",
44
						Operator: "In",
45
						Values:   []string{"test"},
46
					},
47
				},
48
			},
49
			want: false,
50
		},
51
		{
52
			name: "NotIn true",
53
			args: args{
54
				searchData: map[string]string{
55
					"name": "name",
56
				},
57
				matchExp: []v1alpha1.MatchExpression{
58
					{
59
						Key:      "name",
60
						Operator: "NotIn",
61
						Values:   []string{"test"},
62
					},
63
				},
64
			},
65
			want: true,
66
		},
67
		{
68
			name: "NotIn false",
69
			args: args{
70
				searchData: map[string]string{
71
					"name": "test",
72
				},
73
				matchExp: []v1alpha1.MatchExpression{
74
					{
75
						Key:      "name",
76
						Operator: "NotIn",
77
						Values:   []string{"test"},
78
					},
79
				},
80
			},
81
			want: false,
82
		},
83
		{
84
			name: "Exists true",
85
			args: args{
86
				searchData: map[string]string{
87
					"name": "test",
88
				},
89
				matchExp: []v1alpha1.MatchExpression{
90
					{
91
						Key:      "name",
92
						Operator: "Exists",
93
					},
94
				},
95
			},
96
			want: true,
97
		},
98
		{
99
			name: "Exists false",
100
			args: args{
101
				searchData: map[string]string{
102
					"test": "name",
103
				},
104
				matchExp: []v1alpha1.MatchExpression{
105
					{
106
						Key:      "name",
107
						Operator: "Exists",
108
					},
109
				},
110
			},
111
			want: false,
112
		},
113
		{
114
			name: "DoesNotExist true",
115
			args: args{
116
				searchData: map[string]string{
117
					"test": "name",
118
				},
119
				matchExp: []v1alpha1.MatchExpression{
120
					{
121
						Key:      "name",
122
						Operator: "DoesNotExist",
123
					},
124
				},
125
			},
126
			want: true,
127
		},
128
		{
129
			name: "DoesNotExist false",
130
			args: args{
131
				searchData: map[string]string{
132
					"name": "test",
133
				},
134
				matchExp: []v1alpha1.MatchExpression{
135
					{
136
						Key:      "name",
137
						Operator: "DoesNotExist",
138
					},
139
				},
140
			},
141
			want: false,
142
		},
143
		{
144
			name: "NotIn true",
145
			args: args{
146
				searchData: map[string]string{
147
					"test": "test",
148
				},
149
				matchExp: []v1alpha1.MatchExpression{
150
					{
151
						Key:      "name",
152
						Operator: "NotIn",
153
					},
154
				},
155
			},
156
			want: true,
157
		},
158
	}
159
	for _, tt := range tests {
160
		t.Run(tt.name, func(t *testing.T) {
161
			assert.Equalf(t, tt.want, doesMatchExpressionsMatch(tt.args.searchData, tt.args.matchExp), "doesMatchExpressionsMatch(%v, %v)", tt.args.searchData, tt.args.matchExp)
162
		})
163
	}
164
}
165

166
func Test_checkStringsMatchingWithSliceRegexp(t *testing.T) {
167
	type args struct {
168
		rule        []string
169
		comparedVal string
170
	}
171
	tests := []struct {
172
		name string
173
		args args
174
		want bool
175
	}{
176
		{
177
			name: "value",
178
			args: args{
179
				rule:        []string{"eetets", "test"},
180
				comparedVal: "test",
181
			},
182
			want: true,
183
		},
184
		{
185
			name: "star",
186
			args: args{
187
				rule:        []string{"*"},
188
				comparedVal: "test",
189
			},
190
			want: true,
191
		},
192
		{
193
			name: "regex wrong",
194
			args: args{
195
				rule:        []string{"a(b`"},
196
				comparedVal: "test",
197
			},
198
			want: false,
199
		},
200
	}
201
	for _, tt := range tests {
202
		t.Run(tt.name, func(t *testing.T) {
203
			assert.Equalf(t, tt.want, checkStringsMatchingWithSliceRegexp(tt.args.rule, tt.args.comparedVal), "checkStringsMatchingWithSliceRegexp(%v, %v)", tt.args.rule, tt.args.comparedVal)
204
		})
205
	}
206
}
207

208
func Test_existsStringInSlice(t *testing.T) {
209
	type args struct {
210
		rule        []string
211
		comparedVal string
212
	}
213
	tests := []struct {
214
		name string
215
		args args
216
		want bool
217
	}{
218
		{
219
			name: "ok",
220
			args: args{
221
				rule:        []string{"Deployment"},
222
				comparedVal: "DeploymentConfig",
223
			},
224
			want: false,
225
		},
226
		{
227
			name: "ok",
228
			args: args{
229
				rule:        []string{"*"},
230
				comparedVal: "DeploymentConfig",
231
			},
232
			want: true,
233
		},
234
	}
235
	for _, tt := range tests {
236
		t.Run(tt.name, func(t *testing.T) {
237
			assert.Equalf(t, tt.want, existsStringInSlice(tt.args.rule, tt.args.comparedVal), "checkStringsMatchingWithSliceRegexp(%v, %v)", tt.args.rule, tt.args.comparedVal)
238
		})
239
	}
240
}
241

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.