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

Improved test coverage for workloadrebalancer controller #5606

Conversation

anujagrawal699
Copy link
Contributor

Description:
This PR adds a new test case for cluster-wide resources and refactors the existing test structure for the WorkloadRebalancer controller. These changes aim to improve code coverage and reduce cyclomatic complexity.

Changes:

  1. Added a new test case for cluster-wide resource rebalancing:

    • This test case ensures that the controller correctly handles cluster-wide resources, improving our coverage of different resource types.
  2. Refactored the TestRebalancerController_Reconcile function:

    • Extracted the test execution logic into a separate function (runRebalancerTest).
    • Moved binding check logic into separate functions (checkBindings, checkClusterBinding, checkResourceBinding).
    • These changes reduce the cyclomatic complexity of the main test function, making it easier to understand and maintain.

Rationale:

  • The new test case for cluster-wide resources fills a gap in our test coverage, ensuring that this important scenario is properly tested.
  • The refactoring addresses a high cyclomatic complexity warning from golangci-lint, improving code quality and maintainability.

Test Coverage:
pkg/controllers/workloadrebalancer : 63.01% to 75,.4%

What type of PR is this?
/kind failing-test
/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/failing-test Categorizes issue or PR as related to a consistently or frequently failing test. label Sep 25, 2024
@karmada-bot karmada-bot added kind/feature Categorizes issue or PR as related to a new feature. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 25, 2024
@codecov-commenter
Copy link

codecov-commenter commented Sep 25, 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 35.19%. Comparing base (eaa3452) to head (9bb8b5b).
Report is 14 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    #5606      +/-   ##
==========================================
+ Coverage   35.01%   35.19%   +0.17%     
==========================================
  Files         645      645              
  Lines       44861    44869       +8     
==========================================
+ Hits        15708    15791      +83     
+ Misses      27938    27846      -92     
- Partials     1215     1232      +17     
Flag Coverage Δ
unittests 35.19% <ø> (+0.17%) ⬆️

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.

@XiShanYongYe-Chang
Copy link
Member

/cc @chaosi-zju

Comment on lines 267 to 282
name: "reconcile cluster-wide resource rebalancer",
req: controllerruntime.Request{
NamespacedName: types.NamespacedName{Name: clusterRebalancer.Name},
},
existObjects: []client.Object{clusterDeploy, clusterBinding, clusterRebalancer},
existObjsWithStatus: []client.Object{clusterRebalancer},
wantStatus: appsv1alpha1.WorkloadRebalancerStatus{
ObservedWorkloads: []appsv1alpha1.ObservedWorkload{
{
Workload: clusterDeployObj,
Result: appsv1alpha1.RebalanceSuccessful,
},
Copy link
Member

Choose a reason for hiding this comment

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

thanks, but this case is not suitable, since deploy is not cluster-wide resource, and it will never generate clusterResourceBinding. you'd better use other resource like clusterrole~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks i'll work on it.

@anujagrawal699 anujagrawal699 force-pushed the addedTests-controllers/workloadrebalancer branch from ca3ee3f to 48158a0 Compare September 26, 2024 14:19
@anujagrawal699
Copy link
Contributor Author

@chaosi-zju Please take a look.

@@ -139,6 +140,19 @@ var (
},
},
}

clusterRole = &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{Name: "test-cluster-role"},
Copy link
Member

@chaosi-zju chaosi-zju Sep 27, 2024

Choose a reason for hiding this comment

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

This is a cluster wide resource, it is not suitable to use fixed simple names, for the purpose of preventing from affecting other cases. So it is better to name with a random suffix.

BTW, I'd much appreciate it if you can also rename above deployment with a random suffix too~

Signed-off-by: Anuj Agrawal <anujagrawal380@gmail.com>

Changed the resource to clusterrole

Signed-off-by: Anuj Agrawal <anujagrawal380@gmail.com>

Added tests for workloadrebalancer controller

Signed-off-by: Anuj Agrawal <anujagrawal380@gmail.com>
@anujagrawal699 anujagrawal699 force-pushed the addedTests-controllers/workloadrebalancer branch from 48158a0 to 9bb8b5b Compare September 27, 2024 06:13
@chaosi-zju
Copy link
Member

/lgtm

cc @XiShanYongYe-Chang

@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Sep 27, 2024
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~
/approve

@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 Sep 27, 2024
@karmada-bot karmada-bot merged commit 58612d3 into karmada-io:master Sep 27, 2024
12 checks passed
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. 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.

5 participants