diff --git a/internal/management/controller/database_controller_test.go b/internal/management/controller/database_controller_test.go index 3264dd2240..ecf9d4cd60 100644 --- a/internal/management/controller/database_controller_test.go +++ b/internal/management/controller/database_controller_test.go @@ -116,12 +116,14 @@ 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", @@ -129,6 +131,7 @@ var _ = Describe("Managed Database status", func() { ) 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, @@ -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, @@ -179,14 +183,16 @@ 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", @@ -194,6 +200,7 @@ var _ = Describe("Managed Database status", func() { ) 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, @@ -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 @@ -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 @@ -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, @@ -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, @@ -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", @@ -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 })