Skip to content

Commit

Permalink
chore: simplify
Browse files Browse the repository at this point in the history
Signed-off-by: Jaime Silvela <[email protected]>
  • Loading branch information
jsilvela authored and NiccoloFei committed Oct 14, 2024
1 parent b735694 commit 84dc435
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 149 deletions.
270 changes: 137 additions & 133 deletions tests/e2e/declarative_database_management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ 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,
var _ = Describe("Declarative database management", Label(tests.LabelSmoke, tests.LabelBasic,
tests.LabelDeclarativeDatabases), func() {
const (
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
clusterManifest = fixturesDir + "/declarative_databases/cluster.yaml.template"
level = tests.Medium
)

BeforeEach(func() {
Expand All @@ -49,25 +46,20 @@ var _ = Describe("Declarative databases management test", Label(tests.LabelSmoke
}
})

Context("plain vanilla cluster", Ordered, func() {
Context("in a plain vanilla cluster", Ordered, func() {
const (
namespace = "declarative-db"
dbname = "declarative"
namespacePrefix = "declarative-db"
dbname = "declarative"
)
var (
clusterName, databaseObjectName string
database *apiv1.Database
databaseWithDeleteRetainPolicy *apiv1.Database
err error
databaseWithDeleteRetainPolicyCrdName = "db-declarative-delete"
clusterName, namespace string
err error
)

BeforeAll(func() {
// We set the namespace name as a constant, as we are going to test the namespace deletion inside
// the suite (not after), in order to check the finalizers removal
err = env.CreateNamespace(namespace)
// Create a cluster in a namespace we'll delete after the test
namespace, err = env.CreateUniqueTestNamespace(namespacePrefix)
Expect(err).ToNot(HaveOccurred())

clusterName, err = env.GetResourceNameFromYAML(clusterManifest)
Expect(err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -107,135 +99,147 @@ var _ = Describe("Declarative databases management test", Label(tests.LabelSmoke
"postgres",
query)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(stdout).Should(ContainSubstring("1"))
g.Expect(stdout).Should(ContainSubstring("1"), "expected database not found")
}, 30).Should(Succeed())
}

When("Database CRD reclaim policy is set to retain (default) inside spec", func() {
It("can add a declarative database", func() {
By("applying Database CRD manifest", func() {
CreateResourceFromFile(namespace, databaseManifest)
databaseObjectName, err = env.GetResourceNameFromYAML(databaseManifest)
Expect(err).NotTo(HaveOccurred())
})
By("ensuring the Database CRD succeeded reconciliation", func() {
// get database object
database = &apiv1.Database{}
databaseNamespacedName := types.NamespacedName{
Namespace: namespace,
Name: databaseObjectName,
}

Eventually(func(g Gomega) {
err := env.Client.Get(env.Ctx, databaseNamespacedName, database)
Expect(err).ToNot(HaveOccurred())
g.Expect(database.Status.Ready).Should(BeTrue())
}, 300).WithPolling(10 * time.Second).Should(Succeed())
})

By("verifying new database has been created with the expected fields", func() {
primaryPodInfo, err := env.GetClusterPrimary(namespace, clusterName)
Expect(err).ToNot(HaveOccurred())

assertDatabaseExists(namespace, primaryPodInfo.Name, dbname, true)

// NOTE: the `pg_database` table in Postgres does not contain fields
// for the owner nor the template.
// Its fields are dependent on the version of Postgres, so we pick
// a subset that is available to check even on PG v12
expectedDatabaseFields := apiv1.Database{
Spec: apiv1.DatabaseSpec{
Name: "declarative",
LcCtype: "en_US.utf8",
LcCollate: "C", // this is the default value
Encoding: "0", // corresponds to SQL_ASCII
},
}
assertDatabaseHasExpectedFields(namespace, primaryPodInfo.Name, expectedDatabaseFields)
})
assertTestDeclarativeDatabase := func(
databaseManifest string,
retainOnDeletion bool,
expectedDatabaseFields apiv1.Database,
) {
var (
database *apiv1.Database
databaseObjectName string
)
By("applying Database CRD manifest", func() {
CreateResourceFromFile(namespace, databaseManifest)
databaseObjectName, err = env.GetResourceNameFromYAML(databaseManifest)
Expect(err).NotTo(HaveOccurred())
})
By("ensuring the Database CRD succeeded reconciliation", func() {
// get database object
database = &apiv1.Database{}
databaseNamespacedName := types.NamespacedName{
Namespace: namespace,
Name: databaseObjectName,
}

Eventually(func(g Gomega) {
err := env.Client.Get(env.Ctx, databaseNamespacedName, database)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(database.Status.Ready).Should(BeTrue())
}, 300).WithPolling(10 * time.Second).Should(Succeed())
})

It("keeps the db when Database CRD is removed", func() {
By("remove Database CRD", func() {
Expect(utils.DeleteObject(env, database)).To(Succeed())
})
By("verifying new database has been created with the expected fields", func() {
primaryPodInfo, err := env.GetClusterPrimary(namespace, clusterName)
Expect(err).ToNot(HaveOccurred())

By("verifying db is still existing", func() {
primaryPodInfo, err := env.GetClusterPrimary(namespace, clusterName)
Expect(err).ToNot(HaveOccurred())
assertDatabaseExists(namespace, primaryPodInfo.Name, dbname, true)

assertDatabaseExists(namespace, primaryPodInfo.Name, dbname, true)
})
assertDatabaseHasExpectedFields(namespace, primaryPodInfo.Name, expectedDatabaseFields)
})
})
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)
})

By("removing the Database object", func() {
Expect(utils.DeleteObject(env, database)).To(Succeed())
})

It("maintains the db when Database CRD is removed", func() {
By("remove Database CRD", func() {
Expect(utils.DeleteObject(env, databaseWithDeleteRetainPolicy)).To(Succeed())
})
By("verifying the retention policy in the postgres database", func() {
primaryPodInfo, err := env.GetClusterPrimary(namespace, clusterName)
Expect(err).ToNot(HaveOccurred())

By("verifying db has been removed", func() {
primaryPodInfo, err := env.GetClusterPrimary(namespace, clusterName)
Expect(err).ToNot(HaveOccurred())
assertDatabaseExists(namespace, primaryPodInfo.Name, dbname, retainOnDeletion)
})
}

When("Database CRD reclaim policy is set to retain", func() {
It("can manage a declarative database and release it", func() {
databaseManifest := fixturesDir + "/declarative_databases/database.yaml.template"
shouldPgDatabaseBeRetained := true
// NOTE: the `pg_database` table in Postgres does not contain fields
// for the owner nor the template.
// Its fields are dependent on the version of Postgres, so we pick
// a subset that is available to check even on PG v12
expectedDatabaseFields := apiv1.Database{
Spec: apiv1.DatabaseSpec{
Name: "declarative",
LcCtype: "en_US.utf8",
LcCollate: "C", // this is the default value
Encoding: "0", // corresponds to SQL_ASCII
},
}

assertDatabaseExists(namespace, primaryPodInfo.Name, dbname, false)
})
assertTestDeclarativeDatabase(databaseManifest,
shouldPgDatabaseBeRetained, expectedDatabaseFields)
})
})

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())
})
When("Database CRD reclaim policy is set to delete", func() {
It("can manage a declarative database and delete it in Postgres", func() {
// NOTE: the Postgres database 'declarative' created in the previous spec
// was retained after deletion.
// This manifest adopts the existing database and only changes the retention policy
databaseManifest := fixturesDir +
"/declarative_databases/database-with-delete-reclaim-policy.yaml.template"
shouldPgDatabaseBeRetained := false
expectedDatabaseFields := apiv1.Database{
Spec: apiv1.DatabaseSpec{
Name: "declarative",
LcCtype: "en_US.utf8",
LcCollate: "C", // this is the default value
Encoding: "0", // corresponds to SQL_ASCII
},
}

assertTestDeclarativeDatabase(databaseManifest,
shouldPgDatabaseBeRetained, expectedDatabaseFields)
})
})
})

Context("in a Namespace to be deleted manually", func() {
const (
namespace = "declarative-db-finalizers"
)
var (
err error
clusterName string
databaseObjectName string
)
It("will not prevent the deletion of the namespace with lagging finalizers", func() {
By("setting up the new namespace and cluster", func() {
err = env.CreateNamespace(namespace)
Expect(err).ToNot(HaveOccurred())

clusterName, err = env.GetResourceNameFromYAML(clusterManifest)
Expect(err).ToNot(HaveOccurred())

AssertCreateCluster(namespace, clusterName, clusterManifest, env)
})
By("creating the database", func() {
databaseManifest := fixturesDir +
"/declarative_databases/database-with-delete-reclaim-policy.yaml.template"
databaseObjectName, err = env.GetResourceNameFromYAML(databaseManifest)
Expect(err).NotTo(HaveOccurred())
CreateResourceFromFile(namespace, databaseManifest)
})
By("ensuring the database is reconciled successfully", func() {
// get database object
dbObj := &apiv1.Database{}
databaseNamespacedName := types.NamespacedName{
Namespace: namespace,
Name: databaseObjectName,
}
Eventually(func(g Gomega) {
err := env.Client.Get(env.Ctx, databaseNamespacedName, dbObj)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(dbObj.Status.Ready).Should(BeTrue())
}, 300).WithPolling(10 * time.Second).Should(Succeed())
})
By("deleting the namespace and making sure it succeeds before timeout", func() {
err := env.DeleteNamespaceAndWait(namespace, 30)
Expect(err).ToNot(HaveOccurred())
})
})
})
Expand Down
32 changes: 16 additions & 16 deletions tests/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ 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 tests
LabelBasic = "basic"

// LabelClusterMetadata is a label for selecting cluster-metadata test
// LabelClusterMetadata is a label for selecting cluster-metadata tests
LabelClusterMetadata = "cluster-metadata"

// LabelDeclarativeDatabases is a label for selecting the test exercising declarative databases
// LabelDeclarativeDatabases is a label for selecting the declarative databases test
LabelDeclarativeDatabases = "declarative-databases"

// LabelDisruptive is the string for labelling disruptive tests
LabelDisruptive = "disruptive"

// LabelImportingDatabases is a label for selecting importing-databases test
// LabelImportingDatabases is a label for selecting the importing-databases test
LabelImportingDatabases = "importing-databases"

// LabelMaintenance is a label for selecting maintenance test
// LabelMaintenance is a label for selecting maintenance tests
LabelMaintenance = "maintenance"

// LabelNoOpenshift is the string for labelling tests that don't run on Openshift
// LabelNoOpenshift is the string for selecting tests that don't run on Openshift
LabelNoOpenshift = "no-openshift"

// LabelObservability is a label for selecting observability test
// LabelObservability is a label for selecting observability tests
LabelObservability = "observability"

// LabelOperator is a label for only selecting operator tests
Expand All @@ -53,7 +53,7 @@ const (
// LabelPerformance is the string for labelling performance tests
LabelPerformance = "performance"

// LabelPlugin is a label for selecting plugin test
// LabelPlugin is a label for selecting plugin tests
LabelPlugin = "plugin"

// LabelPodScheduling is a label for selecting pod-scheduling test
Expand All @@ -62,31 +62,31 @@ const (
// LabelPostgresConfiguration is a label for selecting postgres-configuration test
LabelPostgresConfiguration = "postgres-configuration"

// LabelRecovery is a label for selecting recovery test
// LabelRecovery is a label for selecting recovery tests
LabelRecovery = "recovery"

// LabelReplication is a label for selecting replication test
// LabelReplication is a label for selecting replication tests
LabelReplication = "replication"

// LabelSecurity is a label for selecting security test
// LabelSecurity is a label for selecting security tests
LabelSecurity = "security"

// LabelSelfHealing is a label for selecting self-healing test
// LabelSelfHealing is a label for selecting self-healing tests
LabelSelfHealing = "self-healing"

// LabelServiceConnectivity is a label for selecting service connections test
// LabelServiceConnectivity is a label for selecting service connections tests
LabelServiceConnectivity = "service-connectivity"

// LabelSmoke is a label for selecting smoke test
// LabelSmoke is a label for selecting smoke tests
LabelSmoke = "smoke"

// LabelSnapshot is a label for selecting snapshot tests
LabelSnapshot = "snapshot"

// LabelStorage is a label for selecting storage test
// LabelStorage is a label for selecting storage tests
LabelStorage = "storage"

// LabelTablespaces is a label for selecting the tablespaces tests
// LabelTablespaces is a label for selecting the tablespaces test
LabelTablespaces = "tablespaces"

// LabelUpgrade is a label for upgrade tests
Expand Down

0 comments on commit 84dc435

Please sign in to comment.