Пример #1
0
def test_pr_test_command_handler(pr_embedded_command_comment_event):
    jobs = [{
        "trigger": "pull_request",
        "job": "tests",
        "metadata": {
            "targets": "fedora-rawhide-x86_64"
        },
    }]
    packit_yaml = (
        "{'specfile_path': 'the-specfile.spec', 'synced_files': [], 'jobs': " +
        str(jobs) + "}")
    flexmock(
        GithubProject,
        full_repo_name="packit-service/hello-world",
        get_file_content=lambda path, ref: packit_yaml,
        get_files=lambda ref, filter_regex: ["the-specfile.spec"],
        get_web_url=lambda: "https://github.com/the-namespace/the-repo",
        get_pr=lambda pr_id: flexmock(head_commit="12345"),
    )
    flexmock(Github, get_repo=lambda full_name_or_id: None)

    config = ServiceConfig()
    config.command_handler_work_dir = SANDCASTLE_WORK_DIR
    flexmock(ServiceConfig).should_receive("get_service_config").and_return(
        config)
    trigger = flexmock(
        job_config_trigger_type=JobConfigTriggerType.pull_request, id=123)
    flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(
        trigger)
    flexmock(PullRequestModel).should_receive("get_by_id").with_args(
        123).and_return(trigger)
    flexmock(LocalProject, refresh_the_arguments=lambda: None)
    flexmock(Allowlist, check_and_report=True)
    flexmock(PullRequestModel).should_receive("get_or_create").with_args(
        pr_id=9,
        namespace="packit-service",
        repo_name="hello-world",
        project_url="https://github.com/packit-service/hello-world",
    ).and_return(
        flexmock(id=9,
                 job_config_trigger_type=JobConfigTriggerType.pull_request))
    pr_embedded_command_comment_event["comment"]["body"] = "/packit test"
    flexmock(GithubProject, get_files="foo.spec")
    flexmock(GithubProject).should_receive("is_private").and_return(False)
    flexmock(Signature).should_receive("apply_async").once()
    flexmock(TestingFarmJobHelper).should_receive(
        "run_testing_farm_on_all").and_return(
            TaskResults(success=True, details={}))

    processing_results = SteveJobs().process_message(
        pr_embedded_command_comment_event)
    event_dict, job, job_config, package_config = get_parameters_from_results(
        processing_results)

    run_testing_farm_handler(
        package_config=package_config,
        event=event_dict,
        job_config=job_config,
    )
Пример #2
0
def test_retest_failed(
    mock_pr_comment_functionality, pr_embedded_command_comment_event
):
    pr = flexmock(head_commit="12345")
    flexmock(GithubProject).should_receive("get_pr").and_return(pr)
    comment = flexmock()
    flexmock(pr).should_receive("get_comment").and_return(comment)
    flexmock(comment).should_receive("add_reaction").with_args("+1").once()

    flexmock(PullRequestModel).should_receive("get_or_create").with_args(
        pr_id=9,
        namespace="packit-service",
        repo_name="hello-world",
        project_url="https://github.com/packit-service/hello-world",
    ).and_return(
        flexmock(id=9, job_config_trigger_type=JobConfigTriggerType.pull_request)
    )

    pr_embedded_command_comment_event["comment"]["body"] = "/packit retest-failed"
    flexmock(GithubProject, get_files="foo.spec")
    flexmock(GithubProject).should_receive("is_private").and_return(False)
    flexmock(Signature).should_receive("apply_async").once()
    flexmock(copr_build).should_receive("get_valid_build_targets").twice().and_return(
        {"test-target"}
    )
    flexmock(TestingFarmJobHelper).should_receive("get_latest_copr_build").and_return(
        flexmock(status=PG_BUILD_STATUS_SUCCESS)
    )

    model = flexmock(TFTTestRunModel, status="failed", target="tf_target")
    flexmock(model).should_receive("get_all_by_commit_target").and_return(flexmock())
    flexmock(AbstractForgeIndependentEvent).should_receive(
        "get_all_tf_failed_targets"
    ).and_return({"tf_target"})

    flexmock(Pushgateway).should_receive("push").twice().and_return()
    flexmock(CoprBuildJobHelper).should_receive("report_status_to_tests").with_args(
        description=TASK_ACCEPTED,
        state=BaseCommitStatus.pending,
        url="",
    ).once()

    processing_results = SteveJobs().process_message(pr_embedded_command_comment_event)
    event_dict, job, job_config, package_config = get_parameters_from_results(
        processing_results
    )
    assert event_dict["targets_override"] == ["tf_target"]
    assert json.dumps(event_dict)

    run_testing_farm_handler(
        package_config=package_config,
        event=event_dict,
        job_config=job_config,
    )
Пример #3
0
def test_pr_comment_build_build_and_test_handler(mock_pr_comment_functionality,
                                                 pr_build_comment_event):
    flexmock(PullRequestModel).should_receive("get_or_create").with_args(
        pr_id=9,
        namespace="packit-service",
        repo_name="hello-world",
        project_url="https://github.com/packit-service/hello-world",
    ).and_return(
        flexmock(id=9,
                 job_config_trigger_type=JobConfigTriggerType.pull_request))
    flexmock(GithubProject, get_files="foo.spec")
    flexmock(GithubProject).should_receive("is_private").and_return(False)
    flexmock(copr_build).should_receive("get_valid_build_targets").and_return(
        set())
    flexmock(CoprBuildJobHelper).should_receive(
        "report_status_to_build").with_args(
            description=TASK_ACCEPTED,
            state=BaseCommitStatus.pending,
            url="",
        ).once()
    flexmock(CoprBuildJobHelper).should_receive(
        "report_status_to_tests").with_args(
            description=TASK_ACCEPTED,
            state=BaseCommitStatus.pending,
            url="",
        ).once()
    flexmock(Signature).should_receive("apply_async").twice()
    flexmock(Pushgateway).should_receive("push").times(3).and_return()
    pr = flexmock(head_commit="12345")
    flexmock(GithubProject).should_receive("get_pr").and_return(pr)
    comment = flexmock()
    flexmock(pr).should_receive("get_comment").and_return(comment)
    flexmock(comment).should_receive("add_reaction").with_args("+1").once()

    processing_results = SteveJobs().process_message(pr_build_comment_event)
    assert len(processing_results) == 2

    copr_build_job = [
        item for item in processing_results
        if item["details"]["job"] == "copr_build"
    ]
    assert copr_build_job

    test_job = [
        item for item in processing_results
        if item["details"]["job"] == "tests"
    ]
    assert test_job

    event_dict, job, job_config, package_config = get_parameters_from_results(
        test_job)
    assert json.dumps(event_dict)
    results = run_testing_farm_handler(
        package_config=package_config,
        event=event_dict,
        job_config=job_config,
    )
    assert first_dict_value(results["job"])["success"]
    assert "already handled" in first_dict_value(
        results["job"])["details"]["msg"]
Пример #4
0
def test_check_rerun_push_testing_farm_handler(
    mock_push_functionality, check_rerun_event_testing_farm
):

    flexmock(TestingFarmJobHelper).should_receive("run_testing_farm").once().and_return(
        TaskResults(success=True, details={})
    )
    flexmock(GithubProject).should_receive("get_files").and_return(["foo.spec"])
    flexmock(GithubProject).should_receive("get_web_url").and_return(
        "https://github.com/the-namespace/the-repo"
    )
    flexmock(GithubProject).should_receive("is_private").and_return(False)
    flexmock(TestingFarmJobHelper).should_receive("get_latest_copr_build").and_return(
        flexmock(status=PG_BUILD_STATUS_SUCCESS)
    )
    flexmock(copr_build).should_receive("get_valid_build_targets").and_return(
        {"fedora-rawhide-x86_64", "fedora-34-x86_64"}
    )
    flexmock(StatusReporterGithubChecks).should_receive("set_status").with_args(
        state=BaseCommitStatus.pending,
        description=TASK_ACCEPTED,
        check_name="testing-farm:fedora-rawhide-x86_64",
        url="",
        links_to_external_services=None,
        markdown_content=None,
    ).once()
    flexmock(Signature).should_receive("apply_async").once()
    flexmock(Pushgateway).should_receive("push").twice().and_return()

    processing_results = SteveJobs().process_message(check_rerun_event_testing_farm)
    event_dict, job, job_config, package_config = get_parameters_from_results(
        processing_results
    )
    assert event_dict["targets_override"] == ["fedora-rawhide-x86_64"]
    assert json.dumps(event_dict)
    results = run_testing_farm_handler(
        package_config=package_config,
        event=event_dict,
        job_config=job_config,
    )
    assert first_dict_value(results["job"])["success"]
