Fix various pylint 1.5.4 warnings
[csit.git] / resources / libraries / python / VatExecutor.py
1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """VAT executor library."""
15
16 import json
17
18 from paramiko.ssh_exception import SSHException
19 from robot.api import logger
20
21 from resources.libraries.python.ssh import SSH, SSHTimeout
22 from resources.libraries.python.constants import Constants
23 from resources.libraries.python.VatHistory import VatHistory
24
25 __all__ = ['VatExecutor']
26
27
28 def cleanup_vat_json_output(json_output, vat_name=None):
29     """Return VAT JSON output cleaned from VAT clutter.
30
31     Clean up VAT JSON output from clutter like vat# prompts and such.
32
33     :param json_output: Cluttered JSON output.
34     :param vat_name: Name of the VAT script.
35     :type json_output: JSON
36     :type vat_name: str
37     :returns: Cleaned up output JSON string.
38     :rtype: JSON
39     """
40
41     retval = json_output
42     clutter = ['vat#', 'dump_interface_table error: Misc']
43     if vat_name:
44         remote_file_path = '{0}/{1}/{2}'.format(Constants.REMOTE_FW_DIR,
45                                                 Constants.RESOURCES_TPL_VAT,
46                                                 vat_name)
47         clutter.append("{0}(2):".format(remote_file_path))
48     for garbage in clutter:
49         retval = retval.replace(garbage, '')
50     return retval
51
52
53 def get_vpp_pid(node):
54     """Get PID of running VPP process.
55
56     :param node: DUT node.
57     :type node: dict
58     :returns: PID of VPP process / List of PIDs if more VPP processes are
59         running on the DUT node.
60     :rtype: int or list
61     """
62     import resources.libraries.python.DUTSetup as PidLib
63     pid = PidLib.DUTSetup.get_vpp_pid(node)
64     return pid
65
66
67 class VatExecutor(object):
68     """Contains methods for executing VAT commands on DUTs."""
69     def __init__(self):
70         self._stdout = None
71         self._stderr = None
72         self._ret_code = None
73         self._script_name = None
74
75     def execute_script(self, vat_name, node, timeout=120, json_out=True,
76                        copy_on_execute=False):
77         """Execute VAT script on remote node, and store the result. There is an
78         option to copy script from local host to remote host before execution.
79         Path is defined automatically.
80
81         :param vat_name: Name of the vat script file. Only the file name of
82             the script is required, the resources path is prepended
83             automatically.
84         :param node: Node to execute the VAT script on.
85         :param timeout: Seconds to allow the script to run.
86         :param json_out: Require JSON output.
87         :param copy_on_execute: If true, copy the file from local host to remote
88             before executing.
89         :type vat_name: str
90         :type node: dict
91         :type timeout: int
92         :type json_out: bool
93         :type copy_on_execute: bool
94         :raises SSHException: If cannot open connection for VAT.
95         :raises SSHTimeout: If VAT execution is timed out.
96         :raises RuntimeError: If VAT script execution fails.
97         """
98         ssh = SSH()
99         try:
100             ssh.connect(node)
101         except:
102             raise SSHException("Cannot open SSH connection to execute VAT "
103                                "command(s) from vat script {name}"
104                                .format(name=vat_name))
105
106         if copy_on_execute:
107             ssh.scp(vat_name, vat_name)
108             remote_file_path = vat_name
109         else:
110             remote_file_path = '{0}/{1}/{2}'.format(Constants.REMOTE_FW_DIR,
111                                                     Constants.RESOURCES_TPL_VAT,
112                                                     vat_name)
113
114         cmd = "{vat_bin} {json} in {vat_path} script".format(
115             vat_bin=Constants.VAT_BIN_NAME,
116             json="json" if json_out is True else "",
117             vat_path=remote_file_path)
118
119         try:
120             ret_code, stdout, stderr = ssh.exec_command_sudo(cmd=cmd,
121                                                              timeout=timeout)
122         except SSHTimeout:
123             logger.error("VAT script execution timeout: {0}".format(cmd))
124             raise
125         except:
126             raise RuntimeError("VAT script execution failed: {0}".format(cmd))
127
128         self._ret_code = ret_code
129         self._stdout = stdout
130         self._stderr = stderr
131         self._script_name = vat_name
132
133     def execute_script_json_out(self, vat_name, node, timeout=120):
134         """Pass all arguments to 'execute_script' method, then cleanup returned
135         json output.
136
137         :param vat_name: Name of the vat script file. Only the file name of
138             the script is required, the resources path is prepended
139             automatically.
140         :param node: Node to execute the VAT script on.
141         :param timeout: Seconds to allow the script to run.
142         :type vat_name: str
143         :type node: dict
144         :type timeout: int
145         """
146         self.execute_script(vat_name, node, timeout, json_out=True)
147         self._stdout = cleanup_vat_json_output(self._stdout, vat_name=vat_name)
148
149     def script_should_have_failed(self):
150         """Read return code from last executed script and raise exception if the
151         script didn't fail."""
152         if self._ret_code is None:
153             raise Exception("First execute the script!")
154         if self._ret_code == 0:
155             raise AssertionError(
156                 "VAT Script execution passed, but failure was expected: {cmd}"
157                 .format(cmd=self._script_name))
158
159     def script_should_have_passed(self):
160         """Read return code from last executed script and raise exception if the
161         script failed."""
162         if self._ret_code is None:
163             raise Exception("First execute the script!")
164         if self._ret_code != 0:
165             raise AssertionError(
166                 "VAT Script execution failed, but success was expected: {cmd}"
167                 .format(cmd=self._script_name))
168
169     def get_script_stdout(self):
170         """Returns value of stdout from last executed script."""
171         return self._stdout
172
173     def get_script_stderr(self):
174         """Returns value of stderr from last executed script."""
175         return self._stderr
176
177     @staticmethod
178     def cmd_from_template(node, vat_template_file, **vat_args):
179         """Execute VAT script on specified node. This method supports
180         script templates with parameters.
181
182         :param node: Node in topology on witch the script is executed.
183         :param vat_template_file: Template file of VAT script.
184         :param vat_args: Arguments to the template file.
185         :returns: List of JSON objects returned by VAT.
186         """
187         with VatTerminal(node) as vat:
188             return vat.vat_terminal_exec_cmd_from_template(vat_template_file,
189                                                            **vat_args)
190
191
192 class VatTerminal(object):
193     """VAT interactive terminal.
194
195     :param node: Node to open VAT terminal on.
196     :param json_param: Defines if outputs from VAT are in JSON format.
197         Default is True.
198     :type node: dict
199     :type json_param: bool
200
201     """
202
203     __VAT_PROMPT = ("vat# ", )
204     __LINUX_PROMPT = (":~$ ", "~]$ ", "~]# ")
205
206     def __init__(self, node, json_param=True):
207         """TODO: Should we document this constructor can raise RuntimeError?"""
208         json_text = ' json' if json_param else ''
209         self.json = json_param
210         self._node = node
211         self._ssh = SSH()
212         self._ssh.connect(self._node)
213         try:
214             self._tty = self._ssh.interactive_terminal_open()
215         except IOError:
216             raise RuntimeError("Cannot open interactive terminal on node {0}".
217                                format(self._node))
218
219         for _ in range(3):
220             try:
221                 self._ssh.interactive_terminal_exec_command(
222                     self._tty,
223                     'sudo -S {0}{1}'.format(Constants.VAT_BIN_NAME, json_text),
224                     self.__VAT_PROMPT)
225             except IOError:
226                 continue
227             else:
228                 break
229         else:
230             vpp_pid = get_vpp_pid(self._node)
231             if vpp_pid:
232                 if isinstance(vpp_pid, int):
233                     logger.trace("VPP running on node {0}".
234                                  format(self._node['host']))
235                 else:
236                     logger.error("More instances of VPP running on node {0}.".
237                                  format(self._node['host']))
238             else:
239                 logger.error("VPP not running on node {0}.".
240                              format(self._node['host']))
241             raise RuntimeError("Failed to open VAT console on node {0}".
242                                format(self._node['host']))
243
244         self._exec_failure = False
245         self.vat_stdout = None
246
247     def __enter__(self):
248         return self
249
250     def __exit__(self, exc_type, exc_val, exc_tb):
251         self.vat_terminal_close()
252
253     def vat_terminal_exec_cmd(self, cmd):
254         """Execute command on the opened VAT terminal.
255
256         :param cmd: Command to be executed.
257         :returns: Command output in python representation of JSON format or
258             None if not in JSON mode.
259         :raise RuntimeError: If VAT command execution fails.
260         """
261         VatHistory.add_to_vat_history(self._node, cmd)
262         logger.debug("Executing command in VAT terminal: {0}".format(cmd))
263         try:
264             out = self._ssh.interactive_terminal_exec_command(self._tty, cmd,
265                                                               self.__VAT_PROMPT)
266             self.vat_stdout = out
267         except IOError:
268             self._exec_failure = True
269             vpp_pid = get_vpp_pid(self._node)
270             if vpp_pid:
271                 if isinstance(vpp_pid, int):
272                     raise RuntimeError("VPP running on node {0} but VAT command"
273                                        " {1} execution failed.".
274                                        format(self._node['host'], cmd))
275                 else:
276                     raise RuntimeError("More instances of VPP running on node "
277                                        "{0}. VAT command {1} execution failed.".
278                                        format(self._node['host'], cmd))
279             else:
280                 raise RuntimeError("VPP not running on node {0}. VAT command "
281                                    "{1} execution failed.".
282                                    format(self._node['host'], cmd))
283
284         logger.debug("VAT output: {0}".format(out))
285         if self.json:
286             obj_start = out.find('{')
287             obj_end = out.rfind('}')
288             array_start = out.find('[')
289             array_end = out.rfind(']')
290
291             if obj_start == -1 and array_start == -1:
292                 raise RuntimeError("VAT command {0}: no JSON data.".format(cmd))
293
294             if obj_start < array_start or array_start == -1:
295                 start = obj_start
296                 end = obj_end + 1
297             else:
298                 start = array_start
299                 end = array_end + 1
300             out = out[start:end]
301             json_out = json.loads(out)
302             return json_out
303         else:
304             return None
305
306     def vat_terminal_close(self):
307         """Close VAT terminal."""
308         # interactive terminal is dead, we only need to close session
309         if not self._exec_failure:
310             try:
311                 self._ssh.interactive_terminal_exec_command(self._tty,
312                                                             'quit',
313                                                             self.__LINUX_PROMPT)
314             except Exception:
315                 vpp_pid = get_vpp_pid(self._node)
316                 if vpp_pid:
317                     if isinstance(vpp_pid, int):
318                         logger.trace("VPP running on node {0}.".
319                                      format(self._node['host']))
320                     else:
321                         logger.error("More instances of VPP running on node "
322                                      "{0}.".format(self._node['host']))
323                 else:
324                     logger.error("VPP not running on node {0}.".
325                                  format(self._node['host']))
326                 raise RuntimeError("Failed to close VAT console on node {0}".
327                                    format(self._node['host']))
328         try:
329             self._ssh.interactive_terminal_close(self._tty)
330         except:
331             raise RuntimeError("Cannot close interactive terminal on node {0}".
332                                format(self._node['host']))
333
334     def vat_terminal_exec_cmd_from_template(self, vat_template_file, **args):
335         """Execute VAT script from a file.
336
337         :param vat_template_file: Template file name of a VAT script.
338         :param args: Dictionary of parameters for VAT script.
339         :returns: List of JSON objects returned by VAT.
340         """
341         file_path = '{}/{}'.format(Constants.RESOURCES_TPL_VAT,
342                                    vat_template_file)
343         with open(file_path, 'r') as template_file:
344             cmd_template = template_file.readlines()
345         ret = []
346         for line_tmpl in cmd_template:
347             vat_cmd = line_tmpl.format(**args)
348             ret.append(self.vat_terminal_exec_cmd(vat_cmd.replace('\n', '')))
349         return ret