Add gitremote triggers (#68)

* add gitremote pull trigger

* add push gitremote trigger

* docs update
This commit is contained in:
2022-10-18 01:46:27 +03:00
committed by GitHub
parent fc0d8387df
commit 342b3cb652
60 changed files with 722 additions and 130 deletions

View File

@ -223,6 +223,23 @@ def test_patch_create_with_newline(mocker: MockerFixture) -> None:
assert Sources.patch_create(Path("local"), "glob").endswith("\n")
def test_push(package_ahriman: Package, mocker: MockerFixture) -> None:
"""
must correctly push files to remote repository
"""
add_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.add")
commit_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.commit")
check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output")
local = Path("local")
Sources.push(Path("local"), package_ahriman.remote, "glob")
add_mock.assert_called_once_with(local, "glob")
commit_mock.assert_called_once_with(local)
check_output_mock.assert_called_once_with(
"git", "push", package_ahriman.remote.git_url, package_ahriman.remote.branch,
exception=None, cwd=local, logger=pytest.helpers.anyvar(int))
def test_add(sources: Sources, mocker: MockerFixture) -> None:
"""
must add files to git
@ -249,6 +266,35 @@ def test_add_skip(sources: Sources, mocker: MockerFixture) -> None:
check_output_mock.assert_not_called()
def test_commit(sources: Sources, mocker: MockerFixture) -> None:
"""
must commit changes
"""
check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output")
local = Path("local")
commit_message = "Commit message"
sources.commit(local, commit_message=commit_message)
check_output_mock.assert_called_once_with(
"git", "commit", "--all", "--message", commit_message,
exception=None, cwd=local, logger=pytest.helpers.anyvar(int)
)
def test_commit_autogenerated(sources: Sources, mocker: MockerFixture) -> None:
"""
must commit changes with autogenerated commit message
"""
check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output")
local = Path("local")
sources.commit(Path("local"))
check_output_mock.assert_called_once_with(
"git", "commit", "--all", "--message", pytest.helpers.anyvar(str, strict=True),
exception=None, cwd=local, logger=pytest.helpers.anyvar(int)
)
def test_diff(sources: Sources, mocker: MockerFixture) -> None:
"""
must calculate diff

View File

@ -0,0 +1,58 @@
import pytest
from pathlib import Path
from pytest_mock import MockerFixture
from unittest import mock
from ahriman.core.configuration import Configuration
from ahriman.core.gitremote import RemotePullTrigger
def test_on_start(configuration: Configuration, mocker: MockerFixture) -> None:
"""
must clone repo on start
"""
clone_mock = mocker.patch("ahriman.core.gitremote.RemotePullTrigger.repo_clone")
trigger = RemotePullTrigger("x86_64", configuration)
trigger.on_start()
clone_mock.assert_called_once_with()
def test_repo_clone(configuration: Configuration, mocker: MockerFixture) -> None:
"""
must clone repository locally and copy its content
"""
fetch_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.fetch")
copy_mock = mocker.patch("ahriman.core.gitremote.RemotePullTrigger.repo_copy")
trigger = RemotePullTrigger("x86_64", configuration)
trigger.repo_clone()
fetch_mock.assert_called_once_with(pytest.helpers.anyvar(int), trigger.remote_source)
copy_mock.assert_called_once_with(pytest.helpers.anyvar(int))
def test_repo_copy(configuration: Configuration, mocker: MockerFixture) -> None:
"""
must copy repository tree from temporary directory to the local cache
"""
mocker.patch("ahriman.core.gitremote.remote_pull_trigger.walk", return_value=[
Path("local") / "package1" / "PKGBUILD",
Path("local") / "package1" / ".SRCINFO",
Path("local") / "package2" / ".SRCINFO",
Path("local") / "package3" / "PKGBUILD",
Path("local") / "package3" / ".SRCINFO",
])
copytree_mock = mocker.patch("shutil.copytree")
init_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.init")
trigger = RemotePullTrigger("x86_64", configuration)
trigger.repo_copy(Path("local"))
copytree_mock.assert_has_calls([
mock.call(Path("local") / "package1", configuration.repository_paths.cache_for("package1"), dirs_exist_ok=True),
mock.call(Path("local") / "package3", configuration.repository_paths.cache_for("package3"), dirs_exist_ok=True),
])
init_mock.assert_has_calls([
mock.call(configuration.repository_paths.cache_for("package1")),
mock.call(configuration.repository_paths.cache_for("package3")),
])

View File

@ -0,0 +1,56 @@
import pytest
from pathlib import Path
from pytest_mock import MockerFixture
from unittest import mock
from ahriman.core.configuration import Configuration
from ahriman.core.gitremote import RemotePushTrigger
from ahriman.models.package import Package
from ahriman.models.result import Result
def test_package_update(package_ahriman: Package, mocker: MockerFixture) -> None:
"""
must update single package
"""
rmtree_mock = mocker.patch("shutil.rmtree")
fetch_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.fetch")
copytree_mock = mocker.patch("shutil.copytree")
local = Path("local")
RemotePushTrigger.package_update(package_ahriman, local)
rmtree_mock.assert_has_calls([
mock.call(local / package_ahriman.base, ignore_errors=True),
mock.call(pytest.helpers.anyvar(int), onerror=pytest.helpers.anyvar(int)), # removal of the TemporaryDirectory
mock.call(local / package_ahriman.base / ".git", ignore_errors=True),
])
fetch_mock.assert_called_once_with(pytest.helpers.anyvar(int), package_ahriman.remote)
copytree_mock.assert_called_once_with(pytest.helpers.anyvar(int), local / package_ahriman.base)
def test_packages_update(result: Result, package_ahriman: Package, mocker: MockerFixture) -> None:
"""
must generate packages update
"""
update_mock = mocker.patch("ahriman.core.gitremote.RemotePushTrigger.package_update",
return_value=[package_ahriman.base])
local = Path("local")
assert list(RemotePushTrigger.packages_update(result, local))
update_mock.assert_called_once_with(package_ahriman, local)
def test_on_result(configuration: Configuration, result: Result, package_ahriman: Package,
mocker: MockerFixture) -> None:
"""
must push changes on result
"""
mocker.patch("ahriman.core.gitremote.RemotePushTrigger.packages_update", return_value=[package_ahriman.base])
fetch_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.fetch")
push_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.push")
trigger = RemotePushTrigger("x86_64", configuration)
trigger.on_result(result, [package_ahriman])
fetch_mock.assert_called_once_with(pytest.helpers.anyvar(int), trigger.remote_source)
push_mock.assert_called_once_with(pytest.helpers.anyvar(int), trigger.remote_source, package_ahriman.base)

View File

@ -4,7 +4,7 @@ from pytest_mock import MockerFixture
from ahriman.core.configuration import Configuration
from ahriman.core.exceptions import ReportFailed
from ahriman.core.report import Report
from ahriman.core.report.report import Report
from ahriman.models.report_settings import ReportSettings
from ahriman.models.result import Result
@ -23,7 +23,7 @@ def test_report_dummy(configuration: Configuration, result: Result, mocker: Mock
must construct dummy report class
"""
mocker.patch("ahriman.models.report_settings.ReportSettings.from_option", return_value=ReportSettings.Disabled)
report_mock = mocker.patch("ahriman.core.report.Report.generate")
report_mock = mocker.patch("ahriman.core.report.report.Report.generate")
Report.load("x86_64", configuration, "disabled").run(result, [])
report_mock.assert_called_once_with([], result)

View File

@ -10,7 +10,7 @@ def test_on_result(configuration: Configuration, mocker: MockerFixture) -> None:
must run report for specified targets
"""
configuration.set_option("report", "target", "email")
run_mock = mocker.patch("ahriman.core.report.Report.run")
run_mock = mocker.patch("ahriman.core.report.report.Report.run")
trigger = ReportTrigger("x86_64", configuration)
trigger.on_result(Result(), [])

View File

@ -4,10 +4,8 @@ from pathlib import Path
from pytest_mock import MockerFixture
from unittest import mock
from ahriman.core.report import Report
from ahriman.core.repository.executor import Executor
from ahriman.models.package import Package
from ahriman.models.result import Result
def test_load_archives(executor: Executor) -> None:
@ -144,17 +142,6 @@ def test_process_remove_nothing(executor: Executor, package_ahriman: Package, pa
repo_remove_mock.assert_not_called()
def test_process_triggers(executor: Executor, package_ahriman: Package, result: Result, mocker: MockerFixture) -> None:
"""
must process report
"""
mocker.patch("ahriman.core.repository.executor.Executor.packages", return_value=[package_ahriman])
triggers_mock = mocker.patch("ahriman.core.triggers.TriggerLoader.on_result")
executor.process_triggers(result)
triggers_mock.assert_called_once_with(result, [package_ahriman])
def test_process_update(executor: Executor, package_ahriman: Package, mocker: MockerFixture) -> None:
"""
must run update process

View File

@ -10,19 +10,6 @@ from ahriman.models.package import Package
from ahriman.models.result import Result
def test_init_at_exit(configuration: Configuration, mocker: MockerFixture) -> None:
"""
must call on_start on init and on_stop on exit
"""
on_start_mock = mocker.patch("ahriman.core.triggers.trigger_loader.TriggerLoader.on_start")
on_stop_mock = mocker.patch("ahriman.core.triggers.trigger_loader.TriggerLoader.on_stop")
trigger_loader = TriggerLoader("x86_64", configuration)
on_start_mock.assert_called_once_with()
del trigger_loader
on_stop_mock.assert_called_once_with()
def test_load_trigger_package(trigger_loader: TriggerLoader) -> None:
"""
must load trigger from package
@ -123,10 +110,34 @@ def test_on_start(trigger_loader: TriggerLoader, package_ahriman: Package, mocke
report_mock = mocker.patch("ahriman.core.report.ReportTrigger.on_start")
trigger_loader.on_start()
assert trigger_loader._on_stop_requested
report_mock.assert_called_once_with()
upload_mock.assert_called_once_with()
def test_on_stop_with_on_start(configuration: Configuration, mocker: MockerFixture) -> None:
"""
must call on_stop on exit if on_start was called
"""
on_stop_mock = mocker.patch("ahriman.core.triggers.trigger_loader.TriggerLoader.on_stop")
trigger_loader = TriggerLoader("x86_64", configuration)
trigger_loader.on_start()
del trigger_loader
on_stop_mock.assert_called_once_with()
def test_on_stop_without_on_start(configuration: Configuration, mocker: MockerFixture) -> None:
"""
must call not on_stop on exit if on_start wasn't called
"""
on_stop_mock = mocker.patch("ahriman.core.triggers.trigger_loader.TriggerLoader.on_stop")
trigger_loader = TriggerLoader("x86_64", configuration)
del trigger_loader
on_stop_mock.assert_not_called()
def test_on_stop(trigger_loader: TriggerLoader, package_ahriman: Package, mocker: MockerFixture) -> None:
"""
must run triggers on stop

View File

@ -5,7 +5,7 @@ from pytest_mock import MockerFixture
from ahriman.core.configuration import Configuration
from ahriman.core.exceptions import SyncFailed
from ahriman.core.upload import Upload
from ahriman.core.upload.upload import Upload
from ahriman.models.upload_settings import UploadSettings
@ -23,7 +23,7 @@ def test_report_dummy(configuration: Configuration, mocker: MockerFixture) -> No
must construct dummy upload class
"""
mocker.patch("ahriman.models.upload_settings.UploadSettings.from_option", return_value=UploadSettings.Disabled)
upload_mock = mocker.patch("ahriman.core.upload.Upload.sync")
upload_mock = mocker.patch("ahriman.core.upload.upload.Upload.sync")
Upload.load("x86_64", configuration, "disabled").run(Path("path"), [])
upload_mock.assert_called_once_with(Path("path"), [])

View File

@ -10,7 +10,7 @@ def test_on_result(configuration: Configuration, mocker: MockerFixture) -> None:
must run report for specified targets
"""
configuration.set_option("upload", "target", "rsync")
run_mock = mocker.patch("ahriman.core.upload.Upload.run")
run_mock = mocker.patch("ahriman.core.upload.upload.Upload.run")
trigger = UploadTrigger("x86_64", configuration)
trigger.on_result(Result(), [])