@@ -218,11 +218,12 @@ func Test_GetGatewayClassesManagedByLBController(t *testing.T) {
218218
219219func Test_GetImpactedGatewaysFromParentRefs (t * testing.T ) {
220220 type args struct {
221- parentRefs []gwv1.ParentReference
222- resourceNS string
223- gateways []* gwv1.Gateway
224- gatewayClasses []* gwv1.GatewayClass
225- gwController string
221+ parentRefs []gwv1.ParentReference
222+ originalParentRefsFromRouteStatus []gwv1.RouteParentStatus
223+ resourceNS string
224+ gateways []* gwv1.Gateway
225+ gatewayClasses []* gwv1.GatewayClass
226+ gwController string
226227 }
227228 tests := []struct {
228229 name string
@@ -271,6 +272,68 @@ func Test_GetImpactedGatewaysFromParentRefs(t *testing.T) {
271272 },
272273 wantErr : nil ,
273274 },
275+ {
276+ name : "valid parent refs with managed gateways and originalParentRefsFromRouteStatus" ,
277+ args : args {
278+ parentRefs : []gwv1.ParentReference {
279+ {
280+ Name : "test-gw" ,
281+ Namespace : (* gwv1 .Namespace )(ptr .To ("test-ns" )),
282+ },
283+ },
284+ resourceNS : "test-ns" ,
285+ originalParentRefsFromRouteStatus : []gwv1.RouteParentStatus {
286+ {
287+ ParentRef : gwv1.ParentReference {
288+ Name : "test-gw-1" ,
289+ Namespace : (* gwv1 .Namespace )(ptr .To ("test-ns" )),
290+ },
291+ },
292+ },
293+ gateways : []* gwv1.Gateway {
294+ {
295+ ObjectMeta : metav1.ObjectMeta {
296+ Name : "test-gw" ,
297+ Namespace : "test-ns" ,
298+ },
299+ Spec : gwv1.GatewaySpec {
300+ GatewayClassName : "nlb-class" ,
301+ },
302+ },
303+ {
304+ ObjectMeta : metav1.ObjectMeta {
305+ Name : "test-gw-1" ,
306+ Namespace : "test-ns" ,
307+ },
308+ Spec : gwv1.GatewaySpec {
309+ GatewayClassName : "nlb-class" ,
310+ },
311+ },
312+ },
313+ gatewayClasses : []* gwv1.GatewayClass {
314+ {
315+ ObjectMeta : metav1.ObjectMeta {
316+ Name : "nlb-class" ,
317+ },
318+ Spec : gwv1.GatewayClassSpec {
319+ ControllerName : constants .NLBGatewayController ,
320+ },
321+ },
322+ },
323+ gwController : constants .NLBGatewayController ,
324+ },
325+ want : []types.NamespacedName {
326+ {
327+ Namespace : "test-ns" ,
328+ Name : "test-gw" ,
329+ },
330+ {
331+ Namespace : "test-ns" ,
332+ Name : "test-gw-1" ,
333+ },
334+ },
335+ wantErr : nil ,
336+ },
274337 {
275338 name : "valid parent refs with unmanaged gateways" ,
276339 args : args {
@@ -454,7 +517,7 @@ func Test_GetImpactedGatewaysFromParentRefs(t *testing.T) {
454517 k8sClient .Create (context .Background (), gwClass )
455518 }
456519
457- got , err := GetImpactedGatewaysFromParentRefs (context .Background (), k8sClient , tt .args .parentRefs , tt .args .resourceNS , tt .args .gwController )
520+ got , err := GetImpactedGatewaysFromParentRefs (context .Background (), k8sClient , tt .args .parentRefs , tt .args .originalParentRefsFromRouteStatus , tt . args . resourceNS , tt .args .gwController )
458521
459522 assert .Equal (t , err , tt .wantErr )
460523 assert .Equal (t , tt .want , got )
@@ -768,3 +831,76 @@ func Test_GetImpactedGatewaysFromLbConfig(t *testing.T) {
768831 })
769832 }
770833}
834+
835+ func TestRemoveDuplicateParentRefs (t * testing.T ) {
836+ namespace := "test-namespace"
837+ tests := []struct {
838+ name string
839+ parentRefs []gwv1.ParentReference
840+ resourceNamespace string
841+ want []gwv1.ParentReference
842+ }{
843+ {
844+ name : "no duplicates" ,
845+ parentRefs : []gwv1.ParentReference {
846+ {Name : "gateway1" },
847+ {Name : "gateway2" },
848+ },
849+ resourceNamespace : namespace ,
850+ want : []gwv1.ParentReference {
851+ {Name : "gateway1" },
852+ {Name : "gateway2" },
853+ },
854+ },
855+ {
856+ name : "one duplicate" ,
857+ parentRefs : []gwv1.ParentReference {
858+ {Name : "gateway1" },
859+ {Name : "gateway1" },
860+ {Name : "gateway2" },
861+ },
862+ resourceNamespace : namespace ,
863+ want : []gwv1.ParentReference {
864+ {Name : "gateway1" },
865+ {Name : "gateway2" },
866+ },
867+ },
868+ {
869+ name : "multiple duplicates" ,
870+ parentRefs : []gwv1.ParentReference {
871+ {Name : "gateway1" },
872+ {Name : "gateway2" },
873+ {Name : "gateway1" },
874+ {Name : "gateway2" },
875+ {Name : "gateway3" },
876+ {Name : "gateway1" },
877+ },
878+ resourceNamespace : namespace ,
879+ want : []gwv1.ParentReference {
880+ {Name : "gateway1" },
881+ {Name : "gateway2" },
882+ {Name : "gateway3" },
883+ },
884+ },
885+ }
886+
887+ for _ , tt := range tests {
888+ t .Run (tt .name , func (t * testing.T ) {
889+ got := removeDuplicateParentRefs (tt .parentRefs , tt .resourceNamespace )
890+
891+ assert .Equal (t , len (tt .want ), len (got ))
892+
893+ actual := make (map [string ]bool )
894+ for _ , ref := range got {
895+ actual [string (ref .Name )] = true
896+ }
897+
898+ expected := make (map [string ]bool )
899+ for _ , ref := range tt .want {
900+ expected [string (ref .Name )] = true
901+ }
902+
903+ assert .Equal (t , expected , actual )
904+ })
905+ }
906+ }
0 commit comments