-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testenv test refactor #221
Testenv test refactor #221
Conversation
80fbcbd
to
a76397d
Compare
Expect(err).NotTo(HaveOccurred()) | ||
Expect(head.String()).NotTo(Equal(headHash)) | ||
}) | ||
// TODO: Implement adding request annotation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding this, it looks like this test was changed in 4fde199#diff-1168fadffa18bd096582ae7f8b6db744fd896bd5600ee1d1ac6ac4474af251b9L292-L334 which removed the actual steps of this test case.
I couldn't figure out why this was removed from the commit and the associated PR.
Thought of adding it separately and leaving a TODO for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep that's pretty random. The tests got rearranged in that commit so they could be run with both git implementations; all I can think of it is that I killed those lines, expecting to yank them elsewhere, and neglected to do the second part.
TODO for me to check: controllers/update_test.go These use testWithRepoAndImagePolicy:
The following are run by looping over (git repo URL)
controllers/git_test.go (already in normal form) controllers/git_error_test.go (already in normal form) pkg/test/files_test.go
pkg/update/filereader_test.go
pkg/update/result_test.go
pkg/update/update_test.go
|
To be honest, I prefer the previous formulation (using ginkgo), which was more readable. To wit: I'm finding it difficult to check whether the same things are tested -- it's fairly obvious in the ginkgo version because the setting up and tearing down is separate to the assertions, and the assertions are separate to each other. Sometimes the reliance of Ginkgo on closures makes it hard to see what's going on, it's true. But flattening it all out is two steps backward for one step forward, I reckon. If ginkgo absolutely must be removed, then I'd like at least to see some of the same structure preserved, where setup/teardown code is shared. |
I had that feeling too initially while refactoring it and felt like it'd be better to restructure the tests in a completely different way. But being my first contribution to this code base, I tried to stick to the existing test layout as much as possible to keep the changes familiar and avoid dropping something entirely different. Was hoping to restructure more later once we move completely away from ginkgo and as I get more familiar with the code base. I believe it can be refactored in ways to make it better with helpers for common tasks. The checklist above would be very helpful in restructuring it and ensuring all the checks are retained. Now that we know what a direct conversion of the tests look like, I can give it another try with something different to help make it more readable and easy to maintain 🙂 . |
It is not a strict requirement (nothing really is – I am just trying to advise and align). However, the kustomize-controller which has been ported awhile ago and seen multiple attributions since, has proven that once the tests are restructured it is much easier to add new tests, especially for new contributors. |
I think we can get something better than both ginkgo and "flat" tests, by factoring out some bits (which I found awkward with ginkgo) and using closures judiciously to share setup code. The particular file I was looking at when I wrote the comment above was controllers/update_test.go, which mixes a lot of what were individual tests together into one long section in TestImageAutomation_e2e after that uses a table appropriately, I think -- though it's still very long and could surely have some bits factored out (this was true of the ginkgo code too, it's not a new problem :-). |
5fa19d9
to
4351713
Compare
This is still not finished, but any feedback in the new structure would be very helpful. Most of the checks in the checklist for update_test.go should be clear and easy to verify with this change. I would recommend reading https://github.com/fluxcd/image-automation-controller/blob/435171316d0f331590e8047559aa7e1ec95ca5e2/controllers/update_test.go instead of the diff. To help myself, I've added a lot of comments. Will remove them once I'm finished. |
4351713
to
aa65971
Compare
This is ready for another review. I hope this is much easier than before to read. But I'm open for more restructuring and simplification. The DCO check is failing with:
Is it okay if I edit @squaremo 's commit signoff message myself? |
aa65971
to
2c169c3
Compare
That was me doing the PR in a directory where I hadn't explicitly set the commit author, sorry about that. Probably better if the author is corrected (to "Michael Bridgen [email protected]", rather than the sign-off. |
8ad81bb
to
6c9d377
Compare
Changed it. Thanks. |
It looks like this scheme is working well in controllers/update_test.go. The first few tests do exactly the same setup (I imagine this is why you mentioned more factoring-out!); perhaps you could use a callback: func TestSignedCommit(t *testing.T) {
testWithRepoAndImagePolicy(NewWithT(t), func(tmp, localRepo) {
policyKey := types.NamespacedName{
Name: imagePolicyName,
Namespace: namespace,
}
commitInRepo(g, repoURL, branch, "Install setter marker", func(tmp string) {
g.Expect(replaceMarker(tmp, policyKey)).To(Succeed())
})
// ...
})
} (I've glossed over a lot of detail there of course, it's possible that the callback would need too many arguments) |
4fa0932
to
e02a671
Compare
Consolidated the common test setup code into Ready for a review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm satisfied that the rewritten suite contains the same tests (thanks for making that pretty easy to check!). The new structure, with subtests sharing setup and common funcs factored out, is at least as good as the structure when using ginkgo I think, and has less magic and fewer lines 👍
I made a PR with a couple of suggestions, but I don't consider them prerequisites to merging this PR.
Thanks for persisting with this work, Sunny 🍍
Add new testenv based TestMain in suite_test.go and move the ginkgo test setup to legacy_suite_test.go. This helps to run both the ginkgo tests and testenv based tests. Signed-off-by: Sunny <[email protected]>
Signed-off-by: Sunny <[email protected]>
Signed-off-by: Sunny <[email protected]>
All the tests use testenv. Remove legacy envtest suite_test. Signed-off-by: Sunny <[email protected]>
In controller-runtime v0.10.0, the client is updated to clean any stale data in the target object when performing any operation. This results in test failure for the code that constructs an object with both spec and status, and creates the object and updates status it with the same object. The fix is to set the status separately on the object before updating it. Refer: kubernetes-sigs/controller-runtime#1640 Signed-off-by: Sunny <[email protected]>
Two steps: 1. TestDiffDirectories did not check if the expected only return value was correct; the intention was there to do so (judging by the comment "change in order"), but my eye for detail failed me. 2. Reversing the directory comparison in the test revealed bugs in the comparison code -- in general, it should skip any directory that is not a directory in the comparator. To make this easier, the code now keeps track of the expected files it saw. That means the check for whether an actual file has an expected counterpart only has two cases, yes or no (rather that trying to account for whether it's a directory and so on). If a directory was skipped while scanning the expected files, it won't be in the actual files anyway. Signed-off-by: Michael Bridgen <[email protected]>
Restructures the tests in update_test.go to separate the individual checks into separate tests with helpers for common operations. Signed-off-by: Sunny <[email protected]>
testWithRepoAndImagePolicy() contains common code to create a git server, git repository and ImagePolicy for the test setup. Also updates some test structure slightly. Signed-off-by: Sunny <[email protected]>
This is just a cosmetic thing -- there's no need for the higher-order func used to create mini test suites, given the protocol and git implementation. This made the Gingko version clearer, arguably, but it can be reduced away here for a bit less nesting. Signed-off-by: Michael Bridgen <[email protected]>
This tidies the random string testWithRepoAndImagePolicy() arguments into a struct, which reduces clutter (and the chance of getting them in the wrong order) in invocations. Signed-off-by: Michael Bridgen <[email protected]>
f1f674a
to
9a23ef8
Compare
Rebased, merged the readability improvements by @squaremo and updated as per the last few suggestions to make the e2e tests more independent. |
9a23ef8
to
8b96c15
Compare
In TestImageUpdateAutomation_e2e, move the ImagePolicy to be created per subtest and not shared in the common test environment. This makes the tests more independent of each other. Signed-off-by: Sunny <[email protected]>
Introduces testenv based tests.
Refactors all the existing tests to use testenv.
reconcilers-dev
.