From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
Daniel Berrange <berrange@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>
Subject: [Qemu-devel] [PATCH 2/3] iotests.py: add FilePath context manager
Date: Thu, 24 Aug 2017 08:22:01 +0100 [thread overview]
Message-ID: <20170824072202.26818-3-stefanha@redhat.com> (raw)
In-Reply-To: <20170824072202.26818-1-stefanha@redhat.com>
The scratch/ (TEST_DIR) directory is not automatically cleaned up after
test execution. It is the responsibility of tests to remove any files
they create.
A nice way of doing this is to declare files at the beginning of the
test and automatically remove them with a context manager:
with iotests.FilePath('test.img') as img_path:
qemu_img(...)
qemu_io(...)
# img_path is guaranteed to be deleted here
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
tests/qemu-iotests/iotests.py | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
index 7233983f3c..07fa1626a0 100644
--- a/tests/qemu-iotests/iotests.py
+++ b/tests/qemu-iotests/iotests.py
@@ -160,6 +160,32 @@ class Timeout:
def timeout(self, signum, frame):
raise Exception(self.errmsg)
+
+class FilePath(object):
+ '''An auto-generated filename that cleans itself up.
+
+ Use this context manager to generate filenames and ensure that the file
+ gets deleted::
+
+ with TestFilePath('test.img') as img_path:
+ qemu_img('create', img_path, '1G')
+ # migration_sock_path is automatically deleted
+ '''
+ def __init__(self, name):
+ filename = '{0}-{1}'.format(os.getpid(), name)
+ self.path = os.path.join(test_dir, filename)
+
+ def __enter__(self):
+ return self.path
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ try:
+ os.remove(self.path)
+ except OSError:
+ pass
+ return False
+
+
class VM(qtest.QEMUQtestMachine):
'''A QEMU VM'''
--
2.13.5
next prev parent reply other threads:[~2017-08-24 7:44 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-24 7:21 [Qemu-devel] [PATCH 0/3] iotests: clean up resources using context managers Stefan Hajnoczi
2017-08-24 7:22 ` [Qemu-devel] [PATCH 1/3] qemu.py: make VM() a context manager Stefan Hajnoczi
2017-08-25 12:44 ` Eduardo Habkost
2017-08-24 7:22 ` Stefan Hajnoczi [this message]
2017-08-24 7:22 ` [Qemu-devel] [PATCH 3/3] qemu-iotests: use context managers for resource cleanup in 194 Stefan Hajnoczi
2017-08-24 8:38 ` [Qemu-devel] [PATCH 0/3] iotests: clean up resources using context managers Fam Zheng
2017-08-24 18:04 ` Stefan Hajnoczi
2017-08-25 7:32 ` Fam Zheng
2017-08-25 8:52 ` Stefan Hajnoczi
2017-08-25 9:29 ` Fam Zheng
2017-08-30 12:44 ` Stefan Hajnoczi
2017-08-30 12:54 ` Fam Zheng
2017-08-28 10:55 ` Markus Armbruster
2017-08-31 11:26 ` Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170824072202.26818-3-stefanha@redhat.com \
--to=stefanha@redhat.com \
--cc=berrange@redhat.com \
--cc=kwolf@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).