99import ldap
1010from ldap .ldapobject import ReconnectLDAPObject
1111from pytest_mh import MultihostHost
12- from pytest_mh .ssh import SSHPowerShellProcess
12+ from pytest_mh .conn import Powershell
1313from pytest_mh .utils .fs import LinuxFileSystem
1414from pytest_mh .utils .services import SystemdServices
1515
@@ -86,10 +86,10 @@ def remove_backup(self, backup_data: Any | None) -> None:
8686 else :
8787 raise TypeError (f"Only PurePath is supported as backup_data, got { type (backup_data )} " )
8888
89- if self .ssh .shell is SSHPowerShellProcess :
90- self .ssh .exec (["Remove-Item" , "-Force" , "-Recurse" , path ])
89+ if isinstance ( self .conn .shell , Powershell ) :
90+ self .conn .exec (["Remove-Item" , "-Force" , "-Recurse" , path ])
9191 else :
92- self .ssh .exec (["rm" , "-fr" , path ])
92+ self .conn .exec (["rm" , "-fr" , path ])
9393
9494 @abstractmethod
9595 def start (self ) -> None :
@@ -213,19 +213,22 @@ def __init__(self, *args, tls: bool = True, **kwargs) -> None:
213213 """Bind password ``config.bindpw``, defaults to ``Secret123``"""
214214
215215 # Lazy properties.
216- self .__conn : ReconnectLDAPObject | None = None
216+ self .__ldap_conn : ReconnectLDAPObject | None = None
217217 self .__naming_context : str | None = None
218218
219219 @property
220220 @retry (on = ldap .SERVER_DOWN )
221- def conn (self ) -> ReconnectLDAPObject :
221+ def ldap_conn (self ) -> ReconnectLDAPObject :
222222 """
223223 LDAP connection (``python-ldap`` library).
224224
225225 :rtype: ReconnectLDAPObject
226226 """
227- if not self .__conn :
228- newconn = ReconnectLDAPObject (f"ldap://{ self .ssh_host } " )
227+ if not self .__ldap_conn :
228+ # Use host from SSH if possible, otherwise fallback to hostname
229+ host = getattr (self .conn , "host" , self .hostname )
230+
231+ newconn = ReconnectLDAPObject (f"ldap://{ host } " )
229232 newconn .protocol_version = ldap .VERSION3
230233 newconn .set_option (ldap .OPT_REFERRALS , 0 )
231234
@@ -235,9 +238,9 @@ def conn(self) -> ReconnectLDAPObject:
235238 newconn .start_tls_s ()
236239
237240 newconn .simple_bind_s (self .binddn , self .bindpw )
238- self .__conn = newconn
241+ self .__ldap_conn = newconn
239242
240- return self .__conn
243+ return self .__ldap_conn
241244
242245 @property
243246 def naming_context (self ) -> str :
@@ -249,7 +252,7 @@ def naming_context(self) -> str:
249252 """
250253 if not self .__naming_context :
251254 attr = "defaultNamingContext"
252- result = self .conn .search_s ("" , ldap .SCOPE_BASE , attrlist = [attr ])
255+ result = self .ldap_conn .search_s ("" , ldap .SCOPE_BASE , attrlist = [attr ])
253256 if len (result ) != 1 :
254257 raise ValueError (f"Unexpected number of results for rootDSE query: { len (result )} " )
255258
@@ -265,9 +268,9 @@ def disconnect(self) -> None:
265268 """
266269 Disconnect LDAP connection.
267270 """
268- if self .__conn is not None :
269- self .__conn .unbind ()
270- self .__conn = None
271+ if self .__ldap_conn is not None :
272+ self .__ldap_conn .unbind ()
273+ self .__ldap_conn = None
271274
272275 def ldap_result_to_dict (
273276 self , result : list [tuple [str , dict [str , list [bytes ]]]]
0 commit comments