diff --git a/internal/controllers/reconciler_test.go b/internal/controllers/reconciler_test.go index 65f5e27db..f8b56b317 100644 --- a/internal/controllers/reconciler_test.go +++ b/internal/controllers/reconciler_test.go @@ -502,7 +502,7 @@ func (c *controllerTest) commonTests() { Expect(secret.StringData["CRYOSTAT_RJMX_PASS"]).To(Equal(oldSecret.StringData["CRYOSTAT_RJMX_PASS"])) }) }) - Context("with an existing Database Secret", func() { + Context("with an existing database Secret", func() { var cr *model.CryostatInstance var oldSecret *corev1.Secret BeforeEach(func() { @@ -519,7 +519,8 @@ func (c *controllerTest) commonTests() { Expect(err).ToNot(HaveOccurred()) Expect(metav1.IsControlledBy(secret, cr.Object)).To(BeTrue()) - Expect(secret.StringData["CONNECTION_KEY"]).To(Equal(oldSecret.StringData["CONNECTION_KEY"])) + Expect(secret.Data["CONNECTION_KEY"]).To(Equal(oldSecret.Data["CONNECTION_KEY"])) + Expect(secret.Data["ENCRYPTION_KEY"]).To(Equal(oldSecret.Data["ENCRYPTION_KEY"])) }) }) Context("with existing Routes", func() { @@ -1610,7 +1611,7 @@ func (c *controllerTest) commonTests() { It("should configure deployment appropriately", func() { t.expectMainDeployment() }) - It("should set Database Secret in CR Status", func() { + It("should set database Secret in CR Status", func() { instance := t.getCryostatInstance() Expect(instance.Status.DatabaseSecret).To(Equal(customSecret.Name)) }) @@ -1619,7 +1620,7 @@ func (c *controllerTest) commonTests() { err := t.Client.Get(context.Background(), types.NamespacedName{Name: t.Name + "-db", Namespace: t.Namespace}, secret) Expect(kerrors.IsNotFound(err)).To(BeTrue()) }) - Context("with an existing Database Secret", func() { + Context("with an existing generated database secret", func() { BeforeEach(func() { t.objs = append(t.objs, t.NewDatabaseSecret()) }) @@ -1628,6 +1629,54 @@ func (c *controllerTest) commonTests() { err := t.Client.Get(context.Background(), types.NamespacedName{Name: t.Name + "-db", Namespace: t.Namespace}, secret) Expect(err).ToNot(HaveOccurred()) }) + It("should not update database Secret in CR Status", func() { + instance := t.getCryostatInstance() + Expect(instance.Status.DatabaseSecret).To(Equal(customSecret.Name)) + }) + It("should not emit a DatabaseSecretMismatched event", func() { + recorder := t.controller.GetConfig().EventRecorder.(*record.FakeRecorder) + Expect(recorder.Events).ToNot(Receive()) + }) + }) + }) + Context("with secret name modified in database options", func() { + Context("from default secret to custom secret", func() { + BeforeEach(func() { + t.objs = append(t.objs, t.NewCryostat().Object, t.NewCustomDatabaseSecret()) + }) + JustBeforeEach(func() { + t.reconcileCryostatFully() + + cryostat := t.getCryostatInstance() + cryostat.Spec.DatabaseOptions = &operatorv1beta2.DatabaseOptions{ + SecretName: &t.NewCustomDatabaseSecret().Name, + } + + t.updateCryostatInstance(cryostat) + _, err := t.reconcile() + Expect(err).To(HaveOccurred()) + }) + It("should emit a DatabaseSecretMismatched event", func() { + t.expectEvent("DatabaseSecretMismatched") + }) + }) + Context("from custom secret to default secret", func() { + BeforeEach(func() { + t.objs = append(t.objs, t.NewCryostatWithDatabaseSecretProvided().Object, t.NewCustomDatabaseSecret()) + }) + JustBeforeEach(func() { + t.reconcileCryostatFully() + + cryostat := t.getCryostatInstance() + cryostat.Spec.DatabaseOptions.SecretName = nil + + t.updateCryostatInstance(cryostat) + _, err := t.reconcile() + Expect(err).To(HaveOccurred()) + }) + It("should emit a DatabaseSecretMismatched event", func() { + t.expectEvent("DatabaseSecretMismatched") + }) }) }) Context("with an existing Cryostat CR", func() { @@ -2376,7 +2425,7 @@ func (t *cryostatTestInput) expectDatabaseSecret() { // Compare to desired spec expectedSecret := t.NewDatabaseSecret() t.checkMetadata(secret, expectedSecret) - Expect(secret.StringData).To(Equal(expectedSecret.StringData)) + Expect(secret.Data).To(Equal(expectedSecret.Data)) } func (t *cryostatTestInput) expectStorageSecret() { @@ -2387,7 +2436,7 @@ func (t *cryostatTestInput) expectStorageSecret() { // Compare to desired spec expectedSecret := t.NewStorageSecret() t.checkMetadata(secret, expectedSecret) - Expect(secret.StringData).To(Equal(expectedSecret.StringData)) + Expect(secret.Data).To(Equal(expectedSecret.Data)) } func (t *cryostatTestInput) expectJMXSecret() { @@ -2971,3 +3020,10 @@ func (t *cryostatTestInput) expectTargetNamespaces() { cr := t.getCryostatInstance() Expect(*cr.TargetNamespaceStatus).To(ConsistOf(t.TargetNamespaces)) } + +func (t *cryostatTestInput) expectEvent(msg string) { + recorder := t.controller.GetConfig().EventRecorder.(*record.FakeRecorder) + var eventMsg string + Expect(recorder.Events).To(Receive(&eventMsg)) + Expect(eventMsg).To(ContainSubstring(msg)) +}