From d96d6de961471bd9a5594ac0e0fc8251f38adc8c Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Wed, 2 Oct 2024 23:03:10 +0800 Subject: [PATCH] feat(main): add controller tests Signed-off-by: cuisongliu --- api/v1beta1/webhook_suite_test.go | 3 -- internal/controller/suite_test.go | 67 +++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/api/v1beta1/webhook_suite_test.go b/api/v1beta1/webhook_suite_test.go index eeb454c..223ecd5 100644 --- a/api/v1beta1/webhook_suite_test.go +++ b/api/v1beta1/webhook_suite_test.go @@ -208,9 +208,6 @@ var _ = BeforeSuite(func() { WebhookInstallOptions: envtest.WebhookInstallOptions{ Paths: []string{filepath.Join("..", "..", "config", "webhook")}, - //MutatingWebhooks: nil, - //ValidatingWebhooks: nil, - IgnoreErrorIfPathMissing: false, }, } diff --git a/internal/controller/suite_test.go b/internal/controller/suite_test.go index 3e31794..e753a24 100644 --- a/internal/controller/suite_test.go +++ b/internal/controller/suite_test.go @@ -17,10 +17,16 @@ limitations under the License. package controller import ( + "context" "fmt" + v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "os" "path/filepath" "runtime" "testing" + "time" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -49,6 +55,67 @@ func TestControllers(t *testing.T) { RunSpecs(t, "Controller Suite") } +var _ = Describe("automq_controller", func() { + Context("automq_controller tests", func() { + ctx := context.Background() + namespaceName := "automq-operator" + namespace := &v1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: namespaceName, + Namespace: namespaceName, + }, + } + automq := &infrav1beta1.AutoMQ{} + automq.Name = "automq-s1" + automq.Namespace = namespaceName + automq.Spec.ClusterID = "rZdE0DjZSrqy96PXrMUZVw" + BeforeEach(func() { + By("Creating the Namespace to perform the tests") + err := k8sClient.Create(ctx, namespace) + Expect(err).To(Not(HaveOccurred())) + + By("Setting the NAMESPACE_NAME ENV VAR which stores the Operand image") + err = os.Setenv("NAMESPACE_NAME", namespaceName) + Expect(err).To(Not(HaveOccurred())) + }) + It("Update Endpoint", func() { + By("creating the custom resource for the automq") + err := k8sClient.Get(ctx, client.ObjectKeyFromObject(automq), automq) + if err != nil && errors.IsNotFound(err) { + // Let's mock our custom resource at the same way that we would + // apply on the cluster the manifest under config/samples + automq.Spec.S3.Endpoint = "http://localhost:9000" + automq.Spec.S3.Bucket = "ko3" + automq.Spec.S3.AccessKeyID = "min" + automq.Spec.S3.SecretAccessKey = "test" + automq.Spec.S3.Region = "us-east-1" + err = k8sClient.Create(ctx, automq) + Expect(err).To(Not(HaveOccurred())) + } + }) + AfterEach(func() { + By("removing the custom resource for the automq") + found := &infrav1beta1.AutoMQ{} + err := k8sClient.Get(ctx, client.ObjectKeyFromObject(automq), found) + Expect(err).To(Not(HaveOccurred())) + + Eventually(func() error { + return k8sClient.Delete(context.TODO(), found) + }, 2*time.Minute, time.Second).Should(Succeed()) + + // TODO(user): Attention if you improve this code by adding other context test you MUST + // be aware of the current delete namespace limitations. + // More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations + By("Deleting the Namespace to perform the tests") + _ = k8sClient.Delete(ctx, namespace) + + By("Removing the Image ENV VAR which stores the Operand image") + _ = os.Unsetenv("NAMESPACE_NAME") + }) + }) + +}) + var _ = BeforeSuite(func() { logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))