Skip to content

Commit

Permalink
chore: review
Browse files Browse the repository at this point in the history
Signed-off-by: Niccolò Fei <[email protected]>
  • Loading branch information
NiccoloFei authored and mnencia committed Oct 22, 2024
1 parent 972d0f7 commit d6728a7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 63 deletions.
79 changes: 16 additions & 63 deletions tests/e2e/declarative_database_management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,9 @@ var _ = Describe("Declarative database management", Label(tests.LabelSmoke, test
})
})

assertDatabaseExists := func(namespace, primaryPod, dbname string, shouldContain bool) {
Eventually(func(g Gomega) {
stdout, _, err := env.ExecQueryInInstancePod(
utils.PodLocator{
Namespace: namespace,
PodName: primaryPod,
},
"postgres",
"\\l")
g.Expect(err).ToNot(HaveOccurred())
if shouldContain {
g.Expect(stdout).Should(ContainSubstring(dbname))
} else {
g.Expect(stdout).ShouldNot(ContainSubstring(dbname))
}
}, 300).Should(Succeed())
}

assertDatabaseHasExpectedFields := func(namespace, primaryPod string, db apiv1.Database) {
query := fmt.Sprintf("select count(*) from pg_database where datname = '%s' "+
"and encoding = %s and datctype = '%s' and datcollate = '%s'",
"and encoding = pg_char_to_encoding('%s') and datctype = '%s' and datcollate = '%s'",
db.Spec.Name, db.Spec.Encoding, db.Spec.LcCtype, db.Spec.LcCollate)
Eventually(func(g Gomega) {
stdout, _, err := env.ExecQueryInInstancePod(
Expand All @@ -107,10 +89,9 @@ var _ = Describe("Declarative database management", Label(tests.LabelSmoke, test
assertTestDeclarativeDatabase := func(
databaseManifest string,
retainOnDeletion bool,
expectedDatabaseFields apiv1.Database,
) {
var (
database *apiv1.Database
database apiv1.Database
databaseObjectName string
)
By("applying Database CRD manifest", func() {
Expand All @@ -120,14 +101,14 @@ var _ = Describe("Declarative database management", Label(tests.LabelSmoke, test
})
By("ensuring the Database CRD succeeded reconciliation", func() {
// get database object
database = &apiv1.Database{}
database = apiv1.Database{}
databaseNamespacedName := types.NamespacedName{
Namespace: namespace,
Name: databaseObjectName,
}

Eventually(func(g Gomega) {
err := env.Client.Get(env.Ctx, databaseNamespacedName, database)
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())
Expand All @@ -137,64 +118,36 @@ var _ = Describe("Declarative database management", Label(tests.LabelSmoke, test
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)
assertDatabaseHasExpectedFields(namespace, primaryPodInfo.Name, database)
})

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

By("verifying the retention policy in the postgres database", func() {
primaryPodInfo, err := env.GetClusterPrimary(namespace, clusterName)
Expect(err).ToNot(HaveOccurred())

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

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

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)
false)
})
})

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"
assertTestDeclarativeDatabase(databaseManifest, true)
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ metadata:
spec:
name: declarative
owner: app
lc_ctype: C
lc_collate: C
encoding: UTF8
databaseReclaimPolicy: delete
cluster:
name: cluster-with-declarative-databases
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ spec:
name: declarative
owner: app
lc_ctype: "en_US.utf8"
lc_collate: C
encoding: SQL_ASCII
template: template0
cluster:
Expand Down

0 comments on commit d6728a7

Please sign in to comment.