Skip to content

Commit

Permalink
Allow to reference a test repo in test job metadata
Browse files Browse the repository at this point in the history
This change allows to specify distinct repository for tests.
  • Loading branch information
mmuzila committed Mar 22, 2021
1 parent 2647b53 commit a5471e1
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 7 deletions.
30 changes: 23 additions & 7 deletions packit_service/worker/testing_farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -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": [
{
Expand Down Expand Up @@ -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
Expand Down
94 changes: 94 additions & 0 deletions tests/unit/test_testing_farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit a5471e1

Please sign in to comment.