Skip to content

test(util/lifted): cover resource-name helpers#7467

Open
apshada wants to merge 1 commit into
karmada-io:masterfrom
apshada:test/lifted-resource-helpers
Open

test(util/lifted): cover resource-name helpers#7467
apshada wants to merge 1 commit into
karmada-io:masterfrom
apshada:test/lifted-resource-helpers

Conversation

@apshada
Copy link
Copy Markdown

@apshada apshada commented May 3, 2026

Adds unit tests for the previously-uncovered helpers in pkg/util/lifted/corev1helpers.go and resourcename.go. Pure test additions, no production-code changes.

…resource helpers

Adds unit tests for IsPrefixedNativeResource, IsHugePageResourceName,
IsAttachableVolumeResourceName, IsExtendedResourceName and IsScalarResourceName.

Signed-off-by: Aditya Pratap Singh Hada <apshada1@gmail.com>
Copilot AI review requested due to automatic review settings May 3, 2026 06:04
@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 test suite by introducing unit tests for previously uncovered helper functions related to Kubernetes resource names. These additions help ensure the correctness and reliability of resource validation logic without modifying any existing production code.

Highlights

  • Test Coverage Expansion: Added comprehensive unit tests for resource-name helper functions in the lifted utility package.
  • No Production Changes: The pull request is strictly limited to test additions, ensuring no impact on existing production logic.
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 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 counter productive. 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

Welcome @apshada! It looks like this is your first PR to karmada-io/karmada 🎉

@karmada-bot karmada-bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 3, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds unit coverage for resource-name helper functions in the pkg/util/lifted package, extending tests around Kubernetes-lifted helper behavior without changing production logic.

Changes:

  • Added table-driven tests for IsPrefixedNativeResource, IsHugePageResourceName, IsAttachableVolumeResourceName, and IsExtendedResourceName.
  • Added coverage for IsScalarResourceName, which lives in resourcename.go but is exercised from the existing helper test file.
  • Expanded negative-case coverage for invalid, empty, and non-native resource names.

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

Copy link
Copy Markdown

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

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 for several resource-related helper functions, including IsPrefixedNativeResource, IsHugePageResourceName, IsAttachableVolumeResourceName, IsExtendedResourceName, and IsScalarResourceName. The review feedback suggests refactoring these tests to use table-driven subtests and parallel execution to improve consistency with existing tests and enhance observability. Additionally, there is a recommendation to move the scalar resource tests to a separate file for better code organization.

Comment on lines +69 to +85
func TestIsPrefixedNativeResource(t *testing.T) {
cases := []struct {
name corev1.ResourceName
want bool
}{
{"kubernetes.io/foo", true},
{"pod.alpha.kubernetes.io/opaque", true},
{"example.com/gpu", false},
{"foo", false},
{"", false},
}
for _, c := range cases {
if got := IsPrefixedNativeResource(c.name); got != c.want {
t.Errorf("IsPrefixedNativeResource(%q) = %v, want %v", c.name, got, c.want)
}
}
}
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

To maintain consistency with the existing TestIsNativeResource in this file and follow Go best practices for table-driven tests, please use subtests (t.Run) and enable parallel execution (t.Parallel()). Additionally, using consistent field names like resourceName and expectVal improves maintainability.

func TestIsPrefixedNativeResource(t *testing.T) {
	testCases := []struct {
		resourceName corev1.ResourceName
		expectVal    bool
	}{
		{resourceName: "kubernetes.io/foo", expectVal: true},
		{resourceName: "pod.alpha.kubernetes.io/opaque", expectVal: true},
		{resourceName: "example.com/gpu", expectVal: false},
		{resourceName: "foo", expectVal: false},
		{resourceName: "", expectVal: false},
	}
	for _, tc := range testCases {
		tc := tc
		t.Run(string(tc.resourceName), func(t *testing.T) {
			t.Parallel()
			if got := IsPrefixedNativeResource(tc.resourceName); got != tc.expectVal {
				t.Errorf("IsPrefixedNativeResource(%q) = %v, want %v", tc.resourceName, got, tc.expectVal)
			}
		})
	}
}