Пример #5
0
def test_pr_test_command_handler_missing_build(pr_embedded_command_comment_event):
    jobs = [
        {
            "trigger": "pull_request",
            "job": "tests",
            "metadata": {"targets": "fedora-rawhide-x86_64"},
        }
    ]
    packit_yaml = (
        "{'specfile_path': 'the-specfile.spec', 'synced_files': [], 'jobs': "
        + str(jobs)
        + "}"
    )
    pr = flexmock(head_commit="12345")
    flexmock(GithubProject).should_receive("get_pr").and_return(pr)
    comment = flexmock()
    flexmock(pr).should_receive("get_comment").and_return(comment)
    flexmock(comment).should_receive("add_reaction").with_args("+1").once()
    flexmock(
        GithubProject,
        full_repo_name="packit-service/hello-world",
        get_file_content=lambda path, ref: packit_yaml,
        get_files=lambda ref, filter_regex: ["the-specfile.spec"],
        get_web_url=lambda: "https://github.com/the-namespace/the-repo",
    )
    flexmock(Github, get_repo=lambda full_name_or_id: None)

    config = ServiceConfig()
    config.command_handler_work_dir = SANDCASTLE_WORK_DIR
    flexmock(ServiceConfig).should_receive("get_service_config").and_return(config)
    trigger = flexmock(
        job_config_trigger_type=JobConfigTriggerType.pull_request, id=123
    )
    flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(trigger)
    flexmock(PullRequestModel).should_receive("get_by_id").with_args(123).and_return(
        trigger
    )
    flexmock(LocalProject, refresh_the_arguments=lambda: None)
    flexmock(Allowlist, check_and_report=True)
    flexmock(PullRequestModel).should_receive("get_or_create").with_args(
        pr_id=9,
        namespace="packit-service",
        repo_name="hello-world",
        project_url="https://github.com/packit-service/hello-world",
    ).and_return(
        flexmock(
            id=9,
            job_config_trigger_type=JobConfigTriggerType.pull_request,
            job_trigger_model_type=JobTriggerModelType.pull_request,
        )
    )
    flexmock(JobTriggerModel).should_receive("get_or_create").with_args(
        type=JobTriggerModelType.pull_request, trigger_id=9
    ).and_return(trigger)

    pr_embedded_command_comment_event["comment"]["body"] = "/packit test"
    flexmock(GithubProject, get_files="foo.spec")
    flexmock(GithubProject).should_receive("is_private").and_return(False)
    flexmock(Signature).should_receive("apply_async").twice()
    flexmock(copr_build).should_receive("get_valid_build_targets").and_return(
        {"test-target", "test-target-without-build"}
    )
    flexmock(TestingFarmJobHelper).should_receive("get_latest_copr_build").and_return(
        flexmock(status=PG_BUILD_STATUS_SUCCESS)
    ).and_return()

    flexmock(TestingFarmJobHelper).should_receive("job_owner").and_return("owner")
    flexmock(TestingFarmJobHelper).should_receive("job_project").and_return("project")
    flexmock(CoprBuildJobHelper).should_receive("report_status_to_tests").once()
    flexmock(CoprBuildJobHelper).should_receive(
        "report_status_to_test_for_chroot"
    ).once()
    flexmock(TestingFarmJobHelper).should_receive("run_testing_farm").once().and_return(
        TaskResults(success=False, details={})
    )
    flexmock(Pushgateway).should_receive("push").twice().and_return()

    processing_results = SteveJobs().process_message(pr_embedded_command_comment_event)
    event_dict, job, job_config, package_config = get_parameters_from_results(
        processing_results
    )
    assert json.dumps(event_dict)

    run_testing_farm_handler(
        package_config=package_config,
        event=event_dict,
        job_config=job_config,
    )
