From 67cb3678385b7b0ad71dbac9f48341b098762700 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sun, 31 Dec 2017 21:52:15 +0530 Subject: [PATCH] storage: Make tests run on special filesystems When the source code directory is on a special filesystem such as vbox shared filesystem, loop back device setup does not seem to work properly. Fix this by creating disk images in temporary directory instead of source code directory. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/storage/tests/test_storage.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/plinth/modules/storage/tests/test_storage.py b/plinth/modules/storage/tests/test_storage.py index d40298e0e..c6e8191a3 100644 --- a/plinth/modules/storage/tests/test_storage.py +++ b/plinth/modules/storage/tests/test_storage.py @@ -22,6 +22,7 @@ Test module for storage module operations. import os import re import subprocess +import tempfile import unittest @@ -50,21 +51,18 @@ class Disk(): def _create_disk_file(self): """Create a temporary file to act as a disk.""" - directory = os.path.dirname(os.path.realpath(__file__)) - disk_file = os.path.join(directory, 'temp_disk.img') - - if os.path.isfile(disk_file): - os.remove(disk_file) + disk_file = tempfile.NamedTemporaryFile() command = 'dd if=/dev/zero of={file} bs=1M count={size}' \ - .format(size=self.size, file=disk_file) + .format(size=self.size, file=disk_file.name) subprocess.run(command.split(), stderr=subprocess.DEVNULL, check=True) self.disk_file = disk_file def _setup_loopback(self): """Setup loop back on the create disk file.""" - command = 'losetup --show --find {file}'.format(file=self.disk_file) + command = 'losetup --show --find {file}'.format( + file=self.disk_file.name) process = subprocess.run(command.split(), stdout=subprocess.PIPE, check=True) device = process.stdout.decode().strip() @@ -77,7 +75,9 @@ class Disk(): def _create_partitions(self): """Create partitions as specified in disk_info.""" steps = [step.split() for step in self.disk_info] - command = ['parted', '--align=optimal', '--script', self.disk_file] + command = [ + 'parted', '--align=optimal', '--script', self.disk_file.name + ] for step in steps: command += step @@ -104,7 +104,7 @@ class Disk(): def _remove_disk_file(self): """Delete the disk_file.""" - os.remove(self.disk_file) + self.disk_file.close() self.disk_file = None def __enter__(self):