예제 #1
0
def test_set_status(
    project,
    commit_sha,
    pr_id,
    has_pr_id,
    pr_object,
    state,
    description,
    check_name,
    url,
    needs_pr_flags,
    uid,
):
    reporter = StatusReporter(project, commit_sha, pr_id)

    project.should_receive("set_commit_status").with_args(
        commit_sha, state, url, description, check_name, trim=True
    ).once()

    if has_pr_id:
        project.should_receive("get_pr").with_args(pr_id).once().and_return(pr_object)

    if needs_pr_flags:
        pr_object.should_receive("set_flag").with_args(
            check_name, description, url, state, uid
        )

    reporter.set_status(state, description, check_name, url)
    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 = CommitStatus.success
            passed = True
        else:
            status = CommitStatus.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

        status_reporter = StatusReporter(self.project, self.event.commit_sha)
        status_reporter.report(
            state=status,
            description=short_msg,
            url=self.event.log_url,
            check_names=TestingFarmJobHelper.get_test_check(
                self.event.copr_chroot),
        )

        return HandlerResults(success=True, details={})
예제 #3
0
def test_set_status_gitlab(
    commit_sha,
    pr_id,
    pr_object,
    state,
    description,
    check_name,
    url,
):
    project = GitlabProject(None, None, None)
    reporter = StatusReporter(project, commit_sha, pr_id)
    act_upon = flexmock(
        pr_object.source_project) if pr_id else flexmock(GitlabProject)

    act_upon.should_receive("set_commit_status").with_args(commit_sha,
                                                           state,
                                                           url,
                                                           description,
                                                           check_name,
                                                           trim=True).once()

    if pr_id is not None:
        flexmock(GitlabProject).should_receive("get_pr").with_args(
            pr_id).and_return(pr_object)

    reporter.set_status(state, description, check_name, url)
예제 #4
0
def test_report_status_by_comment(
    project,
    commit_sha,
    pr_id,
    state,
    check_names,
    url,
    result,
):
    reporter = StatusReporter(project, commit_sha, pr_id)

    comment_body = "\n".join((
        "| Job | Result |",
        "| ------------- | ------------ |",
        f"| [{check_names}]({url}) | {result} |",
    ))

    if pr_id:
        project.should_receive("get_pr").with_args(pr_id=pr_id).and_return(
            flexmock().should_receive("comment").with_args(
                body=comment_body).mock()).once()
    else:
        project.should_receive("commit_comment").with_args(
            commit=commit_sha,
            body=comment_body,
        ).once()

    reporter.report_status_by_comment(state, url, check_names)
