Skip to content

Commit 720feed

Browse files
authored
Merge pull request #4994 from camilamacedo86/improve-scearios-e2e-alpha-update
🌱 (e2e) Make alpha update tests easier to understand and more complete
2 parents 276c237 + 4b1680f commit 720feed

File tree

1 file changed

+95
-22
lines changed

1 file changed

+95
-22
lines changed

test/e2e/alphaupdate/update_test.go

Lines changed: 95 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package alphaupdate
1818

1919
import (
20+
"bytes"
2021
"fmt"
2122
"io"
2223
"net/http"
@@ -98,12 +99,18 @@ var _ = Describe("kubebuilder", func() {
9899
By("cleaning up test artifacts")
99100
_ = os.RemoveAll(filepath.Dir(pathBinFromVersion))
100101
_ = os.RemoveAll(kbc.Dir)
102+
kbc.Destroy()
101103
})
102104

103105
It("should update project from v4.5.2 to v4.6.0 without conflicts", func() {
106+
By("running alpha update from v4.5.2 to v4.6.0")
104107
runAlphaUpdate(kbc.Dir, kbc, toVersion, false)
108+
109+
By("checking that custom code is preserved")
105110
validateCustomCodePreservation(kbc.Dir)
106-
validateConflictMarkers(kbc.Dir, false)
111+
112+
By("checking that no conflict markers are present in the project files")
113+
Expect(hasConflictMarkers(kbc.Dir)).To(BeFalse())
107114

108115
By("checking that go module is upgraded")
109116
validateCommonGoModule(kbc.Dir)
@@ -113,14 +120,52 @@ var _ = Describe("kubebuilder", func() {
113120
})
114121

115122
It("should update project from v4.5.2 to v4.7.0 with --force flag and create conflict markers", func() {
123+
By("modifying original Makefile to use CONTROLLER_TOOLS_VERSION v0.17.3")
124+
modifyMakefileControllerTools(kbc.Dir, "v0.17.3")
125+
116126
By("running alpha update to v4.7.0 with --force flag")
117127
runAlphaUpdate(kbc.Dir, kbc, toVersionWithConflict, true)
118128

119-
By("checking that markers for conflicts are present")
120-
validateConflictMarkers(kbc.Dir, true)
129+
By("checking that custom code is preserved")
130+
validateCustomCodePreservation(kbc.Dir)
121131

122-
By("checking that go module is upgraded")
132+
By("checking that conflict markers are present in the project files")
133+
Expect(hasConflictMarkers(kbc.Dir)).To(BeTrue())
134+
135+
By("checking that go module is upgraded to expected versions")
123136
validateCommonGoModule(kbc.Dir)
137+
138+
By("checking that Makefile is updated and has conflict between old and new versions in Makefile")
139+
makefilePath := filepath.Join(kbc.Dir, "Makefile")
140+
content, err := os.ReadFile(makefilePath)
141+
Expect(err).NotTo(HaveOccurred(), "Failed to read Makefile after update")
142+
makefileStr := string(content)
143+
144+
// Should update to the new version
145+
Expect(makefileStr).To(ContainSubstring(`GOLANGCI_LINT_VERSION ?= v2.1.6`))
146+
147+
// The original project was scaffolded with v0.17.2 (from v4.5.2).
148+
// The user manually updated it to v0.17.3.
149+
// The target upgrade version (v4.7.0) introduces v0.18.0.
150+
//
151+
// Because both the user's version (v0.17.3) and the scaffold version (v0.18.0) differ,
152+
// we expect Git to insert conflict markers around this line in the Makefile:
153+
//
154+
// <<<<<<< HEAD
155+
// CONTROLLER_TOOLS_VERSION ?= v0.18.0
156+
// =======
157+
// CONTROLLER_TOOLS_VERSION ?= v0.17.3
158+
// >>>>>>> tmp-original-*
159+
Expect(makefileStr).To(ContainSubstring("<<<<<<<"),
160+
"Expected conflict marker <<<<<<< in Makefile")
161+
Expect(makefileStr).To(ContainSubstring("======="),
162+
"Expected conflict separator ======= in Makefile")
163+
Expect(makefileStr).To(ContainSubstring(">>>>>>>"),
164+
"Expected conflict marker >>>>>>> in Makefile")
165+
Expect(makefileStr).To(ContainSubstring("CONTROLLER_TOOLS_VERSION ?= v0.17.3"),
166+
"Expected original user version in conflict")
167+
Expect(makefileStr).To(ContainSubstring("CONTROLLER_TOOLS_VERSION ?= v0.18.0"),
168+
"Expected latest scaffold version in conflict")
124169
})
125170

126171
It("should stop when updating the project from v4.5.2 to v4.7.0 without the flag force", func() {
@@ -130,6 +175,9 @@ var _ = Describe("kubebuilder", func() {
130175
By("validating that merge stopped with conflicts requiring manual resolution")
131176
validateConflictState(kbc.Dir)
132177

178+
By("checking that custom code is preserved")
179+
validateCustomCodePreservation(kbc.Dir)
180+
133181
By("checking that go module is upgraded")
134182
validateCommonGoModule(kbc.Dir)
135183
})
@@ -147,6 +195,28 @@ var _ = Describe("kubebuilder", func() {
147195
})
148196
})
149197

198+
func modifyMakefileControllerTools(projectDir, newVersion string) {
199+
makefilePath := filepath.Join(projectDir, "Makefile")
200+
oldLine := "CONTROLLER_TOOLS_VERSION ?= v0.17.2"
201+
newLine := fmt.Sprintf("CONTROLLER_TOOLS_VERSION ?= %s", newVersion)
202+
203+
By("replacing the controller-tools version in the Makefile")
204+
Expect(util.ReplaceInFile(makefilePath, oldLine, newLine)).
205+
To(Succeed(), "Failed to update CONTROLLER_TOOLS_VERSION in Makefile")
206+
207+
By("committing the Makefile change to simulate user customization")
208+
cmds := [][]string{
209+
{"git", "add", "Makefile"},
210+
{"git", "commit", "-m", fmt.Sprintf("User modified CONTROLLER_TOOLS_VERSION to %s", newVersion)},
211+
}
212+
for _, args := range cmds {
213+
cmd := exec.Command(args[0], args[1:]...)
214+
cmd.Dir = projectDir
215+
output, err := cmd.CombinedOutput()
216+
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("Git command failed: %s", output))
217+
}
218+
}
219+
150220
func validateMakefileContent(projectDir string) {
151221
makefilePath := filepath.Join(projectDir, "Makefile")
152222
content, err := os.ReadFile(makefilePath)
@@ -283,34 +353,37 @@ func validateCustomCodePreservation(projectDir string) {
283353
Expect(string(controllerContent)).To(ContainSubstring(controllerImplementation))
284354
}
285355

286-
func validateConflictMarkers(projectDir string, expectMarkers bool) {
287-
files := []string{
288-
filepath.Join(projectDir, "api", "v1", "testoperator_types.go"),
289-
filepath.Join(projectDir, "internal", "controller", "testoperator_controller.go"),
290-
}
291-
found := false
292-
for _, file := range files {
293-
content, err := os.ReadFile(file)
294-
if err != nil {
295-
continue
356+
func hasConflictMarkers(projectDir string) bool {
357+
hasMarker := false
358+
359+
err := filepath.Walk(projectDir, func(path string, info os.FileInfo, err error) error {
360+
if err != nil || info.IsDir() {
361+
return nil
362+
}
363+
364+
content, readErr := os.ReadFile(path)
365+
if readErr != nil || bytes.Contains(content, []byte{0}) {
366+
return nil // skip unreadable or binary files
296367
}
368+
297369
if strings.Contains(string(content), "<<<<<<<") {
298-
found = true
299-
break
370+
hasMarker = true
371+
return fmt.Errorf("conflict marker found in %s", path) // short-circuit early
300372
}
373+
return nil
374+
})
375+
376+
if err != nil && hasMarker {
377+
return true
301378
}
302-
if expectMarkers {
303-
Expect(found).To(BeTrue())
304-
} else {
305-
Expect(found).To(BeFalse())
306-
}
379+
return false
307380
}
308381

309382
func validateConflictState(projectDir string) {
310383
By("validating merge stopped with conflicts requiring manual resolution")
311384

312385
// 1. Check file contents for conflict markers
313-
validateConflictMarkers(projectDir, true)
386+
Expect(hasConflictMarkers(projectDir)).To(BeTrue())
314387

315388
// 2. Check Git status for conflict-tracked files (UU = both modified)
316389
cmd := exec.Command("git", "status", "--porcelain")

0 commit comments

Comments
 (0)