Skip to content

Commit df262fe

Browse files
committed
Add E2E test to verify roles/bindings are deleted
Signed-off-by: Jonathan West <[email protected]>
1 parent acd04d8 commit df262fe

File tree

2 files changed

+100
-4
lines changed

2 files changed

+100
-4
lines changed

controllers/resources_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ var _ = Describe("Resource creation and cleanup tests", func() {
11411141
Expect(r.Client.Get(ctx, client.ObjectKeyFromObject(clusterRole), clusterRole)).To(Succeed())
11421142

11431143
By("Verify existing Role is deleted")
1144-
Expect(r.Client.Get(ctx, client.ObjectKeyFromObject(role), role)).To(HaveOccurred())
1144+
Expect(r.Client.Get(ctx, client.ObjectKeyFromObject(role), role)).ToNot(Succeed())
11451145
})
11461146

11471147
It("Should delete existing ClusterRole when Role is reconciled", func() {
@@ -1161,7 +1161,7 @@ var _ = Describe("Resource creation and cleanup tests", func() {
11611161
Expect(r.Client.Get(ctx, client.ObjectKeyFromObject(role), role)).To(Succeed())
11621162

11631163
By("Verify existing ClusterRole is deleted")
1164-
Expect(r.Client.Get(ctx, client.ObjectKeyFromObject(clusterRole), clusterRole)).To(HaveOccurred())
1164+
Expect(r.Client.Get(ctx, client.ObjectKeyFromObject(clusterRole), clusterRole)).ToNot(Succeed())
11651165
})
11661166

11671167
It("Should delete existing RoleBinding when ClusterRoleBinding is reconciled", func() {
@@ -1187,7 +1187,7 @@ var _ = Describe("Resource creation and cleanup tests", func() {
11871187
Expect(r.Client.Get(ctx, client.ObjectKeyFromObject(clusterRoleBinding), clusterRoleBinding)).To(Succeed())
11881188

11891189
By("Verify RoleBinding is deleted")
1190-
Expect(r.Client.Get(ctx, client.ObjectKeyFromObject(roleBinding), roleBinding)).To(HaveOccurred())
1190+
Expect(r.Client.Get(ctx, client.ObjectKeyFromObject(roleBinding), roleBinding)).ToNot(Succeed())
11911191
})
11921192

11931193
It("Should delete existing ClusterRoleBinding when RoleBinding is reconciled", func() {
@@ -1213,7 +1213,7 @@ var _ = Describe("Resource creation and cleanup tests", func() {
12131213
Expect(r.Client.Get(ctx, client.ObjectKeyFromObject(roleBinding), roleBinding)).To(Succeed())
12141214

12151215
By("Verify ClusterRoleBinding is deleted")
1216-
Expect(r.Client.Get(ctx, client.ObjectKeyFromObject(clusterRole), clusterRole)).To(HaveOccurred())
1216+
Expect(r.Client.Get(ctx, client.ObjectKeyFromObject(clusterRole), clusterRole)).ToNot(Succeed())
12171217
})
12181218
})
12191219
})

tests/e2e/rollout_tests_all.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,5 +595,101 @@ func RunRolloutsTests(namespaceScopedParam bool) {
595595

596596
})
597597
})
598+
599+
When("a namespace-scoped RolloutManager is installed into a namespace that previously contained a cluster-scoped RolloutManager, or vice versa", func() {
600+
601+
It("should cleanup any cluster/role/rolebinding resources that are present in the namespace, that do not match the current .spec.namespaceScoped value of the RolloutManager CR", func() {
602+
603+
var fakeRole rbacv1.Role
604+
var fakeRoleBinding rbacv1.RoleBinding
605+
606+
var fakeClusterRole rbacv1.ClusterRole
607+
var fakeClusterRoleBinding rbacv1.ClusterRoleBinding
608+
609+
By("creating ClusterRole/Binding in the namespace-scoped case, and Role/Binding in the cluster-scoped case")
610+
611+
if namespaceScopedParam {
612+
613+
fakeClusterRole = rbacv1.ClusterRole{
614+
ObjectMeta: metav1.ObjectMeta{
615+
Name: controllers.DefaultArgoRolloutsResourceName,
616+
Namespace: rolloutManager.Namespace,
617+
},
618+
}
619+
Expect(k8sClient.Create(ctx, &fakeClusterRole)).To(Succeed())
620+
621+
fakeClusterRoleBinding = rbacv1.ClusterRoleBinding{
622+
ObjectMeta: metav1.ObjectMeta{
623+
Name: controllers.DefaultArgoRolloutsResourceName,
624+
Namespace: rolloutManager.Namespace,
625+
},
626+
RoleRef: rbacv1.RoleRef{
627+
APIGroup: rbacv1.GroupName,
628+
Kind: "ClusterRole",
629+
Name: fakeClusterRole.Name,
630+
},
631+
Subjects: []rbacv1.Subject{
632+
{
633+
Kind: rbacv1.ServiceAccountKind,
634+
Name: controllers.DefaultArgoRolloutsResourceName,
635+
Namespace: rolloutManager.Namespace,
636+
},
637+
},
638+
}
639+
Expect(k8sClient.Create(ctx, &fakeClusterRoleBinding)).To(Succeed())
640+
641+
} else {
642+
643+
fakeRole = rbacv1.Role{
644+
ObjectMeta: metav1.ObjectMeta{
645+
Name: controllers.DefaultArgoRolloutsResourceName,
646+
Namespace: rolloutManager.Namespace,
647+
},
648+
}
649+
Expect(k8sClient.Create(ctx, &fakeRole)).To(Succeed())
650+
651+
fakeRoleBinding = rbacv1.RoleBinding{
652+
ObjectMeta: metav1.ObjectMeta{
653+
Name: controllers.DefaultArgoRolloutsResourceName,
654+
Namespace: rolloutManager.Namespace,
655+
},
656+
RoleRef: rbacv1.RoleRef{
657+
APIGroup: rbacv1.GroupName,
658+
Kind: "Role",
659+
Name: fakeRole.Name,
660+
},
661+
Subjects: []rbacv1.Subject{
662+
{
663+
Kind: rbacv1.ServiceAccountKind,
664+
Name: controllers.DefaultArgoRolloutsResourceName,
665+
Namespace: rolloutManager.Namespace,
666+
},
667+
},
668+
}
669+
Expect(k8sClient.Create(ctx, &fakeRoleBinding)).To(Succeed())
670+
671+
}
672+
673+
By("creating RolloutManager and waiting for it to be available")
674+
Expect(k8sClient.Create(ctx, &rolloutManager)).To(Succeed())
675+
Eventually(rolloutManager, "1m", "1s").Should(rolloutManagerFixture.HavePhase(rolloutsmanagerv1alpha1.PhaseAvailable))
676+
677+
if namespaceScopedParam {
678+
679+
By("verifying that in the namespace-scoped case, the cluster-scoped resources are deleted after reconciliation")
680+
Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&fakeClusterRole), &fakeClusterRole)).ToNot(Succeed())
681+
Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&fakeClusterRoleBinding), &fakeClusterRoleBinding)).ToNot(Succeed())
682+
683+
} else {
684+
685+
By("verifying that in the cluster-scoped case, the namespace-scoped resources are deleted after reconciliation")
686+
Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&fakeRole), &fakeRole)).ToNot(Succeed())
687+
Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&fakeRoleBinding), &fakeRoleBinding)).ToNot(Succeed())
688+
689+
}
690+
691+
})
692+
})
693+
598694
})
599695
}

0 commit comments

Comments
 (0)