Skip to content

Commit

Permalink
test: add unittests for argocd_rolebinding_controller
Browse files Browse the repository at this point in the history
Signed-off-by: Georgy Khromov <[email protected]>
  • Loading branch information
ggkhrmv committed Sep 3, 2024
1 parent 09f8001 commit 4670908
Show file tree
Hide file tree
Showing 4 changed files with 559 additions and 29 deletions.
41 changes: 19 additions & 22 deletions internal/controller/argocdrole_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/argoproj-labs/argocd-rbac-operator/internal/controller/common"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand All @@ -37,17 +36,16 @@ var _ reconcile.Reconciler = &ArgoCDRoleBindingReconciler{}

func TestArgoCDRoleReconciler_Reconcile(t *testing.T) {
logf.SetLogger(ZapLogger(true))
argocdRole := makeTestRole(addFinalizer(rbacoperatorv1alpha1.ArgoCDRoleFinalizerName))
argocdRole := makeTestRole(addFinalizerRole(rbacoperatorv1alpha1.ArgoCDRoleFinalizerName))

resObjs := []client.Object{argocdRole}
subresObjs := []client.Object{argocdRole}
runtimeObjs := []runtime.Object{}
scheme := makeTestReconcilerScheme(rbacoperatorv1alpha1.AddToScheme)
client := makeTestReconcilerClient(scheme, resObjs, subresObjs, runtimeObjs)
client := makeTestReconcilerClient(scheme, resObjs, subresObjs)
reconciler := makeTestArgoCDRoleReconciler(client, scheme)

assert.NoError(t, reconciler.Client.Create(context.TODO(), makeTestArgoCDNamespace()))
assert.NoError(t, reconciler.Client.Create(context.TODO(), makeTestRBACConfigMap()))
assert.NoError(t, reconciler.Client.Create(context.TODO(), makeTestRBACConfigMap_WithChangedPolicyCSV()))

req := reconcile.Request{
NamespacedName: types.NamespacedName{
Expand Down Expand Up @@ -75,9 +73,9 @@ func TestArgoCDRoleReconciler_AddFinalizer(t *testing.T) {

resObjs := []client.Object{argocdRole}
subresObjs := []client.Object{argocdRole}
runtimeObjs := []runtime.Object{}

scheme := makeTestReconcilerScheme(rbacoperatorv1alpha1.AddToScheme)
client := makeTestReconcilerClient(scheme, resObjs, subresObjs, runtimeObjs)
client := makeTestReconcilerClient(scheme, resObjs, subresObjs)
reconciler := makeTestArgoCDRoleReconciler(client, scheme)

assert.NoError(t, reconciler.Client.Create(context.TODO(), makeTestArgoCDNamespace()))
Expand Down Expand Up @@ -108,9 +106,8 @@ func TestArgoCDRoleReconciler_RoleNotFound(t *testing.T) {

resObjs := []client.Object{}
subresObjs := []client.Object{}
runtimeObjs := []runtime.Object{}
scheme := makeTestReconcilerScheme(rbacoperatorv1alpha1.AddToScheme)
client := makeTestReconcilerClient(scheme, resObjs, subresObjs, runtimeObjs)
client := makeTestReconcilerClient(scheme, resObjs, subresObjs)
reconciler := makeTestArgoCDRoleReconciler(client, scheme)

assert.NoError(t, reconciler.Client.Create(context.TODO(), makeTestArgoCDNamespace()))
Expand All @@ -133,13 +130,13 @@ func TestArgoCDRoleReconciler_RoleNotFound(t *testing.T) {

func TestArgoCDRoleReconciler_CMNotFound(t *testing.T) {
logf.SetLogger(ZapLogger(true))
argocdRole := makeTestRole(addFinalizer(rbacoperatorv1alpha1.ArgoCDRoleFinalizerName))
argocdRole := makeTestRole(addFinalizerRole(rbacoperatorv1alpha1.ArgoCDRoleFinalizerName))

resObjs := []client.Object{argocdRole}
subresObjs := []client.Object{argocdRole}
runtimeObjs := []runtime.Object{}

scheme := makeTestReconcilerScheme(rbacoperatorv1alpha1.AddToScheme)
client := makeTestReconcilerClient(scheme, resObjs, subresObjs, runtimeObjs)
client := makeTestReconcilerClient(scheme, resObjs, subresObjs)
reconciler := makeTestArgoCDRoleReconciler(client, scheme)

assert.NoError(t, reconciler.Client.Create(context.TODO(), makeTestArgoCDNamespace()))
Expand All @@ -158,13 +155,13 @@ func TestArgoCDRoleReconciler_CMNotFound(t *testing.T) {

func TestArgoCDRoleReconciler_HandleFinalizer(t *testing.T) {
logf.SetLogger(ZapLogger(true))
argocdRole := makeTestRole(addFinalizer(rbacoperatorv1alpha1.ArgoCDRoleFinalizerName), deletedAt(time.Now()))
argocdRole := makeTestRole(addFinalizerRole(rbacoperatorv1alpha1.ArgoCDRoleFinalizerName), roleDeletedAt(time.Now()))

resObjs := []client.Object{argocdRole}
subresObjs := []client.Object{argocdRole}
runtimeObjs := []runtime.Object{}

scheme := makeTestReconcilerScheme(rbacoperatorv1alpha1.AddToScheme)
client := makeTestReconcilerClient(scheme, resObjs, subresObjs, runtimeObjs)
client := makeTestReconcilerClient(scheme, resObjs, subresObjs)
reconciler := makeTestArgoCDRoleReconciler(client, scheme)

assert.NoError(t, reconciler.Client.Create(context.TODO(), makeTestArgoCDNamespace()))
Expand Down Expand Up @@ -194,17 +191,17 @@ func TestArgoCDRoleReconciler_RoleHasRoleBinding(t *testing.T) {
logf.SetLogger(ZapLogger(true))

argocdRoleBinding := makeTestRoleBindingWithRoleSubject()
argocdRole := makeTestRole(addFinalizer(rbacoperatorv1alpha1.ArgoCDRoleFinalizerName), addRoleBinding(argocdRoleBinding.Name))
argocdRole := makeTestRole(addFinalizerRole(rbacoperatorv1alpha1.ArgoCDRoleFinalizerName), addRoleBinding(argocdRoleBinding.Name))

resObjs := []client.Object{argocdRole, argocdRoleBinding}
subresObjs := []client.Object{argocdRole, argocdRoleBinding}
runtimeObjs := []runtime.Object{}

scheme := makeTestReconcilerScheme(rbacoperatorv1alpha1.AddToScheme)
client := makeTestReconcilerClient(scheme, resObjs, subresObjs, runtimeObjs)
client := makeTestReconcilerClient(scheme, resObjs, subresObjs)
reconciler := makeTestArgoCDRoleReconciler(client, scheme)

assert.NoError(t, reconciler.Client.Create(context.TODO(), makeTestArgoCDNamespace()))
assert.NoError(t, reconciler.Client.Create(context.TODO(), makeTestRBACConfigMap()))
assert.NoError(t, reconciler.Client.Create(context.TODO(), makeTestRBACConfigMap_WithChangedPolicyCSV()))

req := reconcile.Request{
NamespacedName: types.NamespacedName{
Expand All @@ -229,13 +226,13 @@ func TestArgoCDRoleReconciler_RoleHasRoleBinding(t *testing.T) {
func TestArgoCDRoleReconciler_RoleBindingObjectMissing(t *testing.T) {
logf.SetLogger(ZapLogger(true))

argocdRole := makeTestRole(addFinalizer(rbacoperatorv1alpha1.ArgoCDRoleFinalizerName), addRoleBinding("rb-role-test"))
argocdRole := makeTestRole(addFinalizerRole(rbacoperatorv1alpha1.ArgoCDRoleFinalizerName), addRoleBinding("rb-role-test"))

resObjs := []client.Object{argocdRole}
subresObjs := []client.Object{argocdRole}
runtimeObjs := []runtime.Object{}

scheme := makeTestReconcilerScheme(rbacoperatorv1alpha1.AddToScheme)
client := makeTestReconcilerClient(scheme, resObjs, subresObjs, runtimeObjs)
client := makeTestReconcilerClient(scheme, resObjs, subresObjs)
reconciler := makeTestArgoCDRoleReconciler(client, scheme)

assert.NoError(t, reconciler.Client.Create(context.TODO(), makeTestArgoCDNamespace()))
Expand Down
Loading

0 comments on commit 4670908

Please sign in to comment.