def test_check_names(self) -> List[str]: if not self._test_check_names: self._test_check_names = [ PRCheckName.get_testing_farm_check(chroot) for chroot in self.tests_chroots ] return self._test_check_names
def run(self) -> HandlerResults: logger.debug(f"Received testing-farm result:\n{self.event.result}") logger.debug( f"Received testing-farm test results:\n{self.event.tests}") if self.event.result == TestingFarmResult.passed: status = "success" passed = True else: status = "failure" passed = False if (len(self.event.tests) == 1 and self.event.tests[0].name == "/install/copr-build"): logger.debug("No-fmf scenario discovered.") short_msg = "Installation passed" if passed else "Installation failed" else: short_msg = self.event.message r = BuildStatusReporter(self.project, self.event.commit_sha) r.report( state=status, description=short_msg, url=self.event.log_url, check_names=PRCheckName.get_testing_farm_check( self.event.copr_chroot), ) return HandlerResults(success=True, details={})
def test_copr_build_just_tests_defined(copr_build_start, pc_tests, copr_build): steve = SteveJobs() flexmock(SteveJobs, _is_private=False) flexmock(CoprHelper).should_receive("get_copr_client").and_return( Client( config={ "copr_url": "https://copr.fedorainfracloud.org", "username": "******", })) flexmock(CoprBuildJobHelper).should_receive("copr_build_model").and_return( flexmock()) flexmock(CoprBuildEvent).should_receive("get_package_config").and_return( pc_tests) flexmock(PRCheckName).should_receive("get_build_check").and_return( PACKIT_STG_CHECK) flexmock(PRCheckName).should_receive("get_testing_farm_check").and_return( PACKIT_STG_TESTING_FARM_CHECK) flexmock(CoprBuild).should_receive("get_by_build_id").and_return( copr_build) flexmock(CoprBuildDB).should_receive("get_build").and_return({ "commit_sha": "XXXXX", "pr_id": 24, "repo_name": "hello-world", "repo_namespace": "packit-service", "ref": "XXXX", "https_url": "https://github.com/packit-service/hello-world", }) url = get_log_url(1) flexmock(requests).should_receive("get").and_return(requests.Response()) flexmock( requests.Response).should_receive("raise_for_status").and_return(None) flexmock(CoprBuild).should_receive("set_status").with_args("pending") flexmock(CoprBuild).should_receive("set_build_logs_url") # check if packit-service sets the correct PR status flexmock(BuildStatusReporter).should_receive("report").with_args( state="pending", description="RPM build has started...", url=url, check_names=PACKIT_STG_CHECK, ).never() flexmock(BuildStatusReporter).should_receive("report").with_args( state="pending", description="RPM build has started...", url=url, check_names=PRCheckName.get_testing_farm_check( copr_build_start["chroot"]), ).once() steve.process_message(copr_build_start)
def report_status_to_test_for_chroot(self, description, state, url: str = "", chroot: str = ""): if self.job_tests and chroot in self.tests_chroots: self.status_reporter.report( description=description, state=state, url=url, check_names=PRCheckName.get_testing_farm_check(chroot), )
def run(self) -> HandlerResults: self.local_project = LocalProject( git_project=self.project, working_dir=self.config.command_handler_work_dir ) r = BuildStatusReporter(self.project, self.event.commit_sha) if self.event.result == TestingFarmResult.passed: status = "success" else: status = "failure" r.report( status, self.event.message, None, self.event.log_url, check_names=PRCheckName.get_testing_farm_check(self.event.copr_chroot), ) return HandlerResults(success=True, details={})
def run(self) -> HandlerResults: self.local_project = LocalProject( git_project=self.project, working_dir=self.config.command_handler_work_dir) logger.info("Running testing farm") r = BuildStatusReporter(self.project, self.event.commit_sha) chroots = self.job.metadata.get("targets") logger.debug(f"Testing farm chroots: {chroots}") for chroot in chroots: pipeline_id = str(uuid.uuid4()) logger.debug(f"Pipeline id: {pipeline_id}") payload: dict = { "pipeline": { "id": pipeline_id }, "api": { "token": self.config.testing_farm_secret }, } logger.debug(f"Payload: {payload}") stg = "-stg" if self.config.deployment == Deployment.stg else "" copr_repo_name = ( f"packit/{self.project.namespace}-{self.project.repo}-" f"{self.event.pr_id}{stg}") payload["artifact"] = { "repo-name": self.event.base_repo_name, "repo-namespace": self.event.base_repo_namespace, "copr-repo-name": copr_repo_name, "copr-chroot": chroot, "commit-sha": self.event.commit_sha, "git-url": self.event.project_url, "git-ref": self.base_ref, } logger.debug("Sending testing farm request...") logger.debug(payload) req = self.send_testing_farm_request(TESTING_FARM_TRIGGER_URL, "POST", {}, json.dumps(payload)) logger.debug(f"Request sent: {req}") if not req: msg = "Failed to post request to testing farm API." logger.debug("Failed to post request to testing farm API.") r.report( "failure", msg, None, "", check_names=PRCheckName.get_testing_farm_check(chroot), ) return HandlerResults(success=False, details={"msg": msg}) else: logger.debug( f"Submitted to testing farm with return code: {req.status_code}" ) """ Response: { "id": "9fa3cbd1-83f2-4326-a118-aad59f5", "success": true, "url": "https://console-testing-farm.apps.ci.centos.org/pipeline/<id>" } """ # success set check on pending if req.status_code != 200: # something went wrong msg = req.json()["message"] r.report( "failure", msg, None, check_names=PRCheckName.get_testing_farm_check(chroot), ) return HandlerResults(success=False, details={"msg": msg}) r.report( "pending", "Tests are running ...", None, req.json()["url"], check_names=PRCheckName.get_testing_farm_check(chroot), ) return HandlerResults(success=True, details={})
def run_copr_build(self) -> HandlerResults: if not (self.job_copr_build or self.job_tests): msg = "No copr_build or tests job defined." # we can't report it to end-user at this stage return HandlerResults(success=False, details={"msg": msg}) try: self.report_status_to_all(description="Building SRPM ...", state="pending") # we want to get packit logs from the SRPM creation process # so we stuff them into a StringIO buffer stream = StringIO() handler = logging.StreamHandler(stream) packit_logger = logging.getLogger("packit") packit_logger.setLevel(logging.DEBUG) packit_logger.addHandler(handler) formatter = PackitFormatter(None, "%H:%M:%S") handler.setFormatter(formatter) build_id, _ = self.api.run_copr_build( project=self.job_project, chroots=self.build_chroots, owner=self.job_owner, ) packit_logger.removeHandler(handler) stream.seek(0) logs = stream.read() web_url = get_copr_build_url_for_values(self.job_owner, self.job_project, build_id) srpm_build = SRPMBuild.create(logs) status = "pending" description = "Building RPM ..." for chroot in self.build_chroots: copr_build = CoprBuild.get_or_create( pr_id=self.event.pr_id, build_id=str(build_id), commit_sha=self.event.commit_sha, repo_name=self.event.base_repo_name, namespace=self.event.base_repo_namespace, web_url=web_url, target=chroot, status=status, srpm_build=srpm_build, ) url = get_log_url(id_=copr_build.id) self.status_reporter.report( state=status, description=description, url=url, check_names=PRCheckName.get_build_check(chroot), ) if chroot in self.tests_chroots: self.status_reporter.report( state=status, description=description, url=url, check_names=PRCheckName.get_testing_farm_check(chroot), ) except SandcastleTimeoutReached: return self._process_timeout() except SandcastleCommandFailed as ex: return self._process_failed_command(ex) except ApiException as ex: return self._process_openshift_error(ex) except PackitSRPMException as ex: return self._process_failed_srpm_build(ex) except PackitCoprProjectException as ex: return self._process_copr_submit_exception(ex) except PackitCoprException as ex: return self._process_general_exception(ex) except Exception as ex: return self._process_general_exception(ex) self.copr_build_model.build_id = build_id self.copr_build_model.save() return HandlerResults(success=True, details={})
def run_testing_farm(self, chroot: str) -> HandlerResults: if chroot not in self.tests_chroots: # Leaving here just to be sure that we will discover this situation if it occurs. # Currently not possible to trigger this situation. msg = f"Target '{chroot}' not defined for tests but triggered." logger.error(msg) send_to_sentry(PackitConfigException(msg)) return HandlerResults(success=False, details={"msg": msg},) if chroot not in self.build_chroots: self.report_missing_build_chroot(chroot) return HandlerResults( success=False, details={ "msg": f"Target '{chroot}' not defined for build. " f"Cannot run tests without build." }, ) check_name = PRCheckName.get_testing_farm_check(chroot) self.status_reporter.report( state="pending", description="Build succeeded. Submitting the tests ...", check_names=check_name, ) pipeline_id = str(uuid.uuid4()) logger.debug(f"Pipeline id: {pipeline_id}") payload: dict = { "pipeline": {"id": pipeline_id}, "api": {"token": self.config.testing_farm_secret}, } stg = "-stg" if self.config.deployment == Deployment.stg else "" copr_repo_name = ( f"packit/{self.project.namespace}-{self.project.repo}-" f"{self.event.pr_id}{stg}" ) payload["artifact"] = { "repo-name": self.event.base_repo_name, "repo-namespace": self.event.base_repo_namespace, "copr-repo-name": copr_repo_name, "copr-chroot": chroot, "commit-sha": self.event.commit_sha, "git-url": self.event.project_url, "git-ref": self.base_ref, } logger.debug("Sending testing farm request...") logger.debug(payload) req = self.send_testing_farm_request( TESTING_FARM_TRIGGER_URL, "POST", {}, json.dumps(payload) ) logger.debug(f"Request sent: {req}") if not req: msg = "Failed to post request to testing farm API." logger.debug("Failed to post request to testing farm API.") self.status_reporter.report( state="failure", description=msg, check_names=check_name, ) return HandlerResults(success=False, details={"msg": msg}) else: logger.debug( f"Submitted to testing farm with return code: {req.status_code}" ) """ Response: { "id": "9fa3cbd1-83f2-4326-a118-aad59f5", "success": true, "url": "https://console-testing-farm.apps.ci.centos.org/pipeline/<id>" } """ # success set check on pending if req.status_code != 200: # something went wrong if req.json() and "message" in req.json(): msg = req.json()["message"] else: msg = f"Failed to submit tests: {req.reason}" logger.error(msg) self.status_reporter.report( state="failure", description=msg, check_names=check_name, ) return HandlerResults(success=False, details={"msg": msg}) self.status_reporter.report( state="pending", description="Tests are running ...", url=req.json()["url"], check_names=check_name, ) return HandlerResults(success=True, details={})
def report_missing_build_chroot(self, chroot: str): self.status_reporter.report( state="error", description=f"No build defined for the target '{chroot}'.", check_names=PRCheckName.get_testing_farm_check(chroot), )