From 50dfe0c582908ab9bd2a5a573d7c0fb7ab0f553c Mon Sep 17 00:00:00 2001 From: saiashok0981 Date: Thu, 16 Jul 2026 23:35:05 +0530 Subject: [PATCH] test: add unit tests for SortIpAddresses in pkg/utils/net.go 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 --- pkg/utils/net_test.go | 75 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 pkg/utils/net_test.go diff --git a/pkg/utils/net_test.go b/pkg/utils/net_test.go new file mode 100644 index 00000000000..bc91b2be8c8 --- /dev/null +++ b/pkg/utils/net_test.go @@ -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"}, + }, + } + + 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) + } + }) + } +}