Skip to content

Commit 62dff29

Browse files
authored
Rework SSH debugging output to tmp file (#31)
Rework SSH login and debug output to tmp file.
1 parent 54d6105 commit 62dff29

File tree

2 files changed

+37
-40
lines changed

2 files changed

+37
-40
lines changed

src/pytest_netdut/factories.py

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import pytest
2020
from packaging import version
2121
from .wrappers import CLI, xapi
22-
from functools import wraps
22+
import tempfile
2323

2424
logger = 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-
229203
class _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+
325317
def 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

src/pytest_netdut/px.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -------------------------------------------------------------------------------
2-
# - Copyright (c) 2021 Arista Networks, Inc. All rights reserved.
2+
# - Copyright (c) 2021-2024 Arista Networks, Inc. All rights reserved.
33
# -------------------------------------------------------------------------------
44
# - Author:
55
@@ -144,13 +144,16 @@ def __init__( # pylint: disable=dangerous-default-value,too-many-arguments
144144
enable_cli_timeout=False,
145145
cli_flavor="mos",
146146
ignore_encoding_errors=False,
147+
ssh_debug_filename="/dev/null",
147148
extra_args=[],
148149
):
149150
o = six.moves.urllib.parse.urlparse(url) # pylint: disable=invalid-name
150151
self.cmd = o.scheme
152+
self.ssh_debug_filename = ssh_debug_filename
151153
if self.cmd == "ssh":
152154
self.args = ["%s@%s" % (username, o.hostname)]
153-
self.args += ["-o LogLevel DEBUG3"]
155+
self.args += ["-vvv"]
156+
self.args += [f"-E{ssh_debug_filename}"]
154157
self.args += ["-o ConnectionAttempts 10"]
155158
self.args += ["-o StrictHostKeyChecking no"]
156159
self.args += ["-o UserKnownHostsFile /dev/null"]

0 commit comments

Comments
 (0)