Пример #1
0
def test_check_and_report(
    allowlist: Allowlist,
    allowlist_entries,
    events: Iterable[Tuple[AbstractGithubEvent, bool, Iterable[str]]],
):
    """
    :param allowlist: fixture
    :param events: fixture: [(Event, should-be-approved)]
    """
    flexmock(
        GithubProject,
        create_check_run=lambda *args, **kwargs: None,
        get_issue=lambda *args, **kwargs: flexmock(comment=lambda *args, **
                                                   kwargs: None),
        get_pr=lambda *args, **kwargs: flexmock(source_project=flexmock(),
                                                author=None,
                                                comment=lambda *args, **kwargs:
                                                None),
    )
    job_configs = [
        JobConfig(
            type=JobType.tests,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(_targets=["fedora-rawhide"]),
        )
    ]
    flexmock(PullRequestGithubEvent).should_receive(
        "get_package_config").and_return(flexmock(jobs=job_configs, ))
    flexmock(PullRequestModel).should_receive("get_or_create").and_return(
        flexmock(
            job_config_trigger_type=JobConfigTriggerType.pull_request,
            id=123,
            job_trigger_model_type=JobTriggerModelType.pull_request,
        ))

    flexmock(JobTriggerModel).should_receive("get_or_create").with_args(
        type=JobTriggerModelType.pull_request, trigger_id=123).and_return(
            flexmock(id=2, type=JobTriggerModelType.pull_request))

    git_project = GithubProject("", GithubService(), "")
    for event, is_valid, resolved_through in events:
        flexmock(GithubProject, can_merge_pr=lambda username: is_valid)
        flexmock(
            event,
            project=git_project).should_receive("get_dict").and_return(None)
        # needs to be included when running only `test_allowlist`
        # flexmock(event).should_receive("db_trigger").and_return(
        #     flexmock(job_config_trigger_type=job_configs[0].trigger).mock()
        # )
        flexmock(EventData).should_receive("from_event_dict").and_return(
            flexmock(commit_sha="0000000", pr_id="0"))

        if isinstance(event, PullRequestGithubEvent) and not is_valid:
            # Report the status
            flexmock(CoprHelper).should_receive("get_copr_client").and_return(
                Client(
                    config={
                        "copr_url": "https://copr.fedorainfracloud.org",
                        "username": "******",
                    }))
            flexmock(LocalProject).should_receive(
                "refresh_the_arguments").and_return(None)
            flexmock(LocalProject).should_receive("checkout_pr").and_return(
                None)
            flexmock(StatusReporter).should_receive("report").with_args(
                description="Namespace is not allowed!",
                state=BaseCommitStatus.neutral,
                url=FAQ_URL,
                check_names=[EXPECTED_TESTING_FARM_CHECK_NAME],
                markdown_content=None,
            ).once()
        flexmock(packit_service.worker.build.copr_build).should_receive(
            "get_valid_build_targets").and_return({
                "fedora-rawhide-x86_64",
            })
        mock_model(allowlist_entries, resolved_through)

        assert (allowlist.check_and_report(
            event,
            git_project,
            service_config=flexmock(
                deployment=Deployment.stg,
                command_handler_work_dir="",
                admins=["admin"],
            ),
            job_configs=job_configs,
        ) is is_valid)
Пример #2
0
def test_copr_build_check_names_multiple_jobs(github_pr_event):
    trigger = flexmock(
        job_config_trigger_type=JobConfigTriggerType.pull_request, id=123)
    flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(
        trigger)
    helper = build_helper(
        event=github_pr_event,
        jobs=[
            # We run only the job it's config is passed to the handler.
            # Other one(s) has to be run by a different handler instance.
            JobConfig(
                type=JobType.copr_build,
                trigger=JobConfigTriggerType.pull_request,
                metadata=JobMetadataConfig(targets=["fedora-rawhide-x86_64"],
                                           owner="nobody"),
                actions={ActionName.post_upstream_clone: "ls /*"},
            ),
            JobConfig(
                type=JobType.copr_build,
                trigger=JobConfigTriggerType.pull_request,
                metadata=JobMetadataConfig(targets=["fedora-32-x86_64"],
                                           owner="nobody"),
                actions={ActionName.post_upstream_clone: 'bash -c "ls /*"'},
            ),
        ],
        db_trigger=trigger,
        selected_job=JobConfig(
            type=JobType.copr_build,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(targets=["fedora-32-x86_64"],
                                       owner="nobody"),
            actions={ActionName.post_upstream_clone: 'bash -c "ls /*"'},
        ),
    )
    # we need to make sure that pr_id is set
    # so we can check it out and add it to spec's release field
    assert helper.metadata.pr_id

    flexmock(copr_build).should_receive(
        "get_copr_build_info_url_from_flask").and_return("https://test.url")
    flexmock(StatusReporter).should_receive("set_status").with_args(
        state=CommitStatus.pending,
        description="Building SRPM ...",
        check_name="packit-stg/rpm-build-fedora-32-x86_64",
        url="",
    ).and_return().once()
    flexmock(StatusReporter).should_receive("set_status").with_args(
        state=CommitStatus.pending,
        description="Starting RPM build...",
        check_name="packit-stg/rpm-build-fedora-32-x86_64",
        url="https://test.url",
    ).and_return().once()

    flexmock(GitProject).should_receive(
        "set_commit_status").and_return().never()
    flexmock(SRPMBuildModel).should_receive("create").and_return(
        SRPMBuildModel(success=True))
    flexmock(CoprBuildModel).should_receive("get_or_create").and_return(
        CoprBuildModel(id=1))
    flexmock(PullRequestGithubEvent).should_receive("db_trigger").and_return(
        flexmock())

    flexmock(PackitAPI).should_receive("create_srpm").and_return("my.srpm")

    # copr build
    flexmock(CoprHelper).should_receive(
        "create_copr_project_if_not_exists").with_args(
            project=
            "git.instance.io-the-example-namespace-the-example-repo-342-stg",
            chroots=["fedora-32-x86_64"],
            owner="nobody",
            description=None,
            instructions=None,
            preserve_project=None,
            list_on_homepage=None,
            additional_repos=[],
            request_admin_if_needed=True,
        ).and_return(None)

    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        flexmock(
            config={"copr_url": "https://copr.fedorainfracloud.org/"},
            build_proxy=flexmock().should_receive(
                "create_from_file").and_return(
                    flexmock(id=2,
                             projectname="the-project-name",
                             ownername="packit")).mock(),
            mock_chroot_proxy=flexmock().should_receive("get_list").and_return(
                {
                    "fedora-32-x86_64": "supported",
                    "__to_be_ignored__": None
                }).mock(),
        ))
    flexmock(Pushgateway).should_receive("push_copr_build_created")

    flexmock(Celery).should_receive("send_task").once()
    assert helper.run_copr_build()["success"]
Пример #3
0
                 type=JobType.propose_downstream,
                 trigger=JobConfigTriggerType.release,
             ),
         ],
         {GitHubIssueCommentProposeUpdateHandler},
         id="config=propose_downstream@trigger=issue_comment",
     ),
     pytest.param(
         TheJobTriggerType.pull_request,
         flexmock(
             job_config_trigger_type=JobConfigTriggerType.pull_request),
         [
             JobConfig(
                 type=JobType.copr_build,
                 trigger=JobConfigTriggerType.pull_request,
                 metadata=JobMetadataConfig(
                     targets=["fedora-rawhide-x86_64"]),
                 actions={ActionName.post_upstream_clone: "ls /*"},
             ),
             JobConfig(
                 type=JobType.copr_build,
                 trigger=JobConfigTriggerType.pull_request,
                 metadata=JobMetadataConfig(targets=["fedora-32-x86_64"]),
                 actions={
                     ActionName.post_upstream_clone: 'bash -c "ls /*"'
                 },
             ),
         ],
         {PullRequestCoprBuildHandler},
         id="config=multiple_copr_builds_for_pr@trigger=pull_request",
     ),
 ],
Пример #4
0
             JobConfig(
                 type=JobType.copr_build,
                 trigger=JobConfigTriggerType.pull_request,
             )
         ],
     ),
     None,
 ),
 (
     PackageConfig(
         specfile_path="xxx",
         jobs=[
             JobConfig(
                 type=JobType.copr_build,
                 trigger=JobConfigTriggerType.pull_request,
                 metadata=JobMetadataConfig(project="example"),
             )
         ],
     ),
     "example",
 ),
 (
     PackageConfig(
         specfile_path="xxx",
         jobs=[
             JobConfig(
                 type=JobType.copr_build,
                 trigger=JobConfigTriggerType.release,
                 metadata=JobMetadataConfig(project="example1"),
             ),
             JobConfig(
def test_copr_build_end_testing_farm(copr_build_end, copr_build_pr):
    flexmock(GithubProject).should_receive("is_private").and_return(False)
    flexmock(TestingFarmJobHelper).should_receive("job_owner").and_return(
        "some-owner")
    flexmock(TestingFarmJobHelper).should_receive("job_project").and_return(
        "foo-bar-123-stg")
    config = PackageConfig(jobs=[
        JobConfig(
            type=JobType.copr_build,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(targets=["fedora-rawhide"]),
        ),
        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/foo-bar-123-stg",
            "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='{"url": "some-url"}'.encode(),
                    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(),
    )
Пример #6
0
             )
         ],
     ),
     None,
     {
         "default_br",
     },
 ),
 (
     PackageConfig(
         specfile_path="xxx",
         jobs=[
             JobConfig(
                 type=JobType.propose_downstream,
                 trigger=JobConfigTriggerType.pull_request,
                 metadata=JobMetadataConfig(dist_git_branches=["file"]),
             )
         ],
     ),
     None,
     {
         "file",
     },
 ),
 (
     PackageConfig(
         specfile_path="xxx",
         jobs=[
             JobConfig(
                 type=JobType.propose_downstream,
                 trigger=JobConfigTriggerType.pull_request,
Пример #7
0
 def make_instance(self, data, **_):
     return JobMetadataConfig(**data)
Пример #8
0
def test_check_and_report(
    whitelist: Whitelist, events: List[Tuple[AbstractGithubEvent, bool]]
):
    """
    :param whitelist: fixture
    :param events: fixture: [(Event, should-be-approved)]
    """
    flexmock(
        GithubProject,
        pr_comment=lambda *args, **kwargs: None,
        set_commit_status=lambda *args, **kwargs: None,
        issue_comment=lambda *args, **kwargs: None,
        get_pr=lambda *args, **kwargs: flexmock(source_project=flexmock()),
    )
    job_configs = [
        JobConfig(
            type=JobType.tests,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(targets=["fedora-rawhide"]),
        )
    ]
    flexmock(PullRequestGithubEvent).should_receive("get_package_config").and_return(
        flexmock(
            jobs=job_configs,
        )
    )

    git_project = GithubProject("", GithubService(), "")
    for event, is_valid in events:
        if isinstance(event, PullRequestGithubEvent) and not is_valid:
            # Report the status
            flexmock(CoprHelper).should_receive("get_copr_client").and_return(
                Client(
                    config={
                        "copr_url": "https://copr.fedorainfracloud.org",
                        "username": "******",
                    }
                )
            )
            flexmock(LocalProject).should_receive("refresh_the_arguments").and_return(
                None
            )
            flexmock(LocalProject).should_receive("checkout_pr").and_return(None)
            flexmock(StatusReporter).should_receive("report").with_args(
                description="Account is not whitelisted!",
                state=CommitStatus.error,
                url=FAQ_URL,
                check_names=[EXPECTED_TESTING_FARM_CHECK_NAME],
            ).once()
        flexmock(packit_service.worker.build.copr_build).should_receive(
            "get_valid_build_targets"
        ).and_return(
            {
                "fedora-rawhide-x86_64",
            }
        )

        # get_account returns the whitelist object if it exists
        # returns nothing if it isn't whitelisted
        # then inside the whitelist.py file, a function checks if the status is
        # one of the approved statuses

        # this exact code is used twice above but mypy has an issue with this one only
        whitelist_mock = flexmock(DBWhitelist).should_receive("get_account")
        if not TYPE_CHECKING:
            if is_valid:
                whitelist_mock.and_return(DBWhitelist(status="approved_manually"))
            else:
                whitelist_mock.and_return(None)

        assert (
            whitelist.check_and_report(
                event,
                git_project,
                service_config=flexmock(
                    deployment=Deployment.stg, command_handler_work_dir=""
                ),
                job_configs=job_configs,
            )
            is is_valid
        )
Пример #9
0
STABLE_VERSIONS = ALIASES["fedora-stable"]
STABLE_CHROOTS = {f"{version}-x86_64" for version in STABLE_VERSIONS}
ONE_CHROOT_SET = {list(STABLE_CHROOTS)[0]}
STABLE_KOJI_TARGETS = {f"f{version[-2:]}" for version in STABLE_VERSIONS}
ONE_KOJI_TARGET_SET = {list(STABLE_KOJI_TARGETS)[0]}


@pytest.mark.parametrize(
    "jobs,trigger,job_config_trigger_type,build_chroots,test_chroots",
    [
        pytest.param(
            [
                JobConfig(
                    type=JobType.copr_build,
                    trigger=JobConfigTriggerType.pull_request,
                    metadata=JobMetadataConfig(targets=STABLE_VERSIONS),
                )
            ],
            TheJobTriggerType.pull_request,
            JobConfigTriggerType.pull_request,
            STABLE_CHROOTS,
            set(),
            id="build_with_targets",
        ),
        pytest.param(
            [
                JobConfig(
                    type=JobType.copr_build,
                    trigger=JobConfigTriggerType.pull_request,
                    metadata=JobMetadataConfig(targets=STABLE_VERSIONS),
                )
Пример #10
0
def test_koji_build_check_names(github_pr_event):
    trigger = flexmock(
        job_config_trigger_type=JobConfigTriggerType.pull_request,
        id=123,
        job_trigger_model_type=JobTriggerModelType.pull_request,
    )
    flexmock(JobTriggerModel).should_receive("get_or_create").with_args(
        type=JobTriggerModelType.pull_request, trigger_id=123).and_return(
            flexmock(id=2, type=JobConfigTriggerType.pull_request))
    flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(
        trigger)
    helper = build_helper(
        event=github_pr_event,
        metadata=JobMetadataConfig(_targets=["bright-future"], scratch=True),
        db_trigger=trigger,
    )
    flexmock(koji_build).should_receive("get_all_koji_targets").and_return(
        ["dark-past", "bright-future"]).once()

    koji_build_url = get_koji_build_info_url(1)
    flexmock(StatusReporter).should_receive("set_status").with_args(
        state=BaseCommitStatus.running,
        description="Building SRPM ...",
        check_name="production-build:bright-future",
        url="",
        links_to_external_services=None,
        markdown_content=None,
    ).and_return()
    flexmock(StatusReporter).should_receive("set_status").with_args(
        state=BaseCommitStatus.running,
        description="Building RPM ...",
        check_name="production-build:bright-future",
        url=koji_build_url,
        links_to_external_services=None,
        markdown_content=None,
    ).and_return()

    flexmock(GitProject).should_receive("get_pr").and_return(
        flexmock(source_project=flexmock()))
    flexmock(GitProject).should_receive(
        "set_commit_status").and_return().never()
    flexmock(SRPMBuildModel).should_receive("create_with_new_run").and_return((
        flexmock(status="success", id=1).should_receive("set_url").with_args(
            "https://some.host/my.srpm").mock().should_receive(
                "set_start_time").mock().should_receive("set_status").mock().
        should_receive("set_logs").mock().should_receive(
            "set_end_time").mock(),
        flexmock(),
    ))
    flexmock(KojiBuildModel).should_receive("create").and_return(
        flexmock(id=1))
    flexmock(PackitAPI).should_receive("create_srpm").and_return("my.srpm")

    # koji build
    flexmock(Upstream).should_receive("koji_build").and_return(
        "Uploading srpm: /python-ogr-0.11.1"
        ".dev21+gf2dec9b-1.20200407142424746041.21.gf2dec9b.fc31.src.rpm\n"
        "[====================================] 100% 00:00:11   1.67 MiB 148.10 KiB/sec\n"
        "Created task: 43429338\n"
        "Task info: https://koji.fedoraproject.org/koji/taskinfo?taskID=43429338\n"
    )

    flexmock(PackitAPI).should_receive("init_kerberos_ticket").once()

    assert helper.run_koji_build()["success"]
def test_copr_build_end_failed_testing_farm_no_json(copr_build_end,
                                                    copr_build_pr):
    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(TestingFarmJobHelper).should_receive("job_owner").and_return(
        "some-owner")
    flexmock(TestingFarmJobHelper).should_receive("job_project").and_return(
        "foo-bar-123-stg")
    config = PackageConfig(jobs=[
        JobConfig(
            type=JobType.copr_build,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(targets=["fedora-rawhide"]),
        ),
        JobConfig(
            type=JobType.tests,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(targets=["fedora-rawhide"]),
        ),
    ])

    flexmock(CoprBuildEvent).should_receive("get_package_config").and_return(
        config)
    flexmock(GithubTestingFarmHandler).should_receive(
        "get_package_config_from_repo").and_return(config)
    flexmock(CoprBuildEndHandler).should_receive(
        "was_last_build_successful").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")
    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)
    # 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="some text error".encode(),
                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()

    trigger = flexmock(
        job_config_trigger_type=JobConfigTriggerType.pull_request,
        job_trigger_model_type=JobTriggerModelType.pull_request,
    )
    flexmock(CoprBuildEvent).should_receive("db_trigger").and_return(trigger)

    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=trigger,
        web_url=None,
    ).and_return(tft_test_run_model)

    steve.process_message(copr_build_end)
Пример #12
0
def test_koji_build_failed(github_pr_event):
    trigger = flexmock(
        job_config_trigger_type=JobConfigTriggerType.pull_request,
        id=123,
        job_trigger_model_type=JobTriggerModelType.pull_request,
    )
    flexmock(JobTriggerModel).should_receive("get_or_create").with_args(
        type=JobTriggerModelType.pull_request, trigger_id=123).and_return(
            flexmock(id=2, type=JobTriggerModelType.pull_request))
    flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(
        trigger)
    helper = build_helper(
        event=github_pr_event,
        metadata=JobMetadataConfig(_targets=["bright-future"], scratch=True),
        db_trigger=trigger,
    )
    flexmock(koji_build).should_receive("get_all_koji_targets").and_return(
        ["dark-past", "bright-future"]).once()

    flexmock(StatusReporter).should_receive("set_status").with_args(
        state=BaseCommitStatus.running,
        description="Building SRPM ...",
        check_name="production-build:bright-future",
        url="",
        links_to_external_services=None,
        markdown_content=None,
    ).and_return()

    srpm_build_url = get_srpm_build_info_url(2)
    flexmock(StatusReporter).should_receive("set_status").with_args(
        state=BaseCommitStatus.error,
        description="Submit of the build failed: some error",
        check_name="production-build:bright-future",
        url=srpm_build_url,
        links_to_external_services=None,
        markdown_content=None,
    ).and_return()

    flexmock(GitProject).should_receive("get_pr").and_return(
        flexmock(source_project=flexmock()))
    flexmock(GitProject).should_receive(
        "set_commit_status").and_return().never()
    flexmock(SRPMBuildModel).should_receive("create_with_new_run").and_return((
        flexmock(status="success", id=2).should_receive("set_url").with_args(
            "https://some.host/my.srpm").mock().should_receive(
                "set_start_time").mock().should_receive("set_status").mock().
        should_receive("set_logs").mock().should_receive(
            "set_end_time").mock(),
        flexmock(),
    ))
    flexmock(KojiBuildModel).should_receive("create").and_return(
        flexmock(id=1))
    flexmock(PackitAPI).should_receive("create_srpm").and_return("my.srpm")

    # koji build
    flexmock(sentry_integration).should_receive(
        "send_to_sentry").and_return().once()
    flexmock(Upstream).should_receive("koji_build").and_raise(
        Exception, "some error")

    result = helper.run_koji_build()
    assert not result["success"]
    assert result["details"]["errors"]
    assert result["details"]["errors"]["bright-future"] == "some error"
Пример #13
0
def test_koji_build_failed_kerberos(github_pr_event):
    trigger = flexmock(
        job_config_trigger_type=JobConfigTriggerType.pull_request,
        id=123,
        job_trigger_model_type=JobTriggerModelType.pull_request,
    )
    flexmock(JobTriggerModel).should_receive("get_or_create").with_args(
        type=JobTriggerModelType.pull_request, trigger_id=123).and_return(
            flexmock(id=2, type=JobTriggerModelType.pull_request))
    flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(
        trigger)
    helper = build_helper(
        event=github_pr_event,
        metadata=JobMetadataConfig(_targets=["bright-future"], scratch=True),
        db_trigger=trigger,
    )
    flexmock(koji_build).should_receive("get_all_koji_targets").and_return(
        ["dark-past", "bright-future"]).never()

    flexmock(StatusReporter).should_receive("set_status").with_args(
        state=BaseCommitStatus.running,
        description="Building SRPM ...",
        check_name="production-build:bright-future",
        url="",
        links_to_external_services=None,
        markdown_content=None,
    ).and_return()
    flexmock(StatusReporter).should_receive("set_status").with_args(
        state=BaseCommitStatus.error,
        description=
        "Kerberos authentication error: the bad authentication error",
        check_name="production-build:bright-future",
        url=get_srpm_build_info_url(1),
        links_to_external_services=None,
        markdown_content=None,
    ).and_return()

    flexmock(GitProject).should_receive("get_pr").and_return(
        flexmock(source_project=flexmock()))
    flexmock(GitProject).should_receive(
        "set_commit_status").and_return().never()
    flexmock(SRPMBuildModel).should_receive("create_with_new_run").and_return((
        flexmock(status="success", id=1).should_receive("set_url").with_args(
            "https://some.host/my.srpm").mock().should_receive(
                "set_start_time").mock().should_receive("set_status").mock().
        should_receive("set_logs").mock().should_receive(
            "set_end_time").mock(),
        flexmock(),
    ))
    flexmock(KojiBuildModel).should_receive("create").and_return(
        flexmock(id=1))
    flexmock(PackitAPI).should_receive("create_srpm").and_return("my.srpm")

    flexmock(PackitAPI).should_receive("init_kerberos_ticket").and_raise(
        PackitCommandFailedError,
        "Command failed",
        stdout_output="",
        stderr_output="the bad authentication error",
    )

    response = helper.run_koji_build()
    assert not response["success"]
    assert ("Kerberos authentication error: the bad authentication error" ==
            response["details"]["msg"])
Пример #14
0
def test_copr_build_check_names_gitlab(gitlab_mr_event):
    trigger = flexmock(
        job_config_trigger_type=JobConfigTriggerType.pull_request, id=123)
    flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(
        trigger)
    helper = build_helper(
        event=gitlab_mr_event,
        metadata=JobMetadataConfig(targets=["bright-future-x86_64"],
                                   owner="nobody"),
        db_trigger=trigger,
    )

    flexmock(copr_build).should_receive(
        "get_copr_build_info_url_from_flask").and_return("https://test.url")
    flexmock(StatusReporter).should_receive("set_status").with_args(
        state=CommitStatus.pending,
        description="Building SRPM ...",
        check_name="packit-stg/rpm-build-bright-future-x86_64",
        url="",
    ).and_return()
    flexmock(StatusReporter).should_receive("set_status").with_args(
        state=CommitStatus.pending,
        description="Starting RPM build...",
        check_name="packit-stg/rpm-build-bright-future-x86_64",
        url="https://test.url",
    ).and_return()

    flexmock(GitProject).should_receive(
        "set_commit_status").and_return().never()
    flexmock(SRPMBuildModel).should_receive("create").and_return(
        SRPMBuildModel(success=True))
    flexmock(CoprBuildModel).should_receive("get_or_create").and_return(
        CoprBuildModel(id=1))
    flexmock(MergeRequestGitlabEvent).should_receive("db_trigger").and_return(
        flexmock())

    flexmock(PackitAPI).should_receive("create_srpm").and_return("my.srpm")

    # copr build
    flexmock(CoprHelper).should_receive(
        "create_copr_project_if_not_exists"
    ).with_args(
        project="git.instance.io-the-example-namespace-the-example-repo-1-stg",
        chroots=["bright-future-x86_64"],
        owner="nobody",
        description=None,
        instructions=None,
        preserve_project=None,
        list_on_homepage=None,
        additional_repos=[],
        request_admin_if_needed=True,
    ).and_return(None)

    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        flexmock(
            config={"copr_url": "https://copr.fedorainfracloud.org/"},
            build_proxy=flexmock().should_receive(
                "create_from_file").and_return(
                    flexmock(id=2,
                             projectname="the-project-name",
                             ownername="nobody")).mock(),
            mock_chroot_proxy=flexmock().should_receive("get_list").and_return(
                {
                    "bright-future-x86_64": ""
                }).mock(),
        ))
    flexmock(Pushgateway).should_receive("push_copr_build_created")

    flexmock(Celery).should_receive("send_task").once()

    assert helper.run_copr_build()["success"]
Пример #15
0
def test_check_copr_build(clean_before_and_after, packit_build_752):
    flexmock(Client).should_receive("create_from_config_file").and_return(
        Client(None))
    flexmock(CoprBuildEvent).should_receive("get_package_config").and_return(
        PackageConfig(jobs=[
            JobConfig(
                type=JobType.copr_build,
                trigger=JobConfigTriggerType.pull_request,
                metadata=JobMetadataConfig(targets=[
                    "fedora-30-x86_64",
                    "fedora-rawhide-x86_64",
                    "fedora-31-x86_64",
                    "fedora-32-x86_64",
                ]),
            )
        ]))
    coprs_response = Munch({
        "chroots": [
            "fedora-30-x86_64",
            "fedora-rawhide-x86_64",
            "fedora-31-x86_64",
            "fedora-32-x86_64",
        ],
        "ended_on":
        1583916564,
        "id":
        1300329,
        "ownername":
        "packit",
        "project_dirname":
        "packit-service-packit-752",
        "projectname":
        "packit-service-packit-752",
        "repo_url": ("https://download.copr.fedorainfracloud.org/"
                     "results/packit/packit-service-packit-752"),
        "source_package": {
            "name":
            "packit",
            "url":
            ("https://download.copr.fedorainfracloud.org/"
             "results/packit/packit-service-packit-752/"
             "srpm-builds/01300329/packit-0.8.2.dev122g64ebb47-1.fc31.src.rpm"
             ),
            "version":
            "0.8.2.dev122+g64ebb47-1.fc31",
        },
        "started_on":
        1583916315,
        "state":
        "succeeded",
        "submitted_on":
        1583916261,
        "submitter":
        "packit",
    })
    flexmock(BuildProxy).should_receive("get").and_return(coprs_response)

    chroot_response = Munch({
        "ended_on":
        1583916564,
        "name":
        "fedora-rawhide-x86_64",
        "result_url":
        "https://download.copr.fedorainfracloud.org/"
        "results/packit/packit-service-packit-752/fedora-rawhide-x86_64/"
        "01300329-packit/",
        "started_on":
        1583916315,
        "state":
        "succeeded",
    })
    flexmock(BuildChrootProxy).should_receive("get").with_args(
        BUILD_ID, "fedora-rawhide-x86_64").and_return(chroot_response)

    # Reporting
    flexmock(GithubProject).should_receive("get_pr").and_return(flexmock())
    flexmock(GithubProject).should_receive("get_pr_comments").and_return([])
    flexmock(GithubProject).should_receive("pr_comment").and_return()
    flexmock(GithubProject).should_receive(
        "set_commit_status").and_return().once()

    check_copr_build(BUILD_ID)
    assert packit_build_752.status == PG_COPR_BUILD_STATUS_SUCCESS
Пример #16
0
def test_payload(
    tf_api,
    tf_token,
    internal_tf_token,
    use_internal_tf,
    ps_deployment,
    repo,
    namespace,
    commit_sha,
    project_url,
    git_ref,
    copr_owner,
    copr_project,
    build_id,
    chroot,
    built_packages,
    distro,
    compose,
    arch,
    packages_to_send,
):
    # Soo many things are happening in a single constructor!!!!
    config = flexmock(
        testing_farm_api_url=tf_api,
        testing_farm_secret=tf_token,
        internal_testing_farm_secret=internal_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),
        target_branch_head_commit="deadbeef",
    )
    project = flexmock(
        repo=repo,
        namespace=namespace,
        service="GitHub",
        get_git_urls=lambda: {"git": f"{project_url}.git"},
        get_pr=lambda id_: pr,
        full_repo_name=f"{namespace}/{repo}",
    )
    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(use_internal_tf=use_internal_tf),
        ),
    )

    token_to_use = internal_tf_token if use_internal_tf else tf_token
    assert job_helper.tft_token == token_to_use

    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)
    artifact = {"id": f"{build_id}:{chroot}", "type": "fedora-copr-build"}

    if packages_to_send:
        artifact["packages"] = packages_to_send

    # URLs shortened for clarity
    log_url = "https://copr-be.cloud.fedoraproject.org/results/.../builder-live.log.gz"
    srpm_url = (
        f"https://download.copr.fedorainfracloud.org/results/.../{repo}-0.1-1.src.rpm"
    )
    copr_build = flexmock(
        id=build_id,
        built_packages=[
            {
                "name": repo,
                "version": "0.1",
                "release": "1",
                "arch": "noarch",
                "epoch": "0",
            }
        ],
        build_logs_url=log_url,
    )
    copr_build.should_receive("get_srpm_build").and_return(flexmock(url=srpm_url))

    payload = job_helper._payload(chroot, artifact, copr_build)

    assert payload["api_key"] == token_to_use
    assert payload["test"]["fmf"] == {
        "url": project_url,
        "ref": commit_sha,
    }

    assert payload["environments"] == [
        {
            "arch": arch,
            "os": {"compose": compose},
            "artifacts": [artifact],
            "tmt": {"context": {"distro": distro, "arch": arch, "trigger": "commit"}},
            "variables": {
                "PACKIT_FULL_REPO_NAME": f"{namespace}/{repo}",
                "PACKIT_COMMIT_SHA": commit_sha,
                "PACKIT_PACKAGE_NVR": f"{repo}-0.1-1",
                "PACKIT_BUILD_LOG_URL": log_url,
                "PACKIT_SRPM_URL": srpm_url,
                "PACKIT_TARGET_SHA": "deadbeef",
            },
        }
    ]
    assert payload["notification"]["webhook"]["url"].endswith("/testing-farm/results")
Пример #17
0
 pytest.param(
     PullRequestCoprBuildHandler,
     flexmock(
         trigger=TheJobTriggerType.commit,
         db_trigger=flexmock(
             job_config_trigger_type=JobConfigTriggerType.commit),
     ),
     [
         JobConfig(type=JobType.tests,
                   trigger=JobConfigTriggerType.pull_request),
         JobConfig(type=JobType.copr_build,
                   trigger=JobConfigTriggerType.pull_request),
         JobConfig(
             type=JobType.copr_build,
             trigger=JobConfigTriggerType.commit,
             metadata=JobMetadataConfig(branch="a"),
         ),
         JobConfig(
             type=JobType.copr_build,
             trigger=JobConfigTriggerType.commit,
             metadata=JobMetadataConfig(branch="b"),
         ),
     ],
     [
         JobConfig(
             type=JobType.copr_build,
             trigger=JobConfigTriggerType.commit,
             metadata=JobMetadataConfig(branch="a"),
         ),
         JobConfig(
             type=JobType.copr_build,
Пример #18
0
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"
    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),
        target_branch_head_commit="deadbeef",
    )
    project = flexmock(
        repo=repo,
        namespace=namespace,
        service="GitHub",
        get_git_urls=lambda: {"git": f"{project_url}.git"},
        get_pr=lambda id_: pr,
        full_repo_name=f"{namespace}/{repo}",
    )
    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)

    build_id = 1
    # URLs shortened for clarity
    log_url = "https://copr-be.cloud.fedoraproject.org/results/.../builder-live.log.gz"
    srpm_url = (
        f"https://download.copr.fedorainfracloud.org/results/.../{repo}-0.1-1.src.rpm"
    )
    copr_build = flexmock(
        id=build_id,
        built_packages=[
            {
                "name": repo,
                "version": "0.1",
                "release": "1",
                "arch": "noarch",
                "epoch": "0",
            }
        ],
        build_logs_url=log_url,
    )
    copr_build.should_receive("get_srpm_build").and_return(flexmock(url=srpm_url))

    payload = job_helper._payload(chroot, build=copr_build)
    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
Пример #19
0
STABLE_VERSIONS = ALIASES["fedora-stable"]
STABLE_CHROOTS = {f"{version}-x86_64" for version in STABLE_VERSIONS}
ONE_CHROOT_SET = {list(STABLE_CHROOTS)[0]}
STABLE_KOJI_TARGETS = {f"f{version[-2:]}" for version in STABLE_VERSIONS}
ONE_KOJI_TARGET_SET = {list(STABLE_KOJI_TARGETS)[0]}


@pytest.mark.parametrize(
    "jobs,job_config_trigger_type,build_chroots,test_chroots",
    [
        pytest.param(
            [
                JobConfig(
                    type=JobType.copr_build,
                    trigger=JobConfigTriggerType.pull_request,
                    metadata=JobMetadataConfig(targets=STABLE_VERSIONS),
                )
            ],
            JobConfigTriggerType.pull_request,
            STABLE_CHROOTS,
            set(),
            id="build_with_targets",
        ),
        pytest.param(
            [
                JobConfig(
                    type=JobType.copr_build,
                    trigger=JobConfigTriggerType.pull_request,
                    metadata=JobMetadataConfig(targets=STABLE_VERSIONS),
                )
            ],
Пример #20
0
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")).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: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",
            },
        }],
        "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_owner_project_target_commit").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,
    )
