@@ -15,6 +15,7 @@ import (
1515 "k8s.io/utils/ptr"
1616 "sigs.k8s.io/controller-runtime/pkg/client"
1717 "sigs.k8s.io/controller-runtime/pkg/client/fake"
18+ "sigs.k8s.io/controller-runtime/pkg/client/interceptor"
1819 "sigs.k8s.io/controller-runtime/pkg/cluster"
1920 "sigs.k8s.io/controller-runtime/pkg/log"
2021 "sigs.k8s.io/controller-runtime/pkg/log/zap"
@@ -25,6 +26,7 @@ import (
2526
2627 networkingv1alpha "go.datum.net/network-services-operator/api/v1alpha"
2728 "go.datum.net/network-services-operator/internal/config"
29+ gatewayutil "go.datum.net/network-services-operator/internal/util/gateway"
2830)
2931
3032//nolint:gocyclo
@@ -302,7 +304,8 @@ func TestHTTPProxyReconcile(t *testing.T) {
302304 GatewayClassName : "test-gateway-class" ,
303305 },
304306 Gateway : config.GatewayConfig {
305- TargetDomain : "example.com" ,
307+ ControllerName : gatewayv1 .GatewayController ("test-gateway-class" ),
308+ TargetDomain : "example.com" ,
306309 ListenerTLSOptions : map [gatewayv1.AnnotationKey ]gatewayv1.AnnotationValue {
307310 gatewayv1 .AnnotationKey ("gateway.networking.datumapis.com/certificate-issuer" ): gatewayv1 .AnnotationValue ("test-issuer" ),
308311 },
@@ -312,13 +315,14 @@ func TestHTTPProxyReconcile(t *testing.T) {
312315 type testContext struct {
313316 * testing.T
314317 reconciler * HTTPProxyReconciler
318+ gateway * gatewayv1.Gateway
315319 }
316320
317321 tests := []struct {
318322 name string
319323 httpProxy * networkingv1alpha.HTTPProxy
320324 existingObjects []client.Object
321- postCreateGatewayStatus * gatewayv1.Gateway
325+ postCreateGatewayStatus func ( * gatewayv1.Gateway )
322326 expectedError bool
323327 expectedConditions []metav1.Condition
324328 assert func (t * testContext , cl client.Client , httpProxy * networkingv1alpha.HTTPProxy )
@@ -471,12 +475,12 @@ func TestHTTPProxyReconcile(t *testing.T) {
471475 {
472476 name : "address and hostname propagation" ,
473477 httpProxy : newHTTPProxy (),
474- postCreateGatewayStatus : & gatewayv1.Gateway {
475- Status : gatewayv1.GatewayStatus {
478+ postCreateGatewayStatus : func ( g * gatewayv1.Gateway ) {
479+ g . Status = gatewayv1.GatewayStatus {
476480 Addresses : []gatewayv1.GatewayStatusAddress {
477481 {
478482 Type : ptr .To (gatewayv1 .HostnameAddressType ),
479- Value : "test.example.com" ,
483+ Value : testConfig . Gateway . GatewayDNSAddress ( g ) ,
480484 },
481485 },
482486 Conditions : []metav1.Condition {
@@ -491,7 +495,29 @@ func TestHTTPProxyReconcile(t *testing.T) {
491495 Reason : string (gatewayv1 .GatewayReasonProgrammed ),
492496 },
493497 },
494- },
498+ Listeners : []gatewayv1.ListenerStatus {
499+ {
500+ Name : gatewayutil .DefaultHTTPListenerName ,
501+ Conditions : []metav1.Condition {
502+ {
503+ Type : string (gatewayv1 .GatewayConditionAccepted ),
504+ Status : metav1 .ConditionTrue ,
505+ Reason : string (gatewayv1 .GatewayReasonAccepted ),
506+ },
507+ },
508+ },
509+ {
510+ Name : gatewayutil .DefaultHTTPSListenerName ,
511+ Conditions : []metav1.Condition {
512+ {
513+ Type : string (gatewayv1 .GatewayConditionAccepted ),
514+ Status : metav1 .ConditionTrue ,
515+ Reason : string (gatewayv1 .GatewayReasonAccepted ),
516+ },
517+ },
518+ },
519+ },
520+ }
495521 },
496522 expectedError : false ,
497523 expectedConditions : []metav1.Condition {
@@ -508,11 +534,11 @@ func TestHTTPProxyReconcile(t *testing.T) {
508534 },
509535 assert : func (t * testContext , cl client.Client , httpProxy * networkingv1alpha.HTTPProxy ) {
510536 if assert .Len (t , httpProxy .Status .Addresses , 1 ) {
511- assert .Equal (t , "test.example.com" , httpProxy .Status .Addresses [0 ].Value )
537+ assert .Equal (t , t . gateway . Status . Addresses [ 0 ]. Value , httpProxy .Status .Addresses [0 ].Value )
512538 }
513539
514540 if assert .Len (t , httpProxy .Status .Hostnames , 1 ) {
515- assert .Equal (t , gatewayv1 . Hostname ( "test.example.com " ), httpProxy .Status .Hostnames [0 ])
541+ assert .Equal (t , ptr . Deref ( t . gateway . Spec . Listeners [ 0 ]. Hostname , " " ), httpProxy .Status .Hostnames [0 ])
516542 }
517543 },
518544 },
@@ -665,6 +691,15 @@ func TestHTTPProxyReconcile(t *testing.T) {
665691 t .Run (tt .name , func (t * testing.T ) {
666692 var initialObjects []client.Object
667693
694+ tt .existingObjects = append (tt .existingObjects , & gatewayv1.GatewayClass {
695+ ObjectMeta : metav1.ObjectMeta {
696+ Name : "test-gateway-class" ,
697+ },
698+ Spec : gatewayv1.GatewayClassSpec {
699+ ControllerName : testConfig .Gateway .ControllerName ,
700+ },
701+ })
702+
668703 for _ , obj := range tt .existingObjects {
669704 obj .SetCreationTimestamp (metav1 .Now ())
670705 }
@@ -676,20 +711,35 @@ func TestHTTPProxyReconcile(t *testing.T) {
676711 WithScheme (testScheme ).
677712 WithObjects (initialObjects ... ).
678713 WithStatusSubresource (initialObjects ... ).
679- WithStatusSubresource (& gatewayv1.Gateway {})
680-
681- if tt .postCreateGatewayStatus != nil {
682- fakeClientBuilder .WithStatusSubresource (tt .postCreateGatewayStatus )
683- }
714+ WithStatusSubresource (& gatewayv1.Gateway {}).
715+ WithInterceptorFuncs (interceptor.Funcs {
716+ Create : func (ctx context.Context , client client.WithWatch , obj client.Object , opts ... client.CreateOption ) error {
717+ obj .SetUID (uuid .NewUUID ())
718+ obj .SetCreationTimestamp (metav1 .Now ())
719+ return client .Create (ctx , obj , opts ... )
720+ },
721+ })
684722
685723 fakeClient := fakeClientBuilder .Build ()
724+
725+ fakeDownstreamClient := fake .NewClientBuilder ().
726+ WithScheme (testScheme ).
727+ WithStatusSubresource (& gatewayv1.Gateway {}).
728+ Build ()
729+
686730 mgr := & fakeMockManager {cl : fakeClient }
687731
688732 reconciler := & HTTPProxyReconciler {
689733 mgr : mgr ,
690734 Config : testConfig ,
691735 }
692736
737+ gatewayReconciler := & GatewayReconciler {
738+ mgr : mgr ,
739+ Config : testConfig ,
740+ DownstreamCluster : & fakeCluster {cl : fakeDownstreamClient },
741+ }
742+
693743 req := mcreconcile.Request {
694744 Request : reconcile.Request {
695745 NamespacedName : client .ObjectKeyFromObject (tt .httpProxy ),
@@ -708,14 +758,16 @@ func TestHTTPProxyReconcile(t *testing.T) {
708758 assert .NoError (t , err )
709759 }
710760
761+ _ , err = gatewayReconciler .Reconcile (ctx , req )
762+ assert .NoError (t , err , "unexpected error reconciling gateway" )
763+
711764 var updatedProxy networkingv1alpha.HTTPProxy
712765 err = fakeClient .Get (ctx , client .ObjectKeyFromObject (tt .httpProxy ), & updatedProxy )
713766 assert .NoError (t , err )
714767
715768 var gateway gatewayv1.Gateway
716769 err = fakeClient .Get (ctx , client .ObjectKeyFromObject (tt .httpProxy ), & gateway )
717770 if assert .NoError (t , err ) {
718-
719771 apimeta .SetStatusCondition (& gateway .Status .Conditions , metav1.Condition {
720772 Type : string (gatewayv1 .GatewayConditionAccepted ),
721773 Status : metav1 .ConditionTrue ,
@@ -727,10 +779,9 @@ func TestHTTPProxyReconcile(t *testing.T) {
727779 if assert .NoError (t , fakeClient .Status ().Update (ctx , & gateway ), "unexpected error while updating gateway status" ) {
728780 if tt .postCreateGatewayStatus != nil {
729781
730- gateway . Status = tt .postCreateGatewayStatus . Status
782+ tt .postCreateGatewayStatus ( & gateway )
731783 err = fakeClient .Status ().Update (ctx , & gateway )
732784 assert .NoError (t , err )
733-
734785 }
735786
736787 _ , err = reconciler .Reconcile (ctx , req )
@@ -751,7 +802,12 @@ func TestHTTPProxyReconcile(t *testing.T) {
751802 }
752803
753804 if tt .assert != nil {
754- tt .assert (& testContext {T : t , reconciler : reconciler }, fakeClient , & updatedProxy )
805+ testCtx := & testContext {
806+ T : t ,
807+ reconciler : reconciler ,
808+ gateway : & gateway ,
809+ }
810+ tt .assert (testCtx , fakeClient , & updatedProxy )
755811 }
756812
757813 })
0 commit comments