Skip to content

Commit 6b04e3e

Browse files
Merge pull request #205 from zzzeek/fix_harness
enhance RunURLAssertSuite to support non-DB managing controllers
2 parents 31e51e9 + a6646de commit 6b04e3e

File tree

1 file changed

+49
-55
lines changed

1 file changed

+49
-55
lines changed

api/test/helpers/harnesses.go

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,24 @@ func (harness *MariaDBTestHarness) RunBasicSuite() {
250250
}
251251

252252
harness.UpdateAccount(newAccountName)
253+
harness.mariaDBHelper.SimulateMariaDBAccountCompleted(newAccountName)
253254

254-
harness.runAccountUpdateWithWait(oldAccountName, newAccountName)
255+
mariaDBHelper.Logger.Info(
256+
fmt.Sprintf("Service should move to run fully off MariaDBAccount %s and remove finalizer from %s",
257+
newAccountName, oldAccountName),
258+
)
259+
260+
// finalizer is attached to new account
261+
Eventually(func() []string {
262+
newMariadbAccount := mariaDBHelper.GetMariaDBAccount(newAccountName)
263+
return newMariadbAccount.Finalizers
264+
}, timeout, interval).Should(ContainElement(harness.finalizerName))
265+
266+
// finalizer removed from old account
267+
Eventually(func() []string {
268+
oldMariadbAccount := mariaDBHelper.GetMariaDBAccount(oldAccountName)
269+
return oldMariadbAccount.Finalizers
270+
}, timeout, interval).ShouldNot(ContainElement(harness.finalizerName))
255271

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

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

