Skip to content

Commit

Permalink
Merge pull request #4 from argoproj-labs/test/unittest_cov
Browse files Browse the repository at this point in the history
test[#3]: add unittests for controllers
  • Loading branch information
ggkhrmv authored Sep 3, 2024
2 parents 886920b + 10ce359 commit e577a9f
Show file tree
Hide file tree
Showing 7 changed files with 975 additions and 212 deletions.
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ toolchain go1.22.3
require (
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/stretchr/testify v1.9.0
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
sigs.k8s.io/controller-runtime v0.18.4
)

require github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
require (
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)

require (
github.com/beorn7/perks v1.0.1 // indirect
Expand Down
85 changes: 0 additions & 85 deletions internal/controller/argocd_rbac_operator_test.go

This file was deleted.

253 changes: 253 additions & 0 deletions internal/controller/argocdrole_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
"context"
"testing"
"time"

rbacoperatorv1alpha1 "github.com/argoproj-labs/argocd-rbac-operator/api/v1alpha1"
"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/types"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

var _ reconcile.Reconciler = &ArgoCDRoleReconciler{}
var _ reconcile.Reconciler = &ArgoCDRoleBindingReconciler{}

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

resObjs := []client.Object{argocdRole}
subresObjs := []client.Object{argocdRole}
scheme := makeTestReconcilerScheme(rbacoperatorv1alpha1.AddToScheme)
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_WithChangedPolicyCSV()))

req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: argocdRole.Name,
Namespace: argocdRole.Namespace,
},
}

res, err := reconciler.Reconcile(context.TODO(), req)
assert.NoError(t, err)
if res.Requeue {
t.Fatal("reconcile requeued request")
}

cm := &corev1.ConfigMap{}
err = reconciler.Client.Get(context.TODO(), types.NamespacedName{Name: common.ArgoCDRBACConfigMapName, Namespace: testRBACCMNamespace}, cm)
assert.NoError(t, err)
resCM := makeTestCMArgoCDRoleExpected()
assert.Equal(t, resCM.Data, cm.Data)
}

func TestArgoCDRoleReconciler_AddFinalizer(t *testing.T) {
logf.SetLogger(ZapLogger(true))
argocdRole := makeTestRole()

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

scheme := makeTestReconcilerScheme(rbacoperatorv1alpha1.AddToScheme)
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()))

req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: argocdRole.Name,
Namespace: argocdRole.Namespace,
},
}

res, err := reconciler.Reconcile(context.TODO(), req)
assert.NoError(t, err)
if res.Requeue {
t.Fatal("reconcile requeued request")
}

argocdRoleRes := &rbacoperatorv1alpha1.ArgoCDRole{}
err = reconciler.Client.Get(context.TODO(), types.NamespacedName{Name: argocdRole.Name, Namespace: argocdRole.Namespace}, argocdRoleRes)
assert.NoError(t, err)

assert.Contains(t, argocdRoleRes.GetFinalizers(), rbacoperatorv1alpha1.ArgoCDRoleFinalizerName)
}

func TestArgoCDRoleReconciler_RoleNotFound(t *testing.T) {
logf.SetLogger(ZapLogger(true))

resObjs := []client.Object{}
subresObjs := []client.Object{}
scheme := makeTestReconcilerScheme(rbacoperatorv1alpha1.AddToScheme)
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()))

req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: testRoleName,
Namespace: testNamespace,
},
}

res, err := reconciler.Reconcile(context.TODO(), req)
assert.NoError(t, err)
if res.Requeue {
t.Fatal("reconcile requeued request")
}
assert.Error(t, reconciler.Client.Get(context.TODO(), types.NamespacedName{Name: testRoleName, Namespace: testNamespace}, &rbacoperatorv1alpha1.ArgoCDRole{}))
}

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

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

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

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

req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: testRoleName,
Namespace: testNamespace,
},
}

res, err := reconciler.Reconcile(context.TODO(), req)
assert.Error(t, err)
assert.True(t, res.Requeue)
}

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

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

scheme := makeTestReconcilerScheme(rbacoperatorv1alpha1.AddToScheme)
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(), makeTestCMArgoCDRoleExpected()))

req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: argocdRole.Name,
Namespace: argocdRole.Namespace,
},
}

res, err := reconciler.Reconcile(context.TODO(), req)
assert.NoError(t, err)
if res.Requeue {
t.Fatal("reconcile requeued request")
}

cm := &corev1.ConfigMap{}
err = reconciler.Client.Get(context.TODO(), types.NamespacedName{Name: common.ArgoCDRBACConfigMapName, Namespace: testRBACCMNamespace}, cm)
assert.NoError(t, err)
wantCM := makeTestRBACConfigMap()
assert.Equal(t, wantCM.Data, cm.Data)
}

func TestArgoCDRoleReconciler_RoleHasRoleBinding(t *testing.T) {
logf.SetLogger(ZapLogger(true))

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

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

scheme := makeTestReconcilerScheme(rbacoperatorv1alpha1.AddToScheme)
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_WithChangedPolicyCSV()))

req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: argocdRole.Name,
Namespace: argocdRole.Namespace,
},
}

res, err := reconciler.Reconcile(context.TODO(), req)
assert.NoError(t, err)
if res.Requeue {
t.Fatal("reconcile requeued request")
}

cm := &corev1.ConfigMap{}
err = reconciler.Client.Get(context.TODO(), types.NamespacedName{Name: common.ArgoCDRBACConfigMapName, Namespace: testRBACCMNamespace}, cm)
assert.NoError(t, err)
resCM := makeTestCM_ArgoCDRole_WithRoleBindingRoleSubject_Expected()
assert.Equal(t, resCM.Data, cm.Data)
}

func TestArgoCDRoleReconciler_RoleBindingObjectMissing(t *testing.T) {
logf.SetLogger(ZapLogger(true))

argocdRole := makeTestRole(addFinalizerRole(), addRoleBinding("rb-role-test"))

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

scheme := makeTestReconcilerScheme(rbacoperatorv1alpha1.AddToScheme)
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()))

req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: argocdRole.Name,
Namespace: argocdRole.Namespace,
},
}

res, err := reconciler.Reconcile(context.TODO(), req)
assert.Error(t, err)
if res.Requeue {
t.Fatal("reconcile requeued request")
}
}
Loading

0 comments on commit e577a9f

Please sign in to comment.