def test_copr_build_start(copr_build_start, pc_build_pr, 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(CoprBuildEvent).should_receive("get_package_config").and_return(
        pc_build_pr)
    flexmock(CoprBuildJobHelper).should_receive("get_build_check").and_return(
        EXPECTED_BUILD_CHECK_NAME)

    flexmock(CoprBuildModel).should_receive("get_by_build_id").and_return(
        copr_build_pr)
    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)

    copr_build_pr.should_receive("set_status").with_args("pending").once()
    copr_build_pr.should_receive("set_build_logs_url")

    # check if packit-service set correct PR status
    flexmock(StatusReporter).should_receive("report").with_args(
        state=CommitStatus.pending,
        description="RPM build has started...",
        url=url,
        check_names=EXPECTED_BUILD_CHECK_NAME,
    ).once()

    steve.process_message(copr_build_start)
예제 #2
0
def test_copr_build_existing_project_munch_additional_repos_change(
        cwd_upstream_or_distgit, api_instance):
    u, d, api = api_instance
    owner = "the-owner"
    project = "project-name"
    chroots = ["fedora-rawhide-x86_64"]

    flexmock(ProjectProxy).should_receive("get").and_return(
        munchify({
            "additional_repos": [],
            "auto_prune":
            True,
            "chroot_repos": {
                "fedora-rawhide-x86_64":
                "https://download.copr.fedorainfracloud.org/"
                "results/packit/packit-hello-world-127-stg/fedora-rawhide-x86_64/",
            },
            "contact":
            "https://github.com/packit/packit/issues",
            "description":
            "Continuous builds initiated by packit service.\n"
            "For more info check out https://packit.dev/",
            "devel_mode":
            False,
            "enable_net":
            True,
            "full_name":
            "packit/packit-hello-world-127-stg",
            "homepage":
            "",
            "id":
            34245,
            "ownername":
            owner,
        }))

    flexmock(ProjectProxy).should_receive("edit").and_return().once()

    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        Client(config={"copr_url": "https://copr.fedorainfracloud.org"}))

    build = flexmock(
        id="1",
        ownername=owner,
        projectname=project,
    )
    flexmock(BuildProxy).should_receive("create_from_file").and_return(build)
    build_id, url = api.run_copr_build(
        project=project,
        chroots=chroots,
        owner=owner,
        description=None,
        instructions=None,
        list_on_homepage=False,
        preserve_project=False,
        additional_repos=["new-repo"],
    )

    assert build_id == "1"
    assert url == f"https://copr.fedorainfracloud.org/coprs/build/{build.id}/"
예제 #3
0
def main(srpm, copr_conf=DEFAULT_COPR_CONF, project=DEFAULT_PROJECT):
    """Query COPR info."""
    if not os.path.isfile(srpm):
        print("Error: The given SRPM is not a file:\n%s" % srpm)
        sys.exit(1)

    client = Client.create_from_config_file(copr_conf)

    print(datetime.datetime.now())
    try:
        build_id = launch_build(client, project, srpm)
    except Exception as e:
        mention_expiration_on_creds(copr_conf)
        raise e

    # 2018-03-05: adding sleep to let builds ramp up
    # this after a {'state': ['Not a valid choice.']} exception
    reps = 6
    for naptime in [5] * reps + [10] * reps + [30] * reps:
        time.sleep(naptime)
        tasks = get_build_tasks(client, build_id)
        if tasks:
            break

    check_build_status(client, build_id, tasks)
    check_test_chroot(tasks)
    print()
    print(datetime.datetime.now())
예제 #4
0
def test_copr_build_existing_project(cwd_upstream_or_distgit, api_instance):
    u, d, api = api_instance
    owner = "the-owner"
    project = "project-name"

    flexmock(ProjectProxy).should_receive("get").and_return(
        flexmock(chroot_repos=flexmock(
            keys=lambda: {"fedora-rawhide-x86_64"})))
    flexmock(ProjectProxy).should_receive("edit").and_return(None)

    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        Client(config={"copr_url": "https://copr.fedorainfracloud.org"}))

    build = flexmock(id="1", ownername=owner, projectname=project)
    flexmock(BuildProxy).should_receive("create_from_file").and_return(build)
    build_id, url = api.run_copr_build(
        project=project,
        chroots="fedora-rawhide-x86_64",
        owner=owner,
        description="some description",
        instructions="the instructions",
    )

    assert build_id == "1"
    assert url == ("https://copr.fedorainfracloud.org/coprs/"
                   f"{build.ownername}/{build.projectname}/build/{build.id}/")
예제 #5
0
    def get_copr_builds(self, number_of_builds: int = 5) -> List:
        """
        Get the copr builds of this project done by packit.
        :return: list of builds
        """
        client = CoprClient.create_from_config_file()

        projects = [
            project.name
            for project in reversed(client.project_proxy.get_list(ownername="packit"))
            if project.name.startswith(
                f"{self.upstream_local_project.namespace}-{self.upstream_local_project.repo_name}-"
            )
        ][:5]

        builds: List = []
        for project in projects:
            builds += client.build_proxy.get_list(
                ownername="packit", projectname=project
            )

        logger.debug("Copr builds fetched.")
        return [(build.id, build.projectname, build.state) for build in builds][
            :number_of_builds
        ]
예제 #6
0
def main():
    config_file = os.environ.get("BACKEND_CONFIG", "/etc/copr/copr-be.conf")
    config = BackendConfigReader(config_file).read()
    client = Client({"copr_url": config.frontend_base_url})

    for ownername in os.listdir(config.destdir):
        ownerpath = os.path.join(config.destdir, ownername)

        for projectname in os.listdir(ownerpath):
            projectpath = os.path.join(ownerpath, projectname)

            # It may be a good idea, to not DoS attack the frontend
            # Set whatever number of seconds is necessary
            time.sleep(0)

            # If a project doesn't exist in frontend, it should be removed
            try:
                client.project_proxy.get(ownername=ownername,
                                         projectname=projectname)
            except CoprNoResultException:
                print(projectpath)
                continue

            # If a chroot is not enabled in the project, it should be removed
            for chroot in os.listdir(projectpath):
                if chroot in ["srpm-builds", "modules"]:
                    continue
                if not is_outdated_to_be_deleted(
                        get_chroot_safe(client, ownername, projectname,
                                        chroot)):
                    continue
                print(os.path.join(projectpath, chroot))
예제 #7
0
def test_copr_build_non_existing_project(cwd_upstream_or_distgit,
                                         api_instance):
    u, d, api = api_instance
    owner = "the-owner"
    project = "project-name"

    flexmock(ProjectProxy).should_receive("get").and_raise(
        CoprNoResultException, "project not found")
    flexmock(ProjectProxy).should_receive("edit").and_return(None).times(0)
    flexmock(ProjectProxy).should_receive("add").and_return(None)

    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        Client(config={
            "copr_url": "https://copr.fedorainfracloud.org",
            "username": owner
        }))

    build = flexmock(id="1", ownername=owner, projectname=project)
    flexmock(BuildProxy).should_receive("create_from_file").and_return(build)
    build_id, url = api.run_copr_build(
        project=project,
        chroots="fedora-rawhide-x86_64",
        owner=owner,
        description="some description",
        instructions="the instructions",
    )

    assert build_id == "1"
    assert url == f"https://copr.fedorainfracloud.org/coprs/build/{build.id}/"
예제 #8
0
def test_copr_build_just_tests_defined(copr_build_start, pc_tests, copr_build):
    steve = SteveJobs()
    flexmock(SteveJobs, _is_private=False)
    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        Client(
            config={
                "copr_url": "https://copr.fedorainfracloud.org",
                "username": "******",
            }))
    flexmock(CoprBuildJobHelper).should_receive("copr_build_model").and_return(
        flexmock())
    flexmock(CoprBuildEvent).should_receive("get_package_config").and_return(
        pc_tests)
    flexmock(TestingFarmJobHelper).should_receive(
        "get_build_check").and_return(EXPECTED_BUILD_CHECK_NAME)
    flexmock(TestingFarmJobHelper).should_receive("get_test_check").and_return(
        EXPECTED_TESTING_FARM_CHECK_NAME)

    flexmock(CoprBuild).should_receive("get_by_build_id").and_return(
        copr_build)
    flexmock(CoprBuildDB).should_receive("get_build").and_return({
        "commit_sha":
        "XXXXX",
        "pr_id":
        24,
        "repo_name":
        "hello-world",
        "repo_namespace":
        "packit-service",
        "ref":
        "XXXX",
        "https_url":
        "https://github.com/packit-service/hello-world",
    })

    url = get_log_url(1)
    flexmock(requests).should_receive("get").and_return(requests.Response())
    flexmock(
        requests.Response).should_receive("raise_for_status").and_return(None)

    flexmock(CoprBuild).should_receive("set_status").with_args("pending")
    flexmock(CoprBuild).should_receive("set_build_logs_url")
    # check if packit-service sets the correct PR status
    flexmock(StatusReporter).should_receive("report").with_args(
        state=CommitStatus.pending,
        description="RPM build has started...",
        url=url,
        check_names=EXPECTED_BUILD_CHECK_NAME,
    ).never()

    flexmock(StatusReporter).should_receive("report").with_args(
        state=CommitStatus.pending,
        description="RPM build has started...",
        url=url,
        check_names=TestingFarmJobHelper.get_test_check(
            copr_build_start["chroot"]),
    ).once()

    steve.process_message(copr_build_start)
예제 #9
0
def check_copr_build(build_id: int) -> bool:
    """
    Check the copr_build with given id and refresh the status if needed.

    Used in the babysit task.

    :param build_id: id of the copr_build (CoprBuildModel.build.id)
    :return: True if in case of successful run, False when we need to retry
    """
    logger.debug(f"Getting copr build ID {build_id} from DB.")
    builds = CoprBuildModel.get_all_by_build_id(build_id)
    if not builds:
        logger.warning(f"Copr build {build_id} not in DB.")
        return True

    copr_client = CoprClient.create_from_config_file()
    build_copr = copr_client.build_proxy.get(build_id)

    if not build_copr.ended_on:
        logger.info("The copr build is still in progress.")
        return False

    logger.info(f"The status is {build_copr.state!r}.")

    for build in builds:
        if build.status != "pending":
            logger.info(f"DB state says {build.status!r}, "
                        "things were taken care of already, skipping.")
            continue
        chroot_build = copr_client.build_chroot_proxy.get(
            build_id, build.target)
        event = CoprBuildEvent(
            topic=FedmsgTopic.copr_build_finished.value,
            build_id=build_id,
            build=build,
            chroot=build.target,
            status=(COPR_API_SUCC_STATE if chroot_build.state
                    == COPR_SUCC_STATE else COPR_API_FAIL_STATE),
            owner=build.owner,
            project_name=build.project_name,
            pkg=build_copr.source_package.get(
                "name", ""),  # this seems to be the SRPM name
            timestamp=chroot_build.ended_on,
        )

        job_configs = get_config_for_handler_kls(
            handler_kls=CoprBuildEndHandler,
            event=event,
            package_config=event.get_package_config(),
        )

        for job_config in job_configs:
            CoprBuildEndHandler(
                package_config=event.package_config,
                job_config=job_config,
                data=EventData.from_event_dict(event.get_dict()),
                copr_event=event,
            ).run()
    return True
예제 #10
0
 def get_client(cls):
     try:
         client = Client.create_from_config_file()
     except (CoprNoConfigException, CoprConfigException) as e:
         raise RebaseHelperError(
             'Missing or invalid copr configuration file') from e
     else:
         return client
예제 #11
0
def test_copr_build_not_comment_on_success(copr_build_end, pc_build_pr,
                                           copr_build):
    steve = SteveJobs()
    flexmock(SteveJobs, _is_private=False)
    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        Client(
            config={
                "copr_url": "https://copr.fedorainfracloud.org",
                "username": "******",
            }))
    flexmock(CoprBuildJobHelper).should_receive("copr_build_model").and_return(
        flexmock())
    flexmock(CoprBuildEvent).should_receive("get_package_config").and_return(
        pc_build_pr)
    flexmock(CoprBuildJobHelper).should_receive("get_build_check").and_return(
        EXPECTED_BUILD_CHECK_NAME)

    flexmock(CoprBuildEndHandler).should_receive(
        "was_last_build_successful").and_return(True)
    flexmock(GithubProject).should_receive("pr_comment").never()

    flexmock(CoprBuild).should_receive("get_by_build_id").and_return(
        copr_build)
    flexmock(CoprBuild).should_receive("set_status").with_args("success")
    flexmock(CoprBuildDB).should_receive("get_build").and_return({
        "commit_sha":
        "XXXXX",
        "pr_id":
        24,
        "repo_name":
        "hello-world",
        "repo_namespace":
        "packit-service",
        "ref":
        "XXXX",
        "https_url":
        "https://github.com/packit-service/hello-world",
    })

    url = get_log_url(1)
    flexmock(requests).should_receive("get").and_return(requests.Response())
    flexmock(
        requests.Response).should_receive("raise_for_status").and_return(None)

    # 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=CoprBuildJobHelper.get_build_check(
            copr_build_end["chroot"]),
    ).once()

    # skip testing farm
    flexmock(CoprBuildJobHelper).should_receive("job_tests").and_return(None)

    steve.process_message(copr_build_end)
예제 #12
0
    def get_available_chroots() -> list:
        """
        Gets available copr chroots. Uses cache to avoid repetitive url fetching.

        :return: list of valid chroots
        """

        client = CoprClient.create_from_config_file()
        return list(
            filter(
                lambda chroot: not chroot.startswith("_"),
                client.mock_chroot_proxy.get_list().keys(),
            ))
예제 #13
0
def test_copr_build_no_owner(cwd_upstream_or_distgit, api_instance):
    u, d, api = api_instance
    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        Client(config={"copr_url": "https://copr.fedorainfracloud.org"}))
    with pytest.raises(PackitCoprException) as ex:
        api.run_copr_build(
            project="project-name",
            chroots="fedora-rawhide-x86_64",
            owner=None,
            description="some description",
            instructions="the instructions",
        )
    assert "owner not set" in str(ex)
예제 #14
0
def test_copr_build_existing_project_error_on_change_settings(
    cwd_upstream_or_distgit, api_instance
):
    u, d, api = api_instance
    owner = "the-owner"
    project = "project-name"
    description = "some description"
    instructions = "the instructions"
    chroots = ["fedora-rawhide-x86_64"]

    flexmock(ProjectProxy).should_receive("get").and_return(
        flexmock(
            chroot_repos=flexmock(keys=lambda: chroots),
            description=description,
            instructions=instructions,
            unlisted_on_hp=True,
            delete_after_days=60,
            additional_repos=[],
        )
    )

    flexmock(ProjectProxy).should_receive("request_permissions").with_args(
        ownername=owner, projectname=project, permissions={"admin": True}
    ).and_return()

    flexmock(ProjectProxy).should_receive("edit").and_raise(
        CoprRequestException, "Only owners and admins may update their projects."
    ).once()

    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        Client(config={"copr_url": "https://copr.fedorainfracloud.org"})
    )

    build = flexmock(
        id="1",
        ownername=owner,
        projectname=project,
    )
    flexmock(BuildProxy).should_receive("create_from_file").and_return(build)

    with pytest.raises(PackitCoprSettingsException) as e_info:
        api.run_copr_build(
            project=project,
            chroots=chroots,
            owner=owner,
            description="different description",
            instructions=instructions,
        )
    assert e_info.value.fields_to_change == {
        "description": ("some description", "different description")
    }
