Skip to content

Commit 07b5c5e

Browse files
(ci): Fix mock for alpha update e2e tests
1 parent cf5dbb3 commit 07b5c5e

File tree

1 file changed

+58
-42
lines changed

1 file changed

+58
-42
lines changed

test/e2e/alphaupdate/update_test.go

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,35 @@ const (
3636
toVersion = "v4.6.0"
3737

3838
// Binary patterns for cleanup
39-
binFromVersionPattern = "/tmp/kubebuilder" + fromVersion + "-*"
40-
binToVersionPattern = "/tmp/kubebuilder" + toVersion + "-*"
39+
binFromVersionPath = "/tmp/kubebuilder" + fromVersion + "-*"
40+
pathBinToVersion = "/tmp/kubebuilder" + toVersion + "-*"
41+
42+
controllerImplementation = `// Fetch the TestOperator instance
43+
testOperator := &webappv1.TestOperator{}
44+
err := r.Get(ctx, req.NamespacedName, testOperator)
45+
if err != nil {
46+
if errors.IsNotFound(err) {
47+
log.Info("testOperator resource not found. Ignoring since object must be deleted")
48+
return ctrl.Result{}, nil
49+
}
50+
log.Error(err, "Failed to get testOperator")
51+
return ctrl.Result{}, err
52+
}
53+
54+
log.Info("testOperator reconciled")`
4155
)
4256

