Skip to content

Commit ed30e1b

Browse files
Remove hardcoded list of supported Fedora CI tests (#3039)
Remove hardcoded list of supported Fedora CI tests The Fedora CI comment parser no longer uses a hardcoded list supported test targets. Now it relies on the get_fedora_ci_tests to retrieve all available test targets in the given context. The implements_fedora_ci_test has been overhauled to accept a list of Checkers rather than a singular Callable, making the code easier to read and easy to add additional Checkers. Two new Checkers IsFMFConfigPresent and IsProjectOutsideOfTestsNamespace have been added to replace previously used methods. Fixes #3038 Reviewed-by: gemini-code-assist[bot] Reviewed-by: Nikola Forró Reviewed-by: Alžběta Kučerová
2 parents f87f04c + 09dd1f9 commit ed30e1b

8 files changed

Lines changed: 132 additions & 86 deletions

File tree

packit_service/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ def get_comment_parser_fedora_ci(
341341
prog: str | None = None,
342342
description: str | None = None,
343343
epilog: str | None = None,
344+
supported_test_types: list[str] | None = None,
344345
) -> argparse.ArgumentParser:
345346
parser = _create_base_parser(prog, description, epilog)
346347

@@ -357,7 +358,7 @@ def get_comment_parser_fedora_ci(
357358
test_parser.add_argument(
358359
"test_identifier",
359360
nargs="?",
360-
choices=["installability", "rpmlint", "rpminspect", "custom"],
361+
choices=supported_test_types,
361362
help="specific type of tests to run",
362363
)
363364

packit_service/worker/checker/testing_farm.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,31 @@ def pre_check(self) -> bool:
330330
return False
331331

332332
return True
333+
334+
335+
class IsFMFConfigPresent(_TestingFarmTestTypeChecker):
336+
"""
337+
Check whether FMF configuration is missing in the given project.
338+
"""
339+
340+
def pre_check(self) -> bool:
341+
try:
342+
commit_sha = self.data.event_dict.get("commit_sha")
343+
344+
self.project.get_file_content(
345+
path=".fmf/version",
346+
ref=commit_sha,
347+
)
348+
except FileNotFoundError:
349+
logger.debug("FMF configuration not found in repository.")
350+
return False
351+
return True
352+
353+
354+
class IsProjectOutsideOfTestsNamespace(_TestingFarmTestTypeChecker):
355+
"""
356+
Check whether the current project is located inside the tests namespace.
357+
"""
358+
359+
def pre_check(self) -> bool:
360+
return self.project.namespace != "tests"

packit_service/worker/handlers/testing_farm.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -430,20 +430,14 @@ def get_checkers() -> tuple[type[Checker], ...]:
430430
PermissionOnDistgitForFedoraCI,
431431
)
432432

433-
@classmethod
434-
def filter_ci_tests(cls, tests: list[str]) -> list[str]:
435-
return tests
436-
437433
@classmethod
438434
def get_all_check_names(
439435
cls, service_config: ServiceConfig, project: GitProject, metadata: EventData
440436
) -> list[str]:
441437
return [
442438
DownstreamTestingFarmJobHelper.get_check_name_from_config(t, service_config)
443-
for t in cls.filter_ci_tests(
444-
DownstreamTestingFarmJobHelper.get_fedora_ci_tests(
445-
service_config, project, metadata
446-
)
439+
for t in DownstreamTestingFarmJobHelper.get_fedora_ci_tests(
440+
service_config, project, metadata
447441
)
448442
]
449443

@@ -546,10 +540,8 @@ def run_for_fedora_ci_test(
546540
def _run(self) -> TaskResults:
547541
failed: dict[str, str] = {}
548542

549-
fedora_ci_tests = self.filter_ci_tests(
550-
self.downstream_testing_farm_job_helper.get_fedora_ci_tests(
551-
self.service_config, self.project, self.data
552-
)
543+
fedora_ci_tests = self.downstream_testing_farm_job_helper.get_fedora_ci_tests(
544+
self.service_config, self.project, self.data
553545
)
554546

555547
if not fedora_ci_tests:
@@ -615,10 +607,6 @@ def get_checkers() -> tuple[type[Checker], ...]:
615607
PermissionOnDistgitForFedoraCI,
616608
)
617609

618-
@classmethod
619-
def filter_ci_tests(cls, tests: list[str]) -> list[str]:
620-
return [t for t in tests if t == "custom"]
621-
622610

623611
@configured_as(job_type=JobType.tests)
624612
@reacts_to(event=testing_farm.Result)

packit_service/worker/helpers/testing_farm.py

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
get_packit_commands_from_comment,
4646
)
4747
from packit_service.worker.celery_task import CeleryTask
48+
from packit_service.worker.checker.abstract import Checker
49+
from packit_service.worker.checker.testing_farm import (
50+
IsFMFConfigPresent,
51+
IsProjectOutsideOfTestsNamespace,
52+
)
4853
from packit_service.worker.helpers.build import CoprBuildJobHelper
4954
from packit_service.worker.helpers.fedora_ci import FedoraCIHelper
5055
from packit_service.worker.helpers.testing_farm_client import TestingFarmClient
@@ -685,14 +690,9 @@ def is_fmf_configured(self) -> bool:
685690
if self.custom_fmf:
686691
return True
687692

688-
try:
689-
self.project.get_file_content(
690-
path=f"{self.fmf_path}/.fmf/version",
691-
ref=self.metadata.commit_sha,
692-
)
693-
return True
694-
except FileNotFoundError:
695-
return False
693+
return IsFMFConfigPresent(
694+
package_config=None, job_config=None, event=self.metadata.event_dict, task_name=None
695+
).pre_check()
696696

697697
def report_missing_build_chroot(self, chroot: str):
698698
self.report_status_to_tests_for_chroot(
@@ -1303,9 +1303,11 @@ def cancel_running_tests(self):
13031303
FEDORA_CI_TESTS = {}
13041304

13051305

1306-
def implements_fedora_ci_test(test_name: str, skipif: Optional[Callable] = None) -> Callable:
1306+
def implements_fedora_ci_test(
1307+
test_name: str, checkers: Optional[list[type[Checker]]] = None
1308+
) -> Callable:
13071309
def _update_mapping(function: Callable) -> Callable:
1308-
FEDORA_CI_TESTS[test_name] = (function, skipif)
1310+
FEDORA_CI_TESTS[test_name] = (function, checkers)
13091311
return function
13101312

13111313
return _update_mapping
@@ -1339,7 +1341,10 @@ def koji_helper(self):
13391341

13401342
@staticmethod
13411343
def get_fedora_ci_tests(
1342-
service_config: ServiceConfig, project: GitProject, metadata: EventData
1344+
service_config: ServiceConfig,
1345+
project: GitProject,
1346+
metadata: EventData,
1347+
filter_specific_tests: bool = True,
13431348
) -> list[str]:
13441349
"""
13451350
Gets relevant Fedora CI tests registered using the `@implements_fedora_ci_test()` decorator.
@@ -1351,31 +1356,47 @@ def get_fedora_ci_tests(
13511356
service_config: Service config.
13521357
project: Git project.
13531358
metadata: Event metadata.
1359+
filter_specific_tests: Whether to filter tests based on the command in user's comment.
13541360
13551361
Returns:
13561362
List of registered Fedora CI test names.
13571363
"""
1364+
1365+
def filter_tests(tests):
1366+
if metadata.event_type != pagure.pr.Comment.event_type():
1367+
return tests
1368+
# TODO: remove this once Fedora CI has its own instances and comment_command_prefixes
1369+
# comment_command_prefixes for Fedora CI are /packit-ci and /packit-ci-stg
1370+
comment_command_prefix = (
1371+
"/packit-ci-stg"
1372+
if service_config.comment_command_prefix.endswith("-stg")
1373+
else "/packit-ci"
1374+
)
1375+
commands = get_packit_commands_from_comment(
1376+
metadata.event_dict.get("comment"), comment_command_prefix
1377+
)
1378+
if not commands:
1379+
return []
1380+
if len(commands) > 1 and commands[1] in tests:
1381+
return [commands[1]]
1382+
return tests
1383+
13581384
all_tests = [
13591385
name
1360-
for name, (_, skipif) in FEDORA_CI_TESTS.items()
1361-
if not skipif or not skipif(service_config, project, metadata)
1386+
for name, (_, checkers) in FEDORA_CI_TESTS.items()
1387+
if all(
1388+
checker(
1389+
package_config=None,
1390+
job_config=None,
1391+
event=metadata.event_dict,
1392+
task_name=None,
1393+
).pre_check()
1394+
for checker in checkers
1395+
)
13621396
]
1363-
if metadata.event_type != pagure.pr.Comment.event_type():
1364-
return all_tests
1365-
# TODO: remove this once Fedora CI has its own instances and comment_command_prefixes
1366-
# comment_command_prefixes for Fedora CI are /packit-ci and /packit-ci-stg
1367-
comment_command_prefix = (
1368-
"/packit-ci-stg"
1369-
if service_config.comment_command_prefix.endswith("-stg")
1370-
else "/packit-ci"
1371-
)
1372-
commands = get_packit_commands_from_comment(
1373-
metadata.event_dict.get("comment"), comment_command_prefix
1374-
)
1375-
if not commands:
1376-
return []
1377-
if len(commands) > 1 and commands[1] in all_tests:
1378-
return [commands[1]]
1397+
1398+
if filter_specific_tests:
1399+
return filter_tests(all_tests)
13791400
return all_tests
13801401

13811402
@property
@@ -1495,7 +1516,10 @@ def prepare_and_send_tf_request(
14951516
response=response,
14961517
)
14971518

1498-
@implements_fedora_ci_test("installability")
1519+
@implements_fedora_ci_test(
1520+
"installability",
1521+
checkers=[IsProjectOutsideOfTestsNamespace],
1522+
)
14991523
def _payload_installability(self, distro: str, compose: str) -> dict:
15001524
git_repo = "https://github.com/fedora-ci/installability-pipeline.git"
15011525
git_ref = (
@@ -1542,7 +1566,10 @@ def _payload_installability(self, distro: str, compose: str) -> dict:
15421566
},
15431567
}
15441568

1545-
@implements_fedora_ci_test("rpminspect")
1569+
@implements_fedora_ci_test(
1570+
"rpminspect",
1571+
checkers=[IsProjectOutsideOfTestsNamespace],
1572+
)
15461573
def _payload_rpminspect(self, distro: str, compose: str) -> dict:
15471574
git_repo = "https://github.com/fedora-ci/rpminspect-pipeline.git"
15481575
git_ref = "master"
@@ -1557,7 +1584,10 @@ def _payload_rpminspect(self, distro: str, compose: str) -> dict:
15571584
}
15581585
return payload
15591586

1560-
@implements_fedora_ci_test("rpmlint")
1587+
@implements_fedora_ci_test(
1588+
"rpmlint",
1589+
checkers=[IsProjectOutsideOfTestsNamespace],
1590+
)
15611591
def _payload_rpmlint(self, distro: str, compose: str) -> dict:
15621592
git_repo = "https://github.com/packit/tmt-plans.git"
15631593
git_ref = "main"
@@ -1573,22 +1603,9 @@ def _payload_rpmlint(self, distro: str, compose: str) -> dict:
15731603
}
15741604
return payload
15751605

