-
Notifications
You must be signed in to change notification settings - Fork 189
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
base: main
Are you sure you want to change the base?
Conversation
…bernetes objects Signed-off-by: Marc Nuri <[email protected]>
| 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
| } | ||
|
|
||
| // 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
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: