Skip to content

test: add unit tests for GenFieldMergePatch and binding helper functions#7614

Open
tushar-pandhare wants to merge 2 commits into
karmada-io:masterfrom
tushar-pandhare:test/add-unit-tests-patch-and-binding
Open

test: add unit tests for GenFieldMergePatch and binding helper functions#7614
tushar-pandhare wants to merge 2 commits into
karmada-io:masterfrom
tushar-pandhare:test/add-unit-tests-patch-and-binding

Conversation

@tushar-pandhare

Copy link
Copy Markdown
Contributor

/kind cleanup

What this PR does / why we need it:
Add unit tests for functions in pkg/util/helper that lacked test coverage:

  • GenFieldMergePatch (patch.go) - tests no change, field changed, nested field change
  • GetClusterResourceBindings (binding.go) - tests no bindings, matching binding, non-matching binding
  • GetResourceBindings (binding.go) - tests no bindings, matching binding, non-matching binding
  • GetResourceBindingsByNamespace (binding.go) - tests no bindings, binding in namespace, binding in different namespace

Which issue(s) this PR fixes:
Fixes : #7607

Special notes for your reviewer:
All existing tests pass with the new additions:
ok github.com/karmada-io/karmada/pkg/util/helper 1.441s

Does this PR introduce a user-facing change?:

NONE

Copilot AI review requested due to automatic review settings June 9, 2026 07:19
@karmada-bot karmada-bot added the kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. label Jun 9, 2026
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request improves the test coverage for utility functions within the project. By adding unit tests for key helper functions responsible for patch generation and resource binding retrieval, the changes ensure more robust verification of logic and prevent potential regressions in future updates.

Highlights

  • Unit Test Coverage: Added comprehensive unit tests for GenFieldMergePatch, GetClusterResourceBindings, GetResourceBindings, and GetResourceBindingsByNamespace to improve code reliability.
  • Test Scenarios: Implemented test cases covering various scenarios including no-change, field updates, nested field changes, and binding filtering based on labels and namespaces.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@karmada-bot

Copy link
Copy Markdown
Contributor

[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 xishanyongye-chang for approval. For more information see the Code Review Process.

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

Details 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/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 9, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds unit tests for additional helper functions to validate merge-patch generation and binding retrieval behavior.

Changes:

  • Added table-driven tests for GenFieldMergePatch.
  • Added fake-client based tests for GetClusterResourceBindings, GetResourceBindings, and GetResourceBindingsByNamespace.
  • Introduced a new dependency on k8s.io/apimachinery/pkg/labels in binding helper tests.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
pkg/util/helper/patch_test.go Adds coverage for field-scoped merge patch generation via GenFieldMergePatch.
pkg/util/helper/binding_test.go Adds coverage for fetching bindings using label selectors and namespace scoping with a fake controller-runtime client.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +135 to +140
name: "no change returns nil",
fieldName: "spec",
originField: map[string]string{"key": "value"},
modifiedField: map[string]string{"key": "value"},
expectedPatch: "",
expectErr: false,
Comment on lines +168 to +170
if string(patch) != tt.expectedPatch {
t.Fatalf("want patch: %s, but got: %s", tt.expectedPatch, string(patch))
}
fieldName: "spec",
originField: map[string]string{"key": "old"},
modifiedField: map[string]string{"key": "new"},
expectedPatch: `{"spec":{"key":"new"}}`,
fieldName: "metadata",
originField: map[string]any{"name": "foo", "labels": map[string]string{"env": "dev"}},
modifiedField: map[string]any{"name": "foo", "labels": map[string]string{"env": "prod"}},
expectedPatch: `{"metadata":{"labels":{"env":"prod"}}}`,
Comment on lines +168 to +170
if string(patch) != tt.expectedPatch {
t.Fatalf("want patch: %s, but got: %s", tt.expectedPatch, string(patch))
}
Comment on lines 19 to 23
import (
"context"
"k8s.io/apimachinery/pkg/labels"
"reflect"
"testing"

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces unit tests for several helper functions, including GetClusterResourceBindings, GetResourceBindings, and GetResourceBindingsByNamespace in binding_test.go, and GenFieldMergePatch in patch_test.go. The review feedback recommends separating standard library imports from third-party imports in binding_test.go to follow standard Go practices, and adding an error-path test case to TestGenFieldMergePatch to ensure complete test coverage for error handling.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines 20 to 23
"context"
"k8s.io/apimachinery/pkg/labels"
"reflect"
"testing"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

According to standard Go best practices (enforced by goimports), standard library imports should be grouped separately from third-party/external imports.

Please move "k8s.io/apimachinery/pkg/labels" to the second import group containing the other k8s.io packages.

Suggested change
"context"
"k8s.io/apimachinery/pkg/labels"
"reflect"
"testing"
"context"
"reflect"
"testing"
References
  1. These rules supplement the official Go style guide. Where this guide is silent, standard Go best practices apply. (link)

Comment on lines +150 to +158
{
name: "nested field change",
fieldName: "metadata",
originField: map[string]any{"name": "foo", "labels": map[string]string{"env": "dev"}},
modifiedField: map[string]any{"name": "foo", "labels": map[string]string{"env": "prod"}},
expectedPatch: `{"metadata":{"labels":{"env":"prod"}}}`,
expectErr: false,
},
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The test suite for GenFieldMergePatch defines an expectErr field in the test cases struct, but none of the current test cases actually test the error path (expectErr: true).

Adding a test case with an invalid input (such as a channel, which cannot be marshaled to JSON) will ensure that the error handling of GenFieldMergePatch is fully covered.

		{
			name:          "nested field change",
			fieldName:     "metadata",
			originField:   map[string]any{"name": "foo", "labels": map[string]string{"env": "dev"}},
			modifiedField: map[string]any{"name": "foo", "labels": map[string]string{"env": "prod"}},
			expectedPatch: "{\"metadata\":{\"labels\":{\"env\":\"prod\"}}}",
			expectErr:     false,
		},
		{
			name:          "invalid input returns error",
			fieldName:     "spec",
			originField:   make(chan int),
			modifiedField: map[string]string{"key": "value"},
			expectedPatch: "",
			expectErr:     true,
		},
	}

@tushar-pandhare tushar-pandhare force-pushed the test/add-unit-tests-patch-and-binding branch from 9ae28c0 to 37d0cd2 Compare June 9, 2026 07:28
@codecov-commenter

codecov-commenter commented Jun 9, 2026

Copy link
Copy Markdown

⚠️ 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 42.17%. Comparing base (287d480) to head (6016c62).
⚠️ Report is 13 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    #7614      +/-   ##
==========================================
+ Coverage   42.16%   42.17%   +0.01%     
==========================================
  Files         879      879              
  Lines       54669    54669              
==========================================
+ Hits        23053    23059       +6     
+ Misses      29870    29861       -9     
- Partials     1746     1749       +3     
Flag Coverage Δ
unittests 42.17% <ø> (+0.01%) ⬆️

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

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: Tushar Pandhare <tusharpandharetp@gmail.com>
@tushar-pandhare tushar-pandhare force-pushed the test/add-unit-tests-patch-and-binding branch from 37d0cd2 to bbc020c Compare June 9, 2026 09:06
@tushar-pandhare

Copy link
Copy Markdown
Contributor Author

/cc @RainbowMango

Hi! Could you please review this PR when you get a chance?
Happy to address any feedback. Thank you! 🙏

@karmada-bot karmada-bot requested a review from RainbowMango June 9, 2026 10:24
@karmada-bot karmada-bot added the do-not-merge/contains-merge-commits Indicates a PR which contains merge commits. label Jun 16, 2026
@karmada-bot

Copy link
Copy Markdown
Contributor

Adding label do-not-merge/contains-merge-commits because PR contains merge commits, which are not allowed in this repository.
Use git rebase to reapply your commits on top of the target branch. Detailed instructions for doing so can be found here.

Details

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge/contains-merge-commits Indicates a PR which contains merge commits. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. 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.

test: add unit tests for GenFieldMergePatch in pkg/util/helper/

4 participants