storage: Fix tests by wrestling with auto-mounting of disks

- Also properly cleanup mounted file systems.

- When a file system is created, it is automatically mounted by udiskie (or
FreedomBox itself). This leads loop back setups not getting cleaned and btrfs
check failing to check a mounted disk. Force check in case of btrfs and umount
ignoring errors before cleanup.

Closes: #1839.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2020-04-21 18:42:58 -07:00 committed by James Valleroy
parent 41498ba9f6
commit a327bf650b
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -81,6 +81,15 @@ class Disk():
subprocess.run(command, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, check=True)
def _unmount_file_systems(self):
"""Unmount al partitions if it is mounted by external party."""
if not self.file_system_info:
return
for partition, _ in self.file_system_info:
device = _get_partition_device(self.device, partition)
subprocess.run(['umount', device], check=False)
def _cleanup_loopback(self):
"""Undo the loopback device setup."""
subprocess.run(['losetup', '--detach', self.device])
@ -99,6 +108,7 @@ class Disk():
def __exit__(self, *exc):
"""Exit the context, destroy the test disk."""
self._unmount_file_systems()
self._cleanup_loopback()
self._remove_disk_file()
@ -221,7 +231,7 @@ class TestActions:
def assert_btrfs_file_system_healthy(self, partition_number):
"""Perform a successful ext4 file system check."""
device = _get_partition_device(self.device, partition_number)
command = ['btrfs', 'check', device]
command = ['btrfs', 'check', '--force', device]
subprocess.run(command, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, check=True)