mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-09-16 05:39:56 +00:00
add archive trigger
This commit is contained in:
@ -4,6 +4,7 @@ from pathlib import Path
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.core.alpm.repo import Repo
|
||||
from ahriman.models.package import Package
|
||||
from ahriman.models.repository_paths import RepositoryPaths
|
||||
|
||||
|
||||
@ -56,21 +57,37 @@ def test_repo_init(repo: Repo, mocker: MockerFixture) -> None:
|
||||
assert check_output_mock.call_args[0][0] == "repo-add"
|
||||
|
||||
|
||||
def test_repo_remove(repo: Repo, mocker: MockerFixture) -> None:
|
||||
def test_repo_remove(repo: Repo, package_ahriman: Package,mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must call repo-remove on package addition
|
||||
must call repo-remove on package removal
|
||||
"""
|
||||
filepath = package_ahriman.packages[package_ahriman.base].filepath
|
||||
mocker.patch("pathlib.Path.glob", return_value=[])
|
||||
check_output_mock = mocker.patch("ahriman.core.alpm.repo.check_output")
|
||||
|
||||
repo.remove("package", Path("package.pkg.tar.xz"))
|
||||
repo.remove(package_ahriman.base, filepath)
|
||||
check_output_mock.assert_called_once() # it will be checked later
|
||||
assert check_output_mock.call_args[0][0] == "repo-remove"
|
||||
assert package_ahriman.base in check_output_mock.call_args[0]
|
||||
|
||||
|
||||
def test_repo_remove_guess_package(repo: Repo, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must call repo-remove on package removal if no package name set
|
||||
"""
|
||||
filepath = package_ahriman.packages[package_ahriman.base].filepath
|
||||
mocker.patch("pathlib.Path.glob", return_value=[])
|
||||
check_output_mock = mocker.patch("ahriman.core.alpm.repo.check_output")
|
||||
|
||||
repo.remove(None, filepath)
|
||||
check_output_mock.assert_called_once() # it will be checked later
|
||||
assert check_output_mock.call_args[0][0] == "repo-remove"
|
||||
assert package_ahriman.base in check_output_mock.call_args[0]
|
||||
|
||||
|
||||
def test_repo_remove_fail_no_file(repo: Repo, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must fail on missing file
|
||||
must fail removal on missing file
|
||||
"""
|
||||
mocker.patch("pathlib.Path.glob", return_value=[Path("package.pkg.tar.xz")])
|
||||
mocker.patch("pathlib.Path.unlink", side_effect=FileNotFoundError)
|
||||
|
40
tests/ahriman/core/archive/conftest.py
Normal file
40
tests/ahriman/core/archive/conftest.py
Normal file
@ -0,0 +1,40 @@
|
||||
import pytest
|
||||
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.core.archive import ArchiveTrigger
|
||||
from ahriman.core.archive.archive_tree import ArchiveTree
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.sign.gpg import GPG
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def archive_tree(configuration: Configuration) -> ArchiveTree:
|
||||
"""
|
||||
archive tree fixture
|
||||
|
||||
Args:
|
||||
configuration(Configuration): configuration fixture
|
||||
|
||||
Returns:
|
||||
ArchiveTree: archive tree test instance
|
||||
"""
|
||||
return ArchiveTree(configuration.repository_paths, [])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def archive_trigger(configuration: Configuration, gpg: GPG, mocker: MockerFixture) -> ArchiveTrigger:
|
||||
"""
|
||||
archive trigger fixture
|
||||
|
||||
Args:
|
||||
configuration(Configuration): configuration fixture
|
||||
gpg(GPG): GPG fixture
|
||||
mocker(MockerFixture): mocker object
|
||||
|
||||
Returns:
|
||||
ArchiveTrigger: archive trigger test instance
|
||||
"""
|
||||
mocker.patch("ahriman.core._Context.get", return_value=GPG)
|
||||
_, repository_id = configuration.check_loaded()
|
||||
return ArchiveTrigger(repository_id, configuration)
|
88
tests/ahriman/core/archive/test_archive_tree.py
Normal file
88
tests/ahriman/core/archive/test_archive_tree.py
Normal file
@ -0,0 +1,88 @@
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.core.archive.archive_tree import ArchiveTree
|
||||
from ahriman.core.utils import utcnow
|
||||
from ahriman.models.package import Package
|
||||
|
||||
|
||||
def test_repository_for(archive_tree: ArchiveTree) -> None:
|
||||
"""
|
||||
must correctly generate path to repository
|
||||
"""
|
||||
path = archive_tree.repository_for()
|
||||
assert path.is_relative_to(archive_tree.paths.archive / "repos")
|
||||
assert (archive_tree.repository_id.name, archive_tree.repository_id.architecture) == path.parts[-2:]
|
||||
assert set(map("{:02d}".format, utcnow().timetuple()[:3])).issubset(path.parts)
|
||||
|
||||
|
||||
def test_symlinks_create(archive_tree: ArchiveTree, package_ahriman: Package, package_python_schedule: Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must create symlinks
|
||||
"""
|
||||
_original_exists = Path.exists
|
||||
def exists_mock(path: Path) -> bool:
|
||||
if path.name in (package.filename for package in package_python_schedule.packages.values()):
|
||||
return True
|
||||
return _original_exists(path)
|
||||
|
||||
symlinks_mock = mocker.patch("pathlib.Path.symlink_to")
|
||||
add_mock = mocker.patch("ahriman.core.alpm.repo.Repo.add")
|
||||
mocker.patch("pathlib.Path.glob", autospec=True, side_effect=lambda path, name: [path / name[:-1]])
|
||||
mocker.patch("pathlib.Path.exists", autospec=True, side_effect=exists_mock)
|
||||
|
||||
archive_tree.symlinks_create([package_ahriman, package_python_schedule])
|
||||
symlinks_mock.assert_called_once_with(
|
||||
Path("..") /
|
||||
".." /
|
||||
".." /
|
||||
".." /
|
||||
".." /
|
||||
".." /
|
||||
archive_tree.paths.archive_for(package_ahriman.base)
|
||||
.relative_to(archive_tree.paths.root)
|
||||
.relative_to("archive") /
|
||||
package_ahriman.packages[package_ahriman.base].filename
|
||||
)
|
||||
add_mock.assert_called_once_with(
|
||||
archive_tree.repository_for() / package_ahriman.packages[package_ahriman.base].filename
|
||||
)
|
||||
|
||||
|
||||
def test_symlinks_create_empty_filename(archive_tree: ArchiveTree, package_ahriman: Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must skip symlinks creation if filename is not set
|
||||
"""
|
||||
package_ahriman.packages[package_ahriman.base].filename = None
|
||||
symlinks_mock = mocker.patch("pathlib.Path.symlink_to")
|
||||
|
||||
archive_tree.symlinks_create([package_ahriman])
|
||||
symlinks_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_tree_create(archive_tree: ArchiveTree, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must create repository root if not exists
|
||||
"""
|
||||
owner_guard_mock = mocker.patch("ahriman.models.repository_paths.RepositoryPaths.preserve_owner")
|
||||
mkdir_mock = mocker.patch("pathlib.Path.mkdir")
|
||||
init_mock = mocker.patch("ahriman.core.alpm.repo.Repo.init")
|
||||
|
||||
archive_tree.tree_create()
|
||||
owner_guard_mock.assert_called_once_with(archive_tree.paths.archive)
|
||||
mkdir_mock.assert_called_once_with(0o755, parents=True)
|
||||
init_mock.assert_called_once_with()
|
||||
|
||||
|
||||
def test_tree_create_exists(archive_tree: ArchiveTree, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must skip directory creation if already exists
|
||||
"""
|
||||
mocker.patch("pathlib.Path.exists", return_value=True)
|
||||
mkdir_mock = mocker.patch("pathlib.Path.mkdir")
|
||||
|
||||
archive_tree.tree_create()
|
||||
mkdir_mock.assert_not_called()
|
32
tests/ahriman/core/archive/test_archive_trigger.py
Normal file
32
tests/ahriman/core/archive/test_archive_trigger.py
Normal file
@ -0,0 +1,32 @@
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.core.archive import ArchiveTrigger
|
||||
from ahriman.models.package import Package
|
||||
from ahriman.models.result import Result
|
||||
|
||||
|
||||
def test_on_result(archive_trigger: ArchiveTrigger, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must create symlinks for actual repository
|
||||
"""
|
||||
symlinks_mock = mocker.patch("ahriman.core.archive.archive_tree.ArchiveTree.symlinks_create")
|
||||
archive_trigger.on_result(Result(), [package_ahriman])
|
||||
symlinks_mock.assert_called_once_with([package_ahriman])
|
||||
|
||||
|
||||
def test_on_start(archive_trigger: ArchiveTrigger, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must create repository tree on load
|
||||
"""
|
||||
tree_mock = mocker.patch("ahriman.core.archive.archive_tree.ArchiveTree.tree_create")
|
||||
archive_trigger.on_start()
|
||||
tree_mock.assert_called_once_with()
|
||||
|
||||
|
||||
def test_on_stop(archive_trigger: ArchiveTrigger, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must create repository tree on load
|
||||
"""
|
||||
symlinks_mock = mocker.patch("ahriman.core.archive.archive_tree.ArchiveTree.symlinks_fix")
|
||||
archive_trigger.on_stop()
|
||||
symlinks_mock.assert_called_once_with()
|
@ -186,9 +186,7 @@ def test_package_update(executor: Executor, package_ahriman: Package, user: User
|
||||
Path("..") /
|
||||
".." /
|
||||
".." /
|
||||
executor.paths.archive_for(
|
||||
package_ahriman.base).relative_to(
|
||||
executor.paths.root) /
|
||||
executor.paths.archive_for(package_ahriman.base).relative_to(executor.paths.root) /
|
||||
filepath)
|
||||
# must add package
|
||||
repo_add_mock.assert_called_once_with(executor.paths.repository / filepath)
|
||||
|
Reference in New Issue
Block a user