Skip to content

Commit 583f9aa

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

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os"
2626
"os/exec"
2727
"path/filepath"
28+
"strings"
2829
"time"
2930

3031
. "github.com/onsi/ginkgo/v2"
@@ -97,6 +98,12 @@ var _ = Describe("Manager", Ordered, func() {
9798
// After each test, check for failures and collect logs, events,
9899
// and pod descriptions for debugging.
99100
AfterEach(func() {
101+
By("Cleaning up test CronJob resources")
102+
cmd := exec.Command("kubectl", "delete", "-f", "config/samples/batch_v1_cronjob.yaml", "-n", namespace, "--ignore-not-found=true")
103+
_, _ = utils.Run(cmd)
104+
cmd = exec.Command("kubectl", "delete", "-f", "config/samples/batch_v2_cronjob.yaml", "-n", namespace, "--ignore-not-found=true")
105+
_, _ = utils.Run(cmd)
106+
100107
specReport := CurrentSpecReport()
101108
if specReport.Failed() {
102109
By("Fetching controller manager pod logs")
@@ -327,6 +334,34 @@ var _ = Describe("Manager", Ordered, func() {
327334
// fmt.Sprintf(`controller_runtime_reconcile_total{controller="%s",result="success"} 1`,
328335
// strings.ToLower(<Kind>),
329336
// ))
337+
338+
It("Should successfully convert between v1 and v2 versions", func() {
339+
By("Creating a v1 CronJob with sample data")
340+
cmd := exec.Command("kubectl", "apply", "-f", "config/samples/batch_v1_cronjob.yaml", "-n", namespace)
341+
_, err := utils.Run(cmd)
342+
Expect(err).NotTo(HaveOccurred(), "Failed to create v1 CronJob") By("Verifying the v1 CronJob was created")
343+
cmd = exec.Command("kubectl", "get", "cronjobs.v1.batch.tutorial.kubebuilder.io", "-n", namespace, "-o", "jsonpath={.items[0].metadata.name}")
344+
v1Name, err := utils.Run(cmd)
345+
Expect(err).NotTo(HaveOccurred(), "Failed to get v1 CronJob")
346+
Expect(strings.TrimSpace(v1Name)).NotTo(BeEmpty(), "v1 CronJob name should not be empty")
347+
348+
By("Creating a v2 CronJob with sample data")
349+
cmd = exec.Command("kubectl", "apply", "-f", "config/samples/batch_v2_cronjob.yaml", "-n", namespace)
350+
_, err = utils.Run(cmd)
351+
Expect(err).NotTo(HaveOccurred(), "Failed to create v2 CronJob")
352+
353+
By("Verifying the v2 CronJob was created")
354+
cmd = exec.Command("kubectl", "get", "cronjobs.v2.batch.tutorial.kubebuilder.io", "-n", namespace, "-o", "jsonpath={.items[0].metadata.name}")
355+
v2Name, err := utils.Run(cmd)
356+
Expect(err).NotTo(HaveOccurred(), "Failed to get v2 CronJob")
357+
Expect(strings.TrimSpace(v2Name)).NotTo(BeEmpty(), "v2 CronJob name should not be empty")
358+
359+
By("Verifying conversion webhook is active by checking controller logs")
360+
cmd = exec.Command("kubectl", "logs", "-l", "control-plane=controller-manager", "-n", namespace, "--tail=50")
361+
logs, err := utils.Run(cmd)
362+
Expect(err).NotTo(HaveOccurred(), "Failed to get controller logs")
363+
Expect(logs).To(ContainSubstring("cronjob"), "Controller logs should contain cronjob references")
364+
})
330365
})
331366
})
332367

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)