-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add controllers unit tests (#284)
* add controllers unit tests * change test name * add restore unit tests * use default scope
- Loading branch information
Showing
15 changed files
with
2,087 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.