VatExecutor update 42/842/2
authorMatus Fabian <matfabia@cisco.com>
Fri, 22 Apr 2016 11:58:22 +0000 (13:58 +0200)
committerStefan Kobza <skobza@cisco.com>
Fri, 22 Apr 2016 14:51:07 +0000 (14:51 +0000)
Strip non-JSON data from VAT terminal output
Add method for VAT command retval verification

Change-Id: Ibe68020393cb1a700ad048dc78078f71064707b4
Signed-off-by: Matus Fabian <matfabia@cisco.com>
resources/libraries/python/VatExecutor.py
resources/libraries/python/VatJsonUtil.py

index 9851d93..fd1d8db 100644 (file)
@@ -183,6 +183,21 @@ class VatTerminal(object):
                                                           self.__VAT_PROMPT)
         logger.debug("VAT output: {}".format(out))
         if self.json:
+            obj_start = out.find('{')
+            obj_end = out.rfind('}')
+            array_start = out.find('[')
+            array_end = out.rfind(']')
+
+            if -1 == obj_start and -1 == array_start:
+                raise RuntimeError("No JSON data.")
+
+            if obj_start < array_start or -1 == array_start:
+                start = obj_start
+                end = obj_end + 1
+            else:
+                start = array_start
+                end = array_end + 1
+            out = out[start:end]
             json_out = json.loads(out)
             return json_out
         else:
index 36c5053..c1a9e4b 100644 (file)
@@ -100,3 +100,26 @@ class VatJsonUtil(object):
             ifc['name'] = interface_dict["interface_name"]
             ifc['vpp_sw_index'] = interface_dict["sw_if_index"]
             ifc['mtu'] = interface_dict["mtu"]
+
+    @staticmethod
+    def verify_vat_retval(vat_out, exp_retval=0, err_msg='VAT cmd failed'):
+        """Verify return value of VAT command.
+
+        VAT command JSON output should be object (dict in python) or array. We
+        are looking for something like this: { "retval": 0 }. Verification is
+        skipped if VAT output does not contain return value element or root
+        elemet is array.
+
+        :param vat_out: VAT command output in python representation of JSON.
+        :param exp_retval: Expected return value (default 0).
+        :err_msg: Message to be displayed in case of error (optional).
+        :type vat_out: dict or list
+        :type exp_retval: int
+        :type err_msg: str
+        :raises RuntimeError: If VAT command return value is incorrect.
+        """
+        if type(vat_out) is dict:
+            retval = vat_out.get('retval')
+            if retval is not None:
+                if retval != exp_retval:
+                    raise RuntimeError(err_msg)

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.