k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/kubelet/certificate/kubelet_test.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package certificate 18 19 import ( 20 "net" 21 "reflect" 22 "testing" 23 24 v1 "k8s.io/api/core/v1" 25 netutils "k8s.io/utils/net" 26 ) 27 28 func TestAddressesToHostnamesAndIPs(t *testing.T) { 29 tests := []struct { 30 name string 31 addresses []v1.NodeAddress 32 wantDNSNames []string 33 wantIPs []net.IP 34 }{ 35 { 36 name: "empty", 37 addresses: nil, 38 wantDNSNames: nil, 39 wantIPs: nil, 40 }, 41 { 42 name: "ignore empty values", 43 addresses: []v1.NodeAddress{{Type: v1.NodeHostName, Address: ""}}, 44 wantDNSNames: nil, 45 wantIPs: nil, 46 }, 47 { 48 name: "ignore invalid IPs", 49 addresses: []v1.NodeAddress{ 50 {Type: v1.NodeInternalIP, Address: "1.2"}, 51 {Type: v1.NodeExternalIP, Address: "3.4"}, 52 }, 53 wantDNSNames: nil, 54 wantIPs: nil, 55 }, 56 { 57 name: "dedupe values", 58 addresses: []v1.NodeAddress{ 59 {Type: v1.NodeHostName, Address: "hostname"}, 60 {Type: v1.NodeExternalDNS, Address: "hostname"}, 61 {Type: v1.NodeInternalDNS, Address: "hostname"}, 62 {Type: v1.NodeInternalIP, Address: "1.1.1.1"}, 63 {Type: v1.NodeExternalIP, Address: "1.1.1.1"}, 64 }, 65 wantDNSNames: []string{"hostname"}, 66 wantIPs: []net.IP{netutils.ParseIPSloppy("1.1.1.1")}, 67 }, 68 { 69 name: "order values", 70 addresses: []v1.NodeAddress{ 71 {Type: v1.NodeHostName, Address: "hostname-2"}, 72 {Type: v1.NodeExternalDNS, Address: "hostname-1"}, 73 {Type: v1.NodeInternalDNS, Address: "hostname-3"}, 74 {Type: v1.NodeInternalIP, Address: "2.2.2.2"}, 75 {Type: v1.NodeExternalIP, Address: "1.1.1.1"}, 76 {Type: v1.NodeInternalIP, Address: "3.3.3.3"}, 77 }, 78 wantDNSNames: []string{"hostname-1", "hostname-2", "hostname-3"}, 79 wantIPs: []net.IP{netutils.ParseIPSloppy("1.1.1.1"), netutils.ParseIPSloppy("2.2.2.2"), netutils.ParseIPSloppy("3.3.3.3")}, 80 }, 81 { 82 name: "handle IP and DNS hostnames", 83 addresses: []v1.NodeAddress{ 84 {Type: v1.NodeHostName, Address: "hostname"}, 85 {Type: v1.NodeHostName, Address: "1.1.1.1"}, 86 }, 87 wantDNSNames: []string{"hostname"}, 88 wantIPs: []net.IP{netutils.ParseIPSloppy("1.1.1.1")}, 89 }, 90 } 91 for _, tt := range tests { 92 t.Run(tt.name, func(t *testing.T) { 93 gotDNSNames, gotIPs := addressesToHostnamesAndIPs(tt.addresses) 94 if !reflect.DeepEqual(gotDNSNames, tt.wantDNSNames) { 95 t.Errorf("addressesToHostnamesAndIPs() gotDNSNames = %v, want %v", gotDNSNames, tt.wantDNSNames) 96 } 97 if !reflect.DeepEqual(gotIPs, tt.wantIPs) { 98 t.Errorf("addressesToHostnamesAndIPs() gotIPs = %v, want %v", gotIPs, tt.wantIPs) 99 } 100 }) 101 } 102 }