]> git.puffer.fish Git - matthieu/frr.git/commitdiff
tests: JSON comparison command for LabN topotests
authorDavid Schweizer <dschweizer@opensourcerouting.org>
Mon, 22 Feb 2021 09:31:57 +0000 (10:31 +0100)
committerDavid Schweizer <dschweizer@opensourcerouting.org>
Mon, 22 Feb 2021 09:31:57 +0000 (10:31 +0100)
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 <dschweizer@opensourcerouting.org>
tests/topotests/lib/lutil.py

index 0b6a946fdaf4d2b29075968072ae68bc1346fb99..f8f580632e7175f78a0605086a9dc5861ebeef41 100644 (file)
@@ -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