Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

anujagrawal699
Copy link
Contributor

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:

  1. pkg/resourceinterpreter/customized/webhook/request/resourceinterpretercontext_test.go

Test Coverage:

  1. pkg/resourceinterpreter/customized/webhook/request/resourceinterpretercontext.go: 0% to 90.80%

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?:

NONE

@karmada-bot karmada-bot added the kind/feature Categorizes issue or PR as related to a new feature. label Nov 20, 2024
@karmada-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign ikaven1024 for approval. For more information see the Kubernetes Code Review Process.

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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@karmada-bot karmada-bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Nov 20, 2024
Comment on lines +34 to +39
const (
testAPIVersion = "apps/v1"
testKind = "Deployment"
testName = "test-deployment"
testNamespace = "default"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const (
testAPIVersion = "apps/v1"
testKind = "Deployment"
testName = "test-deployment"
testNamespace = "default"
)
var (
testAPIVersion = "apps/v1"
testKind = "Deployment"
testName = "test-deployment"
testNamespace = "default"
)

Copy link
Member

@chaosi-zju chaosi-zju Nov 22, 2024

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.

Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor

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

Copy link
Member

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.

Comment on lines 105 to 111
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

Copy link
Contributor Author

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 (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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]>
@anujagrawal699 anujagrawal699 force-pushed the addedTests-pkg/resourceinterpreter/customized/webhook/request/resourceinterpretercontext.go branch from 6e61b1f to f8f3274 Compare November 22, 2024 09:04
@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 46.52%. Comparing base (2c82055) to head (f8f3274).
Report is 23 commits behind head on master.

❗ 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              
Flag Coverage Δ
unittests 46.52% <ø> (+0.20%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants