|
| 1 | +// Copyright 2026 The Kubernetes Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package sandbox |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | +) |
| 20 | + |
| 21 | +func TestSelectPodIP(t *testing.T) { |
| 22 | + cases := []struct { |
| 23 | + name string |
| 24 | + ips []string |
| 25 | + expected string |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "no IPs", |
| 29 | + ips: nil, |
| 30 | + expected: "", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "single valid IPv4", |
| 34 | + ips: []string{"10.244.0.42"}, |
| 35 | + expected: "10.244.0.42", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "single valid IPv6", |
| 39 | + ips: []string{"2001:db8::1"}, |
| 40 | + expected: "2001:db8::1", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "dual-stack prioritizing IPv4 first", |
| 44 | + ips: []string{"10.244.0.42", "2001:db8::1"}, |
| 45 | + expected: "10.244.0.42", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "dual-stack prioritizing IPv4 when IPv6 is first", |
| 49 | + ips: []string{"2001:db8::1", "10.244.0.42"}, |
| 50 | + expected: "10.244.0.42", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "multiple IPv6 selects first parseable", |
| 54 | + ips: []string{"2001:db8::1", "2001:db8::2"}, |
| 55 | + expected: "2001:db8::1", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "ignores invalid IP and falls back to valid IPv4", |
| 59 | + ips: []string{"not-a-valid-ip", "10.244.0.42"}, |
| 60 | + expected: "10.244.0.42", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "ignores invalid IP and falls back to valid IPv6", |
| 64 | + ips: []string{"not-a-valid-ip", "2001:db8::1"}, |
| 65 | + expected: "2001:db8::1", |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "all invalid IPs leaves it empty", |
| 69 | + ips: []string{"not-a-valid-ip", "bad-address"}, |
| 70 | + expected: "", |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "normalizes IP address format (IPv4 whitespace)", |
| 74 | + ips: []string{" 192.168.1.1 "}, |
| 75 | + expected: "192.168.1.1", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "normalizes IP address format (IPv6 compression)", |
| 79 | + ips: []string{"2001:db8:0:0:0:0:2:1"}, |
| 80 | + expected: "2001:db8::2:1", |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "normalizes IP address format (IPv4-mapped IPv6)", |
| 84 | + ips: []string{"::ffff:10.0.0.1"}, |
| 85 | + expected: "10.0.0.1", |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + for _, tc := range cases { |
| 90 | + t.Run(tc.name, func(t *testing.T) { |
| 91 | + got := selectPodIP(tc.ips) |
| 92 | + if got != tc.expected { |
| 93 | + t.Errorf("expected selectPodIP %q, got %q", tc.expected, got) |
| 94 | + } |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
0 commit comments