From a5471e107fc582ead561d513dbf41b8e0646655a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Mu=C5=BEila?= Date: Mon, 15 Mar 2021 15:43:46 +0100 Subject: [PATCH] Allow to reference a test repo in test job metadata This change allows to specify distinct repository for tests. --- packit_service/worker/testing_farm.py | 30 +++++++-- tests/unit/test_testing_farm.py | 94 +++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 7 deletions(-) diff --git a/packit_service/worker/testing_farm.py b/packit_service/worker/testing_farm.py index 5c28134fe..d5c0dbcf3 100644 --- a/packit_service/worker/testing_farm.py +++ b/packit_service/worker/testing_farm.py @@ -56,6 +56,20 @@ def tft_api_url(self) -> str: self._tft_api_url += "/" return self._tft_api_url + @property + def fmf_url(self): + return ( + self.job_config.metadata.fmf_url + or self.project.get_pr(self.metadata.pr_id).source_project.get_web_url() + ) + + @property + def fmf_ref(self): + if self.job_config.metadata.fmf_url: + return self.job_config.metadata.fmf_ref + + return self.metadata.commit_sha + def _payload(self, build_id: int, chroot: str) -> dict: """ Testing Farm API: https://testing-farm.gitlab.io/api/ @@ -68,16 +82,14 @@ def _payload(self, build_id: int, chroot: str) -> dict: """ distro, arch = self.chroot2distro_arch(chroot) compose = self.distro2compose(distro) - pr = self.project.get_pr(self.metadata.pr_id) - # url of the source/fork from which the PR has been created - from_url = pr.source_project.get_web_url() + fmf = {"url": self.fmf_url} + if self.fmf_ref: + fmf["ref"] = self.fmf_ref + return { "api_key": self.service_config.testing_farm_secret, "test": { - "fmf": { - "url": from_url, - "ref": self.metadata.commit_sha, - }, + "fmf": fmf, }, "environments": [ { @@ -137,6 +149,10 @@ def _payload_install_test(self, build_id: int, chroot: str) -> dict: } def is_fmf_configured(self) -> bool: + + if self.job_config.metadata.fmf_url is not None: + return True + try: self.project.get_file_content( path=".fmf/version", ref=self.metadata.commit_sha diff --git a/tests/unit/test_testing_farm.py b/tests/unit/test_testing_farm.py index e980c2230..260236f42 100644 --- a/tests/unit/test_testing_farm.py +++ b/tests/unit/test_testing_farm.py @@ -5,6 +5,7 @@ from flexmock import flexmock from ogr.abstract import CommitStatus from packit.config import JobConfig, JobType, JobConfigTriggerType +from packit.config.job_config import JobMetadataConfig from packit.local_project import LocalProject from packit_service.config import PackageConfigGetter @@ -396,6 +397,99 @@ def test_payload( assert payload["notification"]["webhook"]["url"].endswith("/testing-farm/results") +@pytest.mark.parametrize( + ("fmf_url," "fmf_ref," "result_url," "result_ref"), + [ + ( + "https://github.com/mmuzila/test", + "main", + "https://github.com/mmuzila/test", + "main", + ), + ( + None, + None, + "https://github.com/packit/packit", + "feb41e5", + ), + ( + None, + "main", + "https://github.com/packit/packit", + "feb41e5", + ), + ( + "https://github.com/mmuzila/test", + None, + "https://github.com/mmuzila/test", + None, + ), + ], +) +def test_test_repo(fmf_url, fmf_ref, result_url, result_ref): + tf_api = "https://api.dev.testing-farm.io/v0.1/" + tf_token = "very-secret" + ps_deployment = "test" + repo = "packit" + project_url = "https://github.com/packit/packit" + git_ref = "master" + namespace = "packit-service" + commit_sha = "feb41e5" + copr_owner = "me" + copr_project = "cool-project" + build_id = "123456" + chroot = "centos-stream-x86_64" + compose = "Fedora-Rawhide" + + config = flexmock( + testing_farm_api_url=tf_api, + testing_farm_secret=tf_token, + deployment=ps_deployment, + command_handler_work_dir="/tmp", + ) + package_config = flexmock(jobs=[]) + pr = flexmock(source_project=flexmock(get_web_url=lambda: project_url)) + project = flexmock( + repo=repo, + namespace=namespace, + service="GitHub", + get_git_urls=lambda: {"git": f"{project_url}.git"}, + get_pr=lambda id_: pr, + ) + metadata = flexmock( + trigger=flexmock(), + commit_sha=commit_sha, + git_ref=git_ref, + project_url=project_url, + pr_id=123, + ) + db_trigger = flexmock() + + job_helper = TFJobHelper( + service_config=config, + package_config=package_config, + project=project, + metadata=metadata, + db_trigger=db_trigger, + job_config=JobConfig( + type=JobType.tests, + trigger=JobConfigTriggerType.pull_request, + metadata=JobMetadataConfig(fmf_url=fmf_url, fmf_ref=fmf_ref), + ), + ) + job_helper = flexmock(job_helper) + + job_helper.should_receive("job_owner").and_return(copr_owner) + job_helper.should_receive("job_project").and_return(copr_project) + job_helper.should_receive("distro2compose").and_return(compose) + + payload = job_helper._payload(build_id, chroot) + assert payload.get("test") + assert payload["test"].get("fmf") + assert payload["test"]["fmf"].get("url") == result_url + assert payload["test"]["fmf"].get("ref") == result_ref + + def test_get_request_details(): request_id = "123abc" request = {