Пример #6
0
def test_pr_test_command_handler_skip_build_option(pr_embedded_command_comment_event):
    jobs = [
        {
            "trigger": "pull_request",
            "job": "tests",
            "metadata": {"targets": "fedora-rawhide-x86_64", "skip_build": True},
        }
    ]
    packit_yaml = (
        "{'specfile_path': 'the-specfile.spec', 'synced_files': [], 'jobs': "
        + str(jobs)
        + "}"
    )
    pr = flexmock(
        head_commit="12345",
        source_project=flexmock(get_web_url=lambda: "https://github.com/foo/bar"),
        target_branch_head_commit="6789a",
    )
    flexmock(GithubProject).should_receive("get_pr").and_return(pr)
    comment = flexmock()
    flexmock(pr).should_receive("get_comment").and_return(comment)
    flexmock(comment).should_receive("add_reaction").with_args("+1").once()
    flexmock(
        GithubProject,
        full_repo_name="packit-service/hello-world",
        get_file_content=lambda path, ref: packit_yaml,
        get_files=lambda ref, filter_regex: ["the-specfile.spec"],
        get_web_url=lambda: "https://github.com/the-namespace/the-repo",
    )
    flexmock(Github, get_repo=lambda full_name_or_id: None)

    tft_api_url = "https://api.dev.testing-farm.io/v0.1/"
    config = ServiceConfig(
        command_handler_work_dir=SANDCASTLE_WORK_DIR,
        testing_farm_api_url=tft_api_url,
        testing_farm_secret="secret token",
    )

    flexmock(ServiceConfig).should_receive("get_service_config").and_return(config)
    trigger = flexmock(
        job_config_trigger_type=JobConfigTriggerType.pull_request, id=123
    )
    flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(trigger)
    flexmock(PullRequestModel).should_receive("get_by_id").with_args(123).and_return(
        trigger
    )
    flexmock(LocalProject, refresh_the_arguments=lambda: None)
    flexmock(Allowlist, check_and_report=True)
    pr_model = flexmock(
        id=9,
        job_config_trigger_type=JobConfigTriggerType.pull_request,
        job_trigger_model_type=JobTriggerModelType.pull_request,
    )
    flexmock(PullRequestModel).should_receive("get_or_create").with_args(
        pr_id=9,
        namespace="packit-service",
        repo_name="hello-world",
        project_url="https://github.com/packit-service/hello-world",
    ).and_return(pr_model)
    flexmock(JobTriggerModel).should_receive("get_or_create").with_args(
        type=JobTriggerModelType.pull_request, trigger_id=9
    ).and_return(flexmock(id=2, type=JobTriggerModelType.pull_request))
    pr_embedded_command_comment_event["comment"]["body"] = "/packit test"
    flexmock(GithubProject, get_files="foo.spec")
    flexmock(GithubProject).should_receive("is_private").and_return(False)
    flexmock(copr_build).should_receive("get_valid_build_targets").and_return(
        {"fedora-rawhide-x86_64"}
    )
    flexmock(TestingFarmJobHelper).should_receive("get_latest_copr_build").never()
    flexmock(Pushgateway).should_receive("push").twice().and_return()
    flexmock(CoprBuildJobHelper).should_receive("report_status_to_tests").with_args(
        description=TASK_ACCEPTED,
        state=BaseCommitStatus.pending,
        url="",
    ).once()

    payload = {
        "api_key": "secret token",
        "test": {
            "fmf": {
                "url": "https://github.com/foo/bar",
                "ref": "12345",
            },
        },
        "environments": [
            {
                "arch": "x86_64",
                "os": {"compose": "Fedora-Rawhide"},
                "tmt": {
                    "context": {
                        "distro": "fedora-rawhide",
                        "arch": "x86_64",
                        "trigger": "commit",
                    }
                },
                "variables": {
                    "PACKIT_FULL_REPO_NAME": "packit-service/hello-world",
                    "PACKIT_UPSTREAM_NAME": "hello-world",
                    "PACKIT_DOWNSTREAM_NAME": "hello-world",
                    "PACKIT_DOWNSTREAM_URL": "https://src.fedoraproject.org/rpms/hello-world.git",
                    "PACKIT_PACKAGE_NAME": "hello-world",
                    "PACKIT_COMMIT_SHA": "12345",
                    "PACKIT_TARGET_SHA": "6789a",
                },
            }
        ],
        "notification": {
            "webhook": {
                "url": "https://stg.packit.dev/api/testing-farm/results",
                "token": "secret token",
            }
        },
    }

    flexmock(TestingFarmJobHelper).should_receive("is_fmf_configured").and_return(True)
    flexmock(TestingFarmJobHelper).should_receive("distro2compose").and_return(
        "Fedora-Rawhide"
    )

    pipeline_id = "5e8079d8-f181-41cf-af96-28e99774eb68"
    flexmock(TestingFarmJobHelper).should_receive(
        "send_testing_farm_request"
    ).with_args(endpoint="requests", method="POST", data=payload).and_return(
        RequestResponse(
            status_code=200,
            ok=True,
            content=json.dumps({"id": pipeline_id}).encode(),
            json={"id": pipeline_id},
        )
    )

    flexmock(StatusReporter).should_receive("report").with_args(
        state=BaseCommitStatus.running,
        description="Submitting the tests ...",
        check_names="testing-farm:fedora-rawhide-x86_64",
        url="",
        markdown_content=None,
    ).once()

    flexmock(GithubProject).should_receive("get_web_url").and_return(
        "https://github.com/foo/bar"
    )

    tft_test_run_model = flexmock(id=5)
    run_model = flexmock()
    flexmock(RunModel).should_receive("create").and_return(run_model)
    flexmock(TFTTestRunModel).should_receive("create").with_args(
        pipeline_id=pipeline_id,
        commit_sha="12345",
        status=TestingFarmResult.new,
        target="fedora-rawhide-x86_64",
        web_url=None,
        run_model=run_model,
        data={"base_project_url": "https://github.com/foo/bar"},
    ).and_return(tft_test_run_model)

    flexmock(StatusReporter).should_receive("report").with_args(
        description="Tests have been submitted ...",
        state=BaseCommitStatus.running,
        url="https://dashboard.localhost/results/testing-farm/5",
        check_names="testing-farm:fedora-rawhide-x86_64",
        markdown_content=None,
    ).once()
    flexmock(Signature).should_receive("apply_async").once()

    processing_results = SteveJobs().process_message(pr_embedded_command_comment_event)
    event_dict, job, job_config, package_config = get_parameters_from_results(
        processing_results
    )
    assert json.dumps(event_dict)

    run_testing_farm_handler(
        package_config=package_config,
        event=event_dict,
        job_config=job_config,
    )
