From: Michael Goldish Subject: [KVM-AUTOTEST PATCH 1/3] KVM test: VM.send_monitor_cmd() minor cleanup Date: Thu, 15 Oct 2009 12:13:55 +0200 Message-ID: <1255601637-6355-1-git-send-email-mgoldish@redhat.com> Cc: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:50483 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751200AbZJOKRg (ORCPT ); Thu, 15 Oct 2009 06:17:36 -0400 Sender: kvm-owner@vger.kernel.org List-ID: - Move some of the code into a try..finally block - Shutdown the socket before closing it Signed-off-by: Michael Goldish --- client/tests/kvm/kvm_vm.py | 45 ++++++++++++++++++++++++------------------- 1 files changed, 25 insertions(+), 20 deletions(-) diff --git a/client/tests/kvm/kvm_vm.py b/client/tests/kvm/kvm_vm.py index 82f1eb4..a8d96ca 100755 --- a/client/tests/kvm/kvm_vm.py +++ b/client/tests/kvm/kvm_vm.py @@ -465,7 +465,7 @@ class VM: end_time = time.time() + timeout while time.time() < end_time: try: - o += s.recv(16384) + o += s.recv(1024) if o.splitlines()[-1].split()[-1] == "(qemu)": return (True, o) except: @@ -481,27 +481,32 @@ class VM: except: logging.debug("Could not connect to monitor socket") return (1, "") - status, data = read_up_to_qemu_prompt(s, timeout) - if not status: - s.close() - logging.debug("Could not find (qemu) prompt; output so far:" \ - + kvm_utils.format_str_for_message(data)) - return (1, "") - # Send command - s.sendall(command + "\n") - # Receive command output - data = "" - if block: + + # Send the command and get the resulting output + try: status, data = read_up_to_qemu_prompt(s, timeout) - data = "\n".join(data.splitlines()[1:]) if not status: - s.close() - logging.debug("Could not find (qemu) prompt after command;" - " output so far: %s", - kvm_utils.format_str_for_message(data)) - return (1, data) - s.close() - return (0, data) + logging.debug("Could not find (qemu) prompt; output so far:" + + kvm_utils.format_str_for_message(data)) + return (1, "") + # Send command + s.sendall(command + "\n") + # Receive command output + data = "" + if block: + status, data = read_up_to_qemu_prompt(s, timeout) + data = "\n".join(data.splitlines()[1:]) + if not status: + logging.debug("Could not find (qemu) prompt after command; " + "output so far:" + + kvm_utils.format_str_for_message(data)) + return (1, data) + return (0, data) + + # Clean up before exiting + finally: + s.shutdown(socket.SHUT_RDWR) + s.close() def destroy(self, gracefully=True): -- 1.5.4.1