mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-23 18:59:56 +00:00
add backup and restore subcommands
This commit is contained in:
58
tests/ahriman/application/handlers/test_handler_backup.py
Normal file
58
tests/ahriman/application/handlers/test_handler_backup.py
Normal file
@ -0,0 +1,58 @@
|
||||
import argparse
|
||||
|
||||
from pathlib import Path
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from ahriman.application.handlers import Backup
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.models.repository_paths import RepositoryPaths
|
||||
|
||||
|
||||
def _default_args(args: argparse.Namespace) -> argparse.Namespace:
|
||||
"""
|
||||
default arguments for these test cases
|
||||
:param args: command line arguments fixture
|
||||
:return: generated arguments for these test cases
|
||||
"""
|
||||
args.path = Path("result.tar.gz")
|
||||
return args
|
||||
|
||||
|
||||
def test_run(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must run command
|
||||
"""
|
||||
args = _default_args(args)
|
||||
mocker.patch("ahriman.application.handlers.Backup.get_paths", return_value=[Path("path")])
|
||||
tarfile = MagicMock()
|
||||
add_mock = tarfile.__enter__.return_value = MagicMock()
|
||||
mocker.patch("tarfile.TarFile.__new__", return_value=tarfile)
|
||||
|
||||
Backup.run(args, "x86_64", configuration, True, False)
|
||||
add_mock.add.assert_called_once_with(Path("path"))
|
||||
|
||||
|
||||
def test_get_paths(configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must get paths to be archived
|
||||
"""
|
||||
# gnupg export mock
|
||||
mocker.patch("pathlib.Path.is_dir", return_value=True)
|
||||
mocker.patch.object(RepositoryPaths, "root_owner", (42, 42))
|
||||
getpwuid_mock = mocker.patch("pwd.getpwuid", return_value=MagicMock())
|
||||
# well database does not exist so we override it
|
||||
database_mock = mocker.patch("ahriman.core.database.sqlite.SQLite.database_path", return_value=configuration.path)
|
||||
|
||||
paths = Backup.get_paths(configuration)
|
||||
getpwuid_mock.assert_called_once_with(42)
|
||||
database_mock.assert_called_once_with(configuration)
|
||||
assert configuration.path in paths
|
||||
assert all(path.exists() for path in paths if path.name not in (".gnupg", "cache"))
|
||||
|
||||
|
||||
def test_disallow_auto_architecture_run() -> None:
|
||||
"""
|
||||
must not allow multi architecture run
|
||||
"""
|
||||
assert not Backup.ALLOW_AUTO_ARCHITECTURE_RUN
|
@ -50,6 +50,7 @@ def test_run_extract_packages(args: argparse.Namespace, configuration: Configura
|
||||
"""
|
||||
args = _default_args(args)
|
||||
args.from_database = True
|
||||
args.dry_run = True
|
||||
mocker.patch("ahriman.models.repository_paths.RepositoryPaths.tree_create")
|
||||
mocker.patch("ahriman.application.application.Application.add")
|
||||
extract_mock = mocker.patch("ahriman.application.handlers.Rebuild.extract_packages", return_value=[])
|
||||
|
39
tests/ahriman/application/handlers/test_handler_restore.py
Normal file
39
tests/ahriman/application/handlers/test_handler_restore.py
Normal file
@ -0,0 +1,39 @@
|
||||
import argparse
|
||||
|
||||
from pathlib import Path
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from ahriman.application.handlers import Restore
|
||||
from ahriman.core.configuration import Configuration
|
||||
|
||||
|
||||
def _default_args(args: argparse.Namespace) -> argparse.Namespace:
|
||||
"""
|
||||
default arguments for these test cases
|
||||
:param args: command line arguments fixture
|
||||
:return: generated arguments for these test cases
|
||||
"""
|
||||
args.path = Path("result.tar.gz")
|
||||
args.output = Path.cwd()
|
||||
return args
|
||||
|
||||
|
||||
def test_run(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must run command
|
||||
"""
|
||||
args = _default_args(args)
|
||||
tarfile = MagicMock()
|
||||
extract_mock = tarfile.__enter__.return_value = MagicMock()
|
||||
mocker.patch("tarfile.TarFile.__new__", return_value=tarfile)
|
||||
|
||||
Restore.run(args, "x86_64", configuration, True, False)
|
||||
extract_mock.extractall.assert_called_once_with(path=args.output)
|
||||
|
||||
|
||||
def test_disallow_auto_architecture_run() -> None:
|
||||
"""
|
||||
must not allow multi architecture run
|
||||
"""
|
||||
assert not Restore.ALLOW_AUTO_ARCHITECTURE_RUN
|
Reference in New Issue
Block a user