Blame SOURCES/kvm-qemu.py-make-VM-a-context-manager.patch

4a2fec
From da96c55a017fbc750b49550c6e7a523dcdab0dbe Mon Sep 17 00:00:00 2001
4a2fec
From: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
Date: Fri, 22 Dec 2017 11:08:41 +0100
4a2fec
Subject: [PATCH 23/42] qemu.py: make VM() a context manager
4a2fec
4a2fec
RH-Author: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
Message-id: <20171222110900.24813-2-stefanha@redhat.com>
4a2fec
Patchwork-id: 78483
4a2fec
O-Subject: [RHV7.5 qemu-kvm-rhev PATCH 01/20] qemu.py: make VM() a context manager
4a2fec
Bugzilla: 1519721
4a2fec
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
4a2fec
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
4a2fec
There are a number of ways to ensure that the QEMU process is shut down
4a2fec
when the test ends, including atexit.register(), try: finally:, or
4a2fec
unittest.teardown() methods.  All of these require extra code and the
4a2fec
programmer must remember to add vm.shutdown().
4a2fec
4a2fec
A nice solution is context managers:
4a2fec
4a2fec
  with VM(binary) as vm:
4a2fec
      ...
4a2fec
  # vm is guaranteed to be shut down here
4a2fec
4a2fec
Cc: Eduardo Habkost <ehabkost@redhat.com>
4a2fec
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
4a2fec
Message-id: 20170824072202.26818-2-stefanha@redhat.com
4a2fec
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
(cherry picked from commit d792bc3811f22a22a46c7d9a725fd29029f54095)
4a2fec
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
---
4a2fec
 scripts/qemu.py | 16 +++++++++++++++-
4a2fec
 1 file changed, 15 insertions(+), 1 deletion(-)
4a2fec
4a2fec
diff --git a/scripts/qemu.py b/scripts/qemu.py
4a2fec
index 880e3e8..4d8ee10 100644
4a2fec
--- a/scripts/qemu.py
4a2fec
+++ b/scripts/qemu.py
4a2fec
@@ -21,7 +21,14 @@ import qmp.qmp
4a2fec
 
4a2fec
 
4a2fec
 class QEMUMachine(object):
4a2fec
-    '''A QEMU VM'''
4a2fec
+    '''A QEMU VM
4a2fec
+
4a2fec
+    Use this object as a context manager to ensure the QEMU process terminates::
4a2fec
+
4a2fec
+        with VM(binary) as vm:
4a2fec
+            ...
4a2fec
+        # vm is guaranteed to be shut down here
4a2fec
+    '''
4a2fec
 
4a2fec
     def __init__(self, binary, args=[], wrapper=[], name=None, test_dir="/var/tmp",
4a2fec
                  monitor_address=None, socket_scm_helper=None, debug=False):
4a2fec
@@ -40,6 +47,13 @@ class QEMUMachine(object):
4a2fec
         self._socket_scm_helper = socket_scm_helper
4a2fec
         self._debug = debug
4a2fec
 
4a2fec
+    def __enter__(self):
4a2fec
+        return self
4a2fec
+
4a2fec
+    def __exit__(self, exc_type, exc_val, exc_tb):
4a2fec
+        self.shutdown()
4a2fec
+        return False
4a2fec
+
4a2fec
     # This can be used to add an unused monitor instance.
4a2fec
     def add_monitor_telnet(self, ip, port):
4a2fec
         args = 'tcp:%s:%d,server,nowait,telnet' % (ip, port)
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec