@@ -25,10 +25,13 @@ func BuildGatewayClass(name string) gtwapi.GatewayClass {
25
25
return GatewayClass
26
26
}
27
27
28
- func BuildGateway (className gtwapi.ObjectName , status metav1.ConditionStatus ) gtwapi.Gateway {
28
+ func BuildGateway (className gtwapi.ObjectName , status metav1.ConditionStatus , labels map [ string ] string ) gtwapi.Gateway {
29
29
Gateway := gtwapi.Gateway {}
30
30
Gateway .Name = "foobar"
31
31
Gateway .Namespace = "default"
32
+ if labels != nil {
33
+ Gateway .Labels = labels
34
+ }
32
35
Gateway .Spec .GatewayClassName = className
33
36
Gateway .Spec .Listeners = []gtwapi.Listener {
34
37
{
@@ -53,7 +56,7 @@ func TestGatewayAnalyzer(t *testing.T) {
53
56
AcceptedStatus := metav1 .ConditionTrue
54
57
GatewayClass := BuildGatewayClass (string (ClassName ))
55
58
56
- Gateway := BuildGateway (ClassName , AcceptedStatus )
59
+ Gateway := BuildGateway (ClassName , AcceptedStatus , nil )
57
60
// Create a Gateway Analyzer instance with the fake client
58
61
scheme := scheme .Scheme
59
62
@@ -91,7 +94,7 @@ func TestGatewayAnalyzer(t *testing.T) {
91
94
func TestMissingClassGatewayAnalyzer (t * testing.T ) {
92
95
ClassName := gtwapi .ObjectName ("non-existed" )
93
96
AcceptedStatus := metav1 .ConditionTrue
94
- Gateway := BuildGateway (ClassName , AcceptedStatus )
97
+ Gateway := BuildGateway (ClassName , AcceptedStatus , nil )
95
98
96
99
// Create a Gateway Analyzer instance with the fake client
97
100
scheme := scheme .Scheme
@@ -130,7 +133,7 @@ func TestStatusGatewayAnalyzer(t *testing.T) {
130
133
AcceptedStatus := metav1 .ConditionUnknown
131
134
GatewayClass := BuildGatewayClass (string (ClassName ))
132
135
133
- Gateway := BuildGateway (ClassName , AcceptedStatus )
136
+ Gateway := BuildGateway (ClassName , AcceptedStatus , nil )
134
137
135
138
// Create a Gateway Analyzer instance with the fake client
136
139
scheme := scheme .Scheme
@@ -178,3 +181,70 @@ func TestStatusGatewayAnalyzer(t *testing.T) {
178
181
t .Errorf ("Expected message, <%v> , not found in Gateway's analysis results" , want )
179
182
}
180
183
}
184
+
185
+ func TestGatewayAnalyzerLabelSelectorFiltering (t * testing.T ) {
186
+ ClassName := gtwapi .ObjectName ("non-existed" )
187
+ AcceptedStatus := metav1 .ConditionTrue
188
+
189
+ Gateway := BuildGateway (ClassName , AcceptedStatus , map [string ]string {"app" : "gateway" })
190
+ scheme := scheme .Scheme
191
+ err := gtwapi .Install (scheme )
192
+ if err != nil {
193
+ t .Error (err )
194
+ }
195
+ err = apiextensionsv1 .AddToScheme (scheme )
196
+ if err != nil {
197
+ t .Error (err )
198
+ }
199
+ objects := []runtime.Object {
200
+ & Gateway ,
201
+ }
202
+
203
+ fakeClient := fakeclient .NewClientBuilder ().WithScheme (scheme ).WithRuntimeObjects (objects ... ).Build ()
204
+
205
+ analyzerInstance := GatewayAnalyzer {}
206
+ // without label selector should return 1 result
207
+ config := common.Analyzer {
208
+ Client : & kubernetes.Client {
209
+ CtrlClient : fakeClient ,
210
+ },
211
+ Context : context .Background (),
212
+ Namespace : "default" ,
213
+ }
214
+ analysisResults , err := analyzerInstance .Analyze (config )
215
+ if err != nil {
216
+ t .Error (err )
217
+ }
218
+ assert .Equal (t , len (analysisResults ), 1 )
219
+
220
+ // with label selector should return 1 result
221
+ config = common.Analyzer {
222
+ Client : & kubernetes.Client {
223
+ CtrlClient : fakeClient ,
224
+ },
225
+ Context : context .Background (),
226
+ Namespace : "default" ,
227
+ LabelSelector : "app=gateway" ,
228
+ }
229
+ analysisResults , err = analyzerInstance .Analyze (config )
230
+ if err != nil {
231
+ t .Error (err )
232
+ }
233
+ assert .Equal (t , len (analysisResults ), 1 )
234
+
235
+ // with wrong label selector should return 0 result
236
+ config = common.Analyzer {
237
+ Client : & kubernetes.Client {
238
+ CtrlClient : fakeClient ,
239
+ },
240
+ Context : context .Background (),
241
+ Namespace : "default" ,
242
+ LabelSelector : "app=wrong" ,
243
+ }
244
+ analysisResults , err = analyzerInstance .Analyze (config )
245
+ if err != nil {
246
+ t .Error (err )
247
+ }
248
+ assert .Equal (t , len (analysisResults ), 0 )
249
+
250
+ }
0 commit comments