Skip to content

Commit e79777c

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

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

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

Lines changed: 30 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,35 @@ var _ = Describe("Manager", Ordered, func() {
327328
// fmt.Sprintf(`controller_runtime_reconcile_total{controller="%s",result="success"} 1`,
328329
// strings.ToLower(<Kind>),
329330
// ))
331+
It("Should successfully convert between v1 and v2 versions", func() {
332+
By("Creating a v1 CronJob with sample data")
333+
cmd := exec.Command("kubectl", "apply", "-f", "config/samples/batch_v1_cronjob.yaml", "-n", namespace)
334+
_, err := utils.Run(cmd)
335+
Expect(err).NotTo(HaveOccurred(), "Failed to create v1 CronJob")
336+
337+
By("Verifying the v1 CronJob was created")
338+
cmd = exec.Command("kubectl", "get", "cronjobs.v1.batch.tutorial.kubebuilder.io", "-n", namespace, "-o", "jsonpath={.items[0].metadata.name}")
339+
v1Name, err := utils.Run(cmd)
340+
Expect(err).NotTo(HaveOccurred(), "Failed to get v1 CronJob")
341+
Expect(strings.TrimSpace(v1Name)).NotTo(BeEmpty(), "v1 CronJob name should not be empty")
342+
343+
By("Creating a v2 CronJob with sample data")
344+
cmd = exec.Command("kubectl", "apply", "-f", "config/samples/batch_v2_cronjob.yaml", "-n", namespace)
345+
_, err = utils.Run(cmd)
346+
Expect(err).NotTo(HaveOccurred(), "Failed to create v2 CronJob")
347+
348+
By("Verifying the v2 CronJob was created")
349+
cmd = exec.Command("kubectl", "get", "cronjobs.v2.batch.tutorial.kubebuilder.io", "-n", namespace, "-o", "jsonpath={.items[0].metadata.name}")
350+
v2Name, err := utils.Run(cmd)
351+
Expect(err).NotTo(HaveOccurred(), "Failed to get v2 CronJob")
352+
Expect(strings.TrimSpace(v2Name)).NotTo(BeEmpty(), "v2 CronJob name should not be empty")
353+
354+
By("Verifying conversion webhook is active by checking controller logs")
355+
cmd = exec.Command("kubectl", "logs", "-l", "control-plane=controller-manager", "-n", namespace, "--tail=50")
356+
logs, err := utils.Run(cmd)
357+
Expect(err).NotTo(HaveOccurred(), "Failed to get controller logs")
358+
Expect(logs).To(ContainSubstring("cronjob"), "Controller logs should contain cronjob references")
359+
})
330360
})
331361
})
332362

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

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

0 commit comments

Comments
 (0)