From: Rafael Zalamena Date: Tue, 19 Sep 2017 01:19:10 +0000 (-0300) Subject: topotest: improve json error messages X-Git-Tag: frr-7.1-dev~151^2~207 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=7bd28cfc67c77e64382cbd7d532ca88f7e611c42;p=matthieu%2Ffrr.git topotest: improve json error messages Show a diff of the JSON values instead of dumping the whole data structures. --- diff --git a/tests/topotests/lib/topotest.py b/tests/topotests/lib/topotest.py index 5dbef7432f..0e0a1e8876 100644 --- a/tests/topotests/lib/topotest.py +++ b/tests/topotests/lib/topotest.py @@ -22,6 +22,7 @@ # OF THIS SOFTWARE. # +import json import os import errno import re @@ -58,6 +59,17 @@ class json_cmp_result(object): "Returns True if there were errors, otherwise False." return len(self.errors) > 0 +def json_diff(d1, d2): + """ + Returns a string with the difference between JSON data. + """ + json_format_opts = { + 'indent': 4, + 'sort_keys': True, + } + dstr1 = json.dumps(d1, **json_format_opts) + dstr2 = json.dumps(d2, **json_format_opts) + return difflines(dstr2, dstr1, title1='Expected value', title2='Current value', n=0) def json_cmp(d1, d2): """ @@ -110,8 +122,9 @@ def json_cmp(d1, d2): if len(nd2[key]) > len(nd1[key]): result.add_error( '{}["{}"] too few items '.format(parent, key) + - '(have ({}) "{}", expected ({}) "{}")'.format( - len(nd1[key]), str(nd1[key]), len(nd2[key]), str(nd2[key]))) + '(have {}, expected {}:\n {})'.format( + len(nd1[key]), len(nd2[key]), + json_diff(nd1[key], nd2[key]))) continue # List all unmatched items errors @@ -131,15 +144,15 @@ def json_cmp(d1, d2): # If there are unmatched items, error out. if unmatched: result.add_error( - '{}["{}"] value is different (have "{}", expected "{}")'.format( - parent, key, str(nd1[key]), str(nd2[key]))) + '{}["{}"] value is different (\n{})'.format( + parent, key, json_diff(nd1[key], nd2[key]))) continue # Compare JSON values if nd1[key] != nd2[key]: result.add_error( - '{}["{}"] value is different (have "{}", expected "{}")'.format( - parent, key, str(nd1[key]), str(nd2[key]))) + '{}["{}"] value is different (\n{})'.format( + parent, key, json_diff(nd1[key], nd2[key]))) continue if result.has_errors():