test: add unit tests for sortServicePortStatus and sortServiceLoadBalancerIngress#7710
test: add unit tests for sortServicePortStatus and sortServiceLoadBalancerIngress#7710Goutham-Annem wants to merge 1 commit into
Conversation
…ancerIngress Adds TestSortServicePortStatus and TestSortServiceLoadBalancerIngress covering all sort keys: port/protocol/error ordering for PortStatus, and hostname/IP/IPMode ordering for LoadBalancerIngress. Signed-off-by: Goutham Annem <gouthemannem@gmail.com>
|
@Goutham-Annem: The label(s) DetailsIn response to this:
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. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Summary of ChangesHello, 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 internal service helper functions. By introducing unit tests for sorting logic related to service port statuses and load balancer ingresses, the changes ensure that these components behave consistently and correctly under various input conditions, fixing issue #7657. Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces unit tests, specifically TestSortServicePortStatus and TestSortServiceLoadBalancerIngress, to verify the sorting of service port statuses and load balancer ingresses. The review feedback correctly identifies a flaw in the test implementation: because the sorting functions operate in-place, the 1000-iteration loops designed to eliminate randomness only sort an unsorted slice in the first iteration, while the remaining 999 iterations run on already-sorted data. To ensure robust and deterministic testing, the tests should be updated to copy the slices before sorting them in each iteration.
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.
| for range 1000 { // eliminate randomness in sorting | ||
| sortServicePortStatus(tt.ports) | ||
| pass := assert.Equalf(t, tt.want, tt.ports, "sortServicePortStatus(%v)", tt.ports) | ||
| if !pass { | ||
| break | ||
| } | ||
| } |
There was a problem hiding this comment.
The for range 1000 loop is intended to eliminate randomness and ensure deterministic sorting. However, because sortServicePortStatus sorts the slice in-place, tt.ports is sorted in the very first iteration. In the remaining 999 iterations, the function is called on an already-sorted slice, which defeats the purpose of the loop.
To properly test determinism, you should create a fresh copy of the slice in each iteration. Note that the underlying sortServicePortStatus function currently violates strict weak ordering when both elements have a nil Error field (returning true in both directions), which makes deterministic testing even more critical.
for range 1000 {
ports := make([]corev1.PortStatus, len(tt.ports))
copy(ports, tt.ports)
sortServicePortStatus(ports)
pass := assert.Equalf(t, tt.want, ports, "sortServicePortStatus(%v)", tt.ports)
if !pass {
break
}
}| for range 1000 { // eliminate randomness in sorting | ||
| sortServiceLoadBalancerIngress(tt.ingresses) | ||
| pass := assert.Equalf(t, tt.want, tt.ingresses, "sortServiceLoadBalancerIngress(%v)", tt.ingresses) | ||
| if !pass { | ||
| break | ||
| } | ||
| } |
There was a problem hiding this comment.
Similar to TestSortServicePortStatus, sortServiceLoadBalancerIngress sorts the slice in-place. As a result, tt.ingresses is sorted in the first iteration, and the subsequent 999 iterations run on an already-sorted slice.
To ensure the test actually verifies determinism across multiple runs, create a copy of the slice inside the loop. Note that the underlying sortServiceLoadBalancerIngress function currently violates strict weak ordering when both elements have a nil IPMode field (returning true in both directions), which makes deterministic testing even more critical.
for range 1000 {
ingresses := make([]corev1.LoadBalancerIngress, len(tt.ingresses))
copy(ingresses, tt.ingresses)
sortServiceLoadBalancerIngress(ingresses)
pass := assert.Equalf(t, tt.want, ingresses, "sortServiceLoadBalancerIngress(%v)", tt.ingresses)
if !pass {
break
}
}There was a problem hiding this comment.
Pull request overview
Adds new unit tests in the helper utilities test suite to validate the sorting semantics of sortServicePortStatus and sortServiceLoadBalancerIngress in pkg/util/helper/service.go, improving confidence in these non-trivial ordering rules.
Changes:
- Add
TestSortServicePortStatuscovering port number, protocol, nil-vs-non-nil error, and error-string ordering. - Add
TestSortServiceLoadBalancerIngresscovering hostname-vs-IP priority, hostname ordering, IP ordering, and IPMode nil/value ordering.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| for range 1000 { // eliminate randomness in sorting | ||
| sortServicePortStatus(tt.ports) | ||
| pass := assert.Equalf(t, tt.want, tt.ports, "sortServicePortStatus(%v)", tt.ports) | ||
| if !pass { | ||
| break | ||
| } | ||
| } | ||
| }) | ||
| } |
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| for range 1000 { // eliminate randomness in sorting | ||
| sortServiceLoadBalancerIngress(tt.ingresses) | ||
| pass := assert.Equalf(t, tt.want, tt.ingresses, "sortServiceLoadBalancerIngress(%v)", tt.ingresses) | ||
| if !pass { | ||
| break | ||
| } | ||
| } | ||
| }) | ||
| } |
| { | ||
| name: "same port and protocol, nil error sorts first", | ||
| ports: []corev1.PortStatus{ | ||
| {Port: 80, Protocol: "TCP", Error: new("some-error")}, | ||
| {Port: 80, Protocol: "TCP"}, | ||
| }, |
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7710 +/- ##
==========================================
+ Coverage 42.06% 42.08% +0.01%
==========================================
Files 879 879
Lines 54831 54831
==========================================
+ Hits 23063 23073 +10
+ Misses 30023 30017 -6
+ Partials 1745 1741 -4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
What type of PR is this?
/kind test
What this PR does / why we need it
Adds unit tests for two unexported helper functions in
pkg/util/helper/service.go:sortServicePortStatus— sorts[]corev1.PortStatusby port number, then protocol, then nil error before non-nil, then by error string valuesortServiceLoadBalancerIngress— sorts[]corev1.LoadBalancerIngressby non-empty hostname before empty, then hostname alphabetically, then IP, then non-nil IPMode before nil, then by IPMode valueEach test case uses the
for range 1000loop pattern (matching the existingTestDedupeAndSortServiceLoadBalancerIngressstyle) to eliminate any sort-algorithm randomness.Which issue(s) this PR fixes
Fixes #7657
Does this PR introduce a user-facing change?
No
Signed-off-by: Goutham Annem gouthemannem@gmail.com