def test_copr_build_end_failed_testing_farm_no_json(copr_build_end,
                                                    copr_build_pr):
    flexmock(GithubProject).should_receive("is_private").and_return(False)
    flexmock(GithubProject).should_receive("get_pr").and_return(
        flexmock(
            source_project=flexmock(get_web_url=lambda: "abc"),
            target_branch_head_commit="deadbeef",
        ).should_receive("comment").mock())

    config = PackageConfig(jobs=[
        JobConfig(
            type=JobType.copr_build,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(
                _targets=["fedora-rawhide"],
                owner="some-owner",
                project="some-project",
            ),
        ),
        JobConfig(
            type=JobType.tests,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(_targets=["fedora-rawhide"]),
        ),
    ])

    flexmock(AbstractCoprBuildEvent).should_receive(
        "get_package_config").and_return(config)
    flexmock(PackageConfigGetter).should_receive(
        "get_package_config_from_repo").and_return(config)
    flexmock(CoprBuildEndHandler).should_receive(
        "was_last_packit_comment_with_congratulation").and_return(False)

    flexmock(LocalProject).should_receive("refresh_the_arguments").and_return(
        None)

    flexmock(CoprBuildModel).should_receive("get_by_build_id").and_return(
        copr_build_pr)
    flexmock(CoprBuildModel).should_receive("get_by_id").and_return(
        copr_build_pr)
    copr_build_pr.should_call("set_status").with_args("success").once()
    copr_build_pr.should_receive("set_end_time").once()
    url = get_copr_build_info_url(1)
    flexmock(requests).should_receive("get").and_return(requests.Response())
    flexmock(
        requests.Response).should_receive("raise_for_status").and_return(None)
    # check if packit-service set correct PR status
    flexmock(StatusReporter).should_receive("report").with_args(
        state=BaseCommitStatus.success,
        description="RPMs were built successfully.",
        url=url,
        check_names=EXPECTED_BUILD_CHECK_NAME,
        markdown_content=None,
    ).once()

    flexmock(StatusReporter).should_receive("report").with_args(
        state=BaseCommitStatus.pending,
        description="RPMs were built successfully.",
        url=url,
        check_names=EXPECTED_TESTING_FARM_CHECK_NAME,
        markdown_content=None,
    ).once()

    flexmock(TestingFarmJobHelper).should_receive(
        "is_fmf_configured").and_return(True)
    flexmock(TestingFarmJobHelper).should_receive(
        "send_testing_farm_request").and_return(
            RequestResponse(
                status_code=400,
                ok=False,
                content=b"some text error",
                reason="some text error",
                json=None,
            ))

    flexmock(CoprBuildModel).should_receive("set_status").with_args("failure")
    flexmock(StatusReporter).should_receive("report").with_args(
        state=BaseCommitStatus.running,
        description="Build succeeded. Submitting the tests ...",
        check_names=EXPECTED_TESTING_FARM_CHECK_NAME,
        url="",
        markdown_content=None,
    ).once()
    flexmock(StatusReporter).should_receive("report").with_args(
        state=BaseCommitStatus.failure,
        description="Failed to submit tests: some text error",
        check_names=EXPECTED_TESTING_FARM_CHECK_NAME,
        url="",
        markdown_content=None,
    ).once()

    flexmock(Signature).should_receive("apply_async").twice()
    flexmock(Pushgateway).should_receive("push").twice().and_return()

    # skip SRPM url since it touches multiple classes
    flexmock(CoprBuildEndHandler).should_receive("set_srpm_url").and_return(
        None)

    processing_results = SteveJobs().process_message(copr_build_end)
    event_dict, job, job_config, package_config = get_parameters_from_results(
        processing_results)
    assert json.dumps(event_dict)

    flexmock(CoprBuildJobHelper).should_receive(
        "get_built_packages").and_return([])

    run_copr_build_end_handler(
        package_config=package_config,
        event=event_dict,
        job_config=job_config,
    )

    flexmock(TestingFarmHandler).should_receive("db_trigger").and_return(
        copr_build_pr.get_trigger_object())

    run_testing_farm_handler(
        package_config=package_config,
        event=event_dict,
        job_config=job_config,
        chroot="fedora-rawhide-x86_64",
        build_id=flexmock(),
    )