예제 #15
0
def test_copr_build_start(copr_build_start, pc_build, copr_build):
    steve = SteveJobs()
    flexmock(SteveJobs, _is_private=False)
    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        Client(
            config={
                "copr_url": "https://copr.fedorainfracloud.org",
                "username": "******",
            }))
    flexmock(CoprBuildJobHelper).should_receive("copr_build_model").and_return(
        flexmock())
    flexmock(CoprBuildEvent).should_receive("get_package_config").and_return(
        pc_build)
    flexmock(PRCheckName).should_receive("get_build_check").and_return(
        PACKIT_STG_CHECK)

    flexmock(CoprBuild).should_receive("get_by_build_id").and_return(
        copr_build)
    flexmock(CoprBuildDB).should_receive("get_build").and_return({
        "commit_sha":
        "XXXXX",
        "pr_id":
        24,
        "repo_name":
        "hello-world",
        "repo_namespace":
        "packit-service",
        "ref":
        "XXXX",
        "https_url":
        "https://github.com/packit-service/hello-world",
    })

    url = get_log_url(1)
    flexmock(requests).should_receive("get").and_return(requests.Response())
    flexmock(
        requests.Response).should_receive("raise_for_status").and_return(None)

    flexmock(CoprBuild).should_receive("set_status").with_args(
        "pending").once()
    flexmock(CoprBuild).should_receive("set_build_logs_url")
    # check if packit-service set correct PR status
    flexmock(BuildStatusReporter).should_receive("report").with_args(
        state="pending",
        description="RPM build has started...",
        url=url,
        check_names=PACKIT_STG_CHECK,
    ).once()

    steve.process_message(copr_build_start)
예제 #16
0
def test_babysit_copr_build(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())
    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)

    babysit_copr_build(BUILD_ID)
    assert packit_build_752.status == PG_COPR_BUILD_STATUS_SUCCESS
예제 #17
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,
    )
    flexmock(PullRequestEvent).should_receive("get_package_config").and_return(
        flexmock(jobs=[
            JobConfig(
                job=JobType.tests,
                trigger=JobTriggerType.pull_request,
                metadata={"targets": ["fedora-rawhide"]},
            )
        ], ))

    git_project = GithubProject("", GithubService(), "")
    for event, is_valid in events:
        if isinstance(event, PullRequestEvent) 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(Model).should_receive("save").and_return(None)
            flexmock(StatusReporter).should_receive("report").with_args(
                description="Account is not whitelisted!",
                state="error",
                url=FAQ_URL,
                check_names=[EXPECTED_TESTING_FARM_CHECK_NAME],
            ).once()
        assert (whitelist.check_and_report(
            event,
            git_project,
            config=flexmock(deployment=Deployment.stg,
                            command_handler_work_dir=""),
        ) is is_valid)
예제 #18
0
def babysit_copr_build(self, build_id: int):
    """ check status of a copr build and update it in DB """
    logger.debug(f"getting copr build ID {build_id} from DB")
    builds = CoprBuild.get_all_by_build_id(build_id)
    if builds:
        copr_client = CoprClient.create_from_config_file()
        build_copr = copr_client.build_proxy.get(build_id)

        if not build_copr.ended_on:
            logger.info("The copr build is still in progress")
            self.retry()
        logger.info(f"The status is {build_copr.state}")

        # copr doesn't tell status of how a build in the chroot went:
        #   https://bugzilla.redhat.com/show_bug.cgi?id=1813227
        for build in builds:
            if build.status != "pending":
                logger.info(
                    f"DB state says {build.status}, "
                    "things were taken care of already, skipping."
                )
                continue
            event = CoprBuildEvent(
                topic=FedmsgTopic.copr_build_finished.value,
                build_id=build_id,
                build={},
                chroot=build.target,
                status=(
                    COPR_API_SUCC_STATE
                    if build_copr.state == COPR_SUCC_STATE
                    else COPR_API_FAIL_STATE
                ),
                owner=build.owner,
                project_name=build.project_name,
                pkg=build_copr.source_package.get(
                    "name", ""
                ),  # this seems to be the SRPM name
                build_pg=build,
            )
            CoprBuildEndHandler(
                ServiceConfig.get_service_config(), job_config=None, event=event
            ).run()
    else:
        logger.warning(f"Copr build {build_id} not in DB.")
