Skip to content

Commit

Permalink
test: add controllers unit tests (#284)
Browse files Browse the repository at this point in the history
* add controllers unit tests

* change test name

* add restore unit tests

* use default scope
  • Loading branch information
zreigz authored Oct 2, 2024
1 parent ee7174c commit b4e2e9a
Show file tree
Hide file tree
Showing 15 changed files with 2,087 additions and 144 deletions.
73 changes: 73 additions & 0 deletions internal/controller/backup_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package controller

import (
"context"

"github.com/pluralsh/deployment-operator/pkg/test/mocks"
"github.com/stretchr/testify/mock"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

var _ = Describe("Backup Controller", Ordered, func() {
Context("When reconciling a resource", func() {
const (
resourceName = "default"
namespace = "default"
)

ctx := context.Background()

typeNamespacedName := types.NamespacedName{
Name: resourceName,
Namespace: "default",
}

backup := &velerov1.Backup{}

BeforeAll(func() {
By("creating the custom resource for the Kind Backup")
err := kClient.Get(ctx, typeNamespacedName, backup)
if err != nil && errors.IsNotFound(err) {
resource := &velerov1.Backup{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: namespace,
},
Spec: velerov1.BackupSpec{},
}
Expect(kClient.Create(ctx, resource)).To(Succeed())
}
})

AfterAll(func() {
resource := &velerov1.Backup{}
err := kClient.Get(ctx, typeNamespacedName, resource)
Expect(err).NotTo(HaveOccurred())

By("Cleanup the specific resource instance Backup")
Expect(kClient.Delete(ctx, resource)).To(Succeed())
})

It("should successfully reconcile resource", func() {
fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
fakeConsoleClient.On("SaveClusterBackup", mock.Anything, mock.Anything).Return(nil, nil)
reconciler := &BackupReconciler{
Client: kClient,
Scheme: kClient.Scheme(),
ConsoleClient: fakeConsoleClient,
}
_, err := reconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).NotTo(HaveOccurred())
})
})

})
88 changes: 88 additions & 0 deletions internal/controller/constraint_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package controller

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
templatesv1 "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates/v1"
"github.com/open-policy-agent/gatekeeper/v3/apis/status/v1beta1"
constraintstatusv1beta1 "github.com/open-policy-agent/gatekeeper/v3/apis/status/v1beta1"
"github.com/pluralsh/deployment-operator/pkg/test/mocks"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

var _ = Describe("ConstraintPodStatus Controller", Ordered, func() {
Context("When reconciling a resource", func() {
const (
resourceName = "default"
namespace = "default"
kind = "Test"
templateName = "test-template"
)

ctx := context.Background()

typeNamespacedName := types.NamespacedName{
Name: resourceName,
Namespace: "default",
}

cps := new(constraintstatusv1beta1.ConstraintPodStatus)

BeforeAll(func() {
By("creating the custom resource for the Kind ConstraintPodStatus")
err := kClient.Get(ctx, typeNamespacedName, cps)
if err != nil && errors.IsNotFound(err) {
resource := &constraintstatusv1beta1.ConstraintPodStatus{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: namespace,
Labels: map[string]string{v1beta1.ConstraintNameLabel: templateName, v1beta1.ConstraintKindLabel: kind, v1beta1.ConstraintTemplateNameLabel: templateName},
},
Status: constraintstatusv1beta1.ConstraintPodStatusStatus{},
}
Expect(kClient.Create(ctx, resource)).To(Succeed())
}
template := &templatesv1.ConstraintTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: templateName,
Namespace: namespace,
},
Spec: templatesv1.ConstraintTemplateSpec{},
}
Expect(kClient.Create(ctx, template)).To(Succeed())
})

AfterAll(func() {
resource := &constraintstatusv1beta1.ConstraintPodStatus{}
err := kClient.Get(ctx, typeNamespacedName, resource)
Expect(err).NotTo(HaveOccurred())

By("Cleanup the specific resource instance ConstraintPodStatus")
Expect(kClient.Delete(ctx, resource)).To(Succeed())

template := new(templatesv1.ConstraintTemplate)
Expect(kClient.Get(ctx, types.NamespacedName{Name: templateName}, template)).To(Succeed())
Expect(kClient.Delete(ctx, template)).To(Succeed())
})

It("should fail reconcile resource", func() {
fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
reconciler := &ConstraintReconciler{
Client: kClient,
Scheme: kClient.Scheme(),
ConsoleClient: fakeConsoleClient,
Reader: kClient,
}
_, err := reconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).To(HaveOccurred())
})
})

})
124 changes: 124 additions & 0 deletions internal/controller/crd_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package controller

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