def test_copr_build_end_testing_farm(copr_build_end, copr_build_pr):
    tft_api_url = "https://api.dev.testing-farm.io/v0.1/"
    service_config = ServiceConfig(testing_farm_api_url=tft_api_url,
                                   testing_farm_secret="secret token")
    flexmock(ServiceConfig).should_receive("get_service_config").and_return(
        service_config)
    flexmock(GithubProject).should_receive("is_private").and_return(False)
    flexmock(GithubProject).should_receive("get_pr").and_return(
        flexmock(
            source_project=flexmock(
                get_web_url=lambda: "https://github.com/foo/bar"),
            target_branch_head_commit="deadbeef",
        ).should_receive("comment").mock())
    urls.DASHBOARD_URL = "https://dashboard.localhost"

    config = PackageConfig(jobs=[
        JobConfig(
            type=JobType.copr_build,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(
                _targets=["fedora-rawhide"],
                owner="some-owner",
                project="some-project",
            ),
        ),
        JobConfig(
            type=JobType.tests,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(_targets=["fedora-rawhide"]),
        ),
    ])

    flexmock(AbstractCoprBuildEvent).should_receive(
        "get_package_config").and_return(config)
    flexmock(PackageConfigGetter).should_receive(
        "get_package_config_from_repo").and_return(config)
    flexmock(CoprBuildEndHandler).should_receive(
        "was_last_packit_comment_with_congratulation").and_return(False)

    flexmock(LocalProject).should_receive("refresh_the_arguments").and_return(
        None)

    flexmock(CoprBuildModel).should_receive("get_by_build_id").and_return(
        copr_build_pr)
    flexmock(CoprBuildModel).should_receive("get_by_id").and_return(
        copr_build_pr)
    copr_build_pr.should_call("set_status").with_args("success").once()
    copr_build_pr.should_receive("set_end_time").once()
    flexmock(requests).should_receive("get").and_return(requests.Response())
    flexmock(
        requests.Response).should_receive("raise_for_status").and_return(None)
    # check if packit-service set correct PR status
    url = get_copr_build_info_url(1)
    flexmock(StatusReporter).should_receive("report").with_args(
        state=BaseCommitStatus.success,
        description="RPMs were built successfully.",
        url=url,
        check_names=EXPECTED_BUILD_CHECK_NAME,
        markdown_content=None,
    ).once()

    flexmock(StatusReporter).should_receive("report").with_args(
        state=BaseCommitStatus.pending,
        description="RPMs were built successfully.",
        url=url,
        check_names=EXPECTED_TESTING_FARM_CHECK_NAME,
        markdown_content=None,
    ).once()

    payload = {
        "api_key":
        "secret token",
        "test": {
            "fmf": {
                "url": "https://github.com/foo/bar",
                "ref": "0011223344",
            },
        },
        "environments": [{
            "arch":
            "x86_64",
            "os": {
                "compose": "Fedora-Rawhide"
            },
            "tmt": {
                "context": {
                    "distro": "fedora-rawhide",
                    "arch": "x86_64",
                    "trigger": "commit",
                }
            },
            "artifacts": [
                {
                    "id": "1:fedora-rawhide-x86_64",
                    "type": "fedora-copr-build",
                    "packages": ["bar-0.1-1.noarch"],
                },
            ],
            "variables": {
                "PACKIT_FULL_REPO_NAME": "foo/bar",
                "PACKIT_COMMIT_SHA": "0011223344",
                "PACKIT_PACKAGE_NVR": "bar-0.1-1",
                "PACKIT_BUILD_LOG_URL": "https://log-url",
                "PACKIT_TARGET_SHA": "deadbeef",
            },
        }],
        "notification": {
            "webhook": {
                "url": "https://stg.packit.dev/api/testing-farm/results",
                "token": "secret token",
            }
        },
    }

    flexmock(TestingFarmJobHelper).should_receive(
        "is_fmf_configured").and_return(True)
    flexmock(TestingFarmJobHelper).should_receive("distro2compose").with_args(
        "fedora-rawhide", "x86_64").and_return("Fedora-Rawhide")

    pipeline_id = "5e8079d8-f181-41cf-af96-28e99774eb68"
    flexmock(TestingFarmJobHelper).should_receive(
        "send_testing_farm_request").with_args(
            endpoint="requests", method="POST", data=payload).and_return(
                RequestResponse(
                    status_code=200,
                    ok=True,
                    content=json.dumps({
                        "id": pipeline_id
                    }).encode(),
                    json={"id": pipeline_id},
                ))

    flexmock(StatusReporter).should_receive("report").with_args(
        state=BaseCommitStatus.running,
        description="Build succeeded. Submitting the tests ...",
        check_names=EXPECTED_TESTING_FARM_CHECK_NAME,
        url="",
        markdown_content=None,
    ).once()

    flexmock(GithubProject).should_receive("get_web_url").and_return(
        "https://github.com/foo/bar")

    tft_test_run_model = flexmock(id=5)
    flexmock(TFTTestRunModel).should_receive("create").with_args(
        pipeline_id=pipeline_id,
        commit_sha="0011223344",
        status=TestingFarmResult.new,
        target="fedora-rawhide-x86_64",
        web_url=None,
        run_model=copr_build_pr.runs[0],
        data={
            "base_project_url": "https://github.com/foo/bar"
        },
    ).and_return(tft_test_run_model)

    flexmock(StatusReporter).should_receive("report").with_args(
        state=BaseCommitStatus.running,
        description="Tests have been submitted ...",
        url="https://dashboard.localhost/results/testing-farm/5",
        check_names=EXPECTED_TESTING_FARM_CHECK_NAME,
        markdown_content=None,
    ).once()
    flexmock(Signature).should_receive("apply_async").twice()

    # skip SRPM url since it touches multiple classes
    flexmock(CoprBuildEndHandler).should_receive("set_srpm_url").and_return(
        None)

    flexmock(Pushgateway).should_receive("push").twice().and_return()

    processing_results = SteveJobs().process_message(copr_build_end)
    event_dict, job, job_config, package_config = get_parameters_from_results(
        processing_results)
    assert json.dumps(event_dict)

    flexmock(CoprBuildJobHelper).should_receive(
        "get_built_packages").and_return([])

    run_copr_build_end_handler(
        package_config=package_config,
        event=event_dict,
        job_config=job_config,
    )

    flexmock(TestingFarmHandler).should_receive("db_trigger").and_return(
        copr_build_pr.get_trigger_object())

    flexmock(CoprBuildModel).should_receive("get_all_by").and_return(
        [copr_build_pr])

    run_testing_farm_handler(
        package_config=package_config,
        event=event_dict,
        job_config=job_config,
        chroot="fedora-rawhide-x86_64",
        build_id=1,
    )
