Skip to content

test: add unit tests and refactor kernal_version utility#1590

Open
yashisrani wants to merge 5 commits intokmesh-net:mainfrom
yashisrani:improve/test-cases
Open

test: add unit tests and refactor kernal_version utility#1590
yashisrani wants to merge 5 commits intokmesh-net:mainfrom
yashisrani:improve/test-cases

Conversation

@yashisrani
Copy link
Copy Markdown
Contributor

What type of PR is this?

What this PR does / why we need it:

  • This PR improves the code health of the pkg/utils package by adding unit tests for the kernel version utility. To achieve this, the version parsing logic was refactored to be platform-independent, and cross-platform support was added to allow testing in non-Linux environments.

Key Changes

  • Refactoring: Split kernel_version.go to extract isVersionLowerThan for direct testing.
  • Cross-Platform Support: Added kernel_version_linux.go and kernel_version_other.go to handle system-specific uname calls.
  • New Tests: Created kernel_version_test.go checking major/minor version comparisons, invalid formats, and string conversion helpers.

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE
Screenshot 2026-02-27 at 8 53 08 AM

Signed-off-by: Yash Israni <118755067+yashisrani@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 27, 2026 03:24
@kmesh-bot
Copy link
Copy Markdown
Collaborator

[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 lizhencheng9527 for approval. For more information see the Kubernetes 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 significantly enhances the code health of the pkg/utils package by introducing comprehensive unit tests for the kernel version utility. It achieves this by refactoring the version parsing logic to be platform-independent and adding cross-platform support, which allows for robust testing across different operating systems.

Highlights

  • Code Refactoring: The kernel_version.go file was refactored to extract the isVersionLowerThan function, making the version parsing logic platform-independent and more testable.
  • Cross-Platform Support: Introduced kernel_version_linux.go and kernel_version_other.go to provide platform-specific implementations for retrieving the kernel version, enabling testing in non-Linux environments.
  • Unit Testing: New unit tests were added in kernel_version_test.go to cover major/minor version comparisons, invalid version formats, and the int8ToStr helper function.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • pkg/utils/kernel_version.go
    • Removed Linux-specific build tags.
    • Extracted the version comparison logic into a new isVersionLowerThan function.
    • Removed the GetKernelVersion and int8ToStr functions, which are now handled by platform-specific files.
  • pkg/utils/kernel_version_linux.go
    • Added a new file containing the Linux-specific implementation of GetKernelVersion and int8ToStr.
  • pkg/utils/kernel_version_other.go
    • Added a new file containing a dummy GetKernelVersion implementation for non-Linux platforms, along with int8ToStr.
  • pkg/utils/kernel_version_test.go
    • Added a new file with unit tests for the isVersionLowerThan function, covering various version comparison scenarios and edge cases.
    • Added unit tests for the int8ToStr helper function.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Code refactored, Tests now run on every OS, Bugs flee in fear.

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.

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 effectively refactors the kernel version utility for better testability and cross-platform support by separating platform-specific logic using build tags. The addition of unit tests significantly improves the code's reliability. I've identified one area for improvement concerning code duplication of a helper function, which should be addressed to enhance maintainability.

Comment on lines +27 to +36
func int8ToStr(arr []int8) string {
b := make([]byte, 0, len(arr))
for _, v := range arr {
if v == 0x00 {
break
}
b = append(b, byte(v))
}
return string(b)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The int8ToStr function is unused in this file and is a duplicate of the one in kernel_version_linux.go. This introduces dead code and code duplication, which can lead to maintenance issues. This function should be removed from this file. To avoid duplication altogether, the function could be moved from kernel_version_linux.go to the shared kernel_version.go file.

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 improves code health in the pkg/utils package by adding comprehensive unit tests for kernel version utilities and refactoring the code to be platform-independent. The refactoring extracts version parsing logic into a testable function and splits platform-specific syscall operations into separate files using build tags.

Changes:

  • Refactored kernel version logic to extract isVersionLowerThan as a platform-independent, testable function
  • Split implementation into platform-specific files using build tags (kernel_version_linux.go for Linux, kernel_version_other.go for non-Linux)
  • Added comprehensive unit tests covering version comparisons, edge cases, and helper functions

Reviewed changes

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

File Description
pkg/utils/kernel_version.go Removed build constraints and syscall dependency; refactored to use platform-agnostic isVersionLowerThan helper
pkg/utils/kernel_version_linux.go Linux-specific implementation using syscall.Uname to get kernel version
pkg/utils/kernel_version_other.go Non-Linux stub implementation returning dummy version "0.0.0" for testing
pkg/utils/kernel_version_test.go Comprehensive tests for version comparison logic and int8 to string conversion

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

Comment on lines +26 to +36

func int8ToStr(arr []int8) string {
b := make([]byte, 0, len(arr))
for _, v := range arr {
if v == 0x00 {
break
}
b = append(b, byte(v))
}
return string(b)
}
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

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

The int8ToStr function is duplicated in both kernel_version_linux.go and kernel_version_other.go with identical implementations. Since this is a utility function that doesn't depend on platform-specific features, consider extracting it to the main kernel_version.go file to eliminate the duplication and improve maintainability.

Suggested change
func int8ToStr(arr []int8) string {
b := make([]byte, 0, len(arr))
for _, v := range arr {
if v == 0x00 {
break
}
b = append(b, byte(v))
}
return string(b)
}

Copilot uses AI. Check for mistakes.
Comment on lines +23 to +103
func TestIsVersionLowerThan(t *testing.T) {
tests := []struct {
name string
kernelVersion string
major int
minor int
want bool
}{
{
name: "lower major version",
kernelVersion: "4.15.0",
major: 5,
minor: 13,
want: true,
},
{
name: "higher major version",
kernelVersion: "6.0.0",
major: 5,
minor: 13,
want: false,
},
{
name: "same major, lower minor version",
kernelVersion: "5.10.0",
major: 5,
minor: 13,
want: true,
},
{
name: "same major, same minor version",
kernelVersion: "5.13.0",
major: 5,
minor: 13,
want: false,
},
{
name: "same major, higher minor version",
kernelVersion: "5.15.0",
major: 5,
minor: 13,
want: false,
},
{
name: "invalid version format - single number",
kernelVersion: "5",
major: 5,
minor: 13,
want: true,
},
{
name: "invalid version format - non-numeric",
kernelVersion: "abc.def",
major: 5,
minor: 13,
want: true,
},
{
name: "empty version string",
kernelVersion: "",
major: 5,
minor: 13,
want: true,
},
{
name: "complex version string (Ubuntu style)",
kernelVersion: "5.15.0-101-generic",
major: 5,
minor: 13,
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isVersionLowerThan(tt.kernelVersion, tt.major, tt.minor); got != tt.want {
t.Errorf("isVersionLowerThan(%v, %v, %v) = %v, want %v", tt.kernelVersion, tt.major, tt.minor, got, tt.want)
}
})
}
}
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

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

The public API function KernelVersionLowerThan5_13() is not tested. While the internal isVersionLowerThan function has comprehensive tests, the public function should also have tests to ensure the integration works correctly, especially since GetKernelVersion() is now platform-specific and could return different values on different platforms. Consider adding a test that calls KernelVersionLowerThan5_13() to verify the complete flow.

Copilot uses AI. Check for mistakes.
Signed-off-by: Yash Israni <118755067+yashisrani@users.noreply.github.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 27, 2026

Codecov Report

❌ Patch coverage is 57.89474% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 39.71%. Comparing base (e270b0b) to head (08e1bd8).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
pkg/utils/kernel_version_linux.go 58.33% 5 Missing ⚠️
pkg/controller/manage/manage_controller.go 0.00% 2 Missing ⚠️
pkg/utils/kernel_version.go 80.00% 1 Missing ⚠️
Files with missing lines Coverage Δ
pkg/utils/kernel_version.go 86.66% <80.00%> (+86.66%) ⬆️
pkg/controller/manage/manage_controller.go 53.82% <0.00%> (-0.99%) ⬇️
pkg/utils/kernel_version_linux.go 58.33% <58.33%> (ø)

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f9f2819...08e1bd8. Read the comment docs.

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

Copilot AI review requested due to automatic review settings March 3, 2026 11:35
@kmesh-bot
Copy link
Copy Markdown
Collaborator

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/test-infra repository.

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@kmesh-bot kmesh-bot added size/XL and removed size/XXL labels Mar 3, 2026
Copilot AI review requested due to automatic review settings March 3, 2026 12:44
@kmesh-bot kmesh-bot added size/XXL and removed size/XL labels Mar 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants