-
Notifications
You must be signed in to change notification settings - Fork 890
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
Added unit tests for interpretation context handling #5850
base: master
Are you sure you want to change the base?
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: 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 |
const ( | ||
testAPIVersion = "apps/v1" | ||
testKind = "Deployment" | ||
testName = "test-deployment" | ||
testNamespace = "default" | ||
) |
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.
const ( | |
testAPIVersion = "apps/v1" | |
testKind = "Deployment" | |
testName = "test-deployment" | |
testNamespace = "default" | |
) | |
var ( | |
testAPIVersion = "apps/v1" | |
testKind = "Deployment" | |
testName = "test-deployment" | |
testNamespace = "default" | |
) |
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.
why change const
to var
? I think you need to give a reason for the 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.
Thank you for pointing out this issue, my bad. I have searched some specification documents and did not find restrictions on defining const
inside functions. It's just relatively rare for me. Therefore, this comment can be disregarded unless there is a corresponding specification found.
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.
In my humble opinion, whether const
or var
has nothing to do with location, but depend on whether you want the value to be modified.
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.
Thanks for your confirm, agree
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 guess @zhzhuang-zju is concerned that different test cases have the same constants but they can't be shared. Constants we usually use are global, and variables don't have to worry about this.
You can see that we have the same constants such as testName and testNamespace in the test file.
assert.NotNil(t, ctx) | ||
requestJSON, err := json.Marshal(request) | ||
assert.NoError(t, err) | ||
assert.Contains(t, string(requestJSON), testName) | ||
assert.Contains(t, string(requestJSON), testNamespace) | ||
assert.Contains(t, string(requestJSON), testAPIVersion) | ||
assert.Contains(t, string(requestJSON), testKind) |
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.
assert.NotNil(t, ctx) | |
requestJSON, err := json.Marshal(request) | |
assert.NoError(t, err) | |
assert.Contains(t, string(requestJSON), testName) | |
assert.Contains(t, string(requestJSON), testNamespace) | |
assert.Contains(t, string(requestJSON), testAPIVersion) | |
assert.Contains(t, string(requestJSON), testKind) | |
// Verify the object is not nil | |
assert.NotNil(t, ctx) | |
assert.Equal(t, testName, ctx.Request.Name) | |
assert.Equal(t, testNamespace, ctx.Request.Namespace) | |
assert.Equal(t, testAPIVersion, ctx.Request.Kind.Group+ "/" +ctx.Request.Kind.Version) | |
assert.Equal(t, testKind, ctx.Request.Kind.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.
Good Suggestion!
} | ||
|
||
func TestCreateV1alpha1ResourceInterpreterContext(t *testing.T) { | ||
const ( |
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.
const ( | |
var ( |
func TestVerifyResourceInterpreterContextWithPatch(t *testing.T) { | ||
const ( | ||
validPatch = `[{"op": "add", "path": "/spec/replicas", "value": 3}]` | ||
malformedPatch = `[{"op": "add", "path": "/spec/replicas", "value": 3}` // missing closing bracket |
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.
malformedPatch
seems haven‘t been used by the following test case
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 remove it.
|
||
// Additional validation for successful cases | ||
if err == nil && tt.response.Patch != nil { | ||
// Verify patch is valid JSON if present |
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.
cc @XiShanYongYe-Chang Do you think it's necessary to add this check in the func verifyResourceInterpreterContextWithPatch
as well?
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 we should remove it as verifyResourceInterpreterContextWithPatch
function doesn't actually validate JSON.
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.
But if this is a valid check, we could also consider adding it to verifyResourceInterpreterContextWithPatch
.
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.
Let's wait for @XiShanYongYe-Chang 's suggestion.
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.
That's a good question, and I understand it's actually asking if we should advance the validation of patches to method verifyResourceInterpreterContextWithPatch
, because we only verify them when we're using them.
I think if we want to modify this logic, we can do it in a separate pr to allow the error to leak out earlier, which is two things from the current new tests and can be handled separately.
In addition, I think this extra validation could be removed, and it's superfluous for the current method.
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.
Okay @XiShanYongYe-Chang , Thanks!
Signed-off-by: Anuj Agrawal <[email protected]> Added unit tests for interpretation context handling Signed-off-by: Anuj Agrawal <[email protected]>
6e61b1f
to
f8f3274
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #5850 +/- ##
==========================================
+ Coverage 46.31% 46.52% +0.20%
==========================================
Files 661 661
Lines 54326 54400 +74
==========================================
+ Hits 25163 25311 +148
+ Misses 27537 27463 -74
Partials 1626 1626
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚨 Try these New Features:
|
Description:
This PR adds comprehensive unit tests for the interpretation context handling. These tests ensure robust functionality of webhook response processing and validation for various interpreter operations.
Additions:
Test Coverage:
What type of PR is this?
/kind feature
Which issue(s) this PR fixes:
Fixes a part of #5470
Does this PR introduce a user-facing change?: