Using real objects is considered polite
Previously, ToddlerBase and derived classes were used directly, but none of the methods is marked as a class or static method. Amazingly enough, it worked regardless. It's useful to have a constructor, though, so do things conventionally. As we can't just monkey-patch the classes then, use the shared `toddler` fixture which creates the object for the test methods that need it. Signed-off-by: Nils Philippsen <nils@redhat.com>
This commit is contained in:
parent
3f081a66ac
commit
7fe0937190
14 changed files with 167 additions and 225 deletions
|
@ -4,15 +4,15 @@ from unittest.mock import MagicMock, Mock, patch
|
|||
import fedora_messaging.api
|
||||
import pytest
|
||||
|
||||
import toddlers.plugins.flag_commit_build
|
||||
from toddlers.plugins.flag_commit_build import FlagCommitBuild
|
||||
|
||||
|
||||
class TestFlagCommitBuildToddler:
|
||||
def test_accepts_topic_invalid(self):
|
||||
assert (
|
||||
toddlers.plugins.flag_commit_build.FlagCommitBuild.accepts_topic("foo.bar")
|
||||
is False
|
||||
)
|
||||
|
||||
toddler_cls = FlagCommitBuild
|
||||
|
||||
def test_accepts_topic_invalid(self, toddler):
|
||||
assert toddler.accepts_topic("foo.bar") is False
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"topic",
|
||||
|
@ -22,38 +22,28 @@ class TestFlagCommitBuildToddler:
|
|||
"org.fedoraproject.prod.buildsys.build.state.change",
|
||||
],
|
||||
)
|
||||
def test_accepts_topic_valid(self, topic):
|
||||
assert toddlers.plugins.flag_commit_build.FlagCommitBuild.accepts_topic(topic)
|
||||
def test_accepts_topic_valid(self, toddler, topic):
|
||||
assert toddler.accepts_topic(topic)
|
||||
|
||||
def test_process_containerbuild(self, caplog):
|
||||
def test_process_containerbuild(self, toddler, caplog):
|
||||
caplog.set_level(logging.INFO)
|
||||
msg = fedora_messaging.api.Message()
|
||||
msg.id = 123
|
||||
msg.topic = "org.fedoraproject.prod.buildsys.build.state.change"
|
||||
msg.body = {"owner": "containerbuild"}
|
||||
assert (
|
||||
toddlers.plugins.flag_commit_build.FlagCommitBuild.process(
|
||||
config={}, message=msg
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert toddler.process(config={}, message=msg) is None
|
||||
assert caplog.records[-1].message == "Skipping container build"
|
||||
|
||||
def test_process_mbs_build(self, caplog):
|
||||
def test_process_mbs_build(self, toddler, caplog):
|
||||
caplog.set_level(logging.INFO)
|
||||
msg = fedora_messaging.api.Message()
|
||||
msg.id = 123
|
||||
msg.topic = "org.fedoraproject.prod.buildsys.build.state.change"
|
||||
msg.body = {"owner": "mbs/mbs.fedoraproject.org"}
|
||||
assert (
|
||||
toddlers.plugins.flag_commit_build.FlagCommitBuild.process(
|
||||
config={}, message=msg
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert toddler.process(config={}, message=msg) is None
|
||||
assert caplog.records[-1].message == "Skipping MBS builds"
|
||||
|
||||
def test_process_secondary_instance(self, caplog):
|
||||
def test_process_secondary_instance(self, toddler, caplog):
|
||||
caplog.set_level(logging.INFO)
|
||||
msg = fedora_messaging.api.Message()
|
||||
msg.id = 123
|
||||
|
@ -62,15 +52,10 @@ class TestFlagCommitBuildToddler:
|
|||
"owner": "username",
|
||||
"instance": "secondary",
|
||||
}
|
||||
assert (
|
||||
toddlers.plugins.flag_commit_build.FlagCommitBuild.process(
|
||||
config={}, message=msg
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert toddler.process(config={}, message=msg) is None
|
||||
assert caplog.records[-1].message == "Ignoring secondary arch task..."
|
||||
|
||||
def test_process_uninteresting_status(self, caplog):
|
||||
def test_process_uninteresting_status(self, toddler, caplog):
|
||||
caplog.set_level(logging.INFO)
|
||||
msg = fedora_messaging.api.Message()
|
||||
msg.id = 123
|
||||
|
@ -80,19 +65,14 @@ class TestFlagCommitBuildToddler:
|
|||
"instance": "primary",
|
||||
"new": 2,
|
||||
}
|
||||
assert (
|
||||
toddlers.plugins.flag_commit_build.FlagCommitBuild.process(
|
||||
config={}, message=msg
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert toddler.process(config={}, message=msg) is None
|
||||
assert (
|
||||
caplog.records[-1].message
|
||||
== "Build is not in a state we care about, skipping"
|
||||
)
|
||||
|
||||
@patch("toddlers.plugins.flag_commit_build.koji")
|
||||
def test_process_no_git_url(self, mock_koji, caplog):
|
||||
def test_process_no_git_url(self, mock_koji, toddler, caplog):
|
||||
client = Mock()
|
||||
client.getBuild = Mock(return_value={})
|
||||
mock_koji.ClientSession = Mock(return_value=client)
|
||||
|
@ -107,19 +87,14 @@ class TestFlagCommitBuildToddler:
|
|||
"build_id": 42,
|
||||
}
|
||||
config = {"koji_url": "https://koji.fedoraproject.org"}
|
||||
assert (
|
||||
toddlers.plugins.flag_commit_build.FlagCommitBuild.process(
|
||||
config=config, message=msg
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert toddler.process(config=config, message=msg) is None
|
||||
assert (
|
||||
caplog.records[-1].message
|
||||
== "No git url found in the extra information: None"
|
||||
)
|
||||
|
||||
@patch("toddlers.plugins.flag_commit_build.koji")
|
||||
def test_process_invalid_git_url(self, mock_koji, caplog):
|
||||
def test_process_invalid_git_url(self, mock_koji, toddler, caplog):
|
||||
client = Mock()
|
||||
client.getBuild = Mock(
|
||||
return_value={"extra": {"source": {"original_url": "foobar"}}}
|
||||
|
@ -136,17 +111,12 @@ class TestFlagCommitBuildToddler:
|
|||
"build_id": 42,
|
||||
}
|
||||
config = {"koji_url": "https://koji.fedoraproject.org"}
|
||||
assert (
|
||||
toddlers.plugins.flag_commit_build.FlagCommitBuild.process(
|
||||
config=config, message=msg
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert toddler.process(config=config, message=msg) is None
|
||||
assert caplog.records[-1].message == "No # in the git_url: foobar"
|
||||
|
||||
@patch("toddlers.plugins.flag_commit_build.requests_session")
|
||||
@patch("toddlers.plugins.flag_commit_build.koji")
|
||||
def test_process_flag_no_task_id(self, mock_koji, mock_requests, caplog):
|
||||
def test_process_flag_no_task_id(self, mock_koji, mock_requests, toddler, caplog):
|
||||
mock_requests.request.return_value = MagicMock(
|
||||
ok=False, status_code=401, text="invalid"
|
||||
)
|
||||
|
@ -182,12 +152,7 @@ class TestFlagCommitBuildToddler:
|
|||
"pagure_url": "https://src.fedoraproject.org",
|
||||
"pagure_token": "ahah",
|
||||
}
|
||||
assert (
|
||||
toddlers.plugins.flag_commit_build.FlagCommitBuild.process(
|
||||
config=config, message=msg
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert toddler.process(config=config, message=msg) is None
|
||||
assert (
|
||||
caplog.records[-1].message
|
||||
== "Request to https://src.fedoraproject.org returned: 401"
|
||||
|
@ -195,7 +160,7 @@ class TestFlagCommitBuildToddler:
|
|||
|
||||
@patch("toddlers.plugins.flag_commit_build.requests_session")
|
||||
@patch("toddlers.plugins.flag_commit_build.koji")
|
||||
def test_process_flag_failed(self, mock_koji, mock_requests, caplog):
|
||||
def test_process_flag_failed(self, mock_koji, mock_requests, toddler, caplog):
|
||||
mock_requests.request.return_value = MagicMock(
|
||||
ok=False, status_code=401, text="invalid"
|
||||
)
|
||||
|
@ -231,12 +196,7 @@ class TestFlagCommitBuildToddler:
|
|||
"pagure_url": "https://src.fedoraproject.org",
|
||||
"pagure_token": "ahah",
|
||||
}
|
||||
assert (
|
||||
toddlers.plugins.flag_commit_build.FlagCommitBuild.process(
|
||||
config=config, message=msg
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert toddler.process(config=config, message=msg) is None
|
||||
assert (
|
||||
caplog.records[-1].message
|
||||
== "Request to https://src.fedoraproject.org returned: 401"
|
||||
|
@ -244,7 +204,9 @@ class TestFlagCommitBuildToddler:
|
|||
|
||||
@patch("toddlers.plugins.flag_commit_build.requests_session")
|
||||
@patch("toddlers.plugins.flag_commit_build.koji")
|
||||
def test_process_flag_failed_cancel(self, mock_koji, mock_requests, caplog):
|
||||
def test_process_flag_failed_cancel(
|
||||
self, mock_koji, mock_requests, toddler, caplog
|
||||
):
|
||||
mock_requests.request.return_value = MagicMock(
|
||||
ok=False, status_code=401, text="invalid"
|
||||
)
|
||||
|
@ -280,12 +242,7 @@ class TestFlagCommitBuildToddler:
|
|||
"pagure_url": "https://src.fedoraproject.org",
|
||||
"pagure_token": "ahah",
|
||||
}
|
||||
assert (
|
||||
toddlers.plugins.flag_commit_build.FlagCommitBuild.process(
|
||||
config=config, message=msg
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert toddler.process(config=config, message=msg) is None
|
||||
assert (
|
||||
caplog.records[-1].message
|
||||
== "Request to https://src.fedoraproject.org returned: 401"
|
||||
|
@ -293,7 +250,7 @@ class TestFlagCommitBuildToddler:
|
|||
|
||||
@patch("toddlers.plugins.flag_commit_build.requests_session")
|
||||
@patch("toddlers.plugins.flag_commit_build.koji")
|
||||
def test_process_flag_pass(self, mock_koji, mock_requests, caplog):
|
||||
def test_process_flag_pass(self, mock_koji, mock_requests, toddler, caplog):
|
||||
mock_requests.request.return_value = MagicMock(
|
||||
ok=True, status_code=200, text="woohoo!"
|
||||
)
|
||||
|
@ -328,12 +285,7 @@ class TestFlagCommitBuildToddler:
|
|||
"pagure_url": "https://src.fedoraproject.org",
|
||||
"pagure_token": "ahah",
|
||||
}
|
||||
assert (
|
||||
toddlers.plugins.flag_commit_build.FlagCommitBuild.process(
|
||||
config=config, message=msg
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert toddler.process(config=config, message=msg) is None
|
||||
assert (
|
||||
caplog.records[-3].message
|
||||
== "Request to https://src.fedoraproject.org returned: 200"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue