From 4e3828b7710f48c49597a70cef860d544c90348d Mon Sep 17 00:00:00 2001 From: David Schweizer Date: Mon, 22 Feb 2021 10:31:57 +0100 Subject: [PATCH] tests: JSON comparison command for LabN topotests The changes add the "jsoncmp_pass" and the "jsoncmp_fail" commands to compare VTY shell's JSON output to an expected JSON object during topotests using the LabN testing framework. This helps to eliminate false negative test results (i.e. due to routes beeing out of order after convergence or cosmetic changes in VTY shell's text output). Signed-off-by: David Schweizer --- tests/topotests/lib/lutil.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/topotests/lib/lutil.py b/tests/topotests/lib/lutil.py index 0b6a946fda..f8f580632e 100644 --- a/tests/topotests/lib/lutil.py +++ b/tests/topotests/lib/lutil.py @@ -25,6 +25,7 @@ import json import math import time from lib.topolog import logger +from lib.topotest import json_cmp from mininet.net import Mininet @@ -194,6 +195,10 @@ Total %-4d %-4d %d\n\ global net if op != "wait": self.l_line += 1 + + if op == "jsoncmp_pass" or op == "jsoncmp_fail": + returnJson = True + self.log( "%s (#%d) %s:%s COMMAND:%s:%s:%s:%s:%s:" % ( @@ -227,6 +232,33 @@ Total %-4d %-4d %d\n\ ) self.log("COMMAND OUTPUT:%s:" % report) + # JSON comparison + if op == "jsoncmp_pass" or op == "jsoncmp_fail": + try: + expect = json.loads(regexp) + except: + expect = None + self.log( + "WARNING: JSON load failed -- confirm regex input is in JSON format." + ) + json_diff = json_cmp(js, expect) + if json_diff != None: + if op == "jsoncmp_fail": + success = True + else: + success = False + self.log("JSON DIFF:%s:" % json_diff) + ret = success + else: + if op == "jsoncmp_fail": + success = False + else: + success = True + self.result(target, success, result) + if js != None: + return js + return ret + # Experiment: can we achieve the same match behavior via DOTALL # without converting newlines to spaces? out_nl = out -- 2.39.5