1576-
@staticmethod
1577-
def is_fmf_configured(project: GitProject, metadata: EventData) -> bool:
1578-
try:
1579-
project.get_file_content(
1580-
path=".fmf/version",
1581-
ref=metadata.commit_sha,
1582-
)
1583-
except FileNotFoundError:
1584-
return False
1585-
return True
1586-
15871606
@implements_fedora_ci_test(
15881607
"custom",
1589-
skipif=lambda _, project, metadata: not DownstreamTestingFarmJobHelper.is_fmf_configured(
1590-
project, metadata
1591-
),
1608+
checkers=[IsFMFConfigPresent],
15921609
)
15931610
def _payload_custom(self, distro: str, compose: str) -> dict:
15941611
payload = self._get_tf_base_payload(distro, compose)

packit_service/worker/helpers/testing_farm_mixin.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from __future__ import annotations
1010

1111
from abc import abstractmethod
12-
from typing import Protocol
12+
from typing import TYPE_CHECKING, Protocol
1313

1414
from packit.config import JobConfig, PackageConfig
1515

@@ -18,12 +18,14 @@
1818
GetCoprBuildMixin,
1919
GetKojiBuildFromTaskOrPullRequestMixin,
2020
)
21-
from packit_service.worker.helpers.testing_farm import (
22-
DownstreamTestingFarmJobHelper,
23-
TestingFarmJobHelper,
24-
)
2521
from packit_service.worker.mixin import ConfigFromEventMixin
2622

