-
Notifications
You must be signed in to change notification settings - Fork 190
feat(test): add utilities for JSONPath-like access to unstructured Kubernetes objects #519
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
Conversation
…bernetes objects Signed-off-by: Marc Nuri <marc@marcnuri.com>
| return nil, false | ||
| } | ||
| val, exists := m[segment.field] | ||
| if !exists { |
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.
@manusa I think you still need a nil check on val here as well
internal/test/unstructured.go
Outdated
| } | ||
|
|
||
| // FieldInt retrieves an integer field from an unstructured object using JSONPath-like notation. | ||
| // Returns 0 if the field is not found or is not an integer type (int, int64, int32). |
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.
@manusa I'm not 100% sure this behaviour is ideal - there could be scenarios where I'm testing and expecting to receive a value of 0 - for example status.replicas. How would we be able to distinguish between the value actually being 0 vs. not being set/not being an int?
Or do you think that's fine because this is for tests? In that case I'm fine with it but let's add a note in the docstring about ensuring that you don't assert expecting an actual value to be 0
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.
For that purpose you would use test.FieldExists(vm, "spec.instancetype") and expect it to be false
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'll need to revisit for this case for the expecting to be 0
| // FieldString retrieves a string field from an unstructured object using JSONPath-like notation. | ||
| // Examples: | ||
| // - "spec.runStrategy" | ||
| // - "spec.template.spec.volumes[0].containerDisk.image" | ||
| // - "spec.dataVolumeTemplates[0].spec.sourceRef.kind" |
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.
Same as my comment about FieldInt - it would be nice to be able to tell the difference between not set and set to ""
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.
For that purpose you would use test.FieldExists(vm, "spec.instancetype") and expect it to be false
…bernetes objects (review) Signed-off-by: Marc Nuri <marc@marcnuri.com>
|
I've added the nil-check. For the edge cases you mention, there is both I've added some godoc to the simplified accessors so that users are aware of the limitation. |
Cali0707
left a comment
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.
LGTM, thanks @manusa
The tests are way more readable now 😄
This PR adds test utility functions that provide JSONPath-like field access for
unstructured.Unstructuredobjects.The primary goal is to improve test assertion readability by replacing deeply-nested type assertions with clean, concise path-based field access.
Readability Improvement
Before:
After: