Skip to content

test: add unit tests for SortIpAddresses utility function#6128

Open
saiashok0981 wants to merge 1 commit into
fluid-cloudnative:masterfrom
saiashok0981:dev/2
Open

test: add unit tests for SortIpAddresses utility function#6128
saiashok0981 wants to merge 1 commit into
fluid-cloudnative:masterfrom
saiashok0981:dev/2

Conversation

@saiashok0981

Copy link
Copy Markdown

Description
Add comprehensive unit tests for the SortIpAddresses function in pkg/utils/net.go. Prior to this change, SortIpAddresses had zero test coverage despite being an exported utility function used across the codebase to sort IP addresses for CSI node operations.

Motivation
SortIpAddresses is a non-trivial function that performs IP address parsing, deduplication, and byte-level comparison sorting. The absence of tests makes it impossible to detect regressions if the sorting or deduplication behavior changes. This is particularly important because:

The function is called during CSI node publish operations where incorrect IP ordering could affect runtime behavior.
IP address sorting uses bytes.Compare on parsed net.IP values, which has subtle semantics for IPv4-mapped IPv6 addresses and needs regression guards.
The project's CI setup measures code coverage (see codecov.yml) — untested exported functions represent gaps in coverage metrics.
This PR brings SortIpAddresses up to the same testing standard as other utility functions in the package.

Test Name | Scenario -- | -- empty_input | Empty slice returns nil single_ip | Single IP is returned unchanged already_sorted | Pre-sorted IPs maintain order unsorted_ips | IPs correctly reordered (10.0.0.3, 10.0.0.1 → sorted) duplicate_ips_are_deduplicated | Duplicate IPs are removed cross_subnet_ordering | IPs across different /8 subnets sort correctly last_octet_ordering | Numeric last-octet ordering (not lexicographic)

Signed-off-by: saiashok103@gmail.com

SortIpAddresses is an exported utility function in pkg/utils/net.go
that performs IP address deduplication and byte-level sorting. Despite
being used in CSI-related code paths and being a non-trivial operation,
it had zero test coverage before this change.

This commit introduces pkg/utils/net_test.go with 7 table-driven test
cases covering the following scenarios:

  - empty_input: empty slice returns nil (consistent with append semantics)
  - single_ip: single IP is returned unchanged
  - already_sorted: pre-sorted IPs maintain their order
  - unsorted_ips: IPs are correctly reordered
  - duplicate_ips_are_deduplicated: the internal map deduplicates entries
  - cross_subnet_ordering: validates 10.x < 172.x < 192.x ordering
  - last_octet_ordering: numeric ordering (3 < 20 < 100), not lexicographic

The tests use the standard table-driven pattern consistent with other
test files in this package (e.g., slice_test.go, errors_test.go) and
use only stdlib reflect and testing packages.

All tests pass with: go test ./pkg/utils/... -run TestSortIpAddresses

Signed-off-by: saiashok103@gmail.com
@fluid-e2e-bot

fluid-e2e-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