예제 #5
0
def test_commit_comment_instead_of_status(
    project,
    commit_sha,
    pr_id,
    has_pr_id,
    pr_object,
    state,
    description,
    check_name,
    url,
    exception_mock,
):
    reporter = StatusReporter(project, commit_sha, pr_id)

    exception, exception_args, exception_kwargs = exception_mock
    project.should_receive("set_commit_status").with_args(
        commit_sha, state, url, description, check_name,
        trim=True).and_raise(exception, *exception_args,
                             **exception_kwargs).once()
    project.should_receive("commit_comment").with_args(
        commit=commit_sha,
        body="\n".join([
            f"- name: {check_name}",
            f"- state: {state.name}",
            f"- url: {url if url else 'not provided'}",
        ]) + f"\n\n{description}",
    )

    if has_pr_id:
        project.should_receive("get_pr").with_args(pr_id).once().and_return(
            pr_object)

    reporter.set_status(state, description, check_name, url)
    def run(self) -> TaskResults:
        logger.debug(f"Testing farm {self.pipeline_id} result:\n{self.result}")
        logger.debug(f"Testing farm test results:\n{self.tests}")

        test_run_model = TFTTestRunModel.get_by_pipeline_id(
            pipeline_id=self.pipeline_id)
        if not test_run_model:
            logger.warning(
                f"Unknown pipeline_id received from the testing-farm: "
                f"{self.pipeline_id}")

        if test_run_model:
            test_run_model.set_status(self.result)

        if self.result == TestingFarmResult.running:
            status = CommitStatus.pending
            if isinstance(self.project.service, GitlabService):
                # only Gitlab has 'running' state
                status = CommitStatus.running
            summary = self.summary or "Tests are running ..."
        elif self.result == TestingFarmResult.passed:
            status = CommitStatus.success
            summary = self.summary or "Tests passed ..."
        elif self.result == TestingFarmResult.error:
            status = CommitStatus.error
            if isinstance(self.project.service, GitlabService):
                # Gitlab has no 'error' state
                status = CommitStatus.failure
            summary = self.summary or "Error ..."
        else:
            status = CommitStatus.failure
            summary = self.summary or "Tests failed ..."

        if len(self.tests
               ) > 0 and self.tests[0].name == "/packit/install-and-verify":
            logger.debug("No-fmf scenario discovered.")
            summary = ("Installation passed" if status == CommitStatus.success
                       else "Installation failed")

        if test_run_model:
            test_run_model.set_web_url(self.log_url)
        status_reporter = StatusReporter(project=self.project,
                                         commit_sha=self.data.commit_sha,
                                         pr_id=self.data.pr_id)
        status_reporter.report(
            state=status,
            description=summary,
            url=self.log_url,
            check_names=TestingFarmJobHelper.get_test_check(self.copr_chroot),
        )

        return TaskResults(success=True, details={})
예제 #7
0
    def run(self) -> TaskResults:

        logger.debug(f"Received testing-farm result:\n{self.result}")
        logger.debug(f"Received testing-farm test results:\n{self.tests}")

        test_run_model = TFTTestRunModel.get_by_pipeline_id(
            pipeline_id=self.pipeline_id
        )
        if not test_run_model:
            logger.warning(
                f"Unknown pipeline_id received from the testing-farm: "
                f"{self.pipeline_id}"
            )

        if test_run_model:
            test_run_model.set_status(self.result)

        if self.result == TestingFarmResult.passed:
            status = CommitStatus.success
            passed = True
        elif self.result == TestingFarmResult.error:
            status = CommitStatus.error
            passed = False
        else:
            status = CommitStatus.failure
            passed = False

        github_status_url = self.log_url
        if len(self.tests) == 1 and self.tests[0].name == "/install/copr-build":
            logger.debug("No-fmf scenario discovered.")
            short_msg = "Installation passed" if passed else "Installation failed"
        elif self.message.startswith(
            "Command '['git', 'clone'"
        ) and self.message.endswith("failed with exit code 128"):
            short_msg = "Problem with Testing-Farm cluster"
            github_status_url = "https://pagure.io/centos-infra/issue/85"
        else:
            short_msg = self.message

        if test_run_model:
            test_run_model.set_web_url(self.log_url)
        status_reporter = StatusReporter(
            project=self.project, commit_sha=self.data.commit_sha, pr_id=self.data.pr_id
        )
        status_reporter.report(
            state=status,
            description=short_msg,
            url=github_status_url,
            check_names=TestingFarmJobHelper.get_test_check(self.copr_chroot),
        )

        return TaskResults(success=True, details={})
예제 #8
0
def test_set_status_pagure(
    project,
    commit_sha,
    pr_id,
    pr_object,
    state,
    description,
    check_name,
    url,
    state_to_set,
    uid,
):
    project = PagureProject(None, None, PagureService())
    reporter = StatusReporter.get_instance(project=project,
                                           commit_sha=commit_sha,
                                           pr_id=pr_id)
    act_upon = flexmock(
        pr_object.source_project) if pr_id else flexmock(PagureProject)

    act_upon.should_receive("set_commit_status").with_args(commit_sha,
                                                           state_to_set,
                                                           url,
                                                           description,
                                                           check_name,
                                                           trim=True).once()

    if pr_id is not None:
        flexmock(PagureProject).should_receive("get_pr").with_args(
            pr_id).and_return(pr_object)

    reporter.set_status(state, description, check_name, url)
