@@ -457,3 +457,140 @@ func TestGatewaysWithInvalidPolicyRef(t *testing.T) {
457
457
t .Error ("gateway expected not to be listed as with invalid policy ref" )
458
458
}
459
459
}
460
+
461
+ func TestGatewayWrapperKey (t * testing.T ) {
462
+ gw := GatewayWrapper {
463
+ Gateway : & gatewayapiv1alpha2.Gateway {
464
+ ObjectMeta : metav1.ObjectMeta {
465
+ Namespace : "gw-ns" ,
466
+ Name : "gw-1" ,
467
+ Annotations : map [string ]string {"kuadrant.io/ratelimitpolicies" : `[{"Namespace":"app-ns","Name":"policy-1"},{"Namespace":"app-ns","Name":"policy-2"}]` },
468
+ },
469
+ },
470
+ PolicyRefsConfig : & KuadrantRateLimitPolicyRefsConfig {},
471
+ }
472
+ if gw .Key ().Namespace != "gw-ns" || gw .Key ().Name != "gw-1" {
473
+ t .Fail ()
474
+ }
475
+ }
476
+
477
+ func TestGatewayWrapperPolicyRefs (t * testing.T ) {
478
+ gw := GatewayWrapper {
479
+ Gateway : & gatewayapiv1alpha2.Gateway {
480
+ ObjectMeta : metav1.ObjectMeta {
481
+ Namespace : "gw-ns" ,
482
+ Name : "gw-1" ,
483
+ Annotations : map [string ]string {"kuadrant.io/ratelimitpolicies" : `[{"Namespace":"app-ns","Name":"policy-1"},{"Namespace":"app-ns","Name":"policy-2"}]` },
484
+ },
485
+ },
486
+ PolicyRefsConfig : & KuadrantRateLimitPolicyRefsConfig {},
487
+ }
488
+ refs := Map (gw .PolicyRefs (), func (ref k8stypes.NamespacedName ) string { return ref .String () })
489
+ if ! Contains (refs , "app-ns/policy-1" ) {
490
+ t .Error ("GatewayWrapper.PolicyRefs() should contain app-ns/policy-1" )
491
+ }
492
+ if ! Contains (refs , "app-ns/policy-2" ) {
493
+ t .Error ("GatewayWrapper.PolicyRefs() should contain app-ns/policy-2" )
494
+ }
495
+ if len (refs ) != 2 {
496
+ t .Fail ()
497
+ }
498
+ }
499
+
500
+ func TestGatewayWrapperContainsPolicy (t * testing.T ) {
501
+ gw := GatewayWrapper {
502
+ Gateway : & gatewayapiv1alpha2.Gateway {
503
+ ObjectMeta : metav1.ObjectMeta {
504
+ Namespace : "gw-ns" ,
505
+ Name : "gw-1" ,
506
+ Annotations : map [string ]string {"kuadrant.io/ratelimitpolicies" : `[{"Namespace":"app-ns","Name":"policy-1"},{"Namespace":"app-ns","Name":"policy-2"}]` },
507
+ },
508
+ },
509
+ PolicyRefsConfig : & KuadrantRateLimitPolicyRefsConfig {},
510
+ }
511
+ if ! gw .ContainsPolicy (client.ObjectKey {Namespace : "app-ns" , Name : "policy-1" }) {
512
+ t .Error ("GatewayWrapper.ContainsPolicy() should contain app-ns/policy-1" )
513
+ }
514
+ if ! gw .ContainsPolicy (client.ObjectKey {Namespace : "app-ns" , Name : "policy-2" }) {
515
+ t .Error ("GatewayWrapper.ContainsPolicy() should contain app-ns/policy-1" )
516
+ }
517
+ if gw .ContainsPolicy (client.ObjectKey {Namespace : "app-ns" , Name : "policy-3" }) {
518
+ t .Error ("GatewayWrapper.ContainsPolicy() should not contain app-ns/policy-1" )
519
+ }
520
+ }
521
+
522
+ func TestGatewayWrapperAddPolicy (t * testing.T ) {
523
+ gateway := gatewayapiv1alpha2.Gateway {
524
+ ObjectMeta : metav1.ObjectMeta {
525
+ Namespace : "gw-ns" ,
526
+ Name : "gw-1" ,
527
+ Annotations : map [string ]string {"kuadrant.io/ratelimitpolicies" : `[{"Namespace":"app-ns","Name":"policy-1"},{"Namespace":"app-ns","Name":"policy-2"}]` },
528
+ },
529
+ }
530
+ gw := GatewayWrapper {
531
+ Gateway : & gateway ,
532
+ PolicyRefsConfig : & KuadrantRateLimitPolicyRefsConfig {},
533
+ }
534
+ if gw .AddPolicy (client.ObjectKey {Namespace : "app-ns" , Name : "policy-1" }) {
535
+ t .Error ("GatewayWrapper.AddPolicy() expected to return false" )
536
+ }
537
+ if ! gw .AddPolicy (client.ObjectKey {Namespace : "app-ns" , Name : "policy-3" }) {
538
+ t .Error ("GatewayWrapper.AddPolicy() expected to return true" )
539
+ }
540
+ if gw .Annotations ["kuadrant.io/ratelimitpolicies" ] != `[{"Namespace":"app-ns","Name":"policy-1"},{"Namespace":"app-ns","Name":"policy-2"},{"Namespace":"app-ns","Name":"policy-3"}]` {
541
+ t .Error ("GatewayWrapper.AddPolicy() expected to have added policy ref to the annotations" )
542
+ }
543
+ }
544
+
545
+ func TestGatewayDeletePolicy (t * testing.T ) {
546
+ gateway := gatewayapiv1alpha2.Gateway {
547
+ ObjectMeta : metav1.ObjectMeta {
548
+ Namespace : "gw-ns" ,
549
+ Name : "gw-1" ,
550
+ Annotations : map [string ]string {"kuadrant.io/ratelimitpolicies" : `[{"Namespace":"app-ns","Name":"policy-1"},{"Namespace":"app-ns","Name":"policy-2"}]` },
551
+ },
552
+ }
553
+ gw := GatewayWrapper {
554
+ Gateway : & gateway ,
555
+ PolicyRefsConfig : & KuadrantRateLimitPolicyRefsConfig {},
556
+ }
557
+ if ! gw .DeletePolicy (client.ObjectKey {Namespace : "app-ns" , Name : "policy-1" }) {
558
+ t .Error ("GatewayWrapper.DeletePolicy() expected to return true" )
559
+ }
560
+ if gw .DeletePolicy (client.ObjectKey {Namespace : "app-ns" , Name : "policy-3" }) {
561
+ t .Error ("GatewayWrapper.DeletePolicy() expected to return false" )
562
+ }
563
+ if gw .Annotations ["kuadrant.io/ratelimitpolicies" ] != `[{"Namespace":"app-ns","Name":"policy-2"}]` {
564
+ t .Error ("GatewayWrapper.DeletePolicy() expected to have deleted policy ref from the annotations" )
565
+ }
566
+ }
567
+
568
+ func TestGatewayHostnames (t * testing.T ) {
569
+ hostname := gatewayapiv1alpha2 .Hostname ("toystore.com" )
570
+ gateway := gatewayapiv1alpha2.Gateway {
571
+ ObjectMeta : metav1.ObjectMeta {
572
+ Namespace : "gw-ns" ,
573
+ Name : "gw-1" ,
574
+ Annotations : map [string ]string {"kuadrant.io/ratelimitpolicies" : `[{"Namespace":"app-ns","Name":"policy-1"},{"Namespace":"app-ns","Name":"policy-2"}]` },
575
+ },
576
+ Spec : gatewayapiv1alpha2.GatewaySpec {
577
+ Listeners : []gatewayapiv1alpha2.Listener {
578
+ {
579
+ Name : "my-listener" ,
580
+ Hostname : & hostname ,
581
+ },
582
+ },
583
+ },
584
+ }
585
+ gw := GatewayWrapper {
586
+ Gateway : & gateway ,
587
+ PolicyRefsConfig : & KuadrantRateLimitPolicyRefsConfig {},
588
+ }
589
+ hostnames := gw .Hostnames ()
590
+ if ! Contains (hostnames , "toystore.com" ) {
591
+ t .Error ("GatewayWrapper.Hostnames() expected to contain 'toystore.com'" )
592
+ }
593
+ if len (hostnames ) != 1 {
594
+ t .Fail ()
595
+ }
596
+ }
0 commit comments