@@ -141,7 +141,12 @@ pub fn node_primary_address(node: &Node) -> AddressCandidates {
141141
142142#[ cfg( test) ]
143143mod tests {
144- use crate :: utils:: error_full_message;
144+ use stackable_operator:: {
145+ commons:: listener:: AddressType ,
146+ k8s_openapi:: api:: core:: v1:: { Node , NodeAddress , NodeStatus } ,
147+ } ;
148+
149+ use crate :: utils:: { error_full_message, node_primary_address} ;
145150
146151 #[ test]
147152 fn error_messages ( ) {
@@ -159,4 +164,71 @@ mod tests {
159164 "leaf: middleware: root error"
160165 ) ;
161166 }
167+
168+ #[ test]
169+ fn node_with_only_ips_primary_address_returns_external_ip ( ) {
170+ let node = node_from_addresses ( vec ! [ ( "InternalIP" , "10.1.2.3" ) , ( "ExternalIP" , "1.2.3.4" ) ] ) ;
171+ let node_primary_address = node_primary_address ( & node) ;
172+ assert_eq ! (
173+ node_primary_address. pick( AddressType :: Ip ) ,
174+ Some ( ( "1.2.3.4" , AddressType :: Ip ) )
175+ ) ;
176+ assert_eq ! (
177+ node_primary_address. pick( AddressType :: Hostname ) ,
178+ Some ( ( "1.2.3.4" , AddressType :: Ip ) )
179+ ) ;
180+ }
181+
182+ #[ test]
183+ fn node_with_only_hostname_primary_address_returns_hostname ( ) {
184+ let node = node_from_addresses ( vec ! [
185+ ( "Hostname" , "first-hostname" ) ,
186+ ( "Hostname" , "second-hostname" ) ,
187+ ] ) ;
188+ let node_primary_address = node_primary_address ( & node) ;
189+ assert_eq ! (
190+ node_primary_address. pick( AddressType :: Ip ) ,
191+ Some ( ( "first-hostname" , AddressType :: Hostname ) )
192+ ) ;
193+ assert_eq ! (
194+ node_primary_address. pick( AddressType :: Hostname ) ,
195+ Some ( ( "first-hostname" , AddressType :: Hostname ) )
196+ ) ;
197+ }
198+
199+ #[ test]
200+ fn node_with_hostname_and_ips_primary_address ( ) {
201+ let node = node_from_addresses ( vec ! [
202+ ( "Hostname" , "node-0" ) ,
203+ ( "ExternalIP" , "1.2.3.4" ) ,
204+ ( "InternalIP" , "10.1.2.3" ) ,
205+ ] ) ;
206+ let node_primary_address = node_primary_address ( & node) ;
207+ assert_eq ! (
208+ node_primary_address. pick( AddressType :: Ip ) ,
209+ Some ( ( "1.2.3.4" , AddressType :: Ip ) )
210+ ) ;
211+ assert_eq ! (
212+ node_primary_address. pick( AddressType :: Hostname ) ,
213+ Some ( ( "node-0" , AddressType :: Hostname ) )
214+ ) ;
215+ }
216+
217+ fn node_from_addresses < ' a > ( addresses : impl IntoIterator < Item = ( & ' a str , & ' a str ) > ) -> Node {
218+ Node {
219+ status : Some ( NodeStatus {
220+ addresses : Some (
221+ addresses
222+ . into_iter ( )
223+ . map ( |( ty, addr) | NodeAddress {
224+ type_ : ty. to_string ( ) ,
225+ address : addr. to_string ( ) ,
226+ } )
227+ . collect ( ) ,
228+ ) ,
229+ ..Default :: default ( )
230+ } ) ,
231+ ..Default :: default ( )
232+ }
233+ }
162234}
0 commit comments