1919import pytest
2020from packaging import version
2121from .wrappers import CLI , xapi
22- from functools import wraps
22+ import tempfile
2323
2424logger = logging .getLogger (__name__ )
2525
@@ -200,32 +200,6 @@ def _softened(dut_ssh):
200200 return _softened
201201
202202
203- def retry (limit ):
204- def decorator (fn ):
205- @wraps (fn )
206- def wrapper (* args , ** kwargs ):
207- attempt , limit_ = 0 , limit
208- result = None
209- while attempt < limit_ :
210- try :
211- result = fn (* args , ** kwargs )
212- break
213- except Exception as e :
214- attempt += 1
215- logging .error (
216- "An error occurred in %s, attempt %d of %d: %s" ,
217- fn .__name__ ,
218- attempt ,
219- limit_ ,
220- e ,
221- )
222- return result
223-
224- return wrapper
225-
226- return decorator
227-
228-
229203class _CLI_wrapper :
230204 _cli = None
231205
@@ -243,7 +217,6 @@ def close_and_re_init(self):
243217 def close (self , * args , ** kwargs ):
244218 return self ._cli .close (* args , ** kwargs )
245219
246- @retry (3 )
247220 def login (self , * args , ** kwargs ):
248221 return self ._cli .login (* args , ** kwargs )
249222
@@ -322,20 +295,41 @@ def args(self):
322295 return self ._cli .args
323296
324297
298+ class _SSH_CLI_wrapper (_CLI_wrapper ):
299+ def login (self , * args , ** kwargs ):
300+ attempt = 0
301+ while attempt < 3 :
302+ try :
303+ self ._cli .login (* args , ** kwargs )
304+ break
305+ except Exception as e :
306+ attempt += 1
307+ with open (self ._cli .ssh_debug_filename , "r" , encoding = "utf-8" ) as f :
308+ ssh_debug = f .read ()
309+ logging .error (
310+ "An error occurred during login, attempt %d\n %s%s" ,
311+ attempt ,
312+ ssh_debug ,
313+ e ,
314+ )
315+
316+
325317def create_ssh_fixture (name ):
326318 @pytest .fixture (scope = "session" , name = f"{ name } _ssh" )
327319 def _ssh (request ):
328- ssh = _CLI_wrapper (f"ssh://{ request .getfixturevalue (f'{ name } _hostname' )} " )
329- # do not break the fixture if SSH failed
330- # pass the failure into the test
331- try :
320+ with tempfile .NamedTemporaryFile (delete = True ) as ssh_debug_file :
321+ ssh = _SSH_CLI_wrapper (
322+ f"ssh://{ request .getfixturevalue (f'{ name } _hostname' )} " ,
323+ ssh_debug_filename = ssh_debug_file .name ,
324+ )
332325 if ssh .cli_flavor == "mos" :
333- ssh .sendcmd ("enable" )
334- # Disable pagination
335- ssh .sendcmd ("terminal length 0" )
336- except AttributeError :
337- pass
338- yield ssh
326+ # do not break the fixture if SSH failed
327+ # pass the failure into the test
328+ try :
329+ ssh .sendcmd ("enable" )
330+ except AttributeError :
331+ pass
332+ yield ssh
339333
340334 return _ssh
341335
0 commit comments