1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# Copyright (c) 2015 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from ssh import SSH
from robot.api import logger
__all__ = []
class VatExecutor(object):
__TMP_DIR = "/tmp/"
__VAT_BIN = "vpe_api_test"
def __init__(self):
self._stdout = None
self._stderr = None
self._ret_code = None
def execute_script(self, local_path, node, timeout=10, json_out=True):
"""Copy local_path script to node, execute it and return result.
Returns (rc, stdout, stderr tuple).
"""
ssh = SSH()
ssh.connect(node)
local_basename = os.path.basename(local_path)
remote_file_path = self.__TMP_DIR + local_basename
remote_file_out = remote_file_path + ".out"
ssh.scp(local_path, remote_file_path)
cmd = "sudo -S {vat} {json} < {input}".format(vat=self.__VAT_BIN,
json="json" if json_out == True else "",
input=remote_file_path)
(ret_code, stdout, stderr) = ssh.exec_command(cmd, timeout)
self._ret_code = ret_code
self._stdout = stdout
self._stderr = stderr
logger.trace("Command '{0}' returned {1}'".format(cmd, self._ret_code))
logger.trace("stdout: '{0}'".format(self._stdout))
logger.trace("stderr: '{0}'".format(self._stderr))
#TODO: download vpe_api_test output file
self._delete_files(node, remote_file_path, remote_file_out)
def _delete_files(self, node, *files):
ssh = SSH()
ssh.connect(node)
files = " ".join([str(x) for x in files])
ssh.exec_command("rm {0}".format(files))
def script_should_have_failed(self):
if self._ret_code is None:
raise Exception("First execute the script!")
if self._ret_code == 0:
raise AssertionError(
"Script execution passed, but failure was expected")
def script_should_have_passed(self):
if self._ret_code is None:
raise Exception("First execute the script!")
if self._ret_code != 0:
raise AssertionError(
"Script execution failed, but success was expected")
def get_script_stdout(self):
return self._stdout
def get_script_stderr(self):
return self._stderr
|