Comment on lines +87 to +103
func TestIsHugePageResourceName(t *testing.T) {
cases := []struct {
name corev1.ResourceName
want bool
}{
{corev1.ResourceName(string(corev1.ResourceHugePagesPrefix) + "2Mi"), true},
{corev1.ResourceName(string(corev1.ResourceHugePagesPrefix) + "1Gi"), true},
{"huge-pages-2Mi", false}, // missing the hyphenated `hugepages-` prefix
{corev1.ResourceCPU, false},
{"", false},
}
for _, c := range cases {
if got := IsHugePageResourceName(c.name); got != c.want {
t.Errorf("IsHugePageResourceName(%q) = %v, want %v", c.name, got, c.want)
}
}
}
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

Please refactor this test to use subtests and parallel execution for consistency and better observability, similar to the existing tests in this package.

func TestIsHugePageResourceName(t *testing.T) {
	testCases := []struct {
		resourceName corev1.ResourceName
		expectVal    bool
	}{
		{resourceName: corev1.ResourceName(string(corev1.ResourceHugePagesPrefix) + "2Mi"), expectVal: true},
		{resourceName: corev1.ResourceName(string(corev1.ResourceHugePagesPrefix) + "1Gi"), expectVal: true},
		{resourceName: "huge-pages-2Mi", expectVal: false}, // missing the hyphenated hugepages- prefix
		{resourceName: corev1.ResourceCPU, expectVal: false},
		{resourceName: "", expectVal: false},
	}
	for _, tc := range testCases {
		tc := tc
		t.Run(string(tc.resourceName), func(t *testing.T) {
			t.Parallel()
			if got := IsHugePageResourceName(tc.resourceName); got != tc.expectVal {
				t.Errorf("IsHugePageResourceName(%q) = %v, want %v", tc.resourceName, got, tc.expectVal)
			}
		})
	}
}

Comment on lines +105 to +120
func TestIsAttachableVolumeResourceName(t *testing.T) {
cases := []struct {
name corev1.ResourceName
want bool
}{
{corev1.ResourceName(string(corev1.ResourceAttachableVolumesPrefix) + "ebs"), true},
{"attachable_volumes-foo", false}, // missing exact `attachable-volumes-` prefix
{corev1.ResourceMemory, false},
{"", false},
}
for _, c := range cases {
if got := IsAttachableVolumeResourceName(c.name); got != c.want {
t.Errorf("IsAttachableVolumeResourceName(%q) = %v, want %v", c.name, got, c.want)
}
}
}
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

Please refactor this test to use subtests and parallel execution for consistency and better observability.

func TestIsAttachableVolumeResourceName(t *testing.T) {
	testCases := []struct {
		resourceName corev1.ResourceName
		expectVal    bool
	}{
		{resourceName: corev1.ResourceName(string(corev1.ResourceAttachableVolumesPrefix) + "ebs"), expectVal: true},
		{resourceName: "attachable_volumes-foo", expectVal: false}, // missing exact attachable-volumes- prefix
		{resourceName: corev1.ResourceMemory, expectVal: false},
		{resourceName: "", expectVal: false},
	}
	for _, tc := range testCases {
		tc := tc
		t.Run(string(tc.resourceName), func(t *testing.T) {
			t.Parallel()
			if got := IsAttachableVolumeResourceName(tc.resourceName); got != tc.expectVal {
				t.Errorf("IsAttachableVolumeResourceName(%q) = %v, want %v", tc.resourceName, got, tc.expectVal)
			}
		})
	}
}

Comment on lines +122 to +142
func TestIsExtendedResourceName(t *testing.T) {
cases := []struct {
name corev1.ResourceName
want bool
}{
// Native resources are NOT extended.
{corev1.ResourceCPU, false},
{"foo", false},
// Names prefixed with requests. are NOT extended (would clash with quota convention).
{corev1.ResourceName(corev1.DefaultResourceRequestsPrefix + "example.com/gpu"), false},
// Vendor-prefixed resource that satisfies IsQualifiedName once requests. is prepended IS extended.
{"example.com/gpu", true},
// Invalid qualified name → not extended.
{"BAD!!", false},
}
for _, c := range cases {
if got := IsExtendedResourceName(c.name); got != c.want {
t.Errorf("IsExtendedResourceName(%q) = %v, want %v", c.name, got, c.want)
}
}
}
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

