Repository navigation
test: add unit tests for SortIpAddresses utility function #6128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| Copyright 2023 The Fluid Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package utils | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestSortIpAddresses(t *testing.T) { | ||
| 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"}, | ||
| }, | ||
| } | ||
|
Comment on lines
+25
to
+65
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 Specifically, please consider adding test cases for:
Here are some example test cases you could add: { |
||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := SortIpAddresses(tt.ips) | ||
| if !reflect.DeepEqual(got, tt.want) { | ||
| t.Errorf("SortIpAddresses() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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_orderingandcross_subnet_orderingcatch well). Two optional additions would round it out:SortIpAddressesparses everything to 16-bytenet.IPand sorts withbytes.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"].net.ParseIP("not-an-ip")returns nil, andnil.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.