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 test for namespace controller #5368

Conversation

anujagrawal699
Copy link
Contributor

Description:
This PR introduces tests for namespace controller and adds a test file that significantly enchances its code coverage.

Additions:

  1. Added a test file namespace_sync_controller_test.go in pkg/controllers/namespace directory.

Test Coverage:
Test coverage has been increased from 0% to 41.7%, reflecting the complexity of the controller. To check test coverage, run go test -coverprofile=coverage.out in the pkg/controllers/namespace directory.

What type of PR is this?
/kind failing-test
/kind feature

What this PR does / why we need it:
This PR enhances the testing coverage of the namespace controller, which is crucial for managing namespace operations and ensuring the controller’s stability and functionality. By adding these tests, we ensure that potential edge cases and critical functionalities are properly validated, leading to improved stability and performance of the namespace controller.

Which issue(s) this PR fixes:
Fixes a part of #5235

Does this PR introduce a user-facing change?:

NONE

@karmada-bot karmada-bot added the kind/failing-test Categorizes issue or PR as related to a consistently or frequently failing test. label Aug 14, 2024
@karmada-bot karmada-bot added the kind/feature Categorizes issue or PR as related to a new feature. label Aug 14, 2024
@karmada-bot karmada-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 14, 2024
@anujagrawal699
Copy link
Contributor Author

@XiShanYongYe-Chang TestController_SetupWithManager failed during the CI workflow but its running fine locally.

 go test -run TestController_SetupWithManager                          
PASS
ok      github.com/karmada-io/karmada/pkg/controllers/namespace 0.094s

@XiShanYongYe-Chang
Copy link
Member

/assign
Hi @anujagrawal699 the ci is failed.

@karmada-bot karmada-bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 14, 2024
@codecov-commenter
Copy link

codecov-commenter commented Aug 14, 2024

⚠️ 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 29.54%. Comparing base (235ec91) to head (4cc0c20).
Report is 223 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    #5368      +/-   ##
==========================================
+ Coverage   29.01%   29.54%   +0.52%     
==========================================
  Files         632      632              
  Lines       43862    43835      -27     
==========================================
+ Hits        12728    12949     +221     
+ Misses      30218    29934     -284     
- Partials      916      952      +36     
Flag Coverage Δ
unittests 29.54% <ø> (+0.52%) ⬆️

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.

@anujagrawal699
Copy link
Contributor Author

@XiShanYongYe-Chang CI passed.

Copy link
Member

@XiShanYongYe-Chang XiShanYongYe-Chang left a comment

Choose a reason for hiding this comment

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

Thanks a lot @anujagrawal699

}
}

func TestController_buildWorksWithOverridePolicy(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Is it convenient to combine this test method with TestController_buildWorks into one test 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.

I think separate tests will ensure that override policy behavior doesn't interfere with basic functionality. I could create helper functions for common setup code if you say.

Copy link
Member

Choose a reason for hiding this comment

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

You can modify it to see the effect.

assert.NotNil(t, work)
}

func TestController_SetupWithManager(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Can the following three functions, including this one, be organized into table-driven tests? That would be easier to read and maintain. I do have tests in the current style of testing, but they don't seem to work very well at the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that would be more readable and maintainable.

assert.Error(t, err, "SetupWithManager should return an error when schemes are not properly set up")
}

func TestClusterNamespaceFn(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I didn't understand which function body the function tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The two you mentioned are clusterNamespaceFn and clusterOverridePolicyNamespaceFn which are MapFuncs used in the controller's SetupWithManager() method to handle events related to Cluster and ClusterOverridePolicy resources.

Copy link
Member

Choose a reason for hiding this comment

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

I understand what you are saying, but where is the code guarded by these two test methods?

Copy link
Contributor Author

@anujagrawal699 anujagrawal699 Aug 17, 2024

Choose a reason for hiding this comment

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

Okay, i got it. I think i need to refractor it. Thank You for pointing out.

Copy link
Contributor Author

@anujagrawal699 anujagrawal699 Aug 17, 2024

Choose a reason for hiding this comment

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

@XiShanYongYe-Chang I'm not able to test them in isolation. I think we should move ahead without them.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I don't think they need to be tested.

assert.Contains(t, requests, reconcile.Request{NamespacedName: types.NamespacedName{Name: "namespace2"}})
}

func TestClusterOverridePolicyNamespaceFn(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

ditto.

@anujagrawal699
Copy link
Contributor Author

@XiShanYongYe-Chang PTAL

@karmada-bot karmada-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Aug 17, 2024
@anujagrawal699
Copy link
Contributor Author

anujagrawal699 commented Aug 17, 2024

/retest @XiShanYongYe-Chang

@karmada-bot
Copy link
Collaborator

@anujagrawal699: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@NishantBansal2003
Copy link
Contributor

Hey @anujagrawal699, could you please review my PR as well? Thanks!

@anujagrawal699
Copy link
Contributor Author

Hey @anujagrawal699, could you please review my PR as well? Thanks!

Sure!

@XiShanYongYe-Chang
Copy link
Member

/retest

@liangyuanpeng
Copy link
Contributor

/ok-to-test
then you can run /retest to rerun github workflow when all workflow is completed

Copy link
Member

@XiShanYongYe-Chang XiShanYongYe-Chang left a comment

Choose a reason for hiding this comment

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

Thanks
/lgtm
/approve

@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Aug 20, 2024
@karmada-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: XiShanYongYe-Chang

The full list of commands accepted by this bot can be found here.

The pull request process is described 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 approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 20, 2024
@karmada-bot karmada-bot merged commit 2c1e0f8 into karmada-io:master Aug 20, 2024
12 checks passed
@anujagrawal699
Copy link
Contributor Author

Thanks
/lgtm
/approve

Thanks for approval. I could have squashed the commits.

@XiShanYongYe-Chang
Copy link
Member

I could have squashed the commits.

I forgot again.

@anujagrawal699
Copy link
Contributor Author

I could have squashed the commits.

I forgot again.

Its Okay. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/failing-test Categorizes issue or PR as related to a consistently or frequently failing test. kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. ok-to-test size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants