1 # Copyright (c) 2015 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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from robot.api import logger
19 class VatExecutor(object):
22 __VAT_BIN = "vpe_api_test"
29 def execute_script(self, local_path, node, timeout=10, json_out=True):
30 """Copy local_path script to node, execute it and return result.
32 Returns (rc, stdout, stderr tuple).
38 local_basename = os.path.basename(local_path)
39 remote_file_path = self.__TMP_DIR + local_basename
40 remote_file_out = remote_file_path + ".out"
42 ssh.scp(local_path, remote_file_path)
44 cmd = "sudo -S {vat} {json} < {input}".format(vat=self.__VAT_BIN,
45 json="json" if json_out == True else "",
46 input=remote_file_path)
47 (ret_code, stdout, stderr) = ssh.exec_command(cmd, timeout)
48 self._ret_code = ret_code
52 logger.trace("Command '{0}' returned {1}'".format(cmd, self._ret_code))
53 logger.trace("stdout: '{0}'".format(self._stdout))
54 logger.trace("stderr: '{0}'".format(self._stderr))
56 #TODO: download vpe_api_test output file
57 self._delete_files(node, remote_file_path, remote_file_out)
59 def _delete_files(self, node, *files):
62 files = " ".join([str(x) for x in files])
63 ssh.exec_command("rm {0}".format(files))
65 def script_should_have_failed(self):
66 if self._ret_code is None:
67 raise Exception("First execute the script!")
68 if self._ret_code == 0:
70 "Script execution passed, but failure was expected")
72 def script_should_have_passed(self):
73 if self._ret_code is None:
74 raise Exception("First execute the script!")
75 if self._ret_code != 0:
77 "Script execution failed, but success was expected")
79 def get_script_stdout(self):
82 def get_script_stderr(self):