-
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
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!
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.45% +0.13%
==========================================
Files 661 663 +2
Lines 54326 54585 +259
==========================================
+ Hits 25163 25358 +195
- Misses 27537 27596 +59
- Partials 1626 1631 +5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Hi @zhzhuang-zju Can you help check whether the changes have met your expectations? /assign |
/lgtm |
pkg/resourceinterpreter/customized/webhook/request/resourceinterpretercontext_test.go
Outdated
Show resolved
Hide resolved
Signed-off-by: Anuj Agrawal <anujagrawal380@gmail.com> Added unit tests for interpretation context handling Signed-off-by: Anuj Agrawal <anujagrawal380@gmail.com> Added unit tests for interpretation context handling Signed-off-by: Anuj Agrawal <anujagrawal380@gmail.com>
f8f3274
to
1575e6a
Compare
New changes are detected. LGTM label has been removed. |
[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 |
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?: