From 9720397dc9db8ace16e0fc40144a32e5f3f14af6 Mon Sep 17 00:00:00 2001 From: Gabriele Quaresima Date: Tue, 1 Oct 2024 14:07:28 +0200 Subject: [PATCH] test: add e2e case of declarative database with delete reclaim policy Signed-off-by: Gabriele Quaresima --- .../declarative_database_management_test.go | 97 ++++++++++++++++--- ...e-with-delete-reclaim-policy.yaml.template | 10 ++ tests/labels.go | 11 ++- 3 files changed, 103 insertions(+), 15 deletions(-) create mode 100644 tests/e2e/fixtures/declarative_databases/database-with-delete-reclaim-policy.yaml.template diff --git a/tests/e2e/declarative_database_management_test.go b/tests/e2e/declarative_database_management_test.go index ee9f59942a..aee596b3cc 100644 --- a/tests/e2e/declarative_database_management_test.go +++ b/tests/e2e/declarative_database_management_test.go @@ -32,11 +32,14 @@ import ( // - spinning up a cluster, apply a declarative database on it // Set of tests in which we use the declarative database CRD to add new databases on an existing cluster -var _ = Describe("Declarative databases management test", Label(tests.LabelSmoke, tests.LabelBasic), func() { +var _ = Describe("Declarative databases management test", Label(tests.LabelSmoke, tests.LabelBasic, + tests.LabelDeclarativeDatabases), func() { const ( - clusterManifest = fixturesDir + "/declarative_databases/cluster.yaml.template" - databaseManifest = fixturesDir + "/declarative_databases/database.yaml.template" - level = tests.Medium + clusterManifest = fixturesDir + "/declarative_databases/cluster.yaml.template" + databaseManifest = fixturesDir + "/declarative_databases/database.yaml.template" + databaseManifestWithDeleteReclaimPolicy = fixturesDir + + "/declarative_databases/database-with-delete-reclaim-policy.yaml.template" + level = tests.Medium ) BeforeEach(func() { @@ -47,13 +50,15 @@ var _ = Describe("Declarative databases management test", Label(tests.LabelSmoke Context("plain vanilla cluster", Ordered, func() { const ( - namespacePrefix = "declarative-db" - databaseCrdName = "db-declarative" - dbname = "declarative" + namespacePrefix = "declarative-db" + databaseCrdName = "db-declarative" + databaseWithDeleteRetainPolicyCrdName = "db-declarative-delete" + dbname = "declarative" ) var ( - clusterName, namespace string - database *apiv1.Database + clusterName, namespace string + database *apiv1.Database + databaseWithDeleteRetainPolicy *apiv1.Database ) BeforeAll(func() { @@ -110,7 +115,7 @@ var _ = Describe("Declarative databases management test", Label(tests.LabelSmoke }, 300).WithPolling(10 * time.Second).Should(Succeed()) }) - By("verifying new database has been added", func() { + By("verifying the db exists", func() { primaryPodInfo, err := env.GetClusterPrimary(namespace, clusterName) Expect(err).ToNot(HaveOccurred()) @@ -123,7 +128,7 @@ var _ = Describe("Declarative databases management test", Label(tests.LabelSmoke Expect(utils.DeleteObject(env, database)).To(Succeed()) }) - By("verifying database is still existing", func() { + By("verifying db is still existing", func() { primaryPodInfo, err := env.GetClusterPrimary(namespace, clusterName) Expect(err).ToNot(HaveOccurred()) @@ -131,5 +136,75 @@ var _ = Describe("Declarative databases management test", Label(tests.LabelSmoke }) }) }) + When("Database CRD reclaim policy is set to delete inside spec", func() { + It("can add a declarative database", func() { + By("applying Database CRD manifest", func() { + CreateResourceFromFile(namespace, databaseManifestWithDeleteReclaimPolicy) + _, err := env.GetResourceNameFromYAML(databaseManifestWithDeleteReclaimPolicy) + Expect(err).NotTo(HaveOccurred()) + }) + By("ensuring the Database CRD succeeded reconciliation", func() { + // get database object + databaseWithDeleteRetainPolicy = &apiv1.Database{} + databaseNamespacedName := types.NamespacedName{ + Namespace: namespace, + Name: databaseWithDeleteRetainPolicyCrdName, + } + + Eventually(func(g Gomega) { + err := env.Client.Get(env.Ctx, databaseNamespacedName, databaseWithDeleteRetainPolicy) + Expect(err).ToNot(HaveOccurred()) + g.Expect(databaseWithDeleteRetainPolicy.Status.Ready).Should(BeTrue()) + }, 300).WithPolling(10 * time.Second).Should(Succeed()) + }) + + By("verifying the db exists", func() { + primaryPodInfo, err := env.GetClusterPrimary(namespace, clusterName) + Expect(err).ToNot(HaveOccurred()) + + assertDatabaseExists(namespace, primaryPodInfo.Name, dbname, true) + }) + }) + + It("maintains the db when Database CRD is removed", func() { + By("remove Database CRD", func() { + Expect(utils.DeleteObject(env, databaseWithDeleteRetainPolicy)).To(Succeed()) + }) + + By("verifying db has been removed", func() { + primaryPodInfo, err := env.GetClusterPrimary(namespace, clusterName) + Expect(err).ToNot(HaveOccurred()) + + assertDatabaseExists(namespace, primaryPodInfo.Name, dbname, false) + }) + }) + + It("applies the Database CRD one last time to exercise finalizers removal when the namespace "+ + "will be deleted", func() { + By("applying Database CRD manifest", func() { + CreateResourceFromFile(namespace, databaseManifestWithDeleteReclaimPolicy) + _, err := env.GetResourceNameFromYAML(databaseManifestWithDeleteReclaimPolicy) + Expect(err).NotTo(HaveOccurred()) + }) + By("ensuring the Database CRD succeeded reconciliation", func() { + // get database object + databaseWithDeleteRetainPolicy = &apiv1.Database{} + databaseNamespacedName := types.NamespacedName{ + Namespace: namespace, + Name: databaseWithDeleteRetainPolicyCrdName, + } + + Eventually(func(g Gomega) { + err := env.Client.Get(env.Ctx, databaseNamespacedName, databaseWithDeleteRetainPolicy) + Expect(err).ToNot(HaveOccurred()) + g.Expect(databaseWithDeleteRetainPolicy.Status.Ready).Should(BeTrue()) + }, 300).WithPolling(10 * time.Second).Should(Succeed()) + }) + By("deleting the namespace", func() { + err := env.DeleteNamespaceAndWait(namespace, 60) + Expect(err).ToNot(HaveOccurred()) + }) + }) + }) }) }) diff --git a/tests/e2e/fixtures/declarative_databases/database-with-delete-reclaim-policy.yaml.template b/tests/e2e/fixtures/declarative_databases/database-with-delete-reclaim-policy.yaml.template new file mode 100644 index 0000000000..e7d0f2ccb2 --- /dev/null +++ b/tests/e2e/fixtures/declarative_databases/database-with-delete-reclaim-policy.yaml.template @@ -0,0 +1,10 @@ +apiVersion: postgresql.cnpg.io/v1 +kind: Database +metadata: + name: db-declarative-delete +spec: + name: declarative + owner: app + databaseReclaimPolicy: delete + cluster: + name: cluster-with-declarative-databases diff --git a/tests/labels.go b/tests/labels.go index 81b1329281..03a5bb1ab5 100644 --- a/tests/labels.go +++ b/tests/labels.go @@ -23,12 +23,15 @@ const ( // LabelBackupRestore is a label for only selecting backup and restore tests LabelBackupRestore = "backup-restore" - // LabelBasic is a label for selecting basic test + // LabelBasic is a label for selecting basic test LabelBasic = "basic" // LabelClusterMetadata is a label for selecting cluster-metadata test LabelClusterMetadata = "cluster-metadata" + // LabelDeclarativeDatabases is a label for selecting the test exercising declarative databases + LabelDeclarativeDatabases = "declarative-databases" + // LabelDisruptive is the string for labelling disruptive tests LabelDisruptive = "disruptive" @@ -74,7 +77,7 @@ const ( // LabelServiceConnectivity is a label for selecting service connections test LabelServiceConnectivity = "service-connectivity" - // LabelSmoke is a label for selecting smoke test + // LabelSmoke is a label for selecting smoke test LabelSmoke = "smoke" // LabelSnapshot is a label for selecting snapshot tests @@ -83,9 +86,9 @@ const ( // LabelStorage is a label for selecting storage test LabelStorage = "storage" - // LabelTablespaces is a lable for selectin the tablespaces tests + // LabelTablespaces is a label for selecting the tablespaces tests LabelTablespaces = "tablespaces" - // LabelUpgrade is the string for labelling upgrade tests + // LabelUpgrade is a label for upgrade tests LabelUpgrade = "upgrade" )