예제 #9
0
def test_report_status_by_comment(
    commit_sha,
    pr_id,
    state,
    check_names,
    url,
    result,
):
    project = GitlabProject(None, None, None)
    reporter = StatusReporter.get_instance(project=project,
                                           commit_sha=commit_sha,
                                           pr_id=pr_id)
    act_upon = flexmock(GitlabProject)

    comment_body = "\n".join((
        "| Job | Result |",
        "| ------------- | ------------ |",
        f"| [{check_names}]({url}) | {result} |",
        "### Description\n",
        "should include this",
    ))

    if pr_id:
        act_upon.should_receive("get_pr").with_args(pr_id=pr_id).and_return(
            flexmock().should_receive("comment").with_args(
                body=comment_body).mock()).once()
    else:
        act_upon.should_receive("commit_comment").with_args(
            commit=commit_sha,
            body=comment_body,
        ).once()

    reporter.report_status_by_comment(state, url, check_names,
                                      "should include this")
예제 #10
0
def test_set_status_github_check(
    project,
    commit_sha,
    pr_id,
    pr_object,
    state,
    title,
    summary,
    check_name,
    url,
    check_status,
    check_conclusion,
    trigger_id,
):
    project = GithubProject(None, None, None)
    reporter = StatusReporter.get_instance(project=project,
                                           commit_sha=commit_sha,
                                           pr_id=pr_id,
                                           trigger_id=trigger_id)
    act_upon = flexmock(GithubProject)

    act_upon.should_receive("create_check_run").with_args(
        name=check_name,
        commit_sha=commit_sha,
        url=url,
        external_id=str(trigger_id),
        status=check_status,
        conclusion=check_conclusion,
        output=create_github_check_run_output(title, summary),
    ).once()

    reporter.set_status(state, title, check_name, url)
예제 #11
0
 def status_reporter(self) -> StatusReporter:
     if not self._status_reporter:
         self._status_reporter = StatusReporter(
             project=self.project,
             commit_sha=self.metadata.commit_sha,
             pr_id=self.metadata.pr_id,
         )
     return self._status_reporter
예제 #12
0
def test_report_status_by_comment(
    project,
    commit_sha,
    pr_id,
    pr_object,
    state,
    description,
    check_names,
    url,
):
    reporter = StatusReporter(project, commit_sha, pr_id)

    project.should_receive("pr_comment").with_args(
        pr_id, f"| [{check_names}]({url}) | SUCCESS |"
    ).once()

    reporter.report_status_by_comment(state, url, check_names)
