Skip to content

test: add unit tests for SetLabelsAndAnnotationsForWorkload and GetWorkSuspendDispatching#7675

Open
Goutham-Annem wants to merge 1 commit into
karmada-io:masterfrom
Goutham-Annem:test/work-util-missing-tests
Open

test: add unit tests for SetLabelsAndAnnotationsForWorkload and GetWorkSuspendDispatching#7675
Goutham-Annem wants to merge 1 commit into
karmada-io:masterfrom
Goutham-Annem:test/work-util-missing-tests

Conversation

@Goutham-Annem

Copy link
Copy Markdown

What type of PR is this?

/kind feature

What this PR does / why we need it:

Two functions in pkg/util/work.go lacked unit test coverage. This PR adds tests for:

  • GetWorkSuspendDispatching — covers true, false, and nil SuspendDispatching pointer (nil defaults to false)
  • SetLabelsAndAnnotationsForWorkload — verifies that the managed-annotations and managed-labels annotations are always set on the workload, and that the WorkPermanentIDLabel is propagated from the Work object to the workload when present

Which issue(s) this PR fixes:

Fixes #7673

Special notes for your reviewer:

All new tests follow the table-driven pattern and use github.com/stretchr/testify/assert consistent with the existing work_test.go.

Does this PR introduce a user-facing change?:

NONE

…rkSuspendDispatching

Add unit tests for two functions in pkg/util/work.go that lacked
coverage:
- GetWorkSuspendDispatching: covers true, false, and nil
  SuspendDispatching pointer (defaults to false)
- SetLabelsAndAnnotationsForWorkload: covers the managed-annotations
  and managed-labels annotation population, and propagation of the
  WorkPermanentIDLabel from the Work object to the workload

Fixes karmada-io#7673

Signed-off-by: Goutham Annem <annem.usedu@gmail.com>
Copilot AI review requested due to automatic review settings June 27, 2026 18:08
@karmada-bot karmada-bot added kind/feature Categorizes issue or PR as related to a new feature. do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. labels Jun 27, 2026
@karmada-bot

Copy link
Copy Markdown
Contributor

Invalid commit message issues detected

Invalid commit messages

Keywords which can automatically close issues and hashtag(#) mentions are not allowed.

  • 22aea16 test: add unit tests for SetLabelsAndAnnotationsForWorkload and GetWorkSuspendDispatching

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. I understand the commands that are listed here.

@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 chaunceyjiang 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

@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 reliability of the codebase by introducing unit tests for two previously untested utility functions. The changes ensure that workload suspension logic and label/annotation propagation behave as expected under various conditions, addressing a known gap in test coverage.

Highlights

  • Unit Test Coverage: Added comprehensive unit tests for GetWorkSuspendDispatching and SetLabelsAndAnnotationsForWorkload functions in pkg/util/work.go.
  • Test Methodology: Implemented table-driven tests using the testify/assert library to ensure consistency with existing test suites.
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 karmada-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 27, 2026

@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 adds unit tests in pkg/util/work_test.go to verify GetWorkSuspendDispatching and SetLabelsAndAnnotationsForWorkload. The review feedback highlights that TestSetLabelsAndAnnotationsForWorkload contains unused struct fields and duplicate test cases that do not actually set the permanent ID label as named. It is recommended to simplify this test to a single basic test case, as permanent ID propagation is already thoroughly tested in a separate dedicated test.

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 thread pkg/util/work_test.go
Comment on lines +290 to +330
tests := []struct {
name string
workload *unstructured.Unstructured
work *workv1alpha1.Work
wantLabelKey string
wantLabelValue string
wantAnnotationKey string
}{
{
name: "work has permanent ID label — propagated to workload",
workload: &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]any{
"name": "demo",
"namespace": "default",
},
},
},
work: &workv1alpha1.Work{
Spec: workv1alpha1.WorkSpec{},
},
},
{
name: "work has permanent ID label set",
workload: &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]any{
"name": "demo",
"namespace": "default",
},
},
},
work: &workv1alpha1.Work{
Spec: workv1alpha1.WorkSpec{},
},
},
}

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 table-driven test TestSetLabelsAndAnnotationsForWorkload contains several issues:

  1. The struct fields wantLabelKey, wantLabelValue, and wantAnnotationKey are defined but never used in the test cases or assertions.
  2. The two test cases are completely identical and neither of them actually sets the permanent ID label on the work object, despite their names indicating they do.
  3. Since permanent ID propagation is already thoroughly tested in TestSetLabelsAndAnnotationsForWorkload_WithPermanentID, we can simplify this test to a single basic test case and remove the unused fields.
	tests := []struct {
		name     string
		workload *unstructured.Unstructured
		work     *workv1alpha1.Work
	}{
		{
			name: "basic workload and work",
			workload: &unstructured.Unstructured{
				Object: map[string]any{
					"apiVersion": "apps/v1",
					"kind":       "Deployment",
					"metadata": map[string]any{
						"name":      "demo",
						"namespace": "default",
					},
				},
			},
			work: &workv1alpha1.Work{
				Spec: workv1alpha1.WorkSpec{},
			},
		},
	}

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

Adds missing unit test coverage in pkg/util/work_test.go for two helper functions in pkg/util/work.go, improving regression protection around suspend-dispatching indexing and workload metadata management.

Changes:

  • Add table-driven tests for GetWorkSuspendDispatching covering true, false, and nil SuspendDispatching (defaulting to false).
  • Add tests for SetLabelsAndAnnotationsForWorkload ensuring managed-annotations/managed-labels are set, plus a dedicated test for WorkPermanentIDLabel propagation.

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

Comment thread pkg/util/work_test.go
Comment on lines +294 to +297
wantLabelKey string
wantLabelValue string
wantAnnotationKey string
}{
Comment thread pkg/util/work_test.go
wantAnnotationKey string
}{
{
name: "work has permanent ID label — propagated to workload",
Comment thread pkg/util/work_test.go
Comment on lines +314 to +329
{
name: "work has permanent ID label set",
workload: &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]any{
"name": "demo",
"namespace": "default",
},
},
},
work: &workv1alpha1.Work{
Spec: workv1alpha1.WorkSpec{},
},
},
@codecov-commenter

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.06%. Comparing base (56d5d87) to head (22aea16).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7675   +/-   ##
=======================================
  Coverage   42.06%   42.06%           
=======================================
  Files         879      879           
  Lines       54831    54831           
=======================================
+ Hits        23063    23067    +4     
+ Misses      30023    30017    -6     
- Partials     1745     1747    +2     
Flag Coverage Δ
unittests 42.06% <ø> (+<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.

@Goutham-Annem

Copy link
Copy Markdown
Author

Hi, just bumping this for visibility. Happy to address any feedback or questions!

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

Labels

do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: add unit tests for SetLabelsAndAnnotationsForWorkload and GetWorkSuspendDispatching in pkg/util/work.go

4 participants