@@ -17,6 +17,7 @@ limitations under the License.
1717package k8s
1818
1919import (
20+ "slices"
2021 "testing"
2122
2223 apiv1 "k8s.io/api/core/v1"
@@ -60,13 +61,15 @@ func TestGetNodeIP(t *testing.T) {
6061 name string
6162 cs * testclient.Clientset
6263 nodeName string
63- ea string
64+ ea [] string
6465 useInternalIP bool
6566 }{
6667 {
6768 "empty node list" ,
6869 testclient .NewSimpleClientset (),
69- "demo" , "" , true ,
70+ "demo" ,
71+ []string {},
72+ true ,
7073 },
7174 {
7275 "node does not exist" ,
@@ -82,10 +85,12 @@ func TestGetNodeIP(t *testing.T) {
8285 },
8386 },
8487 },
85- }}}), "notexistnode" , "" , true ,
88+ }}}), "notexistnode" ,
89+ []string {},
90+ true ,
8691 },
8792 {
88- "node exist and only has an internal IP address (useInternalIP=false)" ,
93+ "node exists and only has an internal IP address (useInternalIP=false)" ,
8994 testclient .NewSimpleClientset (& apiv1.NodeList {Items : []apiv1.Node {{
9095 ObjectMeta : metav1.ObjectMeta {
9196 Name : "demo" ,
@@ -98,10 +103,56 @@ func TestGetNodeIP(t *testing.T) {
98103 },
99104 },
100105 },
101- }}}), "demo" , "10.0.0.1" , false ,
106+ }}}), "demo" ,
107+ []string {"10.0.0.1" },
108+ false ,
102109 },
103110 {
104- "node exist and only has an internal IP address" ,
111+ "node exists has an internal IP address and an empty external IP address (useInternalIP=false)" ,
112+ testclient .NewSimpleClientset (& apiv1.NodeList {Items : []apiv1.Node {{
113+ ObjectMeta : metav1.ObjectMeta {
114+ Name : "demo" ,
115+ },
116+ Status : apiv1.NodeStatus {
117+ Addresses : []apiv1.NodeAddress {
118+ {
119+ Type : apiv1 .NodeExternalIP ,
120+ Address : "" ,
121+ },
122+ {
123+ Type : apiv1 .NodeInternalIP ,
124+ Address : "10.0.0.1" ,
125+ },
126+ },
127+ },
128+ }}}), "demo" ,
129+ []string {"10.0.0.1" },
130+ false ,
131+ },
132+ {
133+ "node exists and has two internal IP address (useInternalIP=false)" ,
134+ testclient .NewSimpleClientset (& apiv1.NodeList {Items : []apiv1.Node {{
135+ ObjectMeta : metav1.ObjectMeta {
136+ Name : "demo" ,
137+ },
138+ Status : apiv1.NodeStatus {
139+ Addresses : []apiv1.NodeAddress {
140+ {
141+ Type : apiv1 .NodeInternalIP ,
142+ Address : "10.0.0.1" ,
143+ },
144+ {
145+ Type : apiv1 .NodeInternalIP ,
146+ Address : "fd00::1" ,
147+ },
148+ },
149+ },
150+ }}}), "demo" ,
151+ []string {"10.0.0.1" , "fd00::1" },
152+ false ,
153+ },
154+ {
155+ "node exists and only has an internal IP address" ,
105156 testclient .NewSimpleClientset (& apiv1.NodeList {Items : []apiv1.Node {{
106157 ObjectMeta : metav1.ObjectMeta {
107158 Name : "demo" ,
@@ -114,7 +165,31 @@ func TestGetNodeIP(t *testing.T) {
114165 },
115166 },
116167 },
117- }}}), "demo" , "10.0.0.1" , true ,
168+ }}}), "demo" ,
169+ []string {"10.0.0.1" },
170+ true ,
171+ },
172+ {
173+ "node exists and has two internal IP address" ,
174+ testclient .NewSimpleClientset (& apiv1.NodeList {Items : []apiv1.Node {{
175+ ObjectMeta : metav1.ObjectMeta {
176+ Name : "demo" ,
177+ },
178+ Status : apiv1.NodeStatus {
179+ Addresses : []apiv1.NodeAddress {
180+ {
181+ Type : apiv1 .NodeInternalIP ,
182+ Address : "10.0.0.1" ,
183+ },
184+ {
185+ Type : apiv1 .NodeInternalIP ,
186+ Address : "fd00::1" ,
187+ },
188+ },
189+ },
190+ }}}), "demo" ,
191+ []string {"10.0.0.1" , "fd00::1" },
192+ true ,
118193 },
119194 {
120195 "node exist and only has an external IP address" ,
@@ -130,7 +205,27 @@ func TestGetNodeIP(t *testing.T) {
130205 },
131206 },
132207 },
133- }}}), "demo" , "10.0.0.1" , false ,
208+ }}}), "demo" ,
209+ []string {"10.0.0.1" },
210+ false ,
211+ },
212+ {
213+ "node exist and only has an external IP address (useInternalIP=true)" ,
214+ testclient .NewSimpleClientset (& apiv1.NodeList {Items : []apiv1.Node {{
215+ ObjectMeta : metav1.ObjectMeta {
216+ Name : "demo" ,
217+ },
218+ Status : apiv1.NodeStatus {
219+ Addresses : []apiv1.NodeAddress {
220+ {
221+ Type : apiv1 .NodeExternalIP ,
222+ Address : "10.0.0.1" ,
223+ },
224+ },
225+ },
226+ }}}), "demo" ,
227+ []string {},
228+ true ,
134229 },
135230 {
136231 "multiple nodes - choose the right one" ,
@@ -162,10 +257,12 @@ func TestGetNodeIP(t *testing.T) {
162257 },
163258 },
164259 }}),
165- "demo2" , "10.0.0.2" , true ,
260+ "demo2" ,
261+ []string {"10.0.0.2" },
262+ true ,
166263 },
167264 {
168- "node with both IP internal and external IP address - returns external IP" ,
265+ "node with both internal and external IP address - returns external IP" ,
169266 testclient .NewSimpleClientset (& apiv1.NodeList {Items : []apiv1.Node {{
170267 ObjectMeta : metav1.ObjectMeta {
171268 Name : "demo" ,
@@ -182,10 +279,12 @@ func TestGetNodeIP(t *testing.T) {
182279 },
183280 },
184281 }}}),
185- "demo" , "10.0.0.2" , false ,
282+ "demo" ,
283+ []string {"10.0.0.2" },
284+ false ,
186285 },
187286 {
188- "node with both IP internal and external IP address - returns internal IP" ,
287+ "node with both internal and external IP address - returns internal IP" ,
189288 testclient .NewSimpleClientset (& apiv1.NodeList {Items : []apiv1.Node {{
190289 ObjectMeta : metav1.ObjectMeta {
191290 Name : "demo" ,
@@ -202,14 +301,16 @@ func TestGetNodeIP(t *testing.T) {
202301 },
203302 },
204303 }}}),
205- "demo" , "10.0.0.2" , true ,
304+ "demo" ,
305+ []string {"10.0.0.2" },
306+ true ,
206307 },
207308 }
208309
209310 for _ , fk := range fKNodes {
210- address := GetNodeIPOrName (fk .cs , fk .nodeName , fk .useInternalIP )
211- if address != fk .ea {
212- t .Errorf ("%v - expected %s , but returned %s " , fk .name , fk .ea , address )
311+ addresses := GetNodeIPs (fk .cs , fk .nodeName , fk .useInternalIP )
312+ if ! slices . Equal ( addresses , fk .ea ) {
313+ t .Errorf ("%v - expected %v , but returned %v " , fk .name , fk .ea , addresses )
213314 }
214315 }
215316}
0 commit comments