Skip to content

Commit

Permalink
test: improve comments
Browse files Browse the repository at this point in the history
Signed-off-by: Niccolò Fei <[email protected]>
  • Loading branch information
NiccoloFei committed Oct 22, 2024
1 parent f3def85 commit 2a23de3
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions internal/management/controller/database_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,22 @@ var _ = Describe("Managed Database status", func() {

It("adds finalizer and sets status ready on success", func(ctx SpecContext) {
Expect(database.Finalizers).To(BeEmpty())

// Mocking DetectDB
expectedValue := sqlmock.NewRows([]string{""}).AddRow("0")
dbMock.ExpectQuery(`SELECT count(*)
FROM pg_database
WHERE datname = $1`).WithArgs(database.Spec.Name).WillReturnRows(expectedValue)

// Mocking CreateDB
expectedCreate := sqlmock.NewResult(0, 1)
expectedQuery := fmt.Sprintf(
"CREATE DATABASE %s OWNER %s",
pgx.Identifier{database.Spec.Name}.Sanitize(), pgx.Identifier{database.Spec.Owner}.Sanitize(),
)
dbMock.ExpectExec(expectedQuery).WillReturnResult(expectedCreate)

// Reconcile and get the updated object
_, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{
Namespace: database.Namespace,
Name: database.Name,
Expand Down Expand Up @@ -162,6 +165,7 @@ var _ = Describe("Managed Database status", func() {
)
dbMock.ExpectExec(expectedQuery).WillReturnError(expectedError)

// Reconcile and get the updated object
_, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{
Namespace: database.Namespace,
Name: database.Name,
Expand All @@ -179,21 +183,24 @@ var _ = Describe("Managed Database status", func() {
Expect(updatedDatabase.Status.Error).Should(ContainSubstring(expectedError.Error()))
})

It("on deleting it removes finalizers and drops DB", func(ctx SpecContext) {
It("on deletion it removes finalizers and drops DB", func(ctx SpecContext) {
Expect(database.Finalizers).To(BeEmpty())

// Mocking DetectDB
expectedValue := sqlmock.NewRows([]string{""}).AddRow("0")
dbMock.ExpectQuery(`SELECT count(*)
FROM pg_database
WHERE datname = $1`).WithArgs(database.Spec.Name).WillReturnRows(expectedValue)

// Mocking CreateDB
expectedCreate := sqlmock.NewResult(0, 1)
expectedQuery := fmt.Sprintf(
"CREATE DATABASE %s OWNER %s",
pgx.Identifier{database.Spec.Name}.Sanitize(), pgx.Identifier{database.Spec.Owner}.Sanitize(),
)
dbMock.ExpectExec(expectedQuery).WillReturnResult(expectedCreate)

// Reconcile and get the updated object
_, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{
Namespace: database.Namespace,
Name: database.Name,
Expand All @@ -209,7 +216,6 @@ var _ = Describe("Managed Database status", func() {

Expect(updatedDatabase.Status.Ready).Should(BeTrue())
Expect(updatedDatabase.Status.Error).Should(BeEmpty())

Expect(updatedDatabase.Finalizers).NotTo(BeEmpty())

// the next 3 lines are a hacky bit to make sure the next reconciler
Expand All @@ -221,8 +227,8 @@ var _ = Describe("Managed Database status", func() {
Expect(fakeClient.Status().Patch(ctx, &updatedDatabase, client.MergeFrom(currentDatabase))).To(Succeed())

// We now look at the behavior when we delete the Database object

Expect(fakeClient.Delete(ctx, database)).To(Succeed())

// the Database object is Deleted, but its finalizer prevents removal from
// the API
var fadingDatabase apiv1.Database
Expand All @@ -240,6 +246,7 @@ var _ = Describe("Managed Database status", func() {
)
dbMock.ExpectExec(expectedDrop).WillReturnResult(sqlmock.NewResult(0, 1))

// Reconcile and get the updated object
_, err = r.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{
Namespace: database.Namespace,
Name: database.Name,
Expand Down Expand Up @@ -273,10 +280,13 @@ var _ = Describe("Managed Database status", func() {
Scheme: schemeBuilder.BuildWithAllKnownScheme(),
instance: &f,
}

// patching the Database object to reference the newly created Cluster
originalDatabase := database.DeepCopy()
database.Spec.ClusterRef.Name = "cluster-other"
Expect(fakeClient.Patch(ctx, database, client.MergeFrom(originalDatabase))).To(Succeed())

// Reconcile and get the updated object
_, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{
Namespace: database.Namespace,
Name: database.Name,
Expand All @@ -295,8 +305,7 @@ var _ = Describe("Managed Database status", func() {
})

It("skips reconciliation if database object isn't found (deleted database)", func(ctx SpecContext) {
// since the fakeClient has the `cluster-example` cluster, let's reference
// another cluster `cluster-other` that is not found by the fakeClient
// Initialize a new Database but without creating it in the K8S Cluster
otherDatabase := &apiv1.Database{
ObjectMeta: metav1.ObjectMeta{
Name: "db-other",
Expand All @@ -312,10 +321,13 @@ var _ = Describe("Managed Database status", func() {
},
}

// Reconcile the database that hasn't been created in the K8S Cluster
result, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{
Namespace: otherDatabase.Namespace,
Name: otherDatabase.Name,
}})

// Expect the reconciler to exit silently since the object doesn't exist
Expect(err).ToNot(HaveOccurred())
Expect(result).Should(BeZero()) // nothing to do, since the DB is being deleted
})
Expand Down

0 comments on commit 2a23de3

Please sign in to comment.