Blame SOURCES/kvm-iotests.py-add-FilePath-context-manager.patch

4a2fec
From 2715cad830a73bb550a4645054ff2e008f7e08fa Mon Sep 17 00:00:00 2001
4a2fec
From: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
Date: Fri, 22 Dec 2017 11:08:42 +0100
4a2fec
Subject: [PATCH 24/42] iotests.py: add FilePath context manager
4a2fec
4a2fec
RH-Author: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
Message-id: <20171222110900.24813-3-stefanha@redhat.com>
4a2fec
Patchwork-id: 78484
4a2fec
O-Subject: [RHV7.5 qemu-kvm-rhev PATCH 02/20] iotests.py: add FilePath 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
The scratch/ (TEST_DIR) directory is not automatically cleaned up after
4a2fec
test execution.  It is the responsibility of tests to remove any files
4a2fec
they create.
4a2fec
4a2fec
A nice way of doing this is to declare files at the beginning of the
4a2fec
test and automatically remove them with a context manager:
4a2fec
4a2fec
  with iotests.FilePath('test.img') as img_path:
4a2fec
      qemu_img(...)
4a2fec
      qemu_io(...)
4a2fec
  # img_path is guaranteed to be deleted here
4a2fec
4a2fec
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
Message-id: 20170824072202.26818-3-stefanha@redhat.com
4a2fec
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
(cherry picked from commit f4844ac0adabc458ba4610a71155448783d37c73)
4a2fec
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
---
4a2fec
 tests/qemu-iotests/iotests.py | 26 ++++++++++++++++++++++++++
4a2fec
 1 file changed, 26 insertions(+)
4a2fec
4a2fec
diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
4a2fec
index 7233983..07fa162 100644
4a2fec
--- a/tests/qemu-iotests/iotests.py
4a2fec
+++ b/tests/qemu-iotests/iotests.py
4a2fec
@@ -160,6 +160,32 @@ class Timeout:
4a2fec
     def timeout(self, signum, frame):
4a2fec
         raise Exception(self.errmsg)
4a2fec
 
4a2fec
+
4a2fec
+class FilePath(object):
4a2fec
+    '''An auto-generated filename that cleans itself up.
4a2fec
+
4a2fec
+    Use this context manager to generate filenames and ensure that the file
4a2fec
+    gets deleted::
4a2fec
+
4a2fec
+        with TestFilePath('test.img') as img_path:
4a2fec
+            qemu_img('create', img_path, '1G')
4a2fec
+        # migration_sock_path is automatically deleted
4a2fec
+    '''
4a2fec
+    def __init__(self, name):
4a2fec
+        filename = '{0}-{1}'.format(os.getpid(), name)
4a2fec
+        self.path = os.path.join(test_dir, filename)
4a2fec
+
4a2fec
+    def __enter__(self):
4a2fec
+        return self.path
4a2fec
+
4a2fec
+    def __exit__(self, exc_type, exc_val, exc_tb):
4a2fec
+        try:
4a2fec
+            os.remove(self.path)
4a2fec
+        except OSError:
4a2fec
+            pass
4a2fec
+        return False
4a2fec
+
4a2fec
+
4a2fec
 class VM(qtest.QEMUQtestMachine):
4a2fec
     '''A QEMU VM'''
4a2fec
 
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec