github.com/redhat-appstudio/release-service@v0.0.0-20240507143925-083712697924/controllers/utils/handlers/enqueue_matched_test.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package handlers 18 19 import ( 20 "context" 21 22 . "github.com/onsi/ginkgo/v2" 23 . "github.com/onsi/gomega" 24 "github.com/redhat-appstudio/release-service/api/v1alpha1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/types" 27 "sigs.k8s.io/controller-runtime/pkg/controller/controllertest" 28 "sigs.k8s.io/controller-runtime/pkg/event" 29 "sigs.k8s.io/controller-runtime/pkg/reconcile" 30 31 "k8s.io/client-go/util/workqueue" 32 ) 33 34 var _ = Describe("EnqueueRequestForMatchedResource", func() { 35 var ctx = context.TODO() 36 37 var rateLimitingInterface workqueue.RateLimitingInterface 38 var instance EnqueueRequestForMatchedResource 39 var releasePlan *v1alpha1.ReleasePlan 40 var releasePlanAdmission *v1alpha1.ReleasePlanAdmission 41 42 BeforeEach(func() { 43 rateLimitingInterface = &controllertest.Queue{Interface: workqueue.New()} 44 releasePlan = &v1alpha1.ReleasePlan{ 45 ObjectMeta: metav1.ObjectMeta{ 46 Namespace: "default", 47 Name: "rp", 48 }, 49 Spec: v1alpha1.ReleasePlanSpec{ 50 Application: "app", 51 Target: "default", 52 }, 53 Status: v1alpha1.ReleasePlanStatus{ 54 ReleasePlanAdmission: v1alpha1.MatchedReleasePlanAdmission{ 55 Name: "default/rpa", 56 }, 57 }, 58 } 59 releasePlanAdmission = &v1alpha1.ReleasePlanAdmission{ 60 ObjectMeta: metav1.ObjectMeta{ 61 Namespace: "default", 62 Name: "rpa", 63 }, 64 Spec: v1alpha1.ReleasePlanAdmissionSpec{ 65 Applications: []string{"app"}, 66 Origin: "default", 67 }, 68 Status: v1alpha1.ReleasePlanAdmissionStatus{ 69 ReleasePlans: []v1alpha1.MatchedReleasePlan{ 70 {Name: "default/rp"}, 71 }, 72 }, 73 } 74 instance = EnqueueRequestForMatchedResource{} 75 }) 76 77 When("A CreateEvent occurs", func() { 78 It("should not enqueue a request for a ReleasePlan", func() { 79 createEvent := event.CreateEvent{ 80 Object: releasePlan, 81 } 82 83 instance.Create(ctx, createEvent, rateLimitingInterface) 84 Expect(rateLimitingInterface.Len()).To(Equal(0)) 85 }) 86 87 It("should not enqueue a request for a ReleasePlanAdmission", func() { 88 createEvent := event.CreateEvent{ 89 Object: releasePlanAdmission, 90 } 91 92 instance.Create(ctx, createEvent, rateLimitingInterface) 93 Expect(rateLimitingInterface.Len()).To(Equal(0)) 94 }) 95 }) 96 97 When("A UpdateEvent occurs", func() { 98 It("should enqueue a request for both the objectOld and objectNew with ReleasePlans", func() { 99 newReleasePlan := releasePlan.DeepCopy() 100 newReleasePlan.Status.ReleasePlanAdmission.Name = "default/new-rpa" 101 102 updateEvent := event.UpdateEvent{ 103 ObjectOld: releasePlan, 104 ObjectNew: newReleasePlan, 105 } 106 107 instance.Update(ctx, updateEvent, rateLimitingInterface) 108 Expect(rateLimitingInterface.Len()).To(Equal(2)) 109 }) 110 111 It("should enqueue a request for both the objectOld and objectNew with ReleasePlanAdmissions", func() { 112 newReleasePlanAdmission := releasePlanAdmission.DeepCopy() 113 newReleasePlanAdmission.Status.ReleasePlans = []v1alpha1.MatchedReleasePlan{ 114 {Name: "default/new-rp"}, 115 } 116 117 updateEvent := event.UpdateEvent{ 118 ObjectOld: releasePlanAdmission, 119 ObjectNew: newReleasePlanAdmission, 120 } 121 122 instance.Update(ctx, updateEvent, rateLimitingInterface) 123 Expect(rateLimitingInterface.Len()).To(Equal(2)) 124 }) 125 }) 126 127 When("A DeleteEvent occurs", func() { 128 It("should enqueue a request for a ReleasePlan", func() { 129 deleteEvent := event.DeleteEvent{ 130 Object: releasePlan, 131 } 132 133 instance.Delete(ctx, deleteEvent, rateLimitingInterface) 134 Expect(rateLimitingInterface.Len()).To(Equal(1)) 135 136 i, _ := rateLimitingInterface.Get() 137 Expect(i).To(Equal(reconcile.Request{ 138 NamespacedName: types.NamespacedName{ 139 Namespace: "default", 140 Name: "rpa", 141 }, 142 })) 143 }) 144 145 It("should enqueue a request for a ReleasePlanAdmission", func() { 146 deleteEvent := event.DeleteEvent{ 147 Object: releasePlanAdmission, 148 } 149 150 instance.Delete(ctx, deleteEvent, rateLimitingInterface) 151 Expect(rateLimitingInterface.Len()).To(Equal(1)) 152 153 i, _ := rateLimitingInterface.Get() 154 Expect(i).To(Equal(reconcile.Request{ 155 NamespacedName: types.NamespacedName{ 156 Namespace: "default", 157 Name: "rp", 158 }, 159 })) 160 }) 161 }) 162 163 When("A GenericEvent occurs", func() { 164 It("should enqueue a request for a ReleasePlan", func() { 165 genericEvent := event.GenericEvent{ 166 Object: releasePlan, 167 } 168 169 instance.Generic(ctx, genericEvent, rateLimitingInterface) 170 Expect(rateLimitingInterface.Len()).To(Equal(1)) 171 172 i, _ := rateLimitingInterface.Get() 173 Expect(i).To(Equal(reconcile.Request{ 174 NamespacedName: types.NamespacedName{ 175 Namespace: "default", 176 Name: "rpa", 177 }, 178 })) 179 }) 180 181 It("should enqueue a request for a ReleasePlanAdmission", func() { 182 genericEvent := event.GenericEvent{ 183 Object: releasePlanAdmission, 184 } 185 186 instance.Generic(ctx, genericEvent, rateLimitingInterface) 187 Expect(rateLimitingInterface.Len()).To(Equal(1)) 188 189 i, _ := rateLimitingInterface.Get() 190 Expect(i).To(Equal(reconcile.Request{ 191 NamespacedName: types.NamespacedName{ 192 Namespace: "default", 193 Name: "rp", 194 }, 195 })) 196 }) 197 }) 198 199 When("enqueueRequest is called", func() { 200 It("should enqueue a request for a proper namespaced name", func() { 201 enqueueRequest("foo/bar", rateLimitingInterface) 202 Expect(rateLimitingInterface.Len()).To(Equal(1)) 203 204 i, _ := rateLimitingInterface.Get() 205 Expect(i).To(Equal(reconcile.Request{ 206 NamespacedName: types.NamespacedName{ 207 Namespace: "foo", 208 Name: "bar", 209 }, 210 })) 211 }) 212 213 It("should not enqueue a request for an invalid namespaced name", func() { 214 enqueueRequest("bar", rateLimitingInterface) 215 Expect(rateLimitingInterface.Len()).To(Equal(0)) 216 }) 217 }) 218 })