예제 #19
0
def test_copr_build_existing_project(cwd_upstream_or_distgit, api_instance):
    u, d, api = api_instance
    owner = "the-owner"
    project = "project-name"
    description = "some description"
    instructions = "the instructions"
    chroots = ["fedora-rawhide-x86_64"]

    flexmock(ProjectProxy).should_receive("get").and_return(
        flexmock(
            chroot_repos=flexmock(keys=lambda: chroots),
            description=description,
            instructions=instructions,
            unlisted_on_hp=True,
            delete_after_days=60,
            additional_repos=[],
        )
    )

    # no change in settings => no edit
    flexmock(ProjectProxy).should_receive("edit").times(0)

    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        Client(config={"copr_url": "https://copr.fedorainfracloud.org"})
    )

    build = flexmock(
        id="1",
        ownername=owner,
        projectname=project,
    )
    flexmock(BuildProxy).should_receive("create_from_file").and_return(build)
    build_id, url = api.run_copr_build(
        project=project,
        owner=owner,
        chroots=chroots,
        description=description,
        instructions=instructions,
    )

    assert build_id == "1"
    assert url == f"https://copr.fedorainfracloud.org/coprs/build/{build.id}/"
예제 #20
0
def trigger_build(args):
    config = {
        "copr_url": args.copr_url,
        "login": args.login,
        "token": args.token,
        "username": args.username,
    }
    client = Client(config)

    print(f"Changing version of the package to {args.version}...")
    source_dict = {
        "pypi_package_name": args.package,
        "pypi_package_version": args.version,
        "spec_template": "fedora",
        "python_versions": [3],
    }
    client.package_proxy.edit(args.owner, args.project, args.package, source_type="pypi", source_dict=source_dict)

    print("Scheduling new build...")
    client.package_proxy.build(args.owner, args.project, args.package)
def test_copr_build_end(copr_build_end, pc_build_pr, copr_build_pr,
                        pc_comment_pr_succ, pr_comment_called):
    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": "******",
            }))
    pc_build_pr.notifications.pull_request.successful_build = pc_comment_pr_succ
    flexmock(CoprBuildEvent).should_receive("get_package_config").and_return(
        pc_build_pr)
    flexmock(CoprBuildEndHandler).should_receive(
        "was_last_build_successful").and_return(False)
    if pr_comment_called:
        flexmock(GithubProject).should_receive("pr_comment")
    else:
        flexmock(GithubProject).should_receive("pr_comment").never()

    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=CoprBuildJobHelper.get_build_check(
            copr_build_end["chroot"]),
    ).once()

    # skip testing farm
    flexmock(CoprBuildJobHelper).should_receive("job_tests").and_return(None)

    steve.process_message(copr_build_end)
예제 #22
0
def main():
    config_file = os.environ.get("BACKEND_CONFIG", "/etc/copr/copr-be.conf")
    config = BackendConfigReader(config_file).read()
    client = Client({"copr_url": config.frontend_base_url})

    for ownername in os.listdir(config.destdir):
        ownerpath = os.path.join(config.destdir, ownername)

        try:
            for projectname in os.listdir(ownerpath):
                projectpath = os.path.join(ownerpath, projectname)

                # I don't know how to determine whether a PR dir can be deleted or not
                # just ommit the logic for the time being.
                if ":pr:" in projectname:
                    continue

                # It may be a good idea, to not DoS attack the frontend
                # Set whatever number of seconds is necessary
                time.sleep(0)

                # If a project doesn't exist in frontend, it should be removed
                try:
                    client.project_proxy.get(ownername=ownername, projectname=projectname)
                except CoprNoResultException:
                    print(projectpath)
                    continue

                # If a chroot is not enabled in the project, it should be removed
                for chroot in os.listdir(projectpath):
                    if chroot in ["srpm-builds", "modules", "repodata", "pubkey.gpg"]:
                        continue
                    if not is_outdated_to_be_deleted(get_chroot_safe(client, ownername, projectname, chroot)):
                        continue
                    print(os.path.join(projectpath, chroot))
        except NotADirectoryError as ex:
            print(str(ex))
