@@ -3,14 +3,17 @@ package namespace
33import (
44 "context"
55 "fmt"
6+ "time"
67
78 . "github.com/kralicky/kmatch"
89 . "github.com/onsi/ginkgo/v2"
910 . "github.com/onsi/gomega"
11+ v1alpha1 "github.com/rancher/prometheus-federator/internal/helm-project-operator/apis/helm.cattle.io/v1alpha1"
1012 "github.com/rancher/prometheus-federator/internal/helm-project-operator/controllers/common"
1113 "github.com/rancher/prometheus-federator/internal/helm-project-operator/controllers/setup"
1214 "github.com/rancher/prometheus-federator/internal/helmcommon/pkg/crds"
1315 "github.com/rancher/prometheus-federator/internal/test"
16+ "github.com/samber/lo"
1417 corev1 "k8s.io/api/core/v1"
1518 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1619)
@@ -168,6 +171,117 @@ func MultiNamespaceTest(
168171 Eventually (projectGetter .IsProjectRegistrationNamespace (dummyRegistrationNamespace )).Should (BeTrue ())
169172 })
170173
174+ Specify ("when we add namespaces to a project" , func () {
175+ By ("manually adding namespaces to a project" )
176+
177+ nss := []* corev1.Namespace {
178+ {
179+ ObjectMeta : metav1.ObjectMeta {
180+ Name : fmt .Sprintf ("project-%s-ns-1" , targetProjectId ),
181+ Labels : map [string ]string {
182+ opts .ProjectLabel : targetProjectId ,
183+ },
184+ },
185+ },
186+ {
187+ ObjectMeta : metav1.ObjectMeta {
188+ Name : fmt .Sprintf ("project-%s-ns-2" , targetProjectId ),
189+ Labels : map [string ]string {
190+ opts .ProjectLabel : targetProjectId ,
191+ },
192+ },
193+ },
194+ }
195+ for _ , ns := range nss {
196+ t .ObjectTracker ().Add (ns )
197+ Expect (t .K8sClient ().Create (t .Context (), ns )).To (Succeed ())
198+ }
199+
200+ By ("verifying the project getter tracks them correctly" )
201+ Eventually (func () []string {
202+ tracker , err := projectGetter .GetTargetProjectNamespaces (& v1alpha1.ProjectHelmChart {
203+ ObjectMeta : metav1.ObjectMeta {
204+ Namespace : fmt .Sprintf (common .ProjectRegistrationNamespaceFmt , targetProjectId ),
205+ },
206+ })
207+ if err != nil {
208+ return []string {err .Error ()}
209+ }
210+ return tracker
211+ }).Should (ConsistOf (
212+ lo .Map (nss , func (ns * corev1.Namespace , _ int ) string {
213+ return ns .Name
214+ }),
215+ ))
216+
217+ Consistently (func () []string {
218+ tracker , err := projectGetter .GetTargetProjectNamespaces (& v1alpha1.ProjectHelmChart {
219+ ObjectMeta : metav1.ObjectMeta {
220+ Namespace : fmt .Sprintf (common .ProjectRegistrationNamespaceFmt , targetProjectId ),
221+ },
222+ })
223+ if err != nil {
224+ return []string {err .Error ()}
225+ }
226+ return tracker
227+ }, 1 * time .Second , 200 * time .Millisecond ).Should (ConsistOf (
228+ lo .Map (nss , func (ns * corev1.Namespace , _ int ) string {
229+ return ns .Name
230+ }),
231+ ))
232+ })
233+
234+ Specify ("when we delete the namespaces associated to a project" , func () {
235+
236+ By ("manually deleting namespaces associated to a project" )
237+ nss := []* corev1.Namespace {
238+ {
239+ ObjectMeta : metav1.ObjectMeta {
240+ Name : fmt .Sprintf ("project-%s-ns-1" , targetProjectId ),
241+ Labels : map [string ]string {
242+ opts .ProjectLabel : targetProjectId ,
243+ },
244+ },
245+ },
246+ {
247+ ObjectMeta : metav1.ObjectMeta {
248+ Name : fmt .Sprintf ("project-%s-ns-2" , targetProjectId ),
249+ Labels : map [string ]string {
250+ opts .ProjectLabel : targetProjectId ,
251+ },
252+ },
253+ },
254+ }
255+ for _ , ns := range nss {
256+ Expect (t .K8sClient ().Delete (t .Context (), ns )).To (Succeed ())
257+ }
258+
259+ By ("verifying the project getter stops tracking them" )
260+ Eventually (func () []string {
261+ tracker , err := projectGetter .GetTargetProjectNamespaces (& v1alpha1.ProjectHelmChart {
262+ ObjectMeta : metav1.ObjectMeta {
263+ Namespace : fmt .Sprintf (common .ProjectRegistrationNamespaceFmt , targetProjectId ),
264+ },
265+ })
266+ if err != nil {
267+ return []string {err .Error ()}
268+ }
269+ return tracker
270+ }).Should (ConsistOf ([]string {}))
271+
272+ Consistently (func () []string {
273+ tracker , err := projectGetter .GetTargetProjectNamespaces (& v1alpha1.ProjectHelmChart {
274+ ObjectMeta : metav1.ObjectMeta {
275+ Namespace : fmt .Sprintf (common .ProjectRegistrationNamespaceFmt , targetProjectId ),
276+ },
277+ })
278+ if err != nil {
279+ return []string {err .Error ()}
280+ }
281+ return tracker
282+ }, 1 * time .Second , 200 * time .Millisecond ).Should (ConsistOf ([]string {}))
283+ })
284+
171285 Specify ("when we delete the project registration namespace, it should cleanup related resources" , func () {
172286 dummyRegistrationNamespace := & corev1.Namespace {
173287 ObjectMeta : metav1.ObjectMeta {
@@ -181,11 +295,11 @@ func MultiNamespaceTest(
181295 By ("verifying the registration namespace is deleted" )
182296
183297 Eventually (Object (dummyRegistrationNamespace )).ShouldNot (Exist ())
184- Consistently (Object (dummyRegistrationNamespace )).ShouldNot (Exist ())
298+ Consistently (Object (dummyRegistrationNamespace ), 1 * time . Millisecond * 50 ).ShouldNot (Exist ())
185299
186300 By ("verifying the tracker eventually stops tracking the namespace" )
187301 Eventually (projectGetter .IsProjectRegistrationNamespace (dummyRegistrationNamespace )).Should (BeFalse ())
188- Consistently (projectGetter .IsProjectRegistrationNamespace (dummyRegistrationNamespace )).Should (BeFalse ())
302+ Consistently (projectGetter .IsProjectRegistrationNamespace (dummyRegistrationNamespace ), 1 * time . Second , 10 * time . Millisecond ).Should (BeFalse ())
189303 })
190304 })
191305 }
0 commit comments