From: Michael Goldish Subject: [KVM-AUTOTEST PATCH 08/18] KVM test: simplify remote_scp() Date: Fri, 18 Jun 2010 03:14:25 +0300 Message-ID: <1276820075-31310-8-git-send-email-mgoldish@redhat.com> References: <1276820075-31310-1-git-send-email-mgoldish@redhat.com> <1276820075-31310-2-git-send-email-mgoldish@redhat.com> <1276820075-31310-3-git-send-email-mgoldish@redhat.com> <1276820075-31310-4-git-send-email-mgoldish@redhat.com> <1276820075-31310-5-git-send-email-mgoldish@redhat.com> <1276820075-31310-6-git-send-email-mgoldish@redhat.com> <1276820075-31310-7-git-send-email-mgoldish@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: autotest@test.kernel.org, kvm@vger.kernel.org Return-path: In-Reply-To: <1276820075-31310-7-git-send-email-mgoldish@redhat.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: autotest-bounces@test.kernel.org Errors-To: autotest-bounces@test.kernel.org List-Id: kvm.vger.kernel.org No need to use the redundant timeout check (end_time <= time.time()). This is taken care of by read_until_last_line_matches(). The same goes for the exit status check. Also, rename timeout to transfer_timeout and timeout_ to timeout. Signed-off-by: Michael Goldish --- client/tests/kvm/kvm_utils.py | 56 +++++++++++++++------------------------- 1 files changed, 21 insertions(+), 35 deletions(-) diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py index 0ea5a8a..1399892 100644 --- a/client/tests/kvm/kvm_utils.py +++ b/client/tests/kvm/kvm_utils.py @@ -524,20 +524,20 @@ def remote_login(command, password, prompt, linesep="\n", timeout=10): return None -def remote_scp(command, password, timeout=600, login_timeout=10): +def remote_scp(command, password, transfer_timeout=600, login_timeout=10): """ Run the given command using kvm_spawn and provide answers to the questions - asked. If timeout expires while waiting for the transfer to complete , - fail. If login_timeout expires while waiting for output from the child - (e.g. a password prompt), fail. + asked. If transfer_timeout expires while waiting for the transfer to + complete, fail. If login_timeout expires while waiting for output from the + child (e.g. a password prompt), fail. @brief: Transfer files using SCP, given a command line. @param command: The command to execute (e.g. "scp -r foobar root@localhost:/tmp/"). @param password: The password to send in reply to a password prompt. - @param timeout: The time duration (in seconds) to wait for the transfer - to complete. + @param transfer_timeout: The time duration (in seconds) to wait for the + transfer to complete. @param login_timeout: The maximal time duration (in seconds) to wait for each step of the login procedure (i.e. the "Are you sure" prompt or the password prompt) @@ -547,20 +547,15 @@ def remote_scp(command, password, timeout=600, login_timeout=10): sub = kvm_subprocess.kvm_expect(command) password_prompt_count = 0 - _timeout = login_timeout - end_time = time.time() + timeout - logging.debug("Trying to SCP with command '%s', timeout %ss", command, - timeout) + timeout = login_timeout + + logging.debug("Trying to SCP with command '%s', timeout %ss", + command, timeout) while True: - if end_time <= time.time(): - logging.debug("SCP transfer timed out") - sub.close() - return False (match, text) = sub.read_until_last_line_matches( - [r"[Aa]re you sure", r"[Pp]assword:\s*$", r"lost connection", - r"Exit status", r"stalled"], - timeout=_timeout, internal_timeout=0.5) + [r"[Aa]re you sure", r"[Pp]assword:\s*$", r"lost connection"], + timeout=timeout, internal_timeout=0.5) if match == 0: # "Are you sure you want to continue connecting" logging.debug("Got 'Are you sure...'; sending 'yes'") sub.sendline("yes") @@ -570,7 +565,7 @@ def remote_scp(command, password, timeout=600, login_timeout=10): logging.debug("Got password prompt; sending '%s'" % password) sub.sendline(password) password_prompt_count += 1 - _timeout = timeout + timeout = transfer_timeout continue else: logging.debug("Got password prompt again") @@ -580,25 +575,16 @@ def remote_scp(command, password, timeout=600, login_timeout=10): logging.debug("Got 'lost connection'") sub.close() return False - elif match == 3: # "Exit status" - sub.close() - if "Exit status 0" in text: - logging.debug("SCP command completed successfully") - return True - else: - logging.debug("SCP command fail with exit status %s" % text) - return False - elif match == 4: # "stalled" - logging.debug("SCP connection is stalled") - continue - else: # match == None if sub.is_alive(): - continue - logging.debug("SCP process terminated") - status = sub.get_status() - sub.close() - return status == 0 + logging.debug("Timeout expired") + sub.close() + return False + else: + status = sub.get_status() + sub.close() + logging.debug("SCP process terminated with status %s", status) + return status == 0 def scp_to_remote(host, port, username, password, local_path, remote_path, -- 1.5.4.1