Пример #21
0
def test_job_config_validate(raw, is_valid):
    if is_valid:
        JobConfig.get_from_dict(raw)
    else:
        with pytest.raises(ValidationError):
            JobConfig.get_from_dict(raw)


@pytest.mark.parametrize(
    "raw,expected_config",
    [
        (get_job_config_dict_simple(), get_job_config_simple()),
        (get_job_config_dict_full(), get_job_config_full()),
        (
            get_job_config_dict_simple(),
            get_job_config_simple(metadata=JobMetadataConfig(preserve_project=False)),
        ),
        (
            get_job_config_dict_simple(metadata={"preserve_project": False}),
            get_job_config_simple(metadata=JobMetadataConfig(preserve_project=False)),
        ),
        (
            get_job_config_dict_simple(metadata={"preserve_project": True}),
            get_job_config_simple(metadata=JobMetadataConfig(preserve_project=True)),
        ),
    ],
)
def test_job_config_parse(raw, expected_config):
    job_config = JobConfig.get_from_dict(raw_dict=raw)
    assert job_config == expected_config
Пример #22
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)
    flexmock(GithubProject).should_receive("get_pr").and_return(
        flexmock(source_project=flexmock(
            get_web_url=lambda: "abc")).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(),
    )
Пример #23
0
from packit.config import PackageConfig, JobConfig, JobType, JobConfigTriggerType
from packit.config.job_config import JobMetadataConfig
from packit_service.service.events import TheJobTriggerType
from packit_service.worker.build.copr_build import CoprBuildJobHelper


@pytest.mark.parametrize(
    "jobs,trigger,job_config_trigger_type,build_targets,test_targets",
    [
        pytest.param(
            [
                JobConfig(
                    type=JobType.copr_build,
                    trigger=JobConfigTriggerType.pull_request,
                    metadata=JobMetadataConfig(
                        targets=["fedora-29", "fedora-31"]),
                )
            ],
            TheJobTriggerType.pull_request,
            JobConfigTriggerType.pull_request,
            {"fedora-29-x86_64", "fedora-31-x86_64"},
            set(),
            id="build_with_targets",
        ),
        pytest.param(
            [
                JobConfig(
                    type=JobType.copr_build,
                    trigger=JobConfigTriggerType.pull_request,
                    metadata=JobMetadataConfig(
                        targets=["fedora-29", "fedora-31"]),
Пример #24
0
def get_job_config_full(**kwargs):
    """ pass kwargs to JobConfig constructor """
    return JobConfig(type=JobType.propose_downstream,
                     trigger=JobConfigTriggerType.pull_request,
                     metadata=JobMetadataConfig(dist_git_branches=["master"]),
                     **kwargs)
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(TestingFarmJobHelper).should_receive("job_owner").and_return(
        "some-owner")
    flexmock(TestingFarmJobHelper).should_receive("job_project").and_return(
        "foo-bar-123-stg")
    config = PackageConfig(jobs=[
        JobConfig(
            type=JobType.copr_build,
            trigger=JobConfigTriggerType.pull_request,
            metadata=JobMetadataConfig(targets=["fedora-rawhide"]),
        ),
        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="some text error".encode(),
                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(),
    )
Пример #26
0
def test_copr_build_check_names_invalid_chroots(github_pr_event):
    build_targets = [
        "bright-future-x86_64",
        "even-brighter-one-aarch64",
        "fedora-32-x86_64",
    ]

    trigger = flexmock(
        job_config_trigger_type=JobConfigTriggerType.pull_request, id=123)
    flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(
        trigger)
    helper = build_helper(
        event=github_pr_event,
        metadata=JobMetadataConfig(targets=build_targets, owner="packit"),
        db_trigger=trigger,
    )
    # we need to make sure that pr_id is set
    # so we can check it out and add it to spec's release field
    assert helper.metadata.pr_id

    flexmock(copr_build).should_receive(
        "get_copr_build_info_url_from_flask").and_return("https://test.url")
    flexmock(copr_build).should_receive(
        "get_srpm_log_url_from_flask").and_return("https://test.url")

    for target in build_targets:
        flexmock(StatusReporter).should_receive("set_status").with_args(
            state=CommitStatus.pending,
            description="Building SRPM ...",
            check_name=f"packit-stg/rpm-build-{target}",
            url="",
        ).and_return()

    for not_supported_target in ("bright-future-x86_64", "fedora-32-x86_64"):
        flexmock(StatusReporter).should_receive("set_status").with_args(
            state=CommitStatus.error,
            description=f"Not supported target: {not_supported_target}",
            check_name=f"packit-stg/rpm-build-{not_supported_target}",
            url="https://test.url",
        ).and_return()

    flexmock(StatusReporter).should_receive("set_status").with_args(
        state=CommitStatus.pending,
        description="Starting RPM build...",
        check_name="packit-stg/rpm-build-even-brighter-one-aarch64",
        url="https://test.url",
    ).and_return()

    flexmock(GitProject).should_receive(
        "set_commit_status").and_return().never()
    flexmock(GitProject).should_receive("pr_comment").with_args(
        pr_id=342,
        body="There are build targets that are not supported by COPR.\n"
        "<details>\n<summary>Unprocessed build targets</summary>\n\n"
        "```\n"
        "bright-future-x86_64\n"
        "fedora-32-x86_64\n"
        "```\n</details>\n<details>\n"
        "<summary>Available build targets</summary>\n\n"
        "```\n"
        "even-brighter-one-aarch64\n"
        "not-so-bright-future-x86_64\n"
        "```\n</details>",
    ).and_return()
    flexmock(SRPMBuildModel).should_receive("create").and_return(
        SRPMBuildModel(success=True))
    flexmock(CoprBuildModel).should_receive("get_or_create").and_return(
        CoprBuildModel(id=1))
    flexmock(PullRequestGithubEvent).should_receive("db_trigger").and_return(
        flexmock())

    flexmock(PackitAPI).should_receive("create_srpm").and_return("my.srpm")

    # copr build
    flexmock(CoprHelper).should_receive(
        "create_copr_project_if_not_exists").and_return(None)

    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        flexmock(
            config={"copr_url": "https://copr.fedorainfracloud.org/"},
            build_proxy=flexmock().should_receive(
                "create_from_file").and_return(
                    flexmock(id=2,
                             projectname="the-project-name",
                             ownername="packit")).mock(),
            mock_chroot_proxy=flexmock().should_receive("get_list").and_return(
                {
                    "__response__": 200,
                    "not-so-bright-future-x86_64": "",
                    "even-brighter-one-aarch64": "",
                    "__proxy__": "something",
                }).mock(),
        ))

    flexmock(Pushgateway).should_receive("push_copr_build_created")

    flexmock(Celery).should_receive("send_task").once()
    assert helper.run_copr_build()["success"]