예제 #13
0
    def run(self) -> TaskResults:
        logger.debug(f"Testing farm {self.pipeline_id} result:\n{self.result}")

        test_run_model = TFTTestRunModel.get_by_pipeline_id(
            pipeline_id=self.pipeline_id)
        if not test_run_model:
            logger.warning(
                f"Unknown pipeline_id received from the testing-farm: "
                f"{self.pipeline_id}")

        if test_run_model:
            test_run_model.set_status(self.result, created=self.created)

        if self.result == TestingFarmResult.running:
            status = BaseCommitStatus.running
            summary = self.summary or "Tests are running ..."
        elif self.result == TestingFarmResult.passed:
            status = BaseCommitStatus.success
            summary = self.summary or "Tests passed ..."
        elif self.result == TestingFarmResult.error:
            status = BaseCommitStatus.error
            summary = self.summary or "Error ..."
        else:
            status = BaseCommitStatus.failure
            summary = self.summary or "Tests failed ..."

        if self.result == TestingFarmResult.running:
            self.pushgateway.test_runs_started.inc()
        else:
            self.pushgateway.test_runs_finished.inc()
            test_run_time = measure_time(end=datetime.now(timezone.utc),
                                         begin=test_run_model.submitted_time)
            self.pushgateway.test_run_finished_time.observe(test_run_time)

        if test_run_model:
            test_run_model.set_web_url(self.log_url)

        trigger = JobTriggerModel.get_or_create(
            type=self.db_trigger.job_trigger_model_type,
            trigger_id=self.db_trigger.id,
        )
        status_reporter = StatusReporter.get_instance(
            project=self.project,
            commit_sha=self.data.commit_sha,
            trigger_id=trigger.id if trigger else None,
            pr_id=self.data.pr_id,
        )
        status_reporter.report(
            state=status,
            description=summary,
            url=get_testing_farm_info_url(test_run_model.id)
            if test_run_model else self.log_url,
            links_to_external_services={"Testing Farm": self.log_url},
            check_names=TestingFarmJobHelper.get_test_check(self.copr_chroot),
        )

        return TaskResults(success=True, details={})
    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}")

        test_run_model = TFTTestRunModel.get_by_pipeline_id(
            pipeline_id=self.event.pipeline_id)
        if not test_run_model:
            logger.warning(
                f"Unknown pipeline_id received from the testing-farm: "
                f"{self.event.pipeline_id}")

        if test_run_model:
            test_run_model.set_status(self.event.result)

        if self.event.result == TestingFarmResult.passed:
            status = CommitStatus.success
            passed = True

        else:
            status = CommitStatus.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

        if test_run_model:
            test_run_model.set_web_url(self.event.log_url)
        status_reporter = StatusReporter(self.event.project,
                                         self.event.commit_sha)
        status_reporter.report(
            state=status,
            description=short_msg,
            url=self.event.log_url,
            check_names=TestingFarmJobHelper.get_test_check(
                self.event.copr_chroot),
        )

        return HandlerResults(success=True, details={})
예제 #15
0
 def status_reporter(self) -> StatusReporter:
     if not self._status_reporter:
         self._status_reporter = StatusReporter(
             project=self.base_project
             if isinstance(self.project, GitlabProject) and self.is_reporting_allowed
             else self.project,
             commit_sha=self.metadata.commit_sha,
             pr_id=self.metadata.pr_id,
         )
     return self._status_reporter
예제 #16
0
 def status_reporter(self) -> StatusReporter:
     if not self._status_reporter:
         trigger = JobTriggerModel.get_or_create(
             type=self.db_trigger.job_trigger_model_type,
             trigger_id=self.db_trigger.id,
         )
         self._status_reporter = StatusReporter.get_instance(
             project=self.project,
             commit_sha=self.metadata.commit_sha,
             trigger_id=trigger.id if trigger else None,
             pr_id=self.metadata.pr_id,
         )
     return self._status_reporter
예제 #17
0
def test_status_instead_check(
    project,
    commit_sha,
    pr_id,
    pr_object,
    state,
    title,
    summary,
    check_name,
    url,
    check_status,
    check_conclusion,
    commit_state_to_set,
    exception_mock,
    trigger_id,
):
    project = GithubProject(None, None, None)
    reporter = StatusReporter.get_instance(project=project,
                                           commit_sha=commit_sha,
                                           trigger_id=trigger_id,
                                           pr_id=pr_id)
    act_upon = flexmock(GithubProject)

    exception, exception_args, exception_kwargs = exception_mock
    act_upon.should_receive("create_check_run").with_args(
        name=check_name,
        commit_sha=commit_sha,
        url=url,
        external_id=str(trigger_id),
        status=check_status,
        conclusion=check_conclusion,
        output=create_github_check_run_output(title, summary),
    ).and_raise(exception, *exception_args, **exception_kwargs).once()

    act_upon.should_receive("set_commit_status").with_args(commit_sha,
                                                           commit_state_to_set,
                                                           url,
                                                           title,
                                                           check_name,
                                                           trim=True).once()

    reporter.set_status(state, title, check_name, url)
예제 #18
0
 def status_reporter(self) -> StatusReporter:
     if not self._status_reporter:
         self._status_reporter = StatusReporter(
             self.project, self.event.commit_sha, self.event.pr_id
         )
     return self._status_reporter