From 069adfbf7d17b1ac07c7e7c28dab0f67cbce5dc2 Mon Sep 17 00:00:00 2001 From: Francesco Pantano Date: Sun, 22 Sep 2024 14:27:31 +0200 Subject: [PATCH] Add cronJob envTests This patch improves the existing envTest bucket to include cronJob verification and update. It shows how an update in the Glance top level CR is reflected in an update of the CronJob schedule. In addition, the cronJob name now uses instance.Name instead of hardcoding the ServiceName (if the "unique CR" name feature is used it will reflect the name of the global CR). Signed-off-by: Francesco Pantano --- controllers/glance_controller.go | 2 +- test/functional/base_test.go | 8 +++++++ test/functional/glance_controller_test.go | 21 ++++++++++++++++++- test/functional/glance_test_data.go | 5 +++++ test/kuttl/tests/glance_single/01-assert.yaml | 4 ++-- 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/controllers/glance_controller.go b/controllers/glance_controller.go index 5949c8f9..53e07969 100644 --- a/controllers/glance_controller.go +++ b/controllers/glance_controller.go @@ -1016,7 +1016,7 @@ func (r *GlanceReconciler) ensureDBPurgeJob( ) (ctrl.Result, error) { cronSpec := glance.CronJobSpec{ - Name: fmt.Sprintf("%s-dbpurge", glance.ServiceName), + Name: fmt.Sprintf("%s-db-purge", instance.Name), PvcClaim: nil, Command: glance.GlanceManage, Schedule: instance.Spec.DBPurge.Schedule, diff --git a/test/functional/base_test.go b/test/functional/base_test.go index cc5bb404..fd18f3bc 100644 --- a/test/functional/base_test.go +++ b/test/functional/base_test.go @@ -47,6 +47,14 @@ func GetGlanceAPI(name types.NamespacedName) *glancev1.GlanceAPI { return instance } +func GetCronJob(name types.NamespacedName) *batchv1.CronJob { + cron := &batchv1.CronJob{} + Eventually(func(g Gomega) { + g.Expect(k8sClient.Get(ctx, name, cron)).Should(Succeed()) + }, timeout, interval).Should(Succeed()) + return cron +} + func GlanceConditionGetter(name types.NamespacedName) condition.Conditions { instance := GetGlance(name) return instance.Status.Conditions diff --git a/test/functional/glance_controller_test.go b/test/functional/glance_controller_test.go index e5bb47dc..302707fe 100644 --- a/test/functional/glance_controller_test.go +++ b/test/functional/glance_controller_test.go @@ -297,6 +297,26 @@ var _ = Describe("Glance controller", func() { It("should not have a cache pvc (no imageCacheSize provided)", func() { AssertPVCDoesNotExist(glanceTest.GlanceCache) }) + It("configures DB Purge job", func() { + Eventually(func(g Gomega) { + glance := GetGlance(glanceTest.Instance) + cron := GetCronJob(glanceTest.DBPurgeCronJob) + g.Expect(cron.Spec.Schedule).To(Equal(glance.Spec.DBPurge.Schedule)) + }, timeout, interval).Should(Succeed()) + }) + It("update DB Purge job", func() { + Eventually(func(g Gomega) { + glance := GetGlance(glanceTest.Instance) + glance.Spec.DBPurge.Schedule = "*/30 * * * *" + g.Expect(k8sClient.Update(ctx, glance)).To(Succeed()) + }, timeout, interval).Should(Succeed()) + + Eventually(func(g Gomega) { + glance := GetGlance(glanceTest.Instance) + cron := GetCronJob(glanceTest.DBPurgeCronJob) + g.Expect(cron.Spec.Schedule).To(Equal(glance.Spec.DBPurge.Schedule)) + }, timeout, interval).Should(Succeed()) + }) }) When("Glance CR is deleted", func() { BeforeEach(func() { @@ -487,5 +507,4 @@ var _ = Describe("Glance controller", func() { } mariadbSuite.RunBasicSuite() - }) diff --git a/test/functional/glance_test_data.go b/test/functional/glance_test_data.go index ccbdbfc5..a4178013 100644 --- a/test/functional/glance_test_data.go +++ b/test/functional/glance_test_data.go @@ -85,6 +85,7 @@ type GlanceTestData struct { MemcachedInstance string GlanceMemcached types.NamespacedName KeystoneService types.NamespacedName + DBPurgeCronJob types.NamespacedName } // GetGlanceTestData is a function that initialize the GlanceTestData @@ -219,5 +220,9 @@ func GetGlanceTestData(glanceName types.NamespacedName) GlanceTestData { Namespace: glanceName.Namespace, Name: glance.ServiceName, }, + DBPurgeCronJob: types.NamespacedName{ + Namespace: glanceName.Namespace, + Name: fmt.Sprintf("%s-db-purge", glanceName.Name), + }, } } diff --git a/test/kuttl/tests/glance_single/01-assert.yaml b/test/kuttl/tests/glance_single/01-assert.yaml index 1be4a233..a0293b7c 100644 --- a/test/kuttl/tests/glance_single/01-assert.yaml +++ b/test/kuttl/tests/glance_single/01-assert.yaml @@ -7,7 +7,7 @@ # - glance-internal service # - glance-public service # - glance internal and public endpoints -# - glance-dbpurge cronjob +# - glance-db-purge cronjob apiVersion: glance.openstack.org/v1beta1 kind: Glance @@ -145,7 +145,7 @@ spec: apiVersion: batch/v1 kind: CronJob metadata: - name: glance-dbpurge + name: glance-db-purge spec: jobTemplate: metadata: