From a6d233d80c359813cc0a350f07449c03d552d166 Mon Sep 17 00:00:00 2001 From: Vratko Polak Date: Fri, 23 Aug 2019 17:02:04 +0200 Subject: [PATCH] Stop using SSH() in TrafficGenerator.py This is a partial fix for: Ticket: CSIT-1434 and should fix one pylint violation. Many files to remove SSH() from remain. + Use -E (together with previous -S) in ssh.py when sudo=True. Needed in some cases, no downsides expected. Change-Id: Ie2ad03b3c656a4d3965a61a1e5435276f99a28c0 Signed-off-by: Vratko Polak --- resources/libraries/python/TrafficGenerator.py | 73 ++++++++++---------------- resources/libraries/python/ssh.py | 10 ++-- 2 files changed, 34 insertions(+), 49 deletions(-) diff --git a/resources/libraries/python/TrafficGenerator.py b/resources/libraries/python/TrafficGenerator.py index 2e9b787321..d630c7bf75 100644 --- a/resources/libraries/python/TrafficGenerator.py +++ b/resources/libraries/python/TrafficGenerator.py @@ -20,7 +20,7 @@ from robot.libraries.BuiltIn import BuiltIn from .DropRateSearch import DropRateSearch from .Constants import Constants -from .ssh import SSH, exec_cmd_no_error +from .ssh import exec_cmd_no_error, exec_cmd from .topology import NodeType from .topology import NodeSubTypeTG from .topology import Topology @@ -224,16 +224,12 @@ class TrafficGenerator(AbstractMeasurer): if subtype == NodeSubTypeTG.TREX: self._node = tg_node - ssh = SSH() - ssh.connect(self._node) - - (ret, _, _) = ssh.exec_command( - "sudo -E sh -c '{0}/resources/tools/trex/" + exec_cmd_no_error( + self._node, + "sh -c '{0}/resources/tools/trex/" "trex_installer.sh {1}'".format(Constants.REMOTE_FW_DIR, Constants.TREX_INSTALL_VERSION), - timeout=1800) - if int(ret) != 0: - raise RuntimeError('TRex installation failed.') + sudo=True, timeout=1800, message='TRex installation failed.') if1_pci = Topology().get_interface_pci_addr(self._node, tg_if1) if2_pci = Topology().get_interface_pci_addr(self._node, tg_if2) @@ -270,8 +266,9 @@ class TrafficGenerator(AbstractMeasurer): self._ifaces_reordered = True if osi_layer == 'L2' or osi_layer == 'L3': - (ret, _, _) = ssh.exec_command( - "sudo sh -c 'cat << EOF > /etc/trex_cfg.yaml\n" + exec_cmd_no_error( + self._node, + "sh -c 'cat << EOF > /etc/trex_cfg.yaml\n" "- version: 2\n" " interfaces: [\"{0}\",\"{1}\"]\n" " port_info:\n" @@ -284,10 +281,12 @@ class TrafficGenerator(AbstractMeasurer): "0x"+if1_adj_addr.replace(":", ",0x"), "0x"+if1_addr.replace(":", ",0x"), "0x"+if2_adj_addr.replace(":", ",0x"), - "0x"+if2_addr.replace(":", ",0x"))) + "0x"+if2_addr.replace(":", ",0x")), + sudo=True, message='TRex config generation error') elif osi_layer == 'L7': - (ret, _, _) = ssh.exec_command( - "sudo sh -c 'cat << EOF > /etc/trex_cfg.yaml\n" + exec_cmd_no_error( + self._node, + "sh -c 'cat << EOF > /etc/trex_cfg.yaml\n" "- version: 2\n" " interfaces: [\"{0}\",\"{1}\"]\n" " port_info:\n" @@ -298,11 +297,10 @@ class TrafficGenerator(AbstractMeasurer): "EOF'"\ .format(if1_pci, if2_pci, if1_addr, if1_adj_addr, - if2_addr, if2_adj_addr)) + if2_addr, if2_adj_addr), + sudo=True, message='TRex config generation error') else: raise ValueError("Unknown Test Type") - if int(ret) != 0: - raise RuntimeError('TRex config generation error') self._startup_trex(osi_layer) @@ -347,8 +345,8 @@ class TrafficGenerator(AbstractMeasurer): exec_cmd_no_error(self._node, cmd, sudo=True) except RuntimeError: cmd = "sh -c 'cat /tmp/trex.log'" - exec_cmd_no_error(self._node, cmd, sudo=True, - message='Get TRex logs failed!') + exec_cmd_no_error( + self._node, cmd, sudo=True, message='Get TRex logs failed!') raise RuntimeError('Start TRex failed!') # Test if TRex starts successfuly. @@ -377,9 +375,7 @@ class TrafficGenerator(AbstractMeasurer): """ # No need to check subtype, we know it is TREX. - ssh = SSH() - ssh.connect(node) - ret, _, _ = ssh.exec_command_sudo("pidof t-rex") + ret, _, _ = exec_cmd(node, "pidof t-rex", sudo=True) return bool(int(ret) == 0) @staticmethod @@ -394,12 +390,9 @@ class TrafficGenerator(AbstractMeasurer): """ subtype = check_subtype(node) if subtype == NodeSubTypeTG.TREX: - ssh = SSH() - ssh.connect(node) - (ret, _, _) = ssh.exec_command( - "sh -c 'sudo pkill t-rex && sleep 3'") - if int(ret) != 0: - raise RuntimeError('pkill t-rex failed') + exec_cmd_no_error( + node, "sh -c 'sudo pkill t-rex && sleep 3'", + sudo=False, message='pkill t-rex failed') def _parse_traffic_results(self, stdout): """Parse stdout of scripts into fieds of self. @@ -432,16 +425,11 @@ class TrafficGenerator(AbstractMeasurer): :raises RuntimeError: If stop traffic script fails. """ # No need to check subtype, we know it is TREX. - ssh = SSH() - ssh.connect(node) - - (ret, stdout, _) = ssh.exec_command( + stdout, _ = exec_cmd_no_error( + node, "sh -c '{}/resources/tools/trex/" - "trex_stateless_stop.py'".format(Constants.REMOTE_FW_DIR)) - - if int(ret) != 0: - raise RuntimeError('TRex stateless runtime error') - + "trex_stateless_stop.py'".format(Constants.REMOTE_FW_DIR), + message='TRex stateless runtime error') self._parse_traffic_results(stdout) def trex_stl_start_remote_exec( @@ -476,8 +464,6 @@ class TrafficGenerator(AbstractMeasurer): :raises RuntimeError: In case of TG driver issue. """ # No need to check subtype, we know it is TREX. - ssh = SSH() - ssh.connect(self._node) reorder = self._ifaces_reordered # Just to make the next line fit. p_0, p_1 = (rx_port, tx_port) if reorder else (tx_port, rx_port) # Values from Robot can introduce type unicode, @@ -505,12 +491,11 @@ class TrafficGenerator(AbstractMeasurer): command += " --unidirection" command += "'" - (ret, stdout, _) = ssh.exec_command( - command, timeout=float(duration) + 60) + stdout, _ = exec_cmd_no_error( + self._node, command, timeout=float(duration) + 60, + message='TRex stateless runtime error') - if int(ret) != 0: - raise RuntimeError('TRex stateless runtime error') - elif async_call: + if async_call: #no result self._start_time = time.time() self._rate = float(rate[:-3]) if "pps" in rate else rate diff --git a/resources/libraries/python/ssh.py b/resources/libraries/python/ssh.py index cee35868e4..1908da4153 100644 --- a/resources/libraries/python/ssh.py +++ b/resources/libraries/python/ssh.py @@ -241,9 +241,9 @@ class SSH(object): if isinstance(cmd, (list, tuple)): cmd = OptionString(cmd) if cmd_input is None: - command = 'sudo -S {c}'.format(c=cmd) + command = 'sudo -E -S {c}'.format(c=cmd) else: - command = 'sudo -S {c} <<< "{i}"'.format(c=cmd, i=cmd_input) + command = 'sudo -E -S {c} <<< "{i}"'.format(c=cmd, i=cmd_input) return self.exec_command(command, timeout, log_stdout_err=log_stdout_err) @@ -267,7 +267,7 @@ class SSH(object): .format(p=lxc_params, n=lxc_name, c=lxc_cmd) if sudo: - command = 'sudo -S {c}'.format(c=command) + command = 'sudo -E -S {c}'.format(c=command) return self.exec_command(command, timeout) def interactive_terminal_open(self, time_out=45): @@ -453,8 +453,8 @@ def exec_cmd(node, cmd, timeout=600, sudo=False, disconnect=False): if not sudo: (ret_code, stdout, stderr) = ssh.exec_command(cmd, timeout=timeout) else: - (ret_code, stdout, stderr) = ssh.exec_command_sudo(cmd, - timeout=timeout) + (ret_code, stdout, stderr) = ssh.exec_command_sudo( + cmd, timeout=timeout) except SSHException as err: logger.error(repr(err)) return None, None, None -- 2.16.6