Skip to content

Commit

Permalink
iotests: add file_path helper
Browse files Browse the repository at this point in the history
Simple way to have auto generated filenames with auto cleanup. Like
FilePath but without using 'with' statement and without additional
indentation of the whole test.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20180312152126.286890-8-vsementsov@virtuozzo.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
[eblake: grammar tweak]
Signed-off-by: Eric Blake <eblake@redhat.com>
  • Loading branch information
Vladimir Sementsov-Ogievskiy authored and ebblake committed Mar 13, 2018
1 parent 02f3a91 commit ef6e922
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/qemu-iotests/iotests.py
Expand Up @@ -27,6 +27,7 @@
import json
import signal
import logging
import atexit

sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'scripts'))
import qtest
Expand Down Expand Up @@ -250,6 +251,37 @@ def __exit__(self, exc_type, exc_val, exc_tb):
return False


def file_path_remover():
for path in reversed(file_path_remover.paths):
try:
os.remove(path)
except OSError:
pass


def file_path(*names):
''' Another way to get auto-generated filename that cleans itself up.
Use is as simple as:
img_a, img_b = file_path('a.img', 'b.img')
sock = file_path('socket')
'''

if not hasattr(file_path_remover, 'paths'):
file_path_remover.paths = []
atexit.register(file_path_remover)

paths = []
for name in names:
filename = '{0}-{1}'.format(os.getpid(), name)
path = os.path.join(test_dir, filename)
file_path_remover.paths.append(path)
paths.append(path)

return paths[0] if len(paths) == 1 else paths


class VM(qtest.QEMUQtestMachine):
'''A QEMU VM'''

Expand Down

0 comments on commit ef6e922

Please sign in to comment.