Please refactor this test to use subtests and parallel execution for consistency and better observability.

func TestIsExtendedResourceName(t *testing.T) {
	testCases := []struct {
		resourceName corev1.ResourceName
		expectVal    bool
	}{
		// Native resources are NOT extended.
		{resourceName: corev1.ResourceCPU, expectVal: false},
		{resourceName: "foo", expectVal: false},
		// Names prefixed with requests. are NOT extended (would clash with quota convention).
		{resourceName: corev1.ResourceName(corev1.DefaultResourceRequestsPrefix + "example.com/gpu"), expectVal: false},
		// Vendor-prefixed resource that satisfies IsQualifiedName once requests. is prepended IS extended.
		{resourceName: "example.com/gpu", expectVal: true},
		// Invalid qualified name -> not extended.
		{resourceName: "BAD!!", expectVal: false},
	}
	for _, tc := range testCases {
		tc := tc
		t.Run(string(tc.resourceName), func(t *testing.T) {
			t.Parallel()
			if got := IsExtendedResourceName(tc.resourceName); got != tc.expectVal {
				t.Errorf("IsExtendedResourceName(%q) = %v, want %v", tc.resourceName, got, tc.expectVal)
			}
		})
	}
}

Comment on lines +144 to +161
func TestIsScalarResourceName(t *testing.T) {
cases := []struct {
name corev1.ResourceName
want bool
}{
{"example.com/gpu", true}, // extended
{corev1.ResourceName(string(corev1.ResourceHugePagesPrefix) + "2Mi"), true},
{corev1.ResourceName(string(corev1.ResourceAttachableVolumesPrefix) + "ebs"), true},
{"kubernetes.io/foo", true}, // prefixed native
{corev1.ResourceCPU, false},
{"foo", false},
}
for _, c := range cases {
if got := IsScalarResourceName(c.name); got != c.want {
t.Errorf("IsScalarResourceName(%q) = %v, want %v", c.name, got, c.want)
}
}
}
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

If IsScalarResourceName is defined in resourcename.go (as implied by the PR description), this test should ideally be moved to a new file named resourcename_test.go to maintain proper code organization. Additionally, please apply the subtest and parallel execution pattern here as well.

func TestIsScalarResourceName(t *testing.T) {
	testCases := []struct {
		resourceName corev1.ResourceName
		expectVal    bool
	}{
		{resourceName: "example.com/gpu", expectVal: true}, // extended
		{resourceName: corev1.ResourceName(string(corev1.ResourceHugePagesPrefix) + "2Mi"), expectVal: true},
		{resourceName: corev1.ResourceName(string(corev1.ResourceAttachableVolumesPrefix) + "ebs"), expectVal: true},
		{resourceName: "kubernetes.io/foo", expectVal: true}, // prefixed native
		{resourceName: corev1.ResourceCPU, expectVal: false},
		{resourceName: "foo", expectVal: false},
	}
	for _, tc := range testCases {
		tc := tc
		t.Run(string(tc.resourceName), func(t *testing.T) {
			t.Parallel()
			if got := IsScalarResourceName(tc.resourceName); got != tc.expectVal {
				t.Errorf("IsScalarResourceName(%q) = %v, want %v", tc.resourceName, got, tc.expectVal)
			}
		})
	}
}

@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 41.93%. Comparing base (9d87acb) to head (8c01949).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7467      +/-   ##
==========================================
+ Coverage   41.90%   41.93%   +0.03%     
==========================================
  Files         879      879              
  Lines       54326    54326              
==========================================
+ Hits        22766    22784      +18     
+ Misses      29833    29817      -16     
+ Partials     1727     1725       -2     
Flag Coverage Δ
unittests 41.93% <ø> (+0.03%) ⬆️

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.

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

@XiShanYongYe-Chang
Copy link
Copy Markdown
Member

Hi @apshada thanks for your contribution.
The code in this folder was lifted from the upstream source, and there will be comments on the functions. Is this test consistent with the upstream?

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

Labels

size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants