Skip to content

Commit 76ddf42

Browse files
OriTrabelsimssonicbld
authored andcommitted
Fix JSON serialization of neighbor VM restore results in recover.py (#22567)
What is the motivation for this PR tests failed when trying to parse output from _neighbor_vm_recover_bgpd because ModuleResult is not JSON-serializable. How did you do it Add _make_ansible_results_serializable() to recursively convert ModuleResult (and nested structures) to be serializable. How did you verify/test it Reran the test. Signed-off-by: Ori Trabelsi <[email protected]> Signed-off-by: mssonicbld <[email protected]>
1 parent f81429e commit 76ddf42

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

tests/common/plugins/sanity_check/recover.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import time
44

5+
from pytest_ansible.results import ModuleResult
56
from tests.common import config_reload
67
from tests.common.devices.sonic import SonicHost
78
from tests.common.helpers.parallel import parallel_run, reset_ansible_local_tmp
@@ -15,6 +16,21 @@
1516
logger = logging.getLogger(__name__)
1617

1718

19+
def _make_results_serializable(obj):
20+
"""Recursively convert objects to JSON-serializable form."""
21+
if isinstance(obj, ModuleResult):
22+
return _make_results_serializable(dict(obj))
23+
if isinstance(obj, dict):
24+
return {k: _make_results_serializable(v) for k, v in obj.items()}
25+
if isinstance(obj, (list, tuple)):
26+
return [_make_results_serializable(x) for x in obj]
27+
if isinstance(obj, (str, int, float, bool, type(None))):
28+
return obj
29+
if obj is None:
30+
return None
31+
return str(obj)
32+
33+
1834
def reboot_dut(dut, localhost, cmd, reboot_with_running_golden_config=False):
1935
logging.info('Reboot DUT to recover')
2036

@@ -139,7 +155,9 @@ def neighbor_vm_restore(duthost, nbrhosts, tbinfo, result=None):
139155
logger.debug('Results of restoring neighbor VMs: {}'.format(unhealthy_nbrs))
140156
else:
141157
results = parallel_run(_neighbor_vm_recover_bgpd, (), {}, list(nbrhosts.values()), timeout=300)
142-
logger.debug('Results of restoring neighbor VMs: {}'.format(json.dumps(dict(results))))
158+
logger.debug(
159+
'Results of restoring neighbor VMs: {}'.format(
160+
json.dumps(_make_results_serializable(dict(results)))))
143161
return 'config_reload' # May still need to do a config reload
144162

145163

0 commit comments

Comments
 (0)