From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by dpdk.org (Postfix) with ESMTP id 6B1F01B1BB for ; Mon, 8 Jan 2018 10:56:28 +0100 (CET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 08 Jan 2018 01:56:27 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,330,1511856000"; d="scan'208";a="193209981" Received: from dpdk-test38.sh.intel.com ([10.67.119.87]) by fmsmga006.fm.intel.com with ESMTP; 08 Jan 2018 01:56:26 -0800 From: Marvin Liu To: dts@dpdk.org Cc: Marvin Liu Date: Sun, 7 Jan 2018 21:49:21 -0500 Message-Id: <1515379769-11553-9-git-send-email-yong.liu@intel.com> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1515379769-11553-1-git-send-email-yong.liu@intel.com> References: <1515379769-11553-1-git-send-email-yong.liu@intel.com> Subject: [dts] [PATCH v1 08/16] framework/ssh_pexpect: support multiple VMs module X-BeenThere: dts@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: test suite reviews and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 Jan 2018 09:56:29 -0000 1. Protect ssh connection action by parallel lock 2. Enlarge terminal column for serial output 3. Enhance error handling when exception happened 4. Add DUT index argument which can separate lock by DUTs Signed-off-by: Marvin Liu diff --git a/framework/ssh_pexpect.py b/framework/ssh_pexpect.py index 8d6c3dd..3c988b7 100644 --- a/framework/ssh_pexpect.py +++ b/framework/ssh_pexpect.py @@ -3,7 +3,7 @@ import pexpect from pexpect import pxssh from debugger import ignore_keyintr, aware_keyintr from exception import TimeoutException, SSHConnectionException, SSHSessionDeadException -from utils import RED, GREEN +from utils import RED, GREEN, parallel_lock """ Module handle ssh sessions between tester and DUT. @@ -14,31 +14,47 @@ Aslo support transfer files to tester or DUT. class SSHPexpect(object): - def __init__(self, host, username, password): + def __init__(self, host, username, password, dut_id): self.magic_prompt = "MAGIC PROMPT" + self.logger = None + + self.host = host + self.username = username + self.password = password + + self._connect_host(dut_id=dut_id) + + @parallel_lock(num=8) + def _connect_host(self, dut_id=0): + """ + Create connection to assigned crb, parameter dut_id will be used in + parallel_lock thus can assure isolated locks for each crb. + Parallel ssh connections are limited to MaxStartups option in SSHD + configuration file. By default concurrent number is 10, so default + threads number is limited to 8 which less than 10. Lock number can + be modified along with MaxStartups value. + """ try: self.session = pxssh.pxssh() - self.host = host - self.username = username - self.password = password - if ':' in host: - self.ip = host.split(':')[0] - self.port = int(host.split(':')[1]) + if ':' in self.host: + self.ip = self.host.split(':')[0] + self.port = int(self.host.split(':')[1]) self.session.login(self.ip, self.username, self.password, original_prompt='[$#>]', port=self.port, login_timeout=20) else: self.session.login(self.host, self.username, self.password, original_prompt='[$#>]') - self.send_expect('stty -echo', '# ', timeout=2) - except Exception, e: + self.send_expect('stty -echo', '#') + self.send_expect('stty columns 1000', "#") + except Exception as e: print RED(e) if getattr(self, 'port', None): suggestion = "\nSuggession: Check if the fireware on [ %s ] " % \ self.ip + "is stoped\n" print GREEN(suggestion) - raise SSHConnectionException(host) + raise SSHConnectionException(self.host) def init_log(self, logger, name): self.logger = logger @@ -56,24 +72,33 @@ class SSHPexpect(object): return before def send_expect(self, command, expected, timeout=15, verify=False): - ret = self.send_expect_base(command, expected, timeout) - if verify: - ret_status = self.send_expect_base("echo $?", expected, timeout) - if not int(ret_status): - return ret + + try: + ret = self.send_expect_base(command, expected, timeout) + if verify: + ret_status = self.send_expect_base("echo $?", expected, timeout) + if not int(ret_status): + return ret + else: + self.logger.error("Command: %s failure!" % command) + self.logger.error(ret) + return int(ret_status) else: - self.logger.error("Command: %s failure!" % command) - self.logger.error(ret) - return int(ret_status) - else: - return ret + return ret + except Exception as e: + print RED("Exception happened in [%s] and output is [%s]" % (command, self.get_output_before())) + raise(e) def send_command(self, command, timeout=1): - ignore_keyintr() - self.clean_session() - self.__sendline(command) - aware_keyintr() - return self.get_session_before(timeout) + try: + ignore_keyintr() + self.clean_session() + self.__sendline(command) + aware_keyintr() + except Exception as e: + raise(e) + + return self.get_session_before(timeout=timeout) def clean_session(self): self.get_session_before(timeout=0.01) @@ -90,8 +115,9 @@ class SSHPexpect(object): pass aware_keyintr() - before = self.get_output_before() + before = self.get_output_all() self.__flush() + return before def __flush(self): @@ -116,7 +142,6 @@ class SSHPexpect(object): def get_output_before(self): if not self.isalive(): raise SSHSessionDeadException(self.host) - self.session.flush() before = self.session.before.rsplit('\r\n', 1) if before[0] == "[PEXPECT]": before[0] = "" @@ -124,7 +149,6 @@ class SSHPexpect(object): return before[0] def get_output_all(self): - self.session.flush() output = self.session.before output.replace("[PEXPECT]", "") return output -- 1.9.3