]> git.puffer.fish Git - mirror/frr.git/commitdiff
topogen: added JSON output support for vtysh_cmd
authorRafael Zalamena <rzalamena@gmail.com>
Wed, 28 Jun 2017 18:30:44 +0000 (15:30 -0300)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 28 Nov 2018 01:22:11 +0000 (20:22 -0500)
Allow vtysh_cmd() to convert JSON output to Python data structures.
This feature will be used to get vtysh JSON outputs for tests
comparsions.

Usage example:

```py
router = get_topogen().gears['r1']
json_output = router.vtysh_cmd('show ip ospf json', isjson=True)
json_cmp(json_output, {'important_key': 'important_value'})
```

tests/topotests/lib/topogen.py

index f4374015173cac5282ec14669a261902654256e0..97f006c59cda40d57878c19f9cbad7162a876134 100644 (file)
@@ -40,6 +40,7 @@ Basic usage instructions:
 
 import os
 import sys
+import json
 
 from mininet.net import Mininet
 from mininet.log import setLogLevel
@@ -388,7 +389,7 @@ class TopoRouter(TopoGear):
         """
         return self.tgen.net[self.name].startRouter()
 
-    def vtysh_cmd(self, command):
+    def vtysh_cmd(self, command, isjson=False):
         """
         Runs the provided command string in the vty shell and returns a string
         with the response.
@@ -401,7 +402,11 @@ class TopoRouter(TopoGear):
             return self.vtysh_multicmd(command)
 
         vtysh_command = 'vtysh -c "{}" 2>/dev/null'.format(command)
-        return self.run(vtysh_command)
+        output = self.run(vtysh_command)
+        if isjson is False:
+            return output
+
+        return json.loads(output)
 
     def vtysh_multicmd(self, commands, pretty_output=True):
         """