From 69f6e62877dcaafed3dec109767cbcd52c1d1ca7 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Mon, 14 Aug 2023 11:50:59 +0000 Subject: [PATCH] test(backups): more checks regarding tmpdirs and mounting --- .../backup/backuppers/restic_backupper.py | 13 +++++++++---- tests/test_graphql/test_backup.py | 9 +++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/selfprivacy_api/backup/backuppers/restic_backupper.py b/selfprivacy_api/backup/backuppers/restic_backupper.py index 3a5fc49..418aa5b 100644 --- a/selfprivacy_api/backup/backuppers/restic_backupper.py +++ b/selfprivacy_api/backup/backuppers/restic_backupper.py @@ -126,7 +126,9 @@ class ResticBackupper(AbstractBackupper): output, ) - def mount_repo(self, mount_directory): + def mount_repo(self, mount_directory: str) -> subprocess.Popen: + if not exists(mount_directory): + raise FileNotFoundError("no such directory to mount at: ", mount_directory) mount_command = self.restic_command("mount", mount_directory) mount_command.insert(0, "nohup") handle = subprocess.Popen( @@ -139,7 +141,7 @@ class ResticBackupper(AbstractBackupper): raise IOError("failed to mount dir ", mount_directory) return handle - def unmount_repo(self, mount_directory): + def unmount_repo(self, mount_directory: str) -> None: mount_command = ["umount", "-l", mount_directory] with subprocess.Popen( mount_command, stdout=subprocess.PIPE, shell=False @@ -147,10 +149,10 @@ class ResticBackupper(AbstractBackupper): output = handle.communicate()[0].decode("utf-8") # TODO: check for exit code? if "error" in output.lower(): - return IOError("failed to unmount dir ", mount_directory, ": ", output) + raise IOError("failed to unmount dir ", mount_directory, ": ", output) if not listdir(mount_directory) == []: - return IOError("failed to unmount dir ", mount_directory) + raise IOError("failed to unmount dir ", mount_directory) @staticmethod def __flatten_list(list_to_flatten): @@ -363,6 +365,9 @@ class ResticBackupper(AbstractBackupper): self._raw_verified_restore(snapshot_id, target=temp_dir) snapshot_root = temp_dir else: # attempting inplace restore via mount + sync + assert exists( + temp_dir + ) # paranoid check, TemporaryDirectory is supposedly created self.mount_repo(temp_dir) snapshot_root = join(temp_dir, "ids", snapshot_id) diff --git a/tests/test_graphql/test_backup.py b/tests/test_graphql/test_backup.py index 1990ef7..9736f91 100644 --- a/tests/test_graphql/test_backup.py +++ b/tests/test_graphql/test_backup.py @@ -8,6 +8,8 @@ from os import urandom from datetime import datetime, timedelta, timezone from subprocess import Popen +import tempfile + import selfprivacy_api.services as services from selfprivacy_api.services import Service, get_all_services @@ -806,3 +808,10 @@ def test_operations_while_locked(backups, dummy_service): # check that no locks were left Backups.provider().backupper.lock() Backups.provider().backupper.unlock() + + +# a paranoid check to weed out problems with tempdirs that are not dependent on us +def test_tempfile(): + with tempfile.TemporaryDirectory() as temp: + assert path.exists(temp) + assert not path.exists(temp)