23+
if TYPE_CHECKING:
24+
from packit_service.worker.helpers.testing_farm import (
25+
DownstreamTestingFarmJobHelper,
26+
TestingFarmJobHelper,
27+
)
28+
2729

2830
class GetTestingFarmJobHelper(Protocol):
2931
package_config: PackageConfig
@@ -45,6 +47,8 @@ class GetTestingFarmJobHelperMixin(
4547
@property
4648
def testing_farm_job_helper(self) -> TestingFarmJobHelper:
4749
if not self._testing_farm_job_helper:
50+
from packit_service.worker.helpers.testing_farm import TestingFarmJobHelper
51+
4852
self._testing_farm_job_helper = TestingFarmJobHelper(
4953
service_config=self.service_config,
5054
package_config=self.package_config,
@@ -77,6 +81,10 @@ class GetDownstreamTestingFarmJobHelperMixin(
7781
@property
7882
def downstream_testing_farm_job_helper(self) -> DownstreamTestingFarmJobHelper:
7983
if not self._downstream_testing_farm_job_helper:
84+
from packit_service.worker.helpers.testing_farm import (
85+
DownstreamTestingFarmJobHelper,
86+
)
87+
8088
self._downstream_testing_farm_job_helper = DownstreamTestingFarmJobHelper(
8189
service_config=self.service_config,
8290
project=self.project,

packit_service/worker/jobs.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@
9191
from packit_service.worker.helpers.sync_release.propose_downstream import (
9292
ProposeDownstreamJobHelper,
9393
)
94-
from packit_service.worker.helpers.testing_farm import TestingFarmJobHelper
94+
from packit_service.worker.helpers.testing_farm import (
95+
DownstreamTestingFarmJobHelper,
96+
TestingFarmJobHelper,
97+
)
9598
from packit_service.worker.monitoring import Pushgateway
9699
from packit_service.worker.parser import Parser
97100
from packit_service.worker.reporting import BaseCommitStatus
@@ -266,7 +269,13 @@ def parse_comment(
266269
return ParsedComment()
267270

268271
if comment.startswith("/packit-ci"):
269-
parser = get_comment_parser_fedora_ci()
272+
supported_test_types = DownstreamTestingFarmJobHelper.get_fedora_ci_tests(
273+
self.service_config,
274+
self.event.project,
275+
EventData.from_event_dict(self.event.get_dict()),
276+
filter_specific_tests=False,
277+
)
278+
parser = get_comment_parser_fedora_ci(supported_test_types=supported_test_types)
270279
else:
271280
parser = get_comment_parser()
272281

tests/integration/test_pr_comment.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2649,6 +2649,9 @@ def test_downstream_koji_scratch_build_retrigger_via_dist_git_pr_comment(
26492649
.should_receive("get_web_url")
26502650
.and_return("https://src.fedoraproject.org/rpms/python-teamcity-messages")
26512651
.mock()
2652+
.should_receive("get_file_content")
2653+
.and_raise(FileNotFoundError)
2654+
.mock()
26522655
)
26532656
service_config = (
26542657
flexmock(
@@ -2740,6 +2743,10 @@ def test_downstream_koji_scratch_build_retrigger_via_dist_git_pr_comment(
27402743
(123, "koji-web-url")
27412744
).once()
27422745

2746+
flexmock(DownstreamTestingFarmJobHelper).should_receive("get_fedora_ci_tests").and_return(
2747+
["installability", "rpmlint", "rpminspect", "custom"]
2748+
)
2749+
27432750
processing_results = SteveJobs().process_message(pagure_pr_comment_added)
27442751
event_dict, _, job_config, package_config = get_parameters_from_results(
27452752
processing_results,

0 commit comments

Comments
 (0)