type TestReconciler struct{}

func (r *TestReconciler) SetupWithManager(_ ctrl.Manager) error {
return nil
}

var _ = Describe("CRD Controller", Ordered, func() {
Context("When reconciling a resource", func() {
const (
resourceName = "testresources.test.group"
)

ctx := context.Background()

typeNamespacedName := types.NamespacedName{
Name: resourceName,
Namespace: "default",
}

testReconciler := TestReconciler{}
reconcileGroups := map[schema.GroupVersionKind]SetupWithManager{
{
Group: "test.group",
Version: "v1",
Kind: "TestResource",
}: testReconciler.SetupWithManager,
}

crd := &apiextensionsv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "testresources.test.group",
},
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
Group: "test.group",
Names: apiextensionsv1.CustomResourceDefinitionNames{
Plural: "testresources",
Singular: "testresource",
Kind: "TestResource",
ListKind: "TestResourceList",
ShortNames: []string{"tr"},
},
Scope: apiextensionsv1.NamespaceScoped,
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{
{
Name: "v1",
Served: true,
Storage: true,
Schema: &apiextensionsv1.CustomResourceValidation{
OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{
Type: "object",
Properties: map[string]apiextensionsv1.JSONSchemaProps{
"spec": {
Type: "object",
Properties: map[string]apiextensionsv1.JSONSchemaProps{
"name": {
Type: "string",
},
"value": {
Type: "string",
},
},
},
"status": {
Type: "object",
Properties: map[string]apiextensionsv1.JSONSchemaProps{},
},
},
},
},
},
},
},
}

BeforeAll(func() {
By("creating the custom resource")
err := kClient.Get(ctx, typeNamespacedName, &apiextensionsv1.CustomResourceDefinition{})
if err != nil && errors.IsNotFound(err) {
Expect(kClient.Create(ctx, crd)).To(Succeed())
}
})

AfterAll(func() {
resource := &apiextensionsv1.CustomResourceDefinition{}
err := kClient.Get(ctx, typeNamespacedName, resource)
Expect(err).NotTo(HaveOccurred())

By("Cleanup the specific CRD")
Expect(kClient.Delete(ctx, resource)).To(Succeed())
})

It("should successfully reconcile resource", func() {

reconciler := &CrdRegisterControllerReconciler{
Client: kClient,
Scheme: kClient.Scheme(),
ReconcilerGroups: reconcileGroups,
}
_, err := reconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).NotTo(HaveOccurred())
Expect(reconciler.registeredControllers).ToNot(BeNil())
Expect(reconciler.registeredControllers).To(HaveLen(1))
})
})

})
2 changes: 1 addition & 1 deletion internal/controller/customhealth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (r *CustomHealthReconciler) Reconcile(ctx context.Context, req ctrl.Request
utils.MarkCondition(script.SetCondition, v1alpha1.ReadyConditionType, v1.ConditionFalse, v1alpha1.ReadyConditionReason, "")

// Ensure that status updates will always be persisted when exiting this function.
scope, err := NewClusterScope(ctx, r.Client, script)
scope, err := NewDefaultScope(ctx, r.Client, script)
if err != nil {
logger.Error(err, "Failed to create cluster scope")
utils.MarkCondition(script.SetCondition, v1alpha1.ReadyConditionType, v1.ConditionFalse, v1alpha1.ReadyConditionReason, err.Error())
Expand Down
7 changes: 0 additions & 7 deletions internal/controller/customhealth_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ var _ = Describe("Customhealt Controller", Ordered, func() {

It("should successfully reconcile resource", func() {
By("Reconciling the import resource")
_ = struct {
expectedStatus v1alpha1.CustomHealthStatus
}{
expectedStatus: v1alpha1.CustomHealthStatus{
Conditions: []metav1.Condition{},
},
}
reconciler := &CustomHealthReconciler{
Client: kClient,
Scheme: kClient.Scheme(),
Expand Down
49 changes: 0 additions & 49 deletions internal/controller/customhealth_scope.go

This file was deleted.

Loading

0 comments on commit b4e2e9a

Please sign in to comment.