Пример #9
0
def test_copr_build_end_failed_testing_farm_no_json(copr_build_end,
                                                    copr_build_pr):
    flexmock(GithubProject).should_receive("is_private").and_return(False)

    config = PackageConfig(jobs=[
        JobConfig(
            type=JobType.copr_build,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(
                targets=["fedora-rawhide"],
                owner="some-owner",
                project="some-project",
            ),
        ),
        JobConfig(
            type=JobType.tests,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(targets=["fedora-rawhide"]),
        ),
    ])

    flexmock(CoprBuildEvent).should_receive("get_package_config").and_return(
        config)
    flexmock(PackageConfigGetter).should_receive(
        "get_package_config_from_repo").and_return(config)
    flexmock(CoprBuildEndHandler).should_receive(
        "was_last_packit_comment_with_congratulation").and_return(False)
    flexmock(GithubProject).should_receive("pr_comment")

    flexmock(LocalProject).should_receive("refresh_the_arguments").and_return(
        None)

    flexmock(CoprBuildModel).should_receive("get_by_build_id").and_return(
        copr_build_pr)
    copr_build_pr.should_receive("set_status").with_args("success")
    copr_build_pr.should_receive("set_end_time").once()
    url = get_copr_build_info_url_from_flask(1)
    flexmock(requests).should_receive("get").and_return(requests.Response())
    flexmock(
        requests.Response).should_receive("raise_for_status").and_return(None)
    # check if packit-service set correct PR status
    flexmock(StatusReporter).should_receive("report").with_args(
        state=CommitStatus.success,
        description="RPMs were built successfully.",
        url=url,
        check_names=EXPECTED_BUILD_CHECK_NAME,
    ).once()

    flexmock(StatusReporter).should_receive("report").with_args(
        state=CommitStatus.pending,
        description="RPMs were built successfully.",
        url=url,
        check_names=EXPECTED_TESTING_FARM_CHECK_NAME,
    ).once()

    flexmock(TestingFarmJobHelper).should_receive(
        "send_testing_farm_request").and_return(
            RequestResponse(
                status_code=400,
                ok=False,
                content=b"some text error",
                reason="some text error",
                json=None,
            ))

    flexmock(CoprBuildModel).should_receive("set_status").with_args("failure")
    flexmock(StatusReporter).should_receive("report").with_args(
        state=CommitStatus.pending,
        description="Build succeeded. Submitting the tests ...",
        check_names=EXPECTED_TESTING_FARM_CHECK_NAME,
        url="",
    ).once()
    flexmock(StatusReporter).should_receive("report").with_args(
        state=CommitStatus.failure,
        description="Failed to submit tests: some text error",
        check_names=EXPECTED_TESTING_FARM_CHECK_NAME,
        url="",
    ).once()

    tft_test_run_model = flexmock()
    tft_test_run_model.should_receive("set_status").with_args(
        TestingFarmResult.error).and_return().once()
    pipeline_id = "5e8079d8-f181-41cf-af96-28e99774eb68"
    flexmock(uuid).should_receive("uuid4").and_return(uuid.UUID(pipeline_id))
    flexmock(TFTTestRunModel).should_receive("create").with_args(
        pipeline_id=pipeline_id,
        commit_sha="0011223344",
        status=TestingFarmResult.new,
        target="fedora-rawhide-x86_64",
        trigger_model=copr_build_pr.job_trigger.get_trigger_object(),
        web_url=None,
    ).and_return(tft_test_run_model)
    flexmock(Signature).should_receive("apply_async").twice()

    processing_results = SteveJobs().process_message(copr_build_end)
    event_dict, package_config, job = get_parameters_from_results(
        processing_results)

    run_copr_build_end_handler(
        package_config=package_config,
        event=event_dict,
        job_config=job,
    )

    flexmock(GithubTestingFarmHandler).should_receive("db_trigger").and_return(
        copr_build_pr.job_trigger.get_trigger_object())

    run_testing_farm_handler(
        package_config=package_config,
        event=event_dict,
        job_config=job,
        chroot="fedora-rawhide-x86_64",
        build_id=flexmock(),
    )
