diff mbox

[KVM-AUTOTEST,1/3] KVM test: VM.send_monitor_cmd() minor cleanup

Message ID 1255601637-6355-1-git-send-email-mgoldish@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Michael Goldish Oct. 15, 2009, 10:13 a.m. UTC
None
diff mbox

Patch

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):