blob: f10259b5006fb7d75fef48499e65dd50722dfc92 [file] [log] [blame]
Stefan Kobzab1899332015-12-23 17:00:10 +01001# 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:
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.
13import os
14from ssh import SSH
15from robot.api import logger
16
17__all__ = []
18
19class VatExecutor(object):
20
21 __TMP_DIR = "/tmp/"
Damjan Marion08ff7e02016-01-20 13:45:36 +010022 __VAT_BIN = "vpp_api_test"
Stefan Kobzab1899332015-12-23 17:00:10 +010023
24 def __init__(self):
25 self._stdout = None
26 self._stderr = None
27 self._ret_code = None
28
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.
31
32 Returns (rc, stdout, stderr tuple).
33 """
34
35 ssh = SSH()
36 ssh.connect(node)
37
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"
41
42 ssh.scp(local_path, remote_file_path)
43
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
49 self._stdout = stdout
50 self._stderr = stderr
51
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))
55
Damjan Marion08ff7e02016-01-20 13:45:36 +010056 #TODO: download vpp_api_test output file
Stefan Kobzab1899332015-12-23 17:00:10 +010057 self._delete_files(node, remote_file_path, remote_file_out)
58
59 def _delete_files(self, node, *files):
60 ssh = SSH()
61 ssh.connect(node)
62 files = " ".join([str(x) for x in files])
63 ssh.exec_command("rm {0}".format(files))
64
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:
69 raise AssertionError(
70 "Script execution passed, but failure was expected")
71
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:
76 raise AssertionError(
77 "Script execution failed, but success was expected")
78
79 def get_script_stdout(self):
80 return self._stdout
81
82 def get_script_stderr(self):
83 return self._stderr
84