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