Skip to content

Commit

Permalink
Merge pull request #205 from zzzeek/fix_harness
Browse files Browse the repository at this point in the history
enhance RunURLAssertSuite to support non-DB managing controllers
  • Loading branch information
openshift-merge-bot[bot] authored Mar 8, 2024
2 parents 31e51e9 + a6646de commit 6b04e3e
Showing 1 changed file with 49 additions and 55 deletions.
104 changes: 49 additions & 55 deletions api/test/helpers/harnesses.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,24 @@ func (harness *MariaDBTestHarness) RunBasicSuite() {
}

harness.UpdateAccount(newAccountName)
harness.mariaDBHelper.SimulateMariaDBAccountCompleted(newAccountName)

harness.runAccountUpdateWithWait(oldAccountName, newAccountName)
mariaDBHelper.Logger.Info(
fmt.Sprintf("Service should move to run fully off MariaDBAccount %s and remove finalizer from %s",
newAccountName, oldAccountName),
)

// finalizer is attached to new account
Eventually(func() []string {
newMariadbAccount := mariaDBHelper.GetMariaDBAccount(newAccountName)
return newMariadbAccount.Finalizers
}, timeout, interval).Should(ContainElement(harness.finalizerName))

// finalizer removed from old account
Eventually(func() []string {
oldMariadbAccount := mariaDBHelper.GetMariaDBAccount(oldAccountName)
return oldMariadbAccount.Finalizers
}, timeout, interval).ShouldNot(ContainElement(harness.finalizerName))

// CreateOrPatchDBByName will add a label referring to the database
Eventually(func() string {
Expand Down Expand Up @@ -322,33 +338,36 @@ func (harness *MariaDBTestHarness) RunBasicSuite() {
}

// RunURLAssertSuite asserts that a database URL is set up with the correct
// username and password, and that this is updated when the account changes
// username and password, and that this is updated when the account changes.
// account change is detected via finalizer
func (harness *MariaDBTestHarness) RunURLAssertSuite(assertURL assertsURL) {
When(fmt.Sprintf("The %s service is fully running", harness.description), func() {
BeforeEach(func() {
harness.init()
})

BeforeEach(func() {
mariaDBHelper, timeout, interval := harness.mariaDBHelper, harness.timeout, harness.interval
mariaDBHelper := harness.mariaDBHelper

oldAccountName := types.NamespacedName{
Name: "some-old-account",
Namespace: harness.namespace,
}

k8sClient := mariaDBHelper.K8sClient

// create MariaDBAccount / secret ahead of time, to suit controllers
// that dont directly do EnsureMariaDBAccount
mariadbAccount, mariadbSecret := mariaDBHelper.CreateMariaDBAccountAndSecret(oldAccountName, mariadbv1.MariaDBAccountSpec{})
DeferCleanup(k8sClient.Delete, mariaDBHelper.Ctx, mariadbSecret)
DeferCleanup(k8sClient.Delete, mariaDBHelper.Ctx, mariadbAccount)

// create the CR with old account
harness.SetupCR(oldAccountName)

// also simulate that it got completed
mariaDBHelper.SimulateMariaDBAccountCompleted(oldAccountName)

// wait for finalizer to be set on the account
Eventually(func() []string {
oldMariadbAccount := mariaDBHelper.GetMariaDBAccount(oldAccountName)
return oldMariadbAccount.Finalizers
}, timeout, interval).Should(ContainElement(harness.finalizerName))

})
It("Sets the correct database URL for the MariaDBAccount", func() {
oldAccountName := types.NamespacedName{
Expand All @@ -368,25 +387,24 @@ func (harness *MariaDBTestHarness) RunURLAssertSuite(assertURL assertsURL) {

It("Updates the database URL when the MariaDBAccount changes", func() {

oldAccountName := types.NamespacedName{
Name: "some-old-account",
Namespace: harness.namespace,
}

newAccountName := types.NamespacedName{
Name: "some-new-account",
Namespace: harness.namespace,
}

harness.UpdateAccount(newAccountName)
harness.mariaDBHelper.SimulateMariaDBAccountCompleted(newAccountName)
mariaDBHelper := harness.mariaDBHelper

mariadbAccount := harness.mariaDBHelper.GetMariaDBAccount(newAccountName)
mariadbSecret := harness.mariaDBHelper.GetSecret(types.NamespacedName{Name: mariadbAccount.Spec.Secret, Namespace: mariadbAccount.Namespace})
k8sClient := mariaDBHelper.K8sClient

// create MariaDBAccount / secret ahead of time, to suit controllers
// that dont directly do EnsureMariaDBAccount
mariadbAccount, mariadbSecret := mariaDBHelper.CreateMariaDBAccountAndSecret(newAccountName, mariadbv1.MariaDBAccountSpec{})
DeferCleanup(k8sClient.Delete, mariaDBHelper.Ctx, mariadbSecret)
DeferCleanup(k8sClient.Delete, mariaDBHelper.Ctx, mariadbAccount)

harness.runAccountUpdateWithWait(oldAccountName, newAccountName)
harness.UpdateAccount(newAccountName)
harness.mariaDBHelper.SimulateMariaDBAccountCompleted(newAccountName)

// ensure new URL present
assertURL(
newAccountName,
mariadbAccount.Spec.UserName,
Expand Down Expand Up @@ -422,28 +440,29 @@ func (harness *MariaDBTestHarness) RunConfigHashSuite(getConfigHash getsConfigHa

It("Gets a config hash when the MariaDBAccount is complete", func() {
configHash := getConfigHash()
Expect(configHash).NotTo(Equal(""))
Eventually(func(g Gomega) {
g.Expect(configHash).NotTo(Equal(""))
}).Should(Succeed())

})

It("Updates the config hash when the MariaDBAccount changes", func() {

oldAccountName := types.NamespacedName{
Name: "some-old-account",
Namespace: harness.namespace,
}

newAccountName := types.NamespacedName{
Name: "some-new-account",
Namespace: harness.namespace,
}

oldConfigHash := getConfigHash()

harness.runAccountUpdateWithWait(oldAccountName, newAccountName)
harness.UpdateAccount(newAccountName)
harness.mariaDBHelper.SimulateMariaDBAccountCompleted(newAccountName)

newConfigHash := getConfigHash()
Expect(newConfigHash).NotTo(Equal(""))
Expect(newConfigHash).NotTo(Equal(oldConfigHash))
Eventually(func(g Gomega) {
newConfigHash := getConfigHash()
g.Expect(newConfigHash).NotTo(Equal(""))
g.Expect(newConfigHash).NotTo(Equal(oldConfigHash))
}).Should(Succeed())

})

Expand All @@ -453,28 +472,3 @@ func (harness *MariaDBTestHarness) RunConfigHashSuite(getConfigHash getsConfigHa
func (harness *MariaDBTestHarness) init() {
harness.PopulateHarness(harness)
}

func (harness *MariaDBTestHarness) runAccountUpdateWithWait(oldAccountName types.NamespacedName, newAccountName types.NamespacedName) {
mariaDBHelper, timeout, interval := harness.mariaDBHelper, harness.timeout, harness.interval

harness.UpdateAccount(newAccountName)
harness.mariaDBHelper.SimulateMariaDBAccountCompleted(newAccountName)

mariaDBHelper.Logger.Info(
fmt.Sprintf("Service should move to run fully off MariaDBAccount %s and remove finalizer from %s",
newAccountName, oldAccountName),
)

// finalizer is attached to new account
Eventually(func() []string {
newMariadbAccount := mariaDBHelper.GetMariaDBAccount(newAccountName)
return newMariadbAccount.Finalizers
}, timeout, interval).Should(ContainElement(harness.finalizerName))

// finalizer removed from old account
Eventually(func() []string {
oldMariadbAccount := mariaDBHelper.GetMariaDBAccount(oldAccountName)
return oldMariadbAccount.Finalizers
}, timeout, interval).ShouldNot(ContainElement(harness.finalizerName))

}

0 comments on commit 6b04e3e

Please sign in to comment.