Пример #10
0
def test_copr_build_end_testing_farm(copr_build_end, copr_build_pr):
    flexmock(GithubProject).should_receive("is_private").and_return(False)

    config = PackageConfig(jobs=[
        JobConfig(
            type=JobType.copr_build,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(
                targets=["fedora-rawhide"],
                owner="some-owner",
                project="some-project",
            ),
        ),
        JobConfig(
            type=JobType.tests,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(targets=["fedora-rawhide"]),
        ),
    ])

    flexmock(CoprBuildEvent).should_receive("get_package_config").and_return(
        config)
    flexmock(PackageConfigGetter).should_receive(
        "get_package_config_from_repo").and_return(config)
    flexmock(CoprBuildEndHandler).should_receive(
        "was_last_packit_comment_with_congratulation").and_return(False)
    flexmock(GithubProject).should_receive("pr_comment")

    flexmock(LocalProject).should_receive("refresh_the_arguments").and_return(
        None)

    flexmock(CoprBuildModel).should_receive("get_by_build_id").and_return(
        copr_build_pr)
    copr_build_pr.should_receive("set_status").with_args("success")
    copr_build_pr.should_receive("set_end_time").once()
    flexmock(requests).should_receive("get").and_return(requests.Response())
    flexmock(
        requests.Response).should_receive("raise_for_status").and_return(None)
    # check if packit-service set correct PR status
    url = get_copr_build_info_url_from_flask(1)
    flexmock(StatusReporter).should_receive("report").with_args(
        state=CommitStatus.success,
        description="RPMs were built successfully.",
        url=url,
        check_names=EXPECTED_BUILD_CHECK_NAME,
    ).once()

    flexmock(StatusReporter).should_receive("report").with_args(
        state=CommitStatus.pending,
        description="RPMs were built successfully.",
        url=url,
        check_names=EXPECTED_TESTING_FARM_CHECK_NAME,
    ).once()

    pipeline_id = "5e8079d8-f181-41cf-af96-28e99774eb68"
    flexmock(uuid).should_receive("uuid4").and_return(uuid.UUID(pipeline_id))
    payload: dict = {
        "pipeline": {
            "id": pipeline_id
        },
        "api": {
            "token": ""
        },
        "response-url": "https://stg.packit.dev/api/testing-farm/results",
        "artifact": {
            "repo-name": "bar",
            "repo-namespace": "foo",
            "copr-repo-name": "some-owner/some-project",
            "copr-chroot": "fedora-rawhide-x86_64",
            "commit-sha": "0011223344",
            "git-url": "https://github.com/foo/bar.git",
            "git-ref": "0011223344",
        },
    }

    tft_test_run_model = flexmock()
    tft_test_run_model.should_receive("set_status").with_args(
        TestingFarmResult.running).and_return().once()
    flexmock(TFTTestRunModel).should_receive("create").with_args(
        pipeline_id=pipeline_id,
        commit_sha="0011223344",
        status=TestingFarmResult.new,
        target="fedora-rawhide-x86_64",
        trigger_model=copr_build_pr.job_trigger.get_trigger_object(),
        web_url=None,
    ).and_return(tft_test_run_model)

    flexmock(TestingFarmJobHelper).should_receive(
        "send_testing_farm_request").with_args(
            TESTING_FARM_TRIGGER_URL, "POST", {},
            json.dumps(payload)).and_return(
                RequestResponse(
                    status_code=200,
                    ok=True,
                    content=b'{"url": "some-url"}',
                    json={"url": "some-url"},
                ))

    flexmock(StatusReporter).should_receive("report").with_args(
        state=CommitStatus.pending,
        description="Build succeeded. Submitting the tests ...",
        check_names=EXPECTED_TESTING_FARM_CHECK_NAME,
        url="",
    ).once()
    flexmock(StatusReporter).should_receive("report").with_args(
        state=CommitStatus.pending,
        description="Tests are running ...",
        url="some-url",
        check_names=EXPECTED_TESTING_FARM_CHECK_NAME,
    ).once()
    flexmock(Signature).should_receive("apply_async").twice()

    processing_results = SteveJobs().process_message(copr_build_end)
    event_dict, package_config, job = get_parameters_from_results(
        processing_results)

    run_copr_build_end_handler(
        package_config=package_config,
        event=event_dict,
        job_config=job,
    )

    flexmock(GithubTestingFarmHandler).should_receive("db_trigger").and_return(
        copr_build_pr.job_trigger.get_trigger_object())

    run_testing_farm_handler(
        package_config=package_config,
        event=event_dict,
        job_config=job,
        chroot="fedora-rawhide-x86_64",
        build_id=flexmock(),
    )