Skip to content

Commit

Permalink
test(secrets): env tests for secret updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tthvo committed May 24, 2024
1 parent d89deb2 commit 3fecc9e
Showing 1 changed file with 62 additions and 6 deletions.
68 changes: 62 additions & 6 deletions internal/controllers/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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))
})
Expand All @@ -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())
})
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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))
}

0 comments on commit 3fecc9e

Please sign in to comment.