-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
🌱 Add composable matchers to internal/testtypes #5259
🌱 Add composable matchers to internal/testtypes #5259
Conversation
cbbc31a
to
7d049e7
Compare
/retest |
controllers/topology/util_test.go
Outdated
@@ -21,9 +21,8 @@ import ( | |||
|
|||
"github.com/pkg/errors" | |||
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" | |||
"sigs.k8s.io/cluster-api/internal/testtypes" | |||
. "sigs.k8s.io/cluster-api/internal/testtypes" |
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.
Moved from the old PR:
@sbueringer sbueringer 4 days ago Member
I'm not a fan of dot imports to be honest :/ (because of the "magic-factor")
I understand why we want it for matchers, I'm not sure if we should also use it for our builder funcs.
@killianmuldoon killianmuldoon 3 days ago Author Member
Yeah - it's a compromise here from having one package with the two different functions. We > are using . imports across our testing for Gomega. Do you have strong feelings about this? I can definitely see both sides.
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.
@killianmuldoon It's fine for me to keep it, if that's the consensus. Overall I would like to be very careful with dot imports. Gomega is basically the only one I ever saw in Go and I think that's a good thing because it makes the code less readable.
An option could be to have the matchers and builders in separate packages and only import the matchers with a dot import (like the gomega matchers).
I would like to hear opinions from @vincepri and @fabriziopandini on that, though.
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 personally ok with this dot import given that it is our own "additional layer" on top of gomega/testing and it makes tests more readable. However, no problem to accept whatever we agree on.
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 definitely agree with having the dot imports for the matchers. It's more the builder funcs and everything else we currently have and will add in the future in the testtypes package.
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 definitely agree there's a danger here - dot-by-default isn't something we want for the entire testtypes package I don't think. We could have testtypes/matchers below it - other than worries about too many small packages is there any issue with doing it that way and only doing dot import on matchers?
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 don't have a concern about a separate package, only wondering if a slightly different structure would make more sense:
internal
testtypes
testmatchers
Or
internal
test
types
matchers
If I understood correctly the matchers are an extension to gomega matchers for CAPI/Kubernetes so they are not really specific to testtypes
they are just also part of our "test library" (so I'm mostly challenging if they should really be a sub-package of testtypes)
@@ -20,7 +20,7 @@ import ( | |||
"strings" | |||
"testing" | |||
|
|||
"sigs.k8s.io/cluster-api/internal/testtypes" | |||
. "sigs.k8s.io/cluster-api/internal/testtypes" | |||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | |||
|
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.
@@ -19,11 +19,8 @@ package topology | |||
import ( | |||
"testing" | |||
|
|||
"github.com/google/go-cmp/cmp/cmpopts" | |||
. "sigs.k8s.io/cluster-api/internal/testtypes" | |||
|
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.
@@ -21,9 +21,8 @@ import ( | |||
"regexp" | |||
"testing" | |||
|
|||
"sigs.k8s.io/cluster-api/internal/testtypes" | |||
. "sigs.k8s.io/cluster-api/internal/testtypes" | |||
|
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.
controllers/topology/util_test.go
Outdated
@@ -21,9 +21,8 @@ import ( | |||
|
|||
"github.com/pkg/errors" | |||
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" | |||
"sigs.k8s.io/cluster-api/internal/testtypes" | |||
. "sigs.k8s.io/cluster-api/internal/testtypes" |
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 personally ok with this dot import given that it is our own "additional layer" on top of gomega/testing and it makes tests more readable. However, no problem to accept whatever we agree on.
internal/testtypes/matchers.go
Outdated
) | ||
|
||
// This code is a modified version of the mergePatch code from the controllers/topology/internal/mergepatch pkg. | ||
// TODO: Create a shared patchdiff library that can be shared for both patching and matching. |
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.
Replicating my comment from the old PR:
Are we sure we want a separate package for that? Sometimes a little bit copying is better then a new package/dependency
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.
Replicating my response 🙃 (sorry I didn't notice the bad branch name earlier!)
Copying is definitely the right way for now - but having this code in multiple places doesn't seem like the right solution in the long term as its usage might grow - especially if mergepatch goes out of internal. I think having an issue for this is a good idea, and we can decide down the line if we want to actually execute on it.
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.
Sorry looked at the wrong place for the response and didn't see it :/
Might be a good idea to open an issue for it. Not sure how the usual "process" is. TODO in the code or issue or both
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 think maybe we leave the TODO out of the code and open an issue - I always feel like a TODO should focus on something more short term, whereas this might not be resolved for a while. An issue (at least with the Kubernetes bots keeping us honest) forces us to make a decision on how to proceed on a regular basis.
I'll definitely open an issue as soon as we've decided the exact form to merge this in - and I'll take out the TODO if noone has any objections.
7d049e7
to
a5fa1c9
Compare
With the new import style we have builder.NewInfrastructureClusterBuilder() which is repetitive and not as nice as it should be. I think we could transform this (and all the other builders) to builder.InfrastructureCluster and it would read much better - @sbueringer @fabriziopandini WDYT? |
a5fa1c9
to
12d7adc
Compare
I've added a commit with the updated builder names - let me know if it looks like the right approach. The only issue I see is with blocks like:
|
lgtm apart a small nit. |
Looks great! |
This commit adds composable matching objects to the internal testtypes package. These types can be used for advanced comparison between objects in the Cluster API unit and integration tests. A simple integration test is included to show the basic working of the matchers.
34f3fd6
to
6ce8913
Compare
@fabriziopandini I think I've hit all the nits now and I've squashed the commits. |
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: fabriziopandini The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/lgtm |
This commit adds composable matching objects to the internal
testtypes package. These types can be used for advanced comparison
between objects in the Cluster API unit and integration tests.
A simple integration test is included to show the basic working of
the matchers.
Fixes #5157
Note: This is a duplicate / update of #5157 - the branch name was not correct ahead for merge history.