Skip to content

Commit 6f2fbc4

Browse files
test : add coverage for project namespace tracker
Signed-off-by: Alexandre Lamarre <alexandre.lamarre@suse.com>
1 parent 933f14c commit 6f2fbc4

File tree

2 files changed

+203
-2
lines changed

2 files changed

+203
-2
lines changed

internal/helm-project-operator/controllers/namespace/integration.go

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ package namespace
33
import (
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
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package project
2+
3+
import (
4+
"context"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
"github.com/rancher/prometheus-federator/internal/helm-project-operator/apis/helm.cattle.io/v1alpha1"
9+
"github.com/rancher/prometheus-federator/internal/helm-project-operator/controllers/common"
10+
"github.com/rancher/prometheus-federator/internal/helm-project-operator/controllers/namespace"
11+
"github.com/rancher/prometheus-federator/internal/helm-project-operator/controllers/setup"
12+
"github.com/rancher/prometheus-federator/internal/test"
13+
// . "github.com/kralicky/kmatch"
14+
)
15+
16+
func ProjectControllerTest(
17+
operatorNamespace string,
18+
opts common.Options,
19+
valuesOverride v1alpha1.GenericMap,
20+
projectGetter namespace.ProjectGetter,
21+
) func() {
22+
return func() {
23+
var (
24+
t test.TestInterface
25+
stopController context.CancelFunc
26+
appCtx *setup.AppContext
27+
)
28+
BeforeAll(func() {
29+
t = test.GetTestInterface()
30+
31+
a, err := setup.NewAppContext(t.ClientConfig(), operatorNamespace, opts)
32+
Expect(err).To(Succeed())
33+
appCtx = a
34+
35+
})
36+
37+
When("the controller is not yet initialized", func() {
38+
It("should correctly index the rolebinding cache on initialization", func() {
39+
Register(
40+
t.Context(),
41+
operatorNamespace,
42+
opts,
43+
valuesOverride,
44+
appCtx.Apply,
45+
// watches
46+
appCtx.ProjectHelmChart(),
47+
appCtx.ProjectHelmChart().Cache(),
48+
appCtx.Core.ConfigMap(),
49+
appCtx.Core.ConfigMap().Cache(),
50+
appCtx.RBAC.Role(),
51+
appCtx.RBAC.Role().Cache(),
52+
appCtx.RBAC.ClusterRoleBinding(),
53+
appCtx.RBAC.ClusterRoleBinding().Cache(),
54+
// watches and generates
55+
appCtx.HelmController.HelmChart(),
56+
appCtx.HelmLocker.HelmRelease(),
57+
appCtx.Core.Namespace(),
58+
appCtx.Core.Namespace().Cache(),
59+
appCtx.RBAC.RoleBinding(),
60+
appCtx.RBAC.RoleBinding().Cache(),
61+
projectGetter,
62+
)
63+
64+
// TODO : https://github.com/rancher/prometheus-federator/pull/166
65+
By("verifying registed role bindings")
66+
67+
By("starting all controllers")
68+
ctxca, ca := context.WithCancel(t.Context())
69+
stopController = ca
70+
go appCtx.Start(ctxca)
71+
})
72+
})
73+
74+
When("the controller is running", func() {
75+
It("should do something", func() {
76+
})
77+
})
78+
79+
AfterAll(func() {
80+
if stopController != nil {
81+
stopController()
82+
}
83+
t.ObjectTracker().DeleteAll()
84+
})
85+
86+
}
87+
}

0 commit comments

Comments
 (0)