toddlers/tests/test_runner.py
Nils Philippsen 8be9cead78 Fix import order, separation to appease flake8
Signed-off-by: Nils Philippsen <nils@redhat.com>
2020-07-02 12:51:48 +02:00

91 lines
3.1 KiB
Python

import logging
from unittest.mock import Mock, patch
import fedora_messaging.api
import fedora_messaging.exceptions
import pytest
import toddlers.runner
class TestRunningToddler:
def test___init__(self):
runner = toddlers.runner.RunningToddler()
assert sorted([t.name for t in runner.toddlers]) == [
"debug",
"flag_ci_pr",
"flag_commit_build",
"packager_bugzilla_sync",
]
@patch("toddlers.base.ToddlerBase")
def test___init__no_toddlers(self, mock_base, caplog):
mock_base.__subclasses__ = Mock(return_value=[])
caplog.set_level(logging.INFO)
with pytest.raises(
fedora_messaging.exceptions.ConfigurationException,
match=r".* No toddlers found to run .*",
):
runner = toddlers.runner.RunningToddler()
assert sorted([t.name for t in runner.toddlers]) == [
"debug",
"flag_ci_pr",
"flag_commit_build",
"packager_bugzilla_sync",
]
assert caplog.records[-1].message == "Loaded: []"
@patch.dict(
"toddlers.runner.fedora_messaging.config.conf",
{"consumer_config": {"blocked_toddlers": ["debug", "flag_ci_pr"]}},
)
def test___init__blockedlist(self, caplog):
runner = toddlers.runner.RunningToddler()
assert sorted([t.name for t in runner.toddlers]) == [
"flag_commit_build",
"packager_bugzilla_sync",
]
def test___call__(self, caplog):
caplog.set_level(logging.INFO)
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = "foo.bar"
msg.body = {
"owner": "username",
"instance": "secondary",
}
runner = toddlers.runner.RunningToddler()
runner.__call__(msg)
assert any(m.startswith("Topics of interest:") for m in caplog.messages)
assert "Toddlers processing: 123 -- foo.bar" in caplog.messages
assert (
caplog.records[-1].message
== "Toddler 'debug' accepted to process message id: 123"
)
@patch(
"toddlers.plugins.debug.DebugToddler.process",
new=Mock(side_effect=Exception("haha")),
)
def test___call__exception(self, caplog):
caplog.set_level(logging.INFO)
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = "foo.bar"
msg.body = {
"owner": "username",
"instance": "secondary",
}
runner = toddlers.runner.RunningToddler()
with pytest.raises(
fedora_messaging.exceptions.Nack, match=r"A toddler tripped"
):
runner.__call__(msg)
assert any(m.startswith("Topics of interest:") for m in caplog.messages)
assert "Toddlers processing: 123 -- foo.bar" in caplog.messages
assert "Toddler 'debug' accepted to process message id: 123" in caplog.messages
assert (
caplog.records[-1].message
== "Toddler 'debug' failed to process message id: 123 -- putting it back in the queue"
)