summaryrefslogtreecommitdiff
path: root/tests/topotests/lib/topotest.py
diff options
context:
space:
mode:
authorRafael Zalamena <rzalamena@gmail.com>2017-07-05 13:46:28 -0300
committerDonald Sharp <sharpd@cumulusnetworks.com>2018-11-27 20:22:11 -0500
commitdc0d3fc53f86310cb5789dedf647f981975e94fa (patch)
treee0e6c68f1171e51644fe592771493037c5bf9c1f /tests/topotests/lib/topotest.py
parent19ccab570b5a9c231dea2be4793523a7001aee72 (diff)
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.
Diffstat (limited to 'tests/topotests/lib/topotest.py')
-rw-r--r--tests/topotests/lib/topotest.py41
1 files changed, 41 insertions, 0 deletions
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(