[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 trafalgarzzz for approval by writing /assign @trafalgarzzz in a comment. 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

@fluid-e2e-bot

fluid-e2e-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

Hi @saiashok0981. Thanks for your PR.

I'm waiting for a fluid-cloudnative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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

@sonarqubecloud

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces a new test file, pkg/utils/net_test.go, to verify the functionality of the SortIpAddresses utility. The reviewer suggests enhancing the test suite by adding coverage for edge cases, specifically invalid IP strings and mixed IPv4/IPv6 addresses, to ensure greater robustness and reliability.

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/utils/net_test.go
Comment on lines +25 to +65
tests := []struct {
name string
ips []string
want []string
}{
{
name: "empty_input",
ips: []string{},
want: nil,
},
{
name: "single_ip",
ips: []string{"10.0.0.1"},
want: []string{"10.0.0.1"},
},
{
name: "already_sorted",
ips: []string{"10.0.0.1", "10.0.0.2", "10.0.0.3"},
want: []string{"10.0.0.1", "10.0.0.2", "10.0.0.3"},
},
{
name: "unsorted_ips",
ips: []string{"10.0.0.3", "10.0.0.1", "10.0.0.2"},
want: []string{"10.0.0.1", "10.0.0.2", "10.0.0.3"},
},
{
name: "duplicate_ips_are_deduplicated",
ips: []string{"10.0.0.1", "10.0.0.2", "10.0.0.1"},
want: []string{"10.0.0.1", "10.0.0.2"},
},
{
name: "cross_subnet_ordering",
ips: []string{"192.168.1.1", "10.0.0.1", "172.16.0.1"},
want: []string{"10.0.0.1", "172.16.0.1", "192.168.1.1"},
},
{
name: "last_octet_ordering",
ips: []string{"10.0.0.100", "10.0.0.20", "10.0.0.3"},
want: []string{"10.0.0.3", "10.0.0.20", "10.0.0.100"},
},
}

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.

medium

The test suite is a great start, but it could be made more comprehensive by including tests for important edge cases. This would improve the robustness of the SortIpAddresses utility.

Specifically, please consider adding test cases for:

  1. Invalid IP strings: The SortIpAddresses function does not currently specify behavior for invalid IP strings (e.g., "not-an-ip", ""). net.ParseIP returns nil for these, and ip.String() on a nil net.IP results in the string "<nil>" in the output. A test should be added to assert the desired behavior, which would ideally be to filter out invalid IPs.

  2. Mixed IPv4 and IPv6 addresses: As noted in the pull request description, bytes.Compare has specific behavior for different IP versions. Adding a test with a mix of IPv4 and IPv6 addresses would ensure the sorting is correct and provide a valuable regression guard.

Here are some example test cases you could add:

{
name: "with_invalid_ips",
ips: []string{"10.0.0.2", "not-an-ip", "10.0.0.1"},
want: []string{"10.0.0.1", "10.0.0.2"},
},
{
name: "mixed_ipv4_and_ipv6",
ips: []string{"2001:db8::1", "::1", "192.168.1.1", "10.0.0.1"},
want: []string{"::1", "10.0.0.1", "192.168.1.1", "2001:db8::1"},
},

@cheyang

cheyang commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

@saiashok0981 — this PR is blocked from merging by a failing DCO check.
The DCO bot requires every commit to carry a Signed-off-by: trailer; without it the PR cannot land regardless of lgtm / approved labels or otherwise-green CI.

Please resolve this now. For a single-commit PR:

git commit --amend -s
git push --force-with-lease

For multiple commits on this branch:

git rebase --signoff origin/master
git push --force-with-lease

The code review side is finished and any merge labels already on the PR will take effect once DCO turns green. Reference: https://github.com/apps/dco

@cheyang

cheyang commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

/copilot review

Comment thread pkg/utils/net_test.go
name: "last_octet_ordering",
ips: []string{"10.0.0.100", "10.0.0.20", "10.0.0.3"},
want: []string{"10.0.0.3", "10.0.0.20", "10.0.0.100"},
},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice, focused table here — the cases already pin down the important behaviors (dedup, and byte-wise numeric ordering rather than lexical, which last_octet_ordering and cross_subnet_ordering catch well). Two optional additions would round it out:

  1. A mixed IPv4/IPv6 case. SortIpAddresses parses everything to 16-byte net.IP and sorts with bytes.Compare, so cross-version ordering is worth locking down as a regression guard, e.g. ["2001:db8::1", "::1", "192.168.1.1", "10.0.0.1"] -> ["::1", "10.0.0.1", "192.168.1.1", "2001:db8::1"].
  2. An invalid-IP case to document current behavior. Note the function does not filter invalid input: net.ParseIP("not-an-ip") returns nil, and nil.String() yields "<nil>", which then appears in the output. So ["10.0.0.1", "not-an-ip"] currently sorts to ["<nil>", "10.0.0.1"] (the nil parses to an empty slice and sorts first). Worth asserting the real behavior — or, if filtering is the intended contract, that would be a change to the function itself rather than the test.

Both are non-blocking.

@codecov

codecov Bot commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.13%. Comparing base (3e44ad2) to head (50dfe0c).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6128      +/-   ##
==========================================
+ Coverage   65.08%   65.13%   +0.05%     
==========================================
  Files         485      485              
  Lines       33989    33989              
==========================================
+ Hits        22122    22139      +17     
+ Misses      10126    10109      -17     
  Partials     1741     1741              

☔ 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.

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.

2 participants