332349
BeforeEach(func() {
333-
mariaDBHelper, timeout, interval := harness.mariaDBHelper, harness.timeout, harness.interval
350+
mariaDBHelper := harness.mariaDBHelper
334351

335352
oldAccountName := types.NamespacedName{
336353
Name: "some-old-account",
337354
Namespace: harness.namespace,
338355
}
339356

357+
k8sClient := mariaDBHelper.K8sClient
358+
359+
// create MariaDBAccount / secret ahead of time, to suit controllers
360+
// that dont directly do EnsureMariaDBAccount
361+
mariadbAccount, mariadbSecret := mariaDBHelper.CreateMariaDBAccountAndSecret(oldAccountName, mariadbv1.MariaDBAccountSpec{})
362+
DeferCleanup(k8sClient.Delete, mariaDBHelper.Ctx, mariadbSecret)
363+
DeferCleanup(k8sClient.Delete, mariaDBHelper.Ctx, mariadbAccount)
364+
340365
// create the CR with old account
341366
harness.SetupCR(oldAccountName)
342367

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

346-
// wait for finalizer to be set on the account
347-
Eventually(func() []string {
348-
oldMariadbAccount := mariaDBHelper.GetMariaDBAccount(oldAccountName)
349-
return oldMariadbAccount.Finalizers
350-
}, timeout, interval).Should(ContainElement(harness.finalizerName))
351-
352371
})
353372
It("Sets the correct database URL for the MariaDBAccount", func() {
354373
oldAccountName := types.NamespacedName{
@@ -368,25 +387,24 @@ func (harness *MariaDBTestHarness) RunURLAssertSuite(assertURL assertsURL) {
368387

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

371-
oldAccountName := types.NamespacedName{
372-
Name: "some-old-account",
373-
Namespace: harness.namespace,
374-
}
375-
376390
newAccountName := types.NamespacedName{
377391
Name: "some-new-account",
378392
Namespace: harness.namespace,
379393
}
380394

381-
harness.UpdateAccount(newAccountName)
382-
harness.mariaDBHelper.SimulateMariaDBAccountCompleted(newAccountName)
395+
mariaDBHelper := harness.mariaDBHelper
383396

384-
mariadbAccount := harness.mariaDBHelper.GetMariaDBAccount(newAccountName)
385-
mariadbSecret := harness.mariaDBHelper.GetSecret(types.NamespacedName{Name: mariadbAccount.Spec.Secret, Namespace: mariadbAccount.Namespace})
397+
k8sClient := mariaDBHelper.K8sClient
398+
399+
// create MariaDBAccount / secret ahead of time, to suit controllers
400+
// that dont directly do EnsureMariaDBAccount
401+
mariadbAccount, mariadbSecret := mariaDBHelper.CreateMariaDBAccountAndSecret(newAccountName, mariadbv1.MariaDBAccountSpec{})
402+
DeferCleanup(k8sClient.Delete, mariaDBHelper.Ctx, mariadbSecret)
403+
DeferCleanup(k8sClient.Delete, mariaDBHelper.Ctx, mariadbAccount)
386404

387-
harness.runAccountUpdateWithWait(oldAccountName, newAccountName)
405+
harness.UpdateAccount(newAccountName)
406+
harness.mariaDBHelper.SimulateMariaDBAccountCompleted(newAccountName)
388407

389-
// ensure new URL present
390408
assertURL(
391409
newAccountName,
392410
mariadbAccount.Spec.UserName,
@@ -422,28 +440,29 @@ func (harness *MariaDBTestHarness) RunConfigHashSuite(getConfigHash getsConfigHa
422440

423441
It("Gets a config hash when the MariaDBAccount is complete", func() {
424442
configHash := getConfigHash()
425-
Expect(configHash).NotTo(Equal(""))
443+
Eventually(func(g Gomega) {
444+
g.Expect(configHash).NotTo(Equal(""))
445+
}).Should(Succeed())
446+
426447
})
427448

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

430-
oldAccountName := types.NamespacedName{
431-
Name: "some-old-account",
432-
Namespace: harness.namespace,
433-
}
434-
435451
newAccountName := types.NamespacedName{
436452
Name: "some-new-account",
437453
Namespace: harness.namespace,
438454
}
439455

440456
oldConfigHash := getConfigHash()
441457

442-
harness.runAccountUpdateWithWait(oldAccountName, newAccountName)
458+
harness.UpdateAccount(newAccountName)
459+
harness.mariaDBHelper.SimulateMariaDBAccountCompleted(newAccountName)
443460

444-
newConfigHash := getConfigHash()
445-
Expect(newConfigHash).NotTo(Equal(""))
446-
Expect(newConfigHash).NotTo(Equal(oldConfigHash))
461+
Eventually(func(g Gomega) {
462+
newConfigHash := getConfigHash()
463+
g.Expect(newConfigHash).NotTo(Equal(""))
464+
g.Expect(newConfigHash).NotTo(Equal(oldConfigHash))
465+
}).Should(Succeed())
447466

448467
})
449468

@@ -453,28 +472,3 @@ func (harness *MariaDBTestHarness) RunConfigHashSuite(getConfigHash getsConfigHa
453472
func (harness *MariaDBTestHarness) init() {
454473
harness.PopulateHarness(harness)
455474
}
456-
457-
func (harness *MariaDBTestHarness) runAccountUpdateWithWait(oldAccountName types.NamespacedName, newAccountName types.NamespacedName) {
458-
mariaDBHelper, timeout, interval := harness.mariaDBHelper, harness.timeout, harness.interval
459-
460-
harness.UpdateAccount(newAccountName)
461-
harness.mariaDBHelper.SimulateMariaDBAccountCompleted(newAccountName)
462-
463-
mariaDBHelper.Logger.Info(
464-
fmt.Sprintf("Service should move to run fully off MariaDBAccount %s and remove finalizer from %s",
465-
newAccountName, oldAccountName),
466-
)
467-
468-
// finalizer is attached to new account
469-
Eventually(func() []string {
470-
newMariadbAccount := mariaDBHelper.GetMariaDBAccount(newAccountName)
471-
return newMariadbAccount.Finalizers
472-
}, timeout, interval).Should(ContainElement(harness.finalizerName))
473-
474-
// finalizer removed from old account
475-
Eventually(func() []string {
476-
oldMariadbAccount := mariaDBHelper.GetMariaDBAccount(oldAccountName)
477-
return oldMariadbAccount.Finalizers
478-
}, timeout, interval).ShouldNot(ContainElement(harness.finalizerName))
479-
480-
}

0 commit comments

Comments
 (0)