mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-29 13:49:57 +00:00
triggers implementation (#62)
This commit is contained in:
@ -24,7 +24,7 @@ def test_report_dummy(configuration: Configuration, result: Result, mocker: Mock
|
||||
"""
|
||||
mocker.patch("ahriman.models.report_settings.ReportSettings.from_option", return_value=ReportSettings.Disabled)
|
||||
report_mock = mocker.patch("ahriman.core.report.Report.generate")
|
||||
Report.load("x86_64", configuration, "disabled").run([], result)
|
||||
Report.load("x86_64", configuration, "disabled").run(result, [])
|
||||
report_mock.assert_called_once_with([], result)
|
||||
|
||||
|
||||
@ -33,7 +33,7 @@ def test_report_console(configuration: Configuration, result: Result, mocker: Mo
|
||||
must generate console report
|
||||
"""
|
||||
report_mock = mocker.patch("ahriman.core.report.Console.generate")
|
||||
Report.load("x86_64", configuration, "console").run([], result)
|
||||
Report.load("x86_64", configuration, "console").run(result, [])
|
||||
report_mock.assert_called_once_with([], result)
|
||||
|
||||
|
||||
@ -42,7 +42,7 @@ def test_report_email(configuration: Configuration, result: Result, mocker: Mock
|
||||
must generate email report
|
||||
"""
|
||||
report_mock = mocker.patch("ahriman.core.report.Email.generate")
|
||||
Report.load("x86_64", configuration, "email").run([], result)
|
||||
Report.load("x86_64", configuration, "email").run(result, [])
|
||||
report_mock.assert_called_once_with([], result)
|
||||
|
||||
|
||||
@ -51,7 +51,7 @@ def test_report_html(configuration: Configuration, result: Result, mocker: Mocke
|
||||
must generate html report
|
||||
"""
|
||||
report_mock = mocker.patch("ahriman.core.report.HTML.generate")
|
||||
Report.load("x86_64", configuration, "html").run([], result)
|
||||
Report.load("x86_64", configuration, "html").run(result, [])
|
||||
report_mock.assert_called_once_with([], result)
|
||||
|
||||
|
||||
@ -60,5 +60,5 @@ def test_report_telegram(configuration: Configuration, result: Result, mocker: M
|
||||
must generate telegram report
|
||||
"""
|
||||
report_mock = mocker.patch("ahriman.core.report.Telegram.generate")
|
||||
Report.load("x86_64", configuration, "telegram").run([], result)
|
||||
Report.load("x86_64", configuration, "telegram").run(result, [])
|
||||
report_mock.assert_called_once_with([], result)
|
||||
|
17
tests/ahriman/core/report/test_report_trigger.py
Normal file
17
tests/ahriman/core/report/test_report_trigger.py
Normal file
@ -0,0 +1,17 @@
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.report import ReportTrigger
|
||||
from ahriman.models.result import Result
|
||||
|
||||
|
||||
def test_run(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")
|
||||
|
||||
trigger = ReportTrigger("x86_64", configuration)
|
||||
trigger.run(Result(), [])
|
||||
run_mock.assert_called_once_with(Result(), [])
|
@ -3,12 +3,11 @@ import pytest
|
||||
from pathlib import Path
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest import mock
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from ahriman.core.report import Report
|
||||
from ahriman.core.repository.executor import Executor
|
||||
from ahriman.core.upload import Upload
|
||||
from ahriman.models.package import Package
|
||||
from ahriman.models.result import Result
|
||||
|
||||
|
||||
def test_load_archives(executor: Executor) -> None:
|
||||
@ -145,45 +144,15 @@ def test_process_remove_nothing(executor: Executor, package_ahriman: Package, pa
|
||||
repo_remove_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_process_report(executor: Executor, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
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])
|
||||
mocker.patch("ahriman.core.report.Report.load", return_value=Report("x86_64", executor.configuration))
|
||||
report_mock = mocker.patch("ahriman.core.report.Report.run")
|
||||
triggers_mock = mocker.patch("ahriman.core.triggers.TriggerLoader.process")
|
||||
|
||||
executor.process_report(["dummy"], [])
|
||||
report_mock.assert_called_once_with([package_ahriman], [])
|
||||
|
||||
|
||||
def test_process_report_auto(executor: Executor) -> None:
|
||||
"""
|
||||
must process report in auto mode if no targets supplied
|
||||
"""
|
||||
configuration_mock = executor.configuration = MagicMock()
|
||||
executor.process_report(None, [])
|
||||
configuration_mock.getlist.assert_called_once_with("report", "target")
|
||||
|
||||
|
||||
def test_process_upload(executor: Executor, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must process sync
|
||||
"""
|
||||
mocker.patch("ahriman.core.upload.Upload.load", return_value=Upload("x86_64", executor.configuration))
|
||||
upload_mock = mocker.patch("ahriman.core.upload.Upload.run")
|
||||
|
||||
executor.process_sync(["dummy"], [])
|
||||
upload_mock.assert_called_once_with(executor.paths.repository, [])
|
||||
|
||||
|
||||
def test_process_upload_auto(executor: Executor) -> None:
|
||||
"""
|
||||
must process sync in auto mode if no targets supplied
|
||||
"""
|
||||
configuration_mock = executor.configuration = MagicMock()
|
||||
executor.process_sync(None, [])
|
||||
configuration_mock.getlist.assert_called_once_with("upload", "target")
|
||||
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:
|
||||
|
31
tests/ahriman/core/triggers/conftest.py
Normal file
31
tests/ahriman/core/triggers/conftest.py
Normal file
@ -0,0 +1,31 @@
|
||||
import pytest
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.triggers import Trigger, TriggerLoader
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def trigger(configuration: Configuration) -> Trigger:
|
||||
"""
|
||||
fixture for trigger
|
||||
|
||||
Args:
|
||||
configuration(Configuration): configuration fixture
|
||||
|
||||
Returns:
|
||||
Trigger: trigger test instance
|
||||
"""
|
||||
return Trigger("x86_64", configuration)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def trigger_loader(configuration: Configuration) -> TriggerLoader:
|
||||
"""
|
||||
fixture for trigger loader
|
||||
Args:
|
||||
configuration(Configuration): configuration fixture
|
||||
|
||||
Returns:
|
||||
TriggerLoader: trigger loader test instance
|
||||
"""
|
||||
return TriggerLoader("x86_64", configuration)
|
12
tests/ahriman/core/triggers/test_trigger.py
Normal file
12
tests/ahriman/core/triggers/test_trigger.py
Normal file
@ -0,0 +1,12 @@
|
||||
import pytest
|
||||
|
||||
from ahriman.core.triggers import Trigger
|
||||
from ahriman.models.result import Result
|
||||
|
||||
|
||||
def test_run(trigger: Trigger) -> None:
|
||||
"""
|
||||
must raise NotImplemented for missing rum method
|
||||
"""
|
||||
with pytest.raises(NotImplementedError):
|
||||
trigger.run(Result(), [])
|
92
tests/ahriman/core/triggers/test_trigger_loader.py
Normal file
92
tests/ahriman/core/triggers/test_trigger_loader.py
Normal file
@ -0,0 +1,92 @@
|
||||
import pytest
|
||||
|
||||
from pathlib import Path
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.core.exceptions import InvalidExtension
|
||||
from ahriman.core.triggers import TriggerLoader
|
||||
from ahriman.models.package import Package
|
||||
from ahriman.models.result import Result
|
||||
|
||||
|
||||
def test_load_trigger_package(trigger_loader: TriggerLoader) -> None:
|
||||
"""
|
||||
must load trigger from package
|
||||
"""
|
||||
assert trigger_loader._load_trigger("ahriman.core.report.ReportTrigger")
|
||||
|
||||
|
||||
def test_load_trigger_package_invalid_import(trigger_loader: TriggerLoader, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must raise InvalidExtension on invalid import
|
||||
"""
|
||||
mocker.patch("ahriman.core.triggers.trigger_loader.importlib.import_module", side_effect=ModuleNotFoundError())
|
||||
with pytest.raises(InvalidExtension):
|
||||
trigger_loader._load_trigger("random.module")
|
||||
|
||||
|
||||
def test_load_trigger_package_not_trigger(trigger_loader: TriggerLoader) -> None:
|
||||
"""
|
||||
must raise InvalidExtension if imported module is not a type
|
||||
"""
|
||||
with pytest.raises(InvalidExtension):
|
||||
trigger_loader._load_trigger("ahriman.core.util.check_output")
|
||||
|
||||
|
||||
def test_load_trigger_package_error_on_creation(trigger_loader: TriggerLoader, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must raise InvalidException on trigger initialization if any exception is thrown
|
||||
"""
|
||||
mocker.patch("ahriman.core.triggers.trigger.Trigger.__init__", side_effect=Exception())
|
||||
with pytest.raises(InvalidExtension):
|
||||
trigger_loader._load_trigger("ahriman.core.report.ReportTrigger")
|
||||
|
||||
|
||||
def test_load_trigger_package_is_not_trigger(trigger_loader: TriggerLoader) -> None:
|
||||
"""
|
||||
must raise InvalidExtension if loaded class is not a trigger
|
||||
"""
|
||||
with pytest.raises(InvalidExtension):
|
||||
trigger_loader._load_trigger("ahriman.core.sign.gpg.GPG")
|
||||
|
||||
|
||||
def test_load_trigger_path(trigger_loader: TriggerLoader, resource_path_root: Path) -> None:
|
||||
"""
|
||||
must load trigger from path
|
||||
"""
|
||||
path = resource_path_root.parent.parent / "src" / "ahriman" / "core" / "report" / "report_trigger.py"
|
||||
assert trigger_loader._load_trigger(f"{path}.ReportTrigger")
|
||||
|
||||
|
||||
def test_load_trigger_path_not_found(trigger_loader: TriggerLoader) -> None:
|
||||
"""
|
||||
must raise InvalidExtension if file cannot be found
|
||||
"""
|
||||
with pytest.raises(InvalidExtension):
|
||||
trigger_loader._load_trigger("/some/random/path.py.SomeRandomModule")
|
||||
|
||||
|
||||
def test_process(trigger_loader: TriggerLoader, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must run triggers
|
||||
"""
|
||||
upload_mock = mocker.patch("ahriman.core.upload.UploadTrigger.run")
|
||||
report_mock = mocker.patch("ahriman.core.report.ReportTrigger.run")
|
||||
|
||||
trigger_loader.process(Result(), [package_ahriman])
|
||||
report_mock.assert_called_once_with(Result(), [package_ahriman])
|
||||
upload_mock.assert_called_once_with(Result(), [package_ahriman])
|
||||
|
||||
|
||||
def test_process_exception(trigger_loader: TriggerLoader, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must suppress exception during trigger run
|
||||
"""
|
||||
upload_mock = mocker.patch("ahriman.core.upload.UploadTrigger.run", side_effect=Exception())
|
||||
report_mock = mocker.patch("ahriman.core.report.ReportTrigger.run")
|
||||
log_mock = mocker.patch("logging.Logger.exception")
|
||||
|
||||
trigger_loader.process(Result(), [package_ahriman])
|
||||
report_mock.assert_called_once_with(Result(), [package_ahriman])
|
||||
upload_mock.assert_called_once_with(Result(), [package_ahriman])
|
||||
log_mock.assert_called_once()
|
17
tests/ahriman/core/upload/test_upload_trigger.py
Normal file
17
tests/ahriman/core/upload/test_upload_trigger.py
Normal file
@ -0,0 +1,17 @@
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.upload import UploadTrigger
|
||||
from ahriman.models.result import Result
|
||||
|
||||
|
||||
def test_run(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")
|
||||
|
||||
trigger = UploadTrigger("x86_64", configuration)
|
||||
trigger.run(Result(), [])
|
||||
run_mock.assert_called_once_with(configuration.repository_paths.repository, [])
|
Reference in New Issue
Block a user