예제 #23
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,
    )
    flexmock(PullRequestGithubEvent).should_receive(
        "get_package_config").and_return(
            flexmock(jobs=[
                JobConfig(
                    type=JobType.tests,
                    trigger=JobConfigTriggerType.pull_request,
                    metadata=JobMetadataConfig(targets=["fedora-rawhide"]),
                )
            ], ))

    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()

        # 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,
            config=flexmock(deployment=Deployment.stg,
                            command_handler_work_dir=""),
        ) is is_valid)
예제 #24
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
예제 #25
0
 def get_copr_client(self) -> CoprClient:
     """Not static because of the flex-mocking."""
     return CoprClient.create_from_config_file()
예제 #26
0
파일: api.py 프로젝트: dustymabe/packit
 def copr(self):
     if self._copr is None:
         self._copr = CoprClient.create_from_config_file()
     return self._copr
예제 #27
0
def test_copr_build_existing_project_change_settings(cwd_upstream_or_distgit,
                                                     api_instance):
    u, d, api = api_instance
    owner = "the-owner"
    project = "project-name"
    description = "some description"
    instructions = "the instructions"
    chroots = ["fedora-rawhide-x86_64"]

    flexmock(ProjectProxy).should_receive("get").and_return(
        flexmock(
            chroot_repos=flexmock(keys=lambda: chroots),
            description=description,
            instructions=instructions,
            unlisted_on_hp=True,
            delete_after_days=60,
            additional_repos=[],
        ))

    flexmock(ProjectProxy).should_receive(
        "edit"
        # ).with_args(
        # ownername="the-owner",
        # projectname="project-name",
        # chroots=["fedora-rawhide-x86_64"],
        # description="different description",
        # instructions="the instructions",
        # unlisted_on_hp=True,
        # additional_repos=None,
        # delete_after_days=60,
        #
        # Does not work:
        # flexmock.MethodSignatureError:
        # edit(
        #    <copr.v3.proxies.project.ProjectProxy object at 0x7fa53af2f3d0>,
        #    ownername="the-owner",
        #    projectname="project-name",
        #    chroots=["fedora-rawhide-x86_64"],
        #    description="different description",
        #    instructions="the instructions",
        #    unlisted_on_hp=True,
        #    additional_repos=None,
        #    delete_after_days=60,
        # )
    ).and_return().once()

    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        Client(config={"copr_url": "https://copr.fedorainfracloud.org"}))

    build = flexmock(
        id="1",
        ownername=owner,
        projectname=project,
    )
    flexmock(BuildProxy).should_receive("create_from_file").and_return(build)
    build_id, url = api.run_copr_build(
        project=project,
        chroots=chroots,
        owner=owner,
        description="different description",
        instructions=instructions,
    )

    assert build_id == "1"
    assert url == f"https://copr.fedorainfracloud.org/coprs/build/{build.id}/"
예제 #28
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(AbstractCoprBuildEvent).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)

    copr_response_built_packages = Munch({
        "packages": [
            {
                "arch": "noarch",
                "epoch": 0,
                "name": "python3-packit",
                "release": "1.20210930124525726166.main.0.g0b7b36b.fc36",
                "version": "0.38.0",
            },
            {
                "arch": "src",
                "epoch": 0,
                "name": "packit",
                "release": "1.20210930124525726166.main.0.g0b7b36b.fc36",
                "version": "0.38.0",
            },
            {
                "arch": "noarch",
                "epoch": 0,
                "name": "packit",
                "release": "1.20210930124525726166.main.0.g0b7b36b.fc36",
                "version": "0.38.0",
            },
        ],
    })
    flexmock(BuildChrootProxy).should_receive("get_built_packages").with_args(
        BUILD_ID,
        "fedora-rawhide-x86_64").and_return(copr_response_built_packages)

    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)

    pr = flexmock(source_project=flexmock(), target_branch="main")
    pr.should_receive("get_comments").and_return([])
    pr.should_receive("comment").and_return()

    # Reporting
    flexmock(GithubProject).should_receive("get_pr").and_return(pr)
    flexmock(GithubProject).should_receive(
        "create_check_run").and_return().once()
    flexmock(GithubProject).should_receive("get_git_urls").and_return(
        {"git": "https://github.com/packit-service/packit.git"})
    flexmock(packit_service.worker.build.copr_build).should_receive(
        "get_valid_build_targets").and_return({
            "fedora-33-x86_64",
            "fedora-32-x86_64",
            "fedora-31-x86_64",
            "fedora-rawhide-x86_64",
        })

    check_copr_build(BUILD_ID)
    assert packit_build_752.status == PG_BUILD_STATUS_SUCCESS
