-
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
✨ Add have jsonpath matcher to komega #2174
Conversation
Welcome @matt-simons! |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: matt-simons The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Hi @matt-simons. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
Hey, thanks for proposing this, looks pretty interesting. I'm wondering, have you looked at proposing something like this directly to Gomega? I feel like it could be a useful addition in the wider gomega testing world as well It might be useful if you could compare for us the usage of this, versus some vanilla gomega to see how much simpler this makes things, some concrete examples of usage would be great I'm curious what happens if the json path validation fails, is that an error, or a failure in the code? |
Hi Joel, I hadn't considered contributing it to Gomega itself, I suppose there's no reason why it wouldn't work outside of a Kubernetes use case. The only k8s specific part is getting the UnstructuredContent. I'll reach out to the project to see if there's interest. I'll make an example repo with my fork. Currently if the json path validation fails that is an error, in my mind that would typically mean there's an issue with the test itself as most fields are strongly typed unless it was running against something like ConfigMap data. In that case using a KeyWithValue matcher would always be safe. |
/ok-to-test |
I've created a repo to highlight the benefits here: https://github.com/matt-simons/havejsonpath-example To summarise the convenience gain looks a bit like this: Without id := func(element interface{}) string {
return string(element.(appsv1.DeploymentCondition).Type)
}
Eventually(k.Object(deployment)).Should(HaveField("Status.Conditions",
MatchElements(id, IgnoreExtras, Elements{
"Available": MatchFields(IgnoreExtras, Fields{
"Reason": Equal("MinimumReplicasAvailable"),
}),
}),
)) With Eventually(k.Object(deployment)).Should(HaveJSONPath(
`{.status.conditions[?(@.type=="Available")].reason}`, Equal("MinimumReplicasAvailable")),
) |
Just as another example, you could achieve the same with Eventually(k.Object(deployment)).Should(HaveField("Status.Conditions",
ContainElement(SatisfyAll(
HaveField("Type", Equal("Available")), // Find the condition of type Available
HaveField("Reason", Equal("MinimumReplicasAvailable")), // Check the Reason
)),
)) If this were to fail because the list of conditions doesn't contain a condition of type Available, it will print out the entire conditions object, as the failing matcher is |
That's a much nicer way of writing it. In this example the
It doesn't list out the other conditions we weren't looking for and states that the |
I think we should follow the conversation on the gomega issue and see what the gomega maintainers think of the proposal, my suspicion is that they will suggested to use something like my previous example, as that gives you more specific feedback about the failure, but then, we are looking at only one example, more examples my help to show the value of the new matcher |
Are you aware of the The main thing I dislike about this solution is that it's using a different format than the allow/ignore path descriptions that can be used with EqualObject. The idea there was to make it as effortless to use as possible, allowing both struct names as well as json names to be used. Of course using proper JSON paths is a lot more powerful, but I'm not sure whether that's necessary here. Since your approach is about matching single values, we could also think about a |
The Kubernetes project currently lacks enough contributors to adequately respond to all PRs. This bot triages PRs according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale |
The Kubernetes project currently lacks enough active contributors to adequately respond to all PRs. This bot triages PRs according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle rotten |
The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs. This bot triages PRs according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /close |
@k8s-triage-robot: Closed this PR. In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
This PR adds a new matcher to Komega.
It's intended to be used in conjunction with the
Object
andObjectList
helper functions, transforming the actual based on the jsonpath provided. It works similarly to theHaveField
matcher however has all the familiar benefits of using a JSON path. E.g.{.status.conditions[?(@.type=="Ready")]}
.When the JSON path resolves to a single result the result slice is flattened to a single value for convenience. E.g.
HaveJSONPath("{.metadata.name}", Equal("foo"))
.This is my first time contributing so apologies if I'm missing anything or not following any processes correctly :)