github.com/verrazzano/verrazzano@v1.7.1/application-operator/controllers/ingresstrait/ingress_watch_test.go (about) 1 // Copyright (c) 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 package ingresstrait 4 5 import ( 6 "context" 7 "testing" 8 9 "github.com/verrazzano/verrazzano/application-operator/constants" 10 k8net "k8s.io/api/networking/v1" 11 "sigs.k8s.io/controller-runtime/pkg/event" 12 13 "github.com/stretchr/testify/assert" 14 vzapi "github.com/verrazzano/verrazzano/application-operator/apis/app/v1alpha1" 15 vzoam "github.com/verrazzano/verrazzano/application-operator/apis/oam/v1alpha1" 16 corev1 "k8s.io/api/core/v1" 17 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 18 "k8s.io/apimachinery/pkg/runtime" 19 "k8s.io/apimachinery/pkg/types" 20 "sigs.k8s.io/controller-runtime/pkg/client/fake" 21 "sigs.k8s.io/controller-runtime/pkg/reconcile" 22 ) 23 24 // Test_isConsoleIngressUpdated tests the isConsoleIngressUpdated func for the following use case. 25 // GIVEN a request to isConsoleIngressUpdated 26 // WHEN only the Verrazzano Console ingress has changed 27 // THEN true is returned only when the Verrazzano Console ingress has changed, false otherwise 28 func Test_isConsoleIngressUpdated(t *testing.T) { 29 30 asserts := assert.New(t) 31 32 scheme := runtime.NewScheme() 33 _ = corev1.AddToScheme(scheme) 34 _ = vzapi.AddToScheme(scheme) 35 _ = vzoam.AddToScheme(scheme) 36 client := fake.NewClientBuilder().WithScheme(scheme).Build() 37 38 r := newIngressTraitReconciler(client) 39 40 oldIngress := &k8net.Ingress{ 41 ObjectMeta: metav1.ObjectMeta{Name: constants.VzConsoleIngress, Namespace: constants.VerrazzanoSystemNamespace}, 42 Spec: k8net.IngressSpec{ 43 Rules: []k8net.IngressRule{ 44 {Host: "host1"}, 45 {Host: "host2"}, 46 }, 47 TLS: []k8net.IngressTLS{ 48 {Hosts: []string{"host1", "host2"}}, 49 }, 50 }, 51 } 52 newIngress := oldIngress.DeepCopyObject().(*k8net.Ingress) 53 54 asserts.False(r.isConsoleIngressUpdated(event.UpdateEvent{ 55 ObjectOld: oldIngress, 56 ObjectNew: newIngress, 57 })) 58 59 newIngress.Spec.Rules = []k8net.IngressRule{ 60 {Host: "host3"}, 61 } 62 newIngress.Spec.TLS = []k8net.IngressTLS{ 63 {Hosts: []string{"host3"}}, 64 } 65 asserts.True(r.isConsoleIngressUpdated(event.UpdateEvent{ 66 ObjectOld: oldIngress, 67 ObjectNew: newIngress, 68 })) 69 70 oldOtherIngress := &k8net.Ingress{ 71 ObjectMeta: metav1.ObjectMeta{Name: "someingress", Namespace: constants.VerrazzanoSystemNamespace}, 72 Spec: k8net.IngressSpec{ 73 Rules: []k8net.IngressRule{ 74 {Host: "host1"}, 75 {Host: "host2"}, 76 }, 77 TLS: []k8net.IngressTLS{ 78 {Hosts: []string{"host1", "host2"}}, 79 }, 80 }, 81 } 82 newOtherIngress := oldIngress.DeepCopyObject().(*k8net.Ingress) 83 asserts.False(r.isConsoleIngressUpdated(event.UpdateEvent{ 84 ObjectOld: oldOtherIngress, 85 ObjectNew: newOtherIngress, 86 })) 87 } 88 89 // Test_isIstioIngressGatewayUpdated tests the isIstioIngressGatewayUpdated func for the following use case. 90 // GIVEN a request to isIstioIngressGatewayUpdated 91 // WHEN only the IstioIngressGateway has changed 92 // THEN true is returned only when the IstioIngressGateway has changed, false otherwise 93 func Test_isIstioIngressGatewayUpdated(t *testing.T) { 94 95 asserts := assert.New(t) 96 97 scheme := runtime.NewScheme() 98 _ = corev1.AddToScheme(scheme) 99 _ = vzapi.AddToScheme(scheme) 100 _ = vzoam.AddToScheme(scheme) 101 client := fake.NewClientBuilder().WithScheme(scheme).Build() 102 103 r := newIngressTraitReconciler(client) 104 105 oldSvc := &corev1.Service{ 106 ObjectMeta: metav1.ObjectMeta{Name: istioIngressGateway, Namespace: constants.IstioSystemNamespace}, 107 Spec: corev1.ServiceSpec{ 108 Type: "LoadBalancer", 109 }, 110 } 111 newSvc := oldSvc.DeepCopyObject().(*corev1.Service) 112 113 asserts.False(r.isIstioIngressGatewayUpdated(event.UpdateEvent{ 114 ObjectOld: oldSvc, 115 ObjectNew: newSvc, 116 })) 117 118 newSvc.Spec.Type = "NodePort" 119 asserts.True(r.isIstioIngressGatewayUpdated(event.UpdateEvent{ 120 ObjectOld: oldSvc, 121 ObjectNew: newSvc, 122 })) 123 124 oldOtherIngress := &corev1.Service{ 125 ObjectMeta: metav1.ObjectMeta{Name: "somesvc", Namespace: constants.VerrazzanoSystemNamespace}, 126 Spec: corev1.ServiceSpec{ 127 Type: "LoadBalancer", 128 }, 129 } 130 newOtherIngress := oldSvc.DeepCopyObject().(*corev1.Service) 131 asserts.False(r.isIstioIngressGatewayUpdated(event.UpdateEvent{ 132 ObjectOld: oldOtherIngress, 133 ObjectNew: newOtherIngress, 134 })) 135 } 136 137 // Test_createIngressTraitReconcileRequests tests the createIngressTraitReconcileRequests func for the following use case. 138 // GIVEN a request to createIngressTraitReconcileRequests 139 // THEN the correct set of reconcile requests is returned based on the number if IngressTraits across all namespaces 140 func Test_createIngressTraitReconcileRequests(t *testing.T) { 141 142 asserts := assert.New(t) 143 144 scheme := runtime.NewScheme() 145 _ = corev1.AddToScheme(scheme) 146 _ = vzapi.AddToScheme(scheme) 147 _ = vzoam.AddToScheme(scheme) 148 client := fake.NewClientBuilder().WithScheme(scheme).Build() 149 150 reconciler := newIngressTraitReconciler(client) 151 152 asserts.Len(reconciler.createIngressTraitReconcileRequests(), 0) 153 154 _ = client.Create(context.TODO(), &vzoam.IngressTrait{ObjectMeta: metav1.ObjectMeta{Name: "trait1", Namespace: "traitns1"}}) 155 _ = client.Create(context.TODO(), &vzoam.IngressTrait{ObjectMeta: metav1.ObjectMeta{Name: "trait2", Namespace: "traitns1"}}) 156 _ = client.Create(context.TODO(), &vzoam.IngressTrait{ObjectMeta: metav1.ObjectMeta{Name: "trait1", Namespace: "traitns2"}}) 157 _ = client.Create(context.TODO(), &vzoam.IngressTrait{ObjectMeta: metav1.ObjectMeta{Name: "trait1", Namespace: "traitns3"}}) 158 _ = client.Create(context.TODO(), &vzoam.IngressTrait{ObjectMeta: metav1.ObjectMeta{Name: "trait2", Namespace: "traitns3"}}) 159 160 expectedRequests := []reconcile.Request{ 161 {NamespacedName: types.NamespacedName{Name: "trait1", Namespace: "traitns1"}}, 162 {NamespacedName: types.NamespacedName{Name: "trait2", Namespace: "traitns1"}}, 163 {NamespacedName: types.NamespacedName{Name: "trait1", Namespace: "traitns2"}}, 164 {NamespacedName: types.NamespacedName{Name: "trait1", Namespace: "traitns3"}}, 165 {NamespacedName: types.NamespacedName{Name: "trait2", Namespace: "traitns3"}}, 166 } 167 actualRequests := reconciler.createIngressTraitReconcileRequests() 168 asserts.Len(actualRequests, 5) 169 asserts.Equal(expectedRequests, actualRequests) 170 }