예제 #29
0
def test_copr_build_end_testing_farm(copr_build_end, copr_build):
    steve = SteveJobs()
    flexmock(SteveJobs, _is_private=False)
    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        Client(
            config={
                "copr_url": "https://copr.fedorainfracloud.org",
                "username": "******",
            }))
    flexmock(CoprBuildJobHelper).should_receive("job_owner").and_return(
        "some-owner")
    flexmock(CoprBuildJobHelper).should_receive("copr_build_model").and_return(
        flexmock())
    config = PackageConfig(jobs=[
        JobConfig(
            type=JobType.copr_build,
            trigger=JobConfigTriggerType.pull_request,
            metadata={"targets": ["fedora-rawhide"]},
        ),
        JobConfig(
            type=JobType.tests,
            trigger=JobConfigTriggerType.pull_request,
            metadata={"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(CoprBuild).should_receive("get_by_build_id").and_return(
        copr_build)
    flexmock(CoprBuild).should_receive("set_status").with_args("success")
    flexmock(CoprBuildDB).should_receive("get_build").and_return({
        "commit_sha":
        "XXXXX",
        "pr_id":
        24,
        "repo_name":
        "hello-world",
        "repo_namespace":
        "packit-service",
        "ref":
        "XXXX",
        "https_url":
        "https://github.com/packit-service/hello-world",
    })

    url = "https://localhost:5000/copr-build/1/logs"
    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(uuid).should_receive("uuid4").and_return(
        uuid.UUID("5e8079d8-f181-41cf-af96-28e99774eb68"))
    payload: dict = {
        "pipeline": {
            "id": "5e8079d8-f181-41cf-af96-28e99774eb68"
        },
        "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",
        },
    }

    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()

    steve.process_message(copr_build_end)
예제 #30
0
def test_copr_build_end_failed_testing_farm_no_json(copr_build_end,
                                                    copr_build):
    steve = SteveJobs()
    flexmock(SteveJobs, _is_private=False)
    flexmock(CoprHelper).should_receive("get_copr_client").and_return(
        Client(
            config={
                "copr_url": "https://copr.fedorainfracloud.org",
                "username": "******",
            }))
    flexmock(CoprBuildJobHelper).should_receive("job_owner").and_return(
        "some-owner")
    flexmock(CoprBuildJobHelper).should_receive("copr_build_model").and_return(
        flexmock())
    config = PackageConfig(jobs=[
        JobConfig(
            type=JobType.copr_build,
            trigger=JobConfigTriggerType.pull_request,
            metadata={"targets": ["fedora-rawhide"]},
        ),
        JobConfig(
            type=JobType.tests,
            trigger=JobConfigTriggerType.pull_request,
            metadata={"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(CoprBuild).should_receive("get_by_build_id").and_return(
        copr_build)
    flexmock(CoprBuild).should_receive("set_status").with_args("success")
    flexmock(CoprBuildDB).should_receive("get_build").and_return({
        "commit_sha":
        "XXXXX",
        "pr_id":
        24,
        "repo_name":
        "hello-world",
        "repo_namespace":
        "packit-service",
        "ref":
        "XXXX",
        "https_url":
        "https://github.com/packit-service/hello-world",
    })

    url = get_log_url(1)
    flexmock(requests).should_receive("get").and_return(requests.Response())
    flexmock(
        requests.Response).should_receive("raise_for_status").and_return(None)
    # 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(CoprBuild).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()

    steve.process_message(copr_build_end)