Skip to content

Commit

Permalink
Merge pull request FRRouting#16762 from LabNConsulting/chopps/diag-diff
Browse files Browse the repository at this point in the history
improve @Retry decorator
  • Loading branch information
Jafaral authored Sep 7, 2024
2 parents 4ec2f48 + 956edf6 commit 63148da
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
10 changes: 8 additions & 2 deletions tests/topotests/lib/common_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,13 @@ def func_retry(*args, **kwargs):
while True:
seconds_left = (retry_until - datetime.now()).total_seconds()
try:
ret = func(*args, **kwargs)
try:
ret = func(*args, seconds_left=seconds_left, **kwargs)
except TypeError as error:
if "seconds_left" not in str(error):
raise
ret = func(*args, **kwargs)

logger.debug("Function returned %s", ret)

negative_result = ret is False or is_string(ret)
Expand All @@ -1868,7 +1874,7 @@ def func_retry(*args, **kwargs):
return saved_failure

except Exception as error:
logger.info("Function raised exception: %s", str(error))
logger.info('Function raised exception: "%s"', repr(error))
ret = error

if seconds_left < 0 and saved_failure:
Expand Down
20 changes: 11 additions & 9 deletions tests/topotests/mgmt_oper/oper.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def disable_debug(router):


@retry(retry_timeout=30, initial_wait=1)
def _do_oper_test(tgen, qr):
def _do_oper_test(tgen, qr, seconds_left=None):
r1 = tgen.gears["r1"].net

qcmd = (
Expand All @@ -80,6 +80,8 @@ def _do_oper_test(tgen, qr):
expected = open(qr[1], encoding="ascii").read()
output = r1.cmd_nostatus(qcmd.format(qr[0], qr[2] if len(qr) > 2 else ""))

diag = logging.debug if seconds_left else logging.warning

try:
ojson = json.loads(output)
except json.decoder.JSONDecodeError as error:
Expand All @@ -92,31 +94,31 @@ def _do_oper_test(tgen, qr):
logging.error(
"Error decoding json exp result: %s\noutput:\n%s", error, expected
)
logging.warning("FILE: {}".format(qr[1]))
diag("FILE: {}".format(qr[1]))
raise

if dd_json_cmp:
cmpout = json_cmp(ojson, ejson, exact_match=True)
if cmpout:
logging.warning(
diag(
"-------DIFF---------\n%s\n---------DIFF----------",
pprint.pformat(cmpout),
)
else:
cmpout = tt_json_cmp(ojson, ejson, exact=True)
if cmpout:
logging.warning(
diag(
"-------EXPECT--------\n%s\n------END-EXPECT------",
json.dumps(ejson, indent=4),
)
logging.warning(
diag(
"--------GOT----------\n%s\n-------END-GOT--------",
json.dumps(ojson, indent=4),
)
logging.warning("----diff---\n{}".format(cmpout))
logging.warning("Command: {}".format(qcmd.format(qr[0], qr[2] if len(qr) > 2 else "")))
logging.warning("File: {}".format(qr[1]))
assert cmpout is None
diag("----diff---\n{}".format(cmpout))
diag("Command: {}".format(qcmd.format(qr[0], qr[2] if len(qr) > 2 else "")))
diag("File: {}".format(qr[1]))
return cmpout


def do_oper_test(tgen, query_results):
Expand Down

0 comments on commit 63148da

Please sign in to comment.