From d32fb5d4bc4c92dab707099458b07a11ba48aafb Mon Sep 17 00:00:00 2001 From: John Pitman Date: Thu, 17 Aug 2023 14:56:24 -0400 Subject: [PATCH 1/2] Improve test coverage of backend pt2 Signed-off-by: John Pitman --- backend/eventloop/repocred_reconciler_test.go | 99 +++++++++++++++++-- 1 file changed, 92 insertions(+), 7 deletions(-) diff --git a/backend/eventloop/repocred_reconciler_test.go b/backend/eventloop/repocred_reconciler_test.go index 9a00c33d22..ae7e1cb05e 100644 --- a/backend/eventloop/repocred_reconciler_test.go +++ b/backend/eventloop/repocred_reconciler_test.go @@ -9,6 +9,7 @@ import ( managedgitopsv1alpha1 "github.com/redhat-appstudio/managed-gitops/backend-shared/apis/managed-gitops/v1alpha1" db "github.com/redhat-appstudio/managed-gitops/backend-shared/db" "github.com/redhat-appstudio/managed-gitops/backend-shared/util/tests" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/uuid" "sigs.k8s.io/controller-runtime/pkg/client" @@ -90,39 +91,123 @@ var _ = Describe("RepoCred Reconcile Function Tests", func() { err = dbq.CreateAPICRToDatabaseMapping(ctx, &apiCRToDatabaseMappingDb) Expect(err).ToNot(HaveOccurred()) + }) + + It("should set an error status for RepositoryCredentials if the secret field is not set in the CR", func() { + + defer dbq.CloseDatabase() + By("creating a GitOpsDeployment CR in cluster") gitopsDeploymentRepositoryCredentialCR := &managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredential{ ObjectMeta: metav1.ObjectMeta{ Name: apiCRToDatabaseMappingDb.APIResourceName, Namespace: apiCRToDatabaseMappingDb.APIResourceNamespace, }, } - // Create GitOpsDeployment CR in cluster - err = k8sClient.Create(context.Background(), gitopsDeploymentRepositoryCredentialCR) + err := k8sClient.Create(context.Background(), gitopsDeploymentRepositoryCredentialCR) + Expect(err).ToNot(HaveOccurred()) + + By("calling the Reconcile function.") + reconcileRepositoryCredentials(ctx, dbq, k8sClient, log) + By("verifing that status is updated for GitopsDeploymentRepositoryCredentialCR.") + objectMeta := metav1.ObjectMeta{ + Name: apiCRToDatabaseMappingDb.APIResourceName, + Namespace: apiCRToDatabaseMappingDb.APIResourceNamespace, + } + repoCredCR := &managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredential{ObjectMeta: objectMeta} + err = k8sClient.Get(ctx, client.ObjectKeyFromObject(repoCredCR), repoCredCR) Expect(err).ToNot(HaveOccurred()) + Expect(repoCredCR).NotTo(BeNil()) + Expect(repoCredCR.Status.Conditions).To(HaveLen(3)) + Expect(repoCredCR.Status.Conditions[0].Type).To(Equal(managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredentialConditionErrorOccurred)) + Expect(repoCredCR.Status.Conditions[0].Reason).To(Equal(managedgitopsv1alpha1.RepositoryCredentialReasonSecretNotSpecified)) + Expect(repoCredCR.Status.Conditions[0].Status).To(Equal(metav1.ConditionTrue)) + Expect(repoCredCR.Status.Conditions[0].Message).To(Equal("Secret field is missing value")) }) - It("should reconcile status for RepositoryCredentials if the entry is present in RepositoryCredentials DB", func() { + It("should set an error status for RepositoryCredentials if the secret is not found", func() { defer dbq.CloseDatabase() - By("Call Reconcile function.") + By("creating a GitOpsDeployment CR in cluster") + gitopsDeploymentRepositoryCredentialCR := &managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredential{ + ObjectMeta: metav1.ObjectMeta{ + Name: apiCRToDatabaseMappingDb.APIResourceName, + Namespace: apiCRToDatabaseMappingDb.APIResourceNamespace, + }, + Spec: managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredentialSpec{ + Secret: "test-my-missing-secret", + }, + } + err := k8sClient.Create(context.Background(), gitopsDeploymentRepositoryCredentialCR) + Expect(err).ToNot(HaveOccurred()) + By("calling the Reconcile function.") reconcileRepositoryCredentials(ctx, dbq, k8sClient, log) - By("Verify that status is updated for GitopsDeploymentRepositoryCredentialCR.") + By("verifing that status is updated for GitopsDeploymentRepositoryCredentialCR.") objectMeta := metav1.ObjectMeta{ Name: apiCRToDatabaseMappingDb.APIResourceName, Namespace: apiCRToDatabaseMappingDb.APIResourceNamespace, } repoCredCR := &managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredential{ObjectMeta: objectMeta} - - err := k8sClient.Get(ctx, client.ObjectKeyFromObject(repoCredCR), repoCredCR) + err = k8sClient.Get(ctx, client.ObjectKeyFromObject(repoCredCR), repoCredCR) Expect(err).ToNot(HaveOccurred()) Expect(repoCredCR).NotTo(BeNil()) Expect(repoCredCR.Status.Conditions).To(HaveLen(3)) + Expect(repoCredCR.Status.Conditions[0].Type).To(Equal(managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredentialConditionErrorOccurred)) + Expect(repoCredCR.Status.Conditions[0].Reason).To(Equal(managedgitopsv1alpha1.RepositoryCredentialReasonSecretNotFound)) + Expect(repoCredCR.Status.Conditions[0].Status).To(Equal(metav1.ConditionTrue)) + Expect(repoCredCR.Status.Conditions[0].Message).To(Equal("Secret specified not found")) + }) + + It("should set an error status for RepositoryCredentials if the repo and secret exist but the credentials are invalid", func() { + + defer dbq.CloseDatabase() + + By("creating a secret in the cluster") + secret := &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-my-repository-credentials-secret", + Namespace: apiCRToDatabaseMappingDb.APIResourceNamespace, + }, + Data: map[string][]byte{"username": []byte("username"), "password": []byte("password")}, + } + err := k8sClient.Create(ctx, secret) + Expect(err).ToNot(HaveOccurred()) + By("creating a GitOpsDeployment CR in cluster") + gitopsDeploymentRepositoryCredentialCR := &managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredential{ + ObjectMeta: metav1.ObjectMeta{ + Name: apiCRToDatabaseMappingDb.APIResourceName, + Namespace: apiCRToDatabaseMappingDb.APIResourceNamespace, + }, + Spec: managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredentialSpec{ + Repository: "https://github.com/redhat-appstudio/managed-gitops.git", + Secret: secret.Name, + }, + } + err = k8sClient.Create(context.Background(), gitopsDeploymentRepositoryCredentialCR) + Expect(err).ToNot(HaveOccurred()) + + By("calling the Reconcile function.") + reconcileRepositoryCredentials(ctx, dbq, k8sClient, log) + + By("verifing that status is updated for GitopsDeploymentRepositoryCredentialCR.") + objectMeta := metav1.ObjectMeta{ + Name: apiCRToDatabaseMappingDb.APIResourceName, + Namespace: apiCRToDatabaseMappingDb.APIResourceNamespace, + } + repoCredCR := &managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredential{ObjectMeta: objectMeta} + err = k8sClient.Get(ctx, client.ObjectKeyFromObject(repoCredCR), repoCredCR) + Expect(err).ToNot(HaveOccurred()) + Expect(repoCredCR).NotTo(BeNil()) + Expect(repoCredCR.Status.Conditions).To(HaveLen(3)) + Expect(repoCredCR.Status.Conditions[0].Type).To(Equal(managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredentialConditionErrorOccurred)) + Expect(repoCredCR.Status.Conditions[0].Reason).To(Equal(managedgitopsv1alpha1.RepositoryCredentialReasonInvalidCredentials)) + Expect(repoCredCR.Status.Conditions[0].Status).To(Equal(metav1.ConditionTrue)) + Expect(repoCredCR.Status.Conditions[0].Message).To(Equal("Repository Credentials provided test-my-repository-credentials-secret for Repository https://github.com/redhat-appstudio/managed-gitops.git are invalid")) }) }) }) From aed429904626d64e69a8ae6a3fefa700bf7adb50 Mon Sep 17 00:00:00 2001 From: John Pitman Date: Thu, 21 Sep 2023 11:09:40 -0400 Subject: [PATCH 2/2] changes based on review feedback Signed-off-by: John Pitman --- backend/eventloop/repocred_reconciler_test.go | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/backend/eventloop/repocred_reconciler_test.go b/backend/eventloop/repocred_reconciler_test.go index ae7e1cb05e..9b565db49a 100644 --- a/backend/eventloop/repocred_reconciler_test.go +++ b/backend/eventloop/repocred_reconciler_test.go @@ -17,6 +17,15 @@ import ( logger "sigs.k8s.io/controller-runtime/pkg/log" ) +func findCondition(conditions []metav1.Condition, typeName string) *metav1.Condition { + for i := range conditions { + if conditions[i].Type == typeName { + return &conditions[i] + } + } + return nil +} + var _ = Describe("RepoCred Reconcile Function Tests", func() { Context("Testing reconcileRepositoryCredentials function for RepositoryCredentials table entries.", func() { @@ -93,6 +102,18 @@ var _ = Describe("RepoCred Reconcile Function Tests", func() { Expect(err).ToNot(HaveOccurred()) }) + It("should skip the reconcile and not blow up if the GitOpsDeploymentRepositoryCredential CR is not found", func() { + + defer dbq.CloseDatabase() + + By("not creating a GitOpsDeployment CR in cluster and calling the Reconcile function.") + objectMeta := metav1.ObjectMeta{ + Name: apiCRToDatabaseMappingDb.APIResourceName, + Namespace: apiCRToDatabaseMappingDb.APIResourceNamespace, + } + reconcileRepositoryCredentialStatus(ctx, k8sClient, dbq, db.APICRToDatabaseMapping{}, objectMeta, log) + }) + It("should set an error status for RepositoryCredentials if the secret field is not set in the CR", func() { defer dbq.CloseDatabase() @@ -120,10 +141,11 @@ var _ = Describe("RepoCred Reconcile Function Tests", func() { Expect(err).ToNot(HaveOccurred()) Expect(repoCredCR).NotTo(BeNil()) Expect(repoCredCR.Status.Conditions).To(HaveLen(3)) - Expect(repoCredCR.Status.Conditions[0].Type).To(Equal(managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredentialConditionErrorOccurred)) - Expect(repoCredCR.Status.Conditions[0].Reason).To(Equal(managedgitopsv1alpha1.RepositoryCredentialReasonSecretNotSpecified)) - Expect(repoCredCR.Status.Conditions[0].Status).To(Equal(metav1.ConditionTrue)) - Expect(repoCredCR.Status.Conditions[0].Message).To(Equal("Secret field is missing value")) + condition := findCondition(repoCredCR.Status.Conditions, managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredentialConditionErrorOccurred) + Expect(condition).ToNot(BeNil()) + Expect(condition.Reason).To(Equal(managedgitopsv1alpha1.RepositoryCredentialReasonSecretNotSpecified)) + Expect(condition.Status).To(Equal(metav1.ConditionTrue)) + Expect(condition.Message).To(Equal("Secret field is missing value")) }) It("should set an error status for RepositoryCredentials if the secret is not found", func() { @@ -156,10 +178,11 @@ var _ = Describe("RepoCred Reconcile Function Tests", func() { Expect(err).ToNot(HaveOccurred()) Expect(repoCredCR).NotTo(BeNil()) Expect(repoCredCR.Status.Conditions).To(HaveLen(3)) - Expect(repoCredCR.Status.Conditions[0].Type).To(Equal(managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredentialConditionErrorOccurred)) - Expect(repoCredCR.Status.Conditions[0].Reason).To(Equal(managedgitopsv1alpha1.RepositoryCredentialReasonSecretNotFound)) - Expect(repoCredCR.Status.Conditions[0].Status).To(Equal(metav1.ConditionTrue)) - Expect(repoCredCR.Status.Conditions[0].Message).To(Equal("Secret specified not found")) + condition := findCondition(repoCredCR.Status.Conditions, managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredentialConditionErrorOccurred) + Expect(condition).ToNot(BeNil()) + Expect(condition.Reason).To(Equal(managedgitopsv1alpha1.RepositoryCredentialReasonSecretNotFound)) + Expect(condition.Status).To(Equal(metav1.ConditionTrue)) + Expect(condition.Message).To(Equal("Secret specified not found")) }) It("should set an error status for RepositoryCredentials if the repo and secret exist but the credentials are invalid", func() { @@ -204,10 +227,11 @@ var _ = Describe("RepoCred Reconcile Function Tests", func() { Expect(err).ToNot(HaveOccurred()) Expect(repoCredCR).NotTo(BeNil()) Expect(repoCredCR.Status.Conditions).To(HaveLen(3)) - Expect(repoCredCR.Status.Conditions[0].Type).To(Equal(managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredentialConditionErrorOccurred)) - Expect(repoCredCR.Status.Conditions[0].Reason).To(Equal(managedgitopsv1alpha1.RepositoryCredentialReasonInvalidCredentials)) - Expect(repoCredCR.Status.Conditions[0].Status).To(Equal(metav1.ConditionTrue)) - Expect(repoCredCR.Status.Conditions[0].Message).To(Equal("Repository Credentials provided test-my-repository-credentials-secret for Repository https://github.com/redhat-appstudio/managed-gitops.git are invalid")) + condition := findCondition(repoCredCR.Status.Conditions, managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredentialConditionErrorOccurred) + Expect(condition).ToNot(BeNil()) + Expect(condition.Reason).To(Equal(managedgitopsv1alpha1.RepositoryCredentialReasonInvalidCredentials)) + Expect(condition.Status).To(Equal(metav1.ConditionTrue)) + Expect(condition.Message).To(Equal("Repository Credentials provided test-my-repository-credentials-secret for Repository https://github.com/redhat-appstudio/managed-gitops.git are invalid")) }) }) })