-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
✨ komega: add EqualObject matcher #1833
✨ komega: add EqualObject matcher #1833
Conversation
1c5ed04
to
c28e47d
Compare
/lgtm This looks useful from a testing perspective, code is clean, happy to have this merged if others are |
/retest |
Thank you very much! At a first glance it looks good. @killianmuldoon If you have some time, can you please take a look and test if we can use the matcher as is in our tests? (just to make sure there is nothing we're missing by just reviewing) |
c28e47d
to
cf8bf2e
Compare
cf8bf2e
to
b611d0e
Compare
I've just tested it briefly by replacing the import and it seems like it can be used as a drop-in replacement when no custom IgnorePaths are specified (only once so far, and that's a one line change). |
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 the new way of doing IgnorePaths doesn't work on unstructured.Unstructured objects. When I plug this into our tests as a drop in I get failures across these tests
The issue is that paths for unstructured appear as map[string]interface{} so we get something like
the following fields were expected to match but did not:
[Object["metadata"]["annotations"] Object["metadata"]["resourceVersion"]]
This is why the library was using json to compare in Cluster API, so as is this doesn't work as a drop in replacement when using unstructured objects.
I think there's a couple of ways to solve this:
- Go back to generic json comparisons
- Add handling for filtering paths so that it handles both the e.g.
ObjectMeta.ResourceVersion
case and theObject[\"metadata\"][\"resourceVersion\"]
, case
I think the second option fits best with what you're trying to do here and would turn this into a drop in replacement. I'm excited to remove the matchers from Cluster API 😆 so thanks for that @schrej
Then I didn't test thoroughly enough. Should we add another set of default ignores for Unstructured, or do we just add those to the existing one, so it's an actual drop-in replacement? We could also allow to use single quotes in the path, which would make writing tests that involve maps easier. |
I think it might be better to handle the replicas when doing the |
I think we can't infer json paths automatically from field names. This will probably work with the builtin metadata fields, but this doesn't work generically. I think it's even hard to do with metadata:
I guess we can do something by going over JSON tags with reflection but not sure if it's worth the complexity Might be good enough to just add the metadata paths for unstructured and "real" objects to |
I've tried to do so, but wasn't able to find a way quickly. But I've had an idea yesterday evening, so let's see if that works out. |
b611d0e
to
7938d06
Compare
After quite a while I finally got around to working on this again. It's now using a bit of reflection and supports both, go types and yaml. For unstructured only the latter is really useful. I also tested it against sigs.k8s.io/cluster-api/internal/controllers/topology/cluster and it seems to work. But it requires to use a more generic "metadata.annotations" ignore rule. This is because of unstructured.Unstructured not even containing |
7938d06
to
d86c00b
Compare
ab4f064
to
5099a25
Compare
I've added all the test cases from the "old" Matcher now. All work, with the exception of three: I've looked for a solution, but haven't found any. I'm also not sure if it's worth investing too much time into it. I've kept the tests as comments, maybe we'll be able to improve it in the future. @killianmuldoon could you take another look? |
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.
Very nice!
Sorry for the late response.
Just that I understood the limitation correctly. Example:
original has ".spec.parent.child"
modified only has ".spec"
If we want to ignore this diff we have to ignore the entire .spec and not only e.g. .spec.parent.child because the comparison will stop at .spec as modified doesn't have parent/child?
No. You can ignore |
def3332
to
c37d3a0
Compare
Makes sense, thx! Not sure if it's necessary in this repo, but let's squash and then merge |
Co-authored-by: killianmuldoon <[email protected]> Co-authored-by: Stefan Bueringer <[email protected]>
c37d3a0
to
8fe64fa
Compare
Doesn't hurt to squash. Was only split in two commits so it's easier to see what changed from your implementation. @JoelSpeed could you also take another look? |
Makes sense. Thank you very much!! |
Looks good thanks /lgtm |
/approve cc @alvaroaleman (we need top-level approval for the go.mod change) |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: alvaroaleman, sbueringer, schrej 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 |
This adds an EqualObject matcher to the new komega package which allows to compare objects more conveniently.
It's based on the implementation in cluster-api/internal/test/matchers by @killianmuldoon and @sbueringer but the actual comparison logic is completely rewritten. The reason was the lack of support for arrays/lists, which occur quite frequently (e.g. owner references) and my attempt to add array support wasn't that pretty.
It's now using google's cmp library with a custom reporter. It still allows to ignore and filter the matched paths. The API is mostly the same, with the exception of how paths are specified. Instead of providing the paths as arrays, they can now be written in Go syntax. Both type field names as well as json/yaml names are supported.
CC @fabriziopandini, since you suggested moving it to controller-runtime
/assign @sbueringer