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 <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2017-12-31 21:52:15 +05:30 committed by James Valleroy
parent 0c7d41f3f6
commit 67cb367838
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -22,6 +22,7 @@ Test module for storage module operations.
import os import os
import re import re
import subprocess import subprocess
import tempfile
import unittest import unittest
@ -50,21 +51,18 @@ class Disk():
def _create_disk_file(self): def _create_disk_file(self):
"""Create a temporary file to act as a disk.""" """Create a temporary file to act as a disk."""
directory = os.path.dirname(os.path.realpath(__file__)) disk_file = tempfile.NamedTemporaryFile()
disk_file = os.path.join(directory, 'temp_disk.img')
if os.path.isfile(disk_file):
os.remove(disk_file)
command = 'dd if=/dev/zero of={file} bs=1M count={size}' \ 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) subprocess.run(command.split(), stderr=subprocess.DEVNULL, check=True)
self.disk_file = disk_file self.disk_file = disk_file
def _setup_loopback(self): def _setup_loopback(self):
"""Setup loop back on the create disk file.""" """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, process = subprocess.run(command.split(), stdout=subprocess.PIPE,
check=True) check=True)
device = process.stdout.decode().strip() device = process.stdout.decode().strip()
@ -77,7 +75,9 @@ class Disk():
def _create_partitions(self): def _create_partitions(self):
"""Create partitions as specified in disk_info.""" """Create partitions as specified in disk_info."""
steps = [step.split() for step in self.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: for step in steps:
command += step command += step
@ -104,7 +104,7 @@ class Disk():
def _remove_disk_file(self): def _remove_disk_file(self):
"""Delete the disk_file.""" """Delete the disk_file."""
os.remove(self.disk_file) self.disk_file.close()
self.disk_file = None self.disk_file = None
def __enter__(self): def __enter__(self):