From: Rafael Zalamena Date: Wed, 5 Jul 2017 16:46:28 +0000 (-0300) Subject: topotest: add JSON list comparation support X-Git-Tag: frr-7.1-dev~151^2~280 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=dc0d3fc53f86310cb5789dedf647f981975e94fa;p=mirror%2Ffrr.git topotest: add JSON list comparation support Add missing list support for json_cmp(). The missing support was noticed while writing the BGP ECMP topology test. --- diff --git a/tests/topotests/lib/test/test_json.py b/tests/topotests/lib/test/test_json.py index 87bf48dda2..e397194503 100755 --- a/tests/topotests/lib/test/test_json.py +++ b/tests/topotests/lib/test/test_json.py @@ -249,5 +249,85 @@ def test_json_intersect_multilevel_false(): assert json_cmp(dcomplete, dsub5) is not None assert json_cmp(dcomplete, dsub6) is not None +def test_json_with_list_sucess(): + "Test successful json comparisons that have lists." + + dcomplete = { + 'list': [ + { + 'i1': 'item 1', + 'i2': 'item 2', + }, + { + 'i10': 'item 10', + }, + ], + 'i100': 'item 100', + } + + # Test list type + dsub1 = { + 'list': [], + } + # Test list correct list items + dsub2 = { + 'list': [ + { + 'i1': 'item 1', + }, + ], + 'i100': 'item 100', + } + # Test list correct list size + dsub3 = { + 'list': [ + {}, {}, + ], + } + + assert json_cmp(dcomplete, dsub1) is None + assert json_cmp(dcomplete, dsub2) is None + assert json_cmp(dcomplete, dsub3) is None + +def test_json_with_list_failure(): + "Test failed json comparisons that have lists." + + dcomplete = { + 'list': [ + { + 'i1': 'item 1', + 'i2': 'item 2', + }, + { + 'i10': 'item 10', + }, + ], + 'i100': 'item 100', + } + + # Test list type + dsub1 = { + 'list': {}, + } + # Test list incorrect list items + dsub2 = { + 'list': [ + { + 'i1': 'item 2', + }, + ], + 'i100': 'item 100', + } + # Test list correct list size + dsub3 = { + 'list': [ + {}, {}, {}, + ], + } + + assert json_cmp(dcomplete, dsub1) is not None + assert json_cmp(dcomplete, dsub2) is not None + assert json_cmp(dcomplete, dsub3) is not None + if __name__ == '__main__': sys.exit(pytest.main()) diff --git a/tests/topotests/lib/topotest.py b/tests/topotests/lib/topotest.py index 9ed7c198d1..fa065ad178 100644 --- a/tests/topotests/lib/topotest.py +++ b/tests/topotests/lib/topotest.py @@ -91,9 +91,50 @@ def json_cmp(d1, d2, reason=False): continue # If nd1 key is a dict, we have to recurse in it later. if isinstance(nd2[key], type({})): + if not isinstance(nd1[key], type({})): + result.add_error( + '{}["{}"] has different type than expected '.format(parent, key) + + '(have {}, expected {})'.format(type(nd1[key]), type(nd2[key]))) + continue nparent = '{}["{}"]'.format(parent, key) squeue.append((nd1[key], nd2[key], nparent)) continue + # Check list items + if isinstance(nd2[key], type([])): + if not isinstance(nd1[key], type([])): + result.add_error( + '{}["{}"] has different type than expected '.format(parent, key) + + '(have {}, expected {})'.format(type(nd1[key]), type(nd2[key]))) + continue + # Check list size + 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]))) + continue + + # List all unmatched items errors + unmatched = [] + for expected in nd2[key]: + matched = False + for value in nd1[key]: + if json_cmp({'json': value}, {'json': expected}) is None: + matched = True + break + + if matched: + break + if not matched: + unmatched.append(expected) + + # 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]))) + continue + # Compare JSON values if nd1[key] != nd2[key]: result.add_error(