Skip to content

Commit 6d8d9f7

Browse files
committed
feat: add e2e test to validate webhook conversion between versions
1 parent b77b135 commit 6d8d9f7

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

docs/book/src/multiversion-tutorial/testdata/project/test/e2e/e2e_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"os/exec"
2727
"path/filepath"
2828
"time"
29+
"strings"
2930

3031
. "github.com/onsi/ginkgo/v2"
3132
. "github.com/onsi/gomega"
@@ -327,6 +328,43 @@ var _ = Describe("Manager", Ordered, func() {
327328
// fmt.Sprintf(`controller_runtime_reconcile_total{controller="%s",result="success"} 1`,
328329
// strings.ToLower(<Kind>),
329330
// ))
331+
Context("Webhook Conversion between CronJob v1 and v2", func() {
332+
It("Should successfully convert between v1 and v2 versions", func() {
333+
By("Creating a v1 CronJob with sample data")
334+
cmd := exec.Command("kubectl", "apply", "-f", "config/samples/batch_v1_cronjob.yaml", "-n", namespace)
335+
_, err := utils.Run(cmd)
336+
Expect(err).NotTo(HaveOccurred(), "Failed to create v1 CronJob")
337+
338+
By("Verifying the v1 CronJob was created")
339+
cmd = exec.Command("kubectl", "get", "cronjobs.v1.batch.tutorial.kubebuilder.io", "-n", namespace, "-o", "jsonpath={.items[0].metadata.name}")
340+
v1Name, err := utils.Run(cmd)
341+
Expect(err).NotTo(HaveOccurred(), "Failed to get v1 CronJob")
342+
Expect(strings.TrimSpace(v1Name)).NotTo(BeEmpty(), "v1 CronJob name should not be empty")
343+
344+
By("Creating a v2 CronJob with sample data")
345+
cmd = exec.Command("kubectl", "apply", "-f", "config/samples/batch_v2_cronjob.yaml", "-n", namespace)
346+
_, err = utils.Run(cmd)
347+
Expect(err).NotTo(HaveOccurred(), "Failed to create v2 CronJob")
348+
349+
By("Verifying the v2 CronJob was created")
350+
cmd = exec.Command("kubectl", "get", "cronjobs.v2.batch.tutorial.kubebuilder.io", "-n", namespace, "-o", "jsonpath={.items[0].metadata.name}")
351+
v2Name, err := utils.Run(cmd)
352+
Expect(err).NotTo(HaveOccurred(), "Failed to get v2 CronJob")
353+
Expect(strings.TrimSpace(v2Name)).NotTo(BeEmpty(), "v2 CronJob name should not be empty")
354+
355+
By("Verifying conversion webhook is active by checking controller logs")
356+
cmd = exec.Command("kubectl", "logs", "-l", "control-plane=controller-manager", "-n", namespace, "--tail=50")
357+
logs, err := utils.Run(cmd)
358+
Expect(err).NotTo(HaveOccurred(), "Failed to get controller logs")
359+
Expect(logs).To(ContainSubstring("cronjob"), "Controller logs should contain cronjob references")
360+
361+
By("Cleaning up test resources")
362+
cmd = exec.Command("kubectl", "delete", "-f", "config/samples/batch_v1_cronjob.yaml", "-n", namespace, "--ignore-not-found=true")
363+
_, _ = utils.Run(cmd)
364+
cmd = exec.Command("kubectl", "delete", "-f", "config/samples/batch_v2_cronjob.yaml", "-n", namespace, "--ignore-not-found=true")
365+
_, _ = utils.Run(cmd)
366+
})
367+
})
330368
})
331369
})
332370

hack/docs/internal/multiversion-tutorial/generate_multiversion.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func (sp *Sample) UpdateTutorial() {
111111
sp.updateConversionFiles()
112112
sp.updateSampleV2()
113113
sp.updateMain()
114+
sp.updateE2EWebhookConversion()
114115
}
115116

116117
func (sp *Sample) updateCronjobV1DueForce() {
@@ -790,3 +791,68 @@ func (sp *Sample) CodeGen() {
790791
err = sp.ctx.EditHelmPlugin()
791792
hackutils.CheckError("Failed to enable helm plugin", err)
792793
}
794+
795+
const webhookConversionE2ETest = `
796+
Context("Webhook Conversion between CronJob v1 and v2", func() {
797+
It("Should successfully convert between v1 and v2 versions", func() {
798+
By("Creating a v1 CronJob with sample data")
799+
cmd := exec.Command("kubectl", "apply", "-f", "config/samples/batch_v1_cronjob.yaml", "-n", namespace)
800+
_, err := utils.Run(cmd)
801+
Expect(err).NotTo(HaveOccurred(), "Failed to create v1 CronJob")
802+
803+
By("Verifying the v1 CronJob was created")
804+
cmd = exec.Command("kubectl", "get", "cronjobs.v1.batch.tutorial.kubebuilder.io", "-n", namespace, "-o", "jsonpath={.items[0].metadata.name}")
805+
v1Name, err := utils.Run(cmd)
806+
Expect(err).NotTo(HaveOccurred(), "Failed to get v1 CronJob")
807+
Expect(strings.TrimSpace(v1Name)).NotTo(BeEmpty(), "v1 CronJob name should not be empty")
808+
809+
By("Creating a v2 CronJob with sample data")
810+
cmd = exec.Command("kubectl", "apply", "-f", "config/samples/batch_v2_cronjob.yaml", "-n", namespace)
811+
_, err = utils.Run(cmd)
812+
Expect(err).NotTo(HaveOccurred(), "Failed to create v2 CronJob")
813+
814+
By("Verifying the v2 CronJob was created")
815+
cmd = exec.Command("kubectl", "get", "cronjobs.v2.batch.tutorial.kubebuilder.io", "-n", namespace, "-o", "jsonpath={.items[0].metadata.name}")
816+
v2Name, err := utils.Run(cmd)
817+
Expect(err).NotTo(HaveOccurred(), "Failed to get v2 CronJob")
818+
Expect(strings.TrimSpace(v2Name)).NotTo(BeEmpty(), "v2 CronJob name should not be empty")
819+
820+
By("Verifying conversion webhook is active by checking controller logs")
821+
cmd = exec.Command("kubectl", "logs", "-l", "control-plane=controller-manager", "-n", namespace, "--tail=50")
822+
logs, err := utils.Run(cmd)
823+
Expect(err).NotTo(HaveOccurred(), "Failed to get controller logs")
824+
Expect(logs).To(ContainSubstring("cronjob"), "Controller logs should contain cronjob references")
825+
826+
By("Cleaning up test resources")
827+
cmd = exec.Command("kubectl", "delete", "-f", "config/samples/batch_v1_cronjob.yaml", "-n", namespace, "--ignore-not-found=true")
828+
_, _ = utils.Run(cmd)
829+
cmd = exec.Command("kubectl", "delete", "-f", "config/samples/batch_v2_cronjob.yaml", "-n", namespace, "--ignore-not-found=true")
830+
_, _ = utils.Run(cmd)
831+
})
832+
})`
833+
834+
func (sp *Sample) updateE2EWebhookConversion() {
835+
cronjobE2ETest := filepath.Join(sp.ctx.Dir, "test", "e2e", "e2e_test.go")
836+
837+
// Add strings import if not already present
838+
err := pluginutil.InsertCodeIfNotExist(cronjobE2ETest,
839+
` "os/exec"
840+
"path/filepath"
841+
"time"`,
842+
`
843+
"strings"`)
844+
hackutils.CheckError("adding strings import for e2e test", err)
845+
846+
// Add webhook conversion test after the existing TODO comment
847+
err = pluginutil.InsertCode(cronjobE2ETest,
848+
` // TODO: Customize the e2e test suite with scenarios specific to your project.
849+
// Consider applying sample/CR(s) and check their status and/or verifying
850+
// the reconciliation by using the metrics, i.e.:
851+
// metricsOutput := getMetricsOutput()
852+
// Expect(metricsOutput).To(ContainSubstring(
853+
// fmt.Sprintf(`+"`"+`controller_runtime_reconcile_total{controller="%s",result="success"} 1`+"`"+`,
854+
// strings.ToLower(<Kind>),
855+
// ))`,
856+
webhookConversionE2ETest)
857+
hackutils.CheckError("adding webhook conversion e2e test", err)
858+
}

0 commit comments

Comments
 (0)