VXLAN test with dot1q tagging.
[csit.git] / resources / libraries / python / VatExecutor.py
index aff8c36..9851d93 100644 (file)
@@ -34,7 +34,6 @@ def cleanup_vat_json_output(json_output):
 
 
 class VatExecutor(object):
-
     def __init__(self):
         self._stdout = None
         self._stderr = None
@@ -74,14 +73,15 @@ class VatExecutor(object):
         logger.trace("stdout: '{0}'".format(self._stdout))
         logger.trace("stderr: '{0}'".format(self._stderr))
 
-        # TODO: download vpe_api_test output file
+        # TODO: download vpp_api_test output file
         # self._delete_files(node, remote_file_path, remote_file_out)
 
-    def execute_script_json_out(self, vat_name, node, timeout=10,):
+    def execute_script_json_out(self, vat_name, node, timeout=10):
         self.execute_script(vat_name, node, timeout, json_out=True)
         self._stdout = cleanup_vat_json_output(self._stdout)
 
-    def _delete_files(self, node, *files):
+    @staticmethod
+    def _delete_files(node, *files):
         ssh = SSH()
         ssh.connect(node)
         files = " ".join([str(x) for x in files])
@@ -111,16 +111,14 @@ class VatExecutor(object):
     def cmd_from_template(node, vat_template_file, **vat_args):
         """Execute VAT script on specified node. This method supports
          script templates with parameters
-        :param node: node in topology on witch the scrtipt is executed
+        :param node: node in topology on witch the script is executed
         :param vat_template_file: template file of VAT script
         :param vat_args: arguments to the template file
         :return: list of json objects returned by VAT
         """
-        vat = VatTerminal(node)
-        ret = vat.vat_terminal_exec_cmd_from_template(vat_template_file,
-                                                      **vat_args)
-        vat.vat_terminal_close()
-        return ret
+        with VatTerminal(node) as vat:
+            return vat.vat_terminal_exec_cmd_from_template(vat_template_file,
+                                                           **vat_args)
 
     @staticmethod
     def copy_config_to_remote(node, local_path, remote_path):
@@ -144,34 +142,51 @@ class VatTerminal(object):
     """VAT interactive terminal
 
        :param node: Node to open VAT terminal on.
+       :param json_param: Defines if outputs from VAT are in JSON format.
+       Default is True.
+       :type node: dict
+       :type json_param: bool
+
     """
 
     __VAT_PROMPT = "vat# "
     __LINUX_PROMPT = ":~$ "
 
-    def __init__(self, node):
+    def __init__(self, node, json_param=True):
+        json_text = ' json' if json_param else ''
+        self.json = json_param
         self._ssh = SSH()
         self._ssh.connect(node)
         self._tty = self._ssh.interactive_terminal_open()
         self._ssh.interactive_terminal_exec_command(
             self._tty,
-            'sudo -S {vat} json'.format(vat=Constants.VAT_BIN_NAME),
+            'sudo -S {}{}'.format(Constants.VAT_BIN_NAME, json_text),
             self.__VAT_PROMPT)
 
+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        self.vat_terminal_close()
+
     def vat_terminal_exec_cmd(self, cmd):
         """Execute command on the opened VAT terminal.
 
            :param cmd: Command to be executed.
 
-           :return: Command output in python representation of JSON format.
+           :return: Command output in python representation of JSON format or
+           None if not in JSON mode.
         """
-        logger.debug("Executing command in VAT terminal: {}".format(cmd));
+        logger.debug("Executing command in VAT terminal: {}".format(cmd))
         out = self._ssh.interactive_terminal_exec_command(self._tty,
                                                           cmd,
                                                           self.__VAT_PROMPT)
-        logger.debug("VAT output: {}".format(out));
-        json_out = json.loads(out)
-        return json_out
+        logger.debug("VAT output: {}".format(out))
+        if self.json:
+            json_out = json.loads(out)
+            return json_out
+        else:
+            return None
 
     def vat_terminal_close(self):
         """Close VAT terminal."""