4357
var _ = Describe("kubebuilder", func() {
4458
Context("alpha update", func() {
4559
var (
4660
mockProjectDir string
47-
binFromVersionPath string
61+
pathBinFromVersion string
4862
kbc *utils.TestContext
4963
)
5064

5165
BeforeEach(func() {
5266
var err error
53-
By("setting up test context with current kubebuilder binary")
67+
By("setting up test context with binary build from source")
5468
kbc, err = utils.NewTestContext(pluginutil.KubebuilderBinName, "GO111MODULE=on")
5569
Expect(err).NotTo(HaveOccurred())
5670
Expect(kbc.Prepare()).To(Succeed())
@@ -60,20 +74,19 @@ var _ = Describe("kubebuilder", func() {
6074
Expect(err).NotTo(HaveOccurred())
6175

6276
By("downloading kubebuilder v4.5.2 binary to isolated /tmp directory")
63-
binFromVersionPath, err = downloadKubebuilder()
77+
pathBinFromVersion, err = downloadKubebuilder()
6478
Expect(err).NotTo(HaveOccurred())
6579
})
6680

6781
AfterEach(func() {
6882
By("cleaning up test artifacts")
69-
7083
_ = os.RemoveAll(mockProjectDir)
71-
_ = os.RemoveAll(filepath.Dir(binFromVersionPath))
84+
_ = os.RemoveAll(filepath.Dir(pathBinFromVersion))
7285

7386
// Clean up kubebuilder alpha update downloaded binaries
7487
binaryPatterns := []string{
75-
binFromVersionPattern,
76-
binToVersionPattern,
88+
pathBinFromVersion,
89+
pathBinToVersion,
7790
}
7891

7992
for _, pattern := range binaryPatterns {
@@ -83,18 +96,16 @@ var _ = Describe("kubebuilder", func() {
8396
}
8497
}
8598

86-
// Clean up TestContext
87-
if kbc != nil {
88-
kbc.Destroy()
89-
}
99+
_ = os.RemoveAll(kbc.Dir)
90100
})
91101

92-
It("should update project from v4.5.2 to v4.6.0 preserving custom code", func() {
102+
It("should update project from v4.5.2 to v4.6.0 without conflicts", func() {
93103
By("creating mock project with kubebuilder v4.5.2")
94-
createMockProject(mockProjectDir, binFromVersionPath)
104+
createMockProject(mockProjectDir, pathBinFromVersion)
95105

96-
By("injecting custom code in API and controller")
97-
injectCustomCode(mockProjectDir)
106+
By("adding custom code in API and controller")
107+
updateAPI(mockProjectDir)
108+
updateController(mockProjectDir)
98109

99110
By("initializing git repository and committing mock project")
100111
initializeGitRepo(mockProjectDir)
@@ -181,38 +192,43 @@ func createMockProject(projectDir, binaryPath string) {
181192
Expect(err).NotTo(HaveOccurred())
182193
}
183194

184-
func injectCustomCode(projectDir string) {
195+
func updateController(projectDir string) {
196+
controllerFile := filepath.Join(projectDir, "internal", "controller", "testoperator_controller.go")
197+
198+
err := pluginutil.ReplaceInFile(
199+
controllerFile,
200+
"_ = logf.FromContext(ctx)",
201+
"log := logf.FromContext(ctx)",
202+
)
203+
Expect(err).NotTo(HaveOccurred())
204+
205+
err = pluginutil.ReplaceInFile(
206+
controllerFile,
207+
"// TODO(user): your logic here",
208+
controllerImplementation,
209+
)
210+
Expect(err).NotTo(HaveOccurred())
211+
}
212+
213+
func updateAPI(projectDir string) {
185214
typesFile := filepath.Join(projectDir, "api", "v1", "testoperator_types.go")
186-
err := pluginutil.InsertCode(
215+
err := pluginutil.ReplaceInFile(
187216
typesFile,
188217
"Foo string `json:\"foo,omitempty\"`",
189218
`
190219
// +kubebuilder:validation:Minimum=0
191220
// +kubebuilder:validation:Maximum=3
192221
// +kubebuilder:default=1
193-
// Size is the size of the memcached deployment
194222
Size int32 `+"`json:\"size,omitempty\"`",
195223
)
196-
Expect(err).NotTo(HaveOccurred())
197-
controllerFile := filepath.Join(projectDir, "internal", "controller", "testoperator_controller.go")
198-
err = pluginutil.InsertCode(
199-
controllerFile,
200-
"// TODO(user): your logic here",
201-
`// Custom reconciliation logic
202-
log := ctrl.LoggerFrom(ctx)
203-
log.Info("Reconciling TestOperator")
224+
Expect(err).NotTo(HaveOccurred(), "Failed to update testoperator_types.go")
204225

205-
// Fetch the TestOperator instance
206-
testOperator := &webappv1.TestOperator{}
207-
err := r.Get(ctx, req.NamespacedName, testOperator)
208-
if err != nil {
209-
return ctrl.Result{}, client.IgnoreNotFound(err)
210-
}
211-
212-
// Custom logic: log the size field
213-
log.Info("TestOperator size", "size", testOperator.Spec.Size)`,
226+
err = pluginutil.ReplaceInFile(
227+
typesFile,
228+
"Foo is an example field of TestOperator. Edit wordpress_types.go to remove/update",
229+
"size is an example field of TestOperator",
214230
)
215-
Expect(err).NotTo(HaveOccurred())
231+
Expect(err).NotTo(HaveOccurred(), "Failed to replace Foo with size in testoperator_types.go")
216232
}
217233

218234
func initializeGitRepo(projectDir string) {
@@ -270,16 +286,16 @@ func runAlphaUpdate(projectDir string, kbc *utils.TestContext) {
270286
}
271287

272288
func validateCustomCodePreservation(projectDir string) {
289+
By("validating the API")
273290
typesFile := filepath.Join(projectDir, "api", "v1", "testoperator_types.go")
274291
content, err := os.ReadFile(typesFile)
275292
Expect(err).NotTo(HaveOccurred())
276293
Expect(string(content)).To(ContainSubstring("Size int32 `json:\"size,omitempty\"`"))
277-
Expect(string(content)).To(ContainSubstring("Size is the size of the memcached deployment"))
294+
Expect(string(content)).To(ContainSubstring("size is an example field of TestOperator"))
278295

296+
By("validating the Controller")
279297
controllerFile := filepath.Join(projectDir, "internal", "controller", "testoperator_controller.go")
280298
content, err = os.ReadFile(controllerFile)
281299
Expect(err).NotTo(HaveOccurred())
282-
Expect(string(content)).To(ContainSubstring("Custom reconciliation logic"))
283-
Expect(string(content)).To(ContainSubstring("log.Info(\"Reconciling TestOperator\")"))
284-
Expect(string(content)).To(ContainSubstring("log.Info(\"TestOperator size\", \"size\", testOperator.Spec.Size)"))
300+
Expect(string(content)).To(ContainSubstring(controllerImplementation))
285301
}

0 commit comments

Comments
 (0)