toddlers/tests/plugins/test_flag_ci_pr.py
Michal Konečný d9c03e08ce Fix formatting for for black > 23
Signed-off-by: Michal Konečný <mkonecny@redhat.com>
2023-02-03 14:33:39 +01:00

246 lines
8.9 KiB
Python

import logging
from unittest.mock import MagicMock
import fedora_messaging.api
import pytest
from toddlers.plugins.flag_ci_pr import FlagCIPR
class TestFlagCIPRToddler:
toddler_cls = FlagCIPR
def test_accepts_topic_invalid(self, toddler):
assert toddler.accepts_topic("foo.bar") is False
@pytest.mark.parametrize(
"topic",
[
"org.centos.#.ci.dist-git-pr.test.error",
"org.centos.prod.ci.dist-git-pr.test.error",
"org.centos.stage.ci.dist-git-pr.test.error",
"org.centos.#.ci.dist-git-pr.test.complete"
"org.centos.prod.ci.dist-git-pr.test.complete"
"org.centos.stg.ci.dist-git-pr.test.complete"
"org.centos.#.ci.dist-git-pr.test.running",
"org.centos.prod.ci.dist-git-pr.test.running",
"org.centos.stg.ci.dist-git-pr.test.running",
],
)
def test_accepts_topic_valid(self, toddler, topic):
assert toddler.accepts_topic(topic)
def test_process_invalid(self, toddler):
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = "toddlers.test.topic"
assert toddler.process(config={}, message=msg) is None
def test_process_invalid_status(self, toddler, caplog):
caplog.set_level(logging.INFO)
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = "org.centos.stg.ci.dist-git-pr.test.invalid"
assert toddler.process(config={}, message=msg) is None
assert (
caplog.records[-1].message
== "Pipeline state is not 'complete' or 'running' or 'error'."
)
def test_process_no_version(self, toddler, caplog):
caplog.set_level(logging.INFO)
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = "org.centos.stg.ci.dist-git-pr.test.complete"
assert toddler.process(config={}, message=msg) is None
assert caplog.records[-1].message == "Unsupported msg version, ignoring"
def test_process_invalid_complete_status(self, toddler, caplog):
caplog.set_level(logging.INFO)
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = "org.centos.stg.ci.dist-git-pr.test.complete"
msg.body = {
"version": "0.2.1",
"test": {"category": "functional", "type": "tier0", "result": "invalid"},
}
assert toddler.process(config={}, message=msg) is None
assert (
caplog.records[-1].message
== "Build is not in one of the expected status, ignoring"
)
def test_process_no_artifact(self, toddler, caplog):
caplog.set_level(logging.INFO)
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = "org.centos.stg.ci.dist-git-pr.test.complete"
msg.body = {
"version": "0.2.1",
"test": {"category": "functional", "type": "tier0", "result": "passed"},
"artifact": {"commit_hash": "abcdefghijklmnopqrst"},
}
assert toddler.process(config={}, message=msg) is None
assert (
caplog.records[-1].message
== "Invalid message: {'commit_hash': 'abcdefghijklmnopqrst'}, could not extract "
"the PR id from it"
)
def test_process_request_failed(self, toddler, caplog):
toddler.requests_session.request.return_value = MagicMock(
ok=False, status_code=401, text="invalid"
)
caplog.set_level(logging.INFO)
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = "org.centos.stg.ci.dist-git-pr.test.running"
msg.body = {
"version": "0.2.1",
"test": {"category": "functional", "type": "tier0", "result": "passed"},
"artifact": {
"id": 456,
"commit_hash": "abcdefghijklmnopqrst",
"repository": "namespace/name",
},
"run": {"url": "https://example.com/testing"},
}
config = {
"dist_git_token_seed": "example_seed",
"dist_git_url": "https://pagure.io",
"dist_git_token": "ahah",
}
assert toddler.process(config=config, message=msg) == 1
assert (
caplog.records[-1].message == "Request to https://pagure.io returned: 401"
)
def test_process_msg_no_category(self, toddler, caplog):
toddler.requests_session.request.return_value = MagicMock(
ok=True, status_code=200, text="invalid"
)
caplog.set_level(logging.INFO)
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = "org.centos.stg.ci.dist-git-pr.test.complete"
msg.body = {
"version": "0.2.1",
"test": {"type": "tier0", "result": "passed"},
"artifact": {
"id": 456,
"commit_hash": "abcdefghijklmnopqrst",
"repository": "https://host.c/namespace/name",
},
"run": {"url": "https://example.com/testing"},
}
config = {
"dist_git_token_seed": "example_seed",
"dist_git_url": "https://pagure.io",
"dist_git_token": "ahah",
}
assert toddler.process(config=config, message=msg) is None
assert (
caplog.records[-1].message
== "Invalid message send, no `category` in the `test` section, ignoring - id: 123"
)
def test_process_msg_no_type(self, toddler, caplog):
toddler.requests_session.request.return_value = MagicMock(
ok=True, status_code=200, text="invalid"
)
caplog.set_level(logging.INFO)
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = "org.centos.stg.ci.dist-git-pr.test.complete"
msg.body = {
"version": "0.2.1",
"test": {"category": "functional", "result": "passed"},
"artifact": {
"id": 456,
"commit_hash": "abcdefghijklmnopqrst",
"repository": "https://host.c/namespace/name",
},
"run": {"url": "https://example.com/testing"},
}
config = {
"dist_git_token_seed": "example_seed",
"dist_git_url": "https://pagure.io",
"dist_git_token": "ahah",
}
assert toddler.process(config=config, message=msg) is None
assert (
caplog.records[-1].message
== "Invalid message send, no `type` in the `test` section, ignoring - id: 123"
)
def test_process_msg_no_result(self, toddler, caplog):
toddler.requests_session.request.return_value = MagicMock(
ok=True, status_code=200, text="invalid"
)
caplog.set_level(logging.INFO)
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = "org.centos.stg.ci.dist-git-pr.test.complete"
msg.body = {
"version": "0.2.1",
"test": {"category": "functional", "type": "tier0"},
"artifact": {
"id": 456,
"commit_hash": "abcdefghijklmnopqrst",
"repository": "https://host.c/namespace/name",
},
"run": {"url": "https://example.com/testing"},
}
config = {
"dist_git_token_seed": "example_seed",
"dist_git_url": "https://pagure.io",
"dist_git_token": "ahah",
}
assert toddler.process(config=config, message=msg) is None
assert (
caplog.records[-1].message
== "Invalid message send, no `result` in the `test` section, ignoring - id: 123"
)
def test_process_valid(self, toddler, caplog):
toddler.requests_session.request.return_value = MagicMock(
ok=True, status_code=200, text="invalid"
)
caplog.set_level(logging.INFO)
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = "org.centos.stg.ci.dist-git-pr.test.error"
msg.body = {
"version": "0.2.1",
"test": {"category": "functional", "type": "tier0", "result": "passed"},
"artifact": {
"id": 456,
"commit_hash": "abcdefghijklmnopqrst",
"repository": "https://host.c/namespace/name",
},
"run": {"url": "https://example.com/testing"},
}
config = {
"dist_git_token_seed": "example_seed",
"dist_git_url": "https://pagure.io",
"dist_git_token": "ahah",
}
assert toddler.process(config=config, message=msg) is None
assert (
caplog.records[-3].message == "Request to https://pagure.io returned: 200"
)
assert caplog.records[-2].message == "All clear"
assert (
caplog.records[-1].message
== "User-URL: https://host.c/namespace/name/pull-request/456"
)