From 08739f7ca8b925755db426ad5bbeee1586fdc2a5 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 14 Apr 2023 11:20:03 +0000 Subject: [PATCH] refactor(backups): make api accept a list of folders --- selfprivacy_api/backup/__init__.py | 8 ++++---- selfprivacy_api/backup/backuper.py | 4 ++-- selfprivacy_api/backup/restic_backuper.py | 10 +++++++--- selfprivacy_api/services/test_service/__init__.py | 6 +++--- tests/test_graphql/test_backup.py | 6 +++--- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/selfprivacy_api/backup/__init__.py b/selfprivacy_api/backup/__init__.py index 708d4a5..f89a54e 100644 --- a/selfprivacy_api/backup/__init__.py +++ b/selfprivacy_api/backup/__init__.py @@ -194,11 +194,11 @@ class Backups: @staticmethod def back_up(service: Service): """The top-level function to back up a service""" - folder = service.get_folders() + folders = service.get_folders() repo_name = service.get_id() service.pre_backup() - snapshot = Backups.provider().backuper.start_backup(folder, repo_name) + snapshot = Backups.provider().backuper.start_backup(folders, repo_name) Backups._store_last_snapshot(repo_name, snapshot) service.post_restore() @@ -238,9 +238,9 @@ class Backups: @staticmethod def restore_service_from_snapshot(service: Service, snapshot_id: str): repo_name = service.get_id() - folder = service.get_folders() + folders = service.get_folders() - Backups.provider().backuper.restore_from_backup(repo_name, snapshot_id, folder) + Backups.provider().backuper.restore_from_backup(repo_name, snapshot_id, folders) @staticmethod def restore_snapshot(snapshot: Snapshot): diff --git a/selfprivacy_api/backup/backuper.py b/selfprivacy_api/backup/backuper.py index 5bba9d5..908c1fc 100644 --- a/selfprivacy_api/backup/backuper.py +++ b/selfprivacy_api/backup/backuper.py @@ -13,7 +13,7 @@ class AbstractBackuper(ABC): raise NotImplementedError @abstractmethod - def start_backup(self, folder: str, repo_name: str): + def start_backup(self, folders: List[str], repo_name: str): raise NotImplementedError @abstractmethod @@ -26,7 +26,7 @@ class AbstractBackuper(ABC): raise NotImplementedError @abstractmethod - def restore_from_backup(self, repo_name: str, snapshot_id: str, folder: str): + def restore_from_backup(self, repo_name: str, snapshot_id: str, folders: List[str]): """Restore a target folder using a snapshot""" raise NotImplementedError diff --git a/selfprivacy_api/backup/restic_backuper.py b/selfprivacy_api/backup/restic_backuper.py index cc81361..2af7d44 100644 --- a/selfprivacy_api/backup/restic_backuper.py +++ b/selfprivacy_api/backup/restic_backuper.py @@ -57,15 +57,19 @@ class ResticBackuper(AbstractBackuper): command.extend(args) return command - def start_backup(self, folder: str, repo_name: str): + def start_backup(self, folders: List[str], repo_name: str): """ Start backup with restic """ + + # but maybe it is ok to accept a union of a string and an array of strings + assert not isinstance(folders, str) + backup_command = self.restic_command( repo_name, "backup", "--json", - folder, + folders[0], ) with subprocess.Popen( backup_command, @@ -145,7 +149,7 @@ class ResticBackuper(AbstractBackuper): except ValueError as e: raise ValueError("cannot restore a snapshot: " + output) from e - def restore_from_backup(self, repo_name, snapshot_id, folder): + def restore_from_backup(self, repo_name, snapshot_id, folders): """ Restore from backup with restic """ diff --git a/selfprivacy_api/services/test_service/__init__.py b/selfprivacy_api/services/test_service/__init__.py index 2906244..e790be9 100644 --- a/selfprivacy_api/services/test_service/__init__.py +++ b/selfprivacy_api/services/test_service/__init__.py @@ -1,6 +1,7 @@ """Class representing Bitwarden service""" import base64 import typing +from typing import List from selfprivacy_api.jobs import Job from selfprivacy_api.services.service import Service, ServiceDnsRecord, ServiceStatus @@ -111,9 +112,8 @@ class DummyService(Service): return "sda1" @classmethod - def get_folders(cls) -> str: - # for now only a single folder - return cls.location + def get_folders(cls) -> List[str]: + return [cls.location] @staticmethod def get_dns_records() -> typing.List[ServiceDnsRecord]: diff --git a/tests/test_graphql/test_backup.py b/tests/test_graphql/test_backup.py index 9c8af3f..460fa44 100644 --- a/tests/test_graphql/test_backup.py +++ b/tests/test_graphql/test_backup.py @@ -143,10 +143,10 @@ def test_one_snapshot(backups, dummy_service): def test_backup_returns_snapshot(backups, dummy_service): - service_folder = dummy_service.get_folders() + service_folders = dummy_service.get_folders() provider = Backups.provider() name = dummy_service.get_id() - snapshot = provider.backuper.start_backup(service_folder, name) + snapshot = provider.backuper.start_backup(service_folders, name) assert snapshot.id is not None assert snapshot.service_name == name @@ -154,7 +154,7 @@ def test_backup_returns_snapshot(backups, dummy_service): def test_restore(backups, dummy_service): - service_folder = dummy_service.get_folders() + service_folder = dummy_service.get_folders()[0] file_to_nuke = listdir(service_folder)[0] assert file_to_nuke is not None path_to_nuke = path.join(service_folder, file_to_nuke)