Ejemplo n.º 1
0
def test_copr_build_fails_in_packit(pull_request_event):
    # status is set for each build-target (4x):
    #  - Building SRPM ...
    #  - Build failed, check latest comment for details.
    helper = build_helper(event=pull_request_event)
    templ = "packit-stg/rpm-build-fedora-{ver}-x86_64"
    for v in ["29", "30", "31", "rawhide"]:
        flexmock(GitProject).should_receive("set_commit_status").with_args(
            "528b803be6f93e19ca4130bf4976f2800a3004c4",
            CommitStatus.pending,
            "",
            "Building SRPM ...",
            templ.format(ver=v),
            trim=True,
        ).and_return().once()
    for v in ["29", "30", "31", "rawhide"]:
        flexmock(GitProject).should_receive("set_commit_status").with_args(
            "528b803be6f93e19ca4130bf4976f2800a3004c4",
            CommitStatus.failure,
            "https://localhost:5000/srpm-build/2/logs",
            "SRPM build failed, check the logs for details.",
            templ.format(ver=v),
            trim=True,
        ).and_return().once()
    flexmock(SRPMBuildModel).should_receive("create").and_return(SRPMBuildModel(id=2))
    flexmock(CoprBuildModel).should_receive("get_or_create").and_return(
        CoprBuildModel(id=1)
    )
    flexmock(sentry_integration).should_receive("send_to_sentry").and_return().once()
    flexmock(PackitAPI).should_receive("run_copr_build").and_raise(
        FailedCreateSRPM, "some error"
    )
    assert not helper.run_copr_build()["success"]
Ejemplo n.º 2
0
def test_copr_build_success(github_pr_event):
    # status is set for each build-target (4x):
    #  - Building SRPM ...
    #  - Starting RPM build...
    helper = build_helper(event=github_pr_event)
    flexmock(GitProject).should_receive("set_commit_status").and_return().times(8)
    flexmock(GitProject).should_receive("get_pr").and_return(flexmock())
    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="the-owner")
            )
            .mock(),
        )
    )

    flexmock(Celery).should_receive("send_task").once()
    assert helper.run_copr_build()["success"]
Ejemplo n.º 3
0
def test_copr_build_check_names(pull_request_event):
    flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(
        flexmock(job_config_trigger_type=JobConfigTriggerType.release)
    )
    helper = build_helper(
        event=pull_request_event,
        metadata=JobMetadataConfig(targets=["bright-future-x86_64"], owner="nobody"),
    )
    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="Building RPM ...",
        check_name="packit-stg/rpm-build-bright-future-x86_64",
        url="https://localhost:5000/copr-build/1/logs",
    ).and_return()

    flexmock(GitProject).should_receive("set_commit_status").and_return().never()
    flexmock(SRPMBuildModel).should_receive("create").and_return(SRPMBuildModel())
    flexmock(CoprBuildModel).should_receive("get_or_create").and_return(
        CoprBuildModel(id=1)
    )
    flexmock(PullRequestEvent).should_receive("db_trigger").and_return(flexmock())
    flexmock(PackitAPI).should_receive("run_copr_build").and_return(1, None)
    flexmock(Celery).should_receive("send_task").once()

    config = ServiceConfig.get_service_config()
    config.disable_sentry = True
    assert helper.run_copr_build()["success"]
Ejemplo n.º 4
0
def test_copr_build_for_release(release_event):
    # status is set for each build-target (4x):
    #  - Building SRPM ...
    #  - Building RPM ...
    branch_build_job = JobConfig(
        type=JobType.build,
        trigger=JobConfigTriggerType.release,
        metadata=JobMetadataConfig(
            targets=[
                "fedora-29-x86_64",
                "fedora-30-x86_64",
                "fedora-31-x86_64",
                "fedora-rawhide-x86_64",
            ],
            owner="nobody",
            dist_git_branches=["build-branch"],
        ),
    )
    flexmock(AddReleaseDbTrigger).should_receive("db_trigger").and_return(
        flexmock(job_config_trigger_type=JobConfigTriggerType.release)
    )
    helper = build_helper(jobs=[branch_build_job], event=release_event)
    flexmock(ReleaseEvent).should_receive("get_project").and_return(helper.project)
    flexmock(GitProject).should_receive("set_commit_status").and_return().times(8)
    flexmock(GitProject).should_receive("get_sha_from_tag").and_return("123456").once()
    flexmock(SRPMBuildModel).should_receive("create").and_return(SRPMBuildModel())
    flexmock(CoprBuildModel).should_receive("get_or_create").and_return(
        CoprBuildModel(id=1)
    )
    flexmock(PackitAPI).should_receive("run_copr_build").and_return(1, None).once()
    flexmock(Celery).should_receive("send_task").once()
    assert helper.run_copr_build()["success"]
Ejemplo n.º 5
0
def test_copr_build_check_names(github_pr_event):
    flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(
        flexmock(job_config_trigger_type=JobConfigTriggerType.release))
    helper = build_helper(
        event=github_pr_event,
        metadata=JobMetadataConfig(targets=["bright-future-x86_64"],
                                   owner="nobody"),
    )

    flexmock(copr_build).should_receive(
        "get_copr_build_log_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(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="the-example-namespace-the-example-repo-342-stg",
            chroots=["bright-future-x86_64"],
            owner="nobody",
            description=None,
            instructions=None,
        ).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="the-owner")).mock(),
        ))

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

    assert helper.run_copr_build()["success"]
Ejemplo n.º 6
0
def test_copr_build_for_release(release_event):
    # status is set for each build-target (4x):
    #  - Building SRPM ...
    #  - Starting RPM build...
    branch_build_job = JobConfig(
        type=JobType.build,
        trigger=JobConfigTriggerType.release,
        metadata=JobMetadataConfig(
            targets=[
                "fedora-29-x86_64",
                "fedora-30-x86_64",
                "fedora-31-x86_64",
                "fedora-rawhide-x86_64",
            ],
            owner="nobody",
            dist_git_branches=["build-branch"],
        ),
    )
    trigger = flexmock(job_config_trigger_type=JobConfigTriggerType.release, id=123)
    flexmock(AddReleaseDbTrigger).should_receive("db_trigger").and_return(trigger)
    flexmock(release_event.project).should_receive("get_sha_from_tag").and_return(
        "123456"
    )
    helper = build_helper(
        jobs=[branch_build_job],
        event=release_event,
        db_trigger=trigger,
    )
    flexmock(ReleaseEvent).should_receive("get_project").and_return(helper.project)
    flexmock(GitProject).should_receive("set_commit_status").and_return().times(8)
    flexmock(SRPMBuildModel).should_receive("create").and_return(
        SRPMBuildModel(success=True)
    )
    flexmock(CoprBuildModel).should_receive("get_or_create").and_return(
        CoprBuildModel(id=1)
    )

    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="the-owner")
            )
            .mock(),
        )
    )

    flexmock(Celery).should_receive("send_task").once()
    assert helper.run_copr_build()["success"]
Ejemplo n.º 7
0
def test_copr_build_for_branch(branch_push_event):
    # status is set for each build-target (4x):
    #  - Building SRPM ...
    #  - Starting RPM build...
    branch_build_job = JobConfig(
        type=JobType.build,
        trigger=JobConfigTriggerType.commit,
        metadata=JobMetadataConfig(
            targets=DEFAULT_TARGETS,
            owner="nobody",
            dist_git_branches=["build-branch"],
        ),
    )
    trigger = flexmock(job_config_trigger_type=JobConfigTriggerType.commit,
                       id=123)
    flexmock(AddBranchPushDbTrigger).should_receive("db_trigger").and_return(
        trigger)
    helper = build_helper(
        jobs=[branch_build_job],
        event=branch_push_event,
        db_trigger=trigger,
    )
    flexmock(GitProject).should_receive(
        "set_commit_status").and_return().times(8)
    flexmock(SRPMBuildModel).should_receive("create").and_return(
        SRPMBuildModel(success=True))
    flexmock(CoprBuildModel).should_receive("get_or_create").and_return(
        CoprBuildModel(id=1))
    flexmock(PushGitHubEvent).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="the-owner")).mock(),
            mock_chroot_proxy=flexmock().should_receive("get_list").and_return(
                {target: ""
                 for target in DEFAULT_TARGETS}).mock(),
        ))
    flexmock(Pushgateway).should_receive("push_copr_build_created")

    flexmock(Celery).should_receive("send_task").once()
    assert helper.run_copr_build()["success"]
Ejemplo n.º 8
0
def test_copr_build_success_set_test_check(github_pr_event):
    # status is set for each test-target (2x):
    #  - Building SRPM ...
    #  - Starting RPM build...
    test_job = JobConfig(
        type=JobType.tests,
        trigger=JobConfigTriggerType.pull_request,
        metadata=JobMetadataConfig(
            owner="nobody",
            targets=["bright-future-x86_64", "brightest-future-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(
        jobs=[test_job],
        event=github_pr_event,
        db_trigger=trigger,
    )
    flexmock(GitProject).should_receive(
        "set_commit_status").and_return().times(4)
    flexmock(GitProject).should_receive("get_pr").and_return(flexmock())
    flexmock(SRPMBuildModel).should_receive("create").and_return(
        SRPMBuildModel(success=True))
    flexmock(CoprBuildModel).should_receive("get_or_create").and_return(
        CoprBuildModel(id=1))

    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="the-owner")).mock(),
            mock_chroot_proxy=flexmock().should_receive("get_list").and_return(
                {
                    "bright-future-x86_64": "",
                    "brightest-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"]
Ejemplo n.º 9
0
def test_copr_build_success(pull_request_event):
    # status is set for each build-target (4x):
    #  - Building SRPM ...
    #  - Building RPM ...
    helper = build_helper(event=pull_request_event)
    flexmock(GitProject).should_receive("set_commit_status").and_return().times(8)
    flexmock(SRPMBuildModel).should_receive("create").and_return(SRPMBuildModel())
    flexmock(CoprBuildModel).should_receive("get_or_create").and_return(
        CoprBuildModel(id=1)
    )
    flexmock(PullRequestEvent).should_receive("db_trigger").and_return(flexmock())
    flexmock(PackitAPI).should_receive("run_copr_build").and_return(1, None).once()
    flexmock(Celery).should_receive("send_task").once()
    assert helper.run_copr_build()["success"]
Ejemplo n.º 10
0
def test_get_logs(client):
    chroot = "foo-1-x86_64"
    state = "pending"
    build_id = 2

    project = GitProjectModel()
    project.namespace = "john-foo"
    project.repo_name = "bar"

    pr = PullRequestModel()
    pr.pr_id = 234
    pr.project = project

    srpm_build = SRPMBuildModel()
    srpm_build.logs = "asd<br>qwe"

    c = CoprBuildModel()
    c.target = chroot
    c.build_id = str(build_id)
    c.srpm_build = srpm_build
    c.status = state
    c.web_url = (
        "https://copr.fedorainfracloud.org/coprs/john-foo-bar/john-foo-bar/build/2/"
    )
    c.build_logs_url = "https://localhost:5000/build/2/foo-1-x86_64/logs"

    flexmock(CoprBuildModel).should_receive("get_by_id").and_return(c)
    flexmock(CoprBuildModel).should_receive("get_project").and_return(project)
    flexmock(CoprBuildModel).should_receive("job_trigger").and_return(
        flexmock(get_trigger_object=lambda: pr))

    url = f"/copr-build/1/logs"
    logs_url = get_copr_build_log_url_from_flask(1)
    assert logs_url.endswith(url)

    resp = client.get(url)
    expected = (
        "<html><head>"
        f"<title>COPR build {project.namespace}/{project.repo_name}:"
        f" PR #{pr.pr_id}</title></head><body>"
        f"COPR build ID: {c.build_id}<br>"
        f"Submitted: {optional_time(c.build_submitted_time)}<br>"
        f"State: {c.status}<br><br>"
        f'Build web interface URL: <a href="{c.web_url}">{c.web_url}</a><br>'
        f'Build logs: <a href="{c.build_logs_url}">{c.build_logs_url}</a><br>'
        "SRPM creation logs:<br><br>"
        f"<pre>{c.srpm_build.logs}</pre>"
        "<br></body></html>")
    assert resp.data == expected.encode()
Ejemplo n.º 11
0
def test_copr_build_no_targets(github_pr_event):
    # status is set for each build-target (fedora-stable => 2x):
    #  - Building SRPM ...
    #  - Starting RPM build...
    helper = build_helper(
        event=github_pr_event,
        metadata=JobMetadataConfig(owner="nobody"),
        db_trigger=flexmock(
            job_config_trigger_type=JobConfigTriggerType.pull_request, id=123),
    )

    flexmock(copr_build).should_receive(
        "get_srpm_log_url_from_flask").and_return("https://test.url")
    flexmock(copr_build).should_receive("get_build_targets").and_return(
        {"fedora-32-x86_64", "fedora-31-x86_64"})
    flexmock(GitProject).should_receive(
        "set_commit_status").and_return().times(4)
    flexmock(GitProject).should_receive("get_pr").and_return(flexmock())
    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="the-owner")).mock(),
            mock_chroot_proxy=flexmock().should_receive("get_list").and_return(
                {
                    target: ""
                    for target in {"fedora-32-x86_64", "fedora-31-x86_64"}
                }).mock(),
        ))
    flexmock(Pushgateway).should_receive("push_copr_build_created")

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

    assert helper.run_copr_build()["success"]
Ejemplo n.º 12
0
def test_get_logs(client):
    chroot = "foo-1-x86_64"
    state = "success"
    build_id = 2

    project = GitProjectModel()
    project.namespace = "john-foo"
    project.repo_name = "bar"

    pr = PullRequestModel()
    pr.pr_id = 234
    pr.project = project

    srpm = SRPMBuildModel()
    srpm.url = "https://some.random.copr.subdomain.org/my_srpm.srpm"

    c = CoprBuildModel()
    c.target = chroot
    c.build_id = str(build_id)
    c.srpm_build_id = 11
    c.status = state
    c.srpm_build = srpm
    c.web_url = (
        "https://copr.fedorainfracloud.org/coprs/john-foo-bar/john-foo-bar/build/2/"
    )
    c.build_logs_url = "https://localhost:5000/build/2/foo-1-x86_64/logs"
    c.owner = "packit"
    c.project_name = "example_project"

    flexmock(CoprBuildModel).should_receive("get_by_id").and_return(c)
    flexmock(CoprBuildModel).should_receive("get_project").and_return(project)
    flexmock(CoprBuildModel).should_receive("job_trigger").and_return(
        flexmock(get_trigger_object=lambda: pr))

    url = "/copr-build/1"
    logs_url = get_copr_build_info_url_from_flask(1)
    assert logs_url.endswith(url)

    resp = client.get(url).data.decode()
    assert f"srpm-build/{c.srpm_build_id}/logs" in resp
    assert c.web_url in resp
    assert c.build_logs_url in resp
    assert c.target in resp
    assert "Status: success" in resp
    assert "You can install" in resp

    assert "Download SRPM" in resp
    assert srpm.url in resp
Ejemplo n.º 13
0
def test_copr_build_success_gitlab_comment(gitlab_mr_event):
    helper = build_helper(
        event=gitlab_mr_event,
        db_trigger=flexmock(
            job_config_trigger_type=JobConfigTriggerType.pull_request, id=123),
    )
    flexmock(BaseBuildJobHelper).should_receive(
        "is_gitlab_instance").and_return(True)
    flexmock(BaseBuildJobHelper).should_receive("base_project").and_return(
        GitProject(
            repo="the-example-repo",
            service=flexmock(),
            namespace="the-example-namespace",
        ))
    flexmock(GitProject).should_receive("request_access").and_return()
    flexmock(GitProject).should_receive("pr_comment").and_return()
    flexmock(BaseBuildJobHelper).should_receive(
        "is_reporting_allowed").and_return(False)
    flexmock(GitProject).should_receive("get_pr").and_return(flexmock())
    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").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="the-owner")).mock(),
            mock_chroot_proxy=flexmock().should_receive("get_list").and_return(
                {target: ""
                 for target in DEFAULT_TARGETS}).mock(),
        ))
    flexmock(Pushgateway).should_receive("push_copr_build_created")

    flexmock(Celery).should_receive("send_task").once()
    assert helper.run_copr_build()["success"]
Ejemplo n.º 14
0
def test_copr_build_fails_in_packit(github_pr_event):
    # status is set for each build-target (4x):
    #  - Building SRPM ...
    #  - Build failed, check latest comment for details.
    helper = build_helper(
        event=github_pr_event,
        db_trigger=flexmock(
            job_config_trigger_type=JobConfigTriggerType.pull_request, id=123),
    )
    templ = "packit-stg/rpm-build-fedora-{ver}-x86_64"
    flexmock(copr_build).should_receive(
        "get_srpm_log_url_from_flask").and_return("https://test.url")
    for v in ["29", "30", "31", "rawhide"]:
        flexmock(GitProject).should_receive("set_commit_status").with_args(
            "528b803be6f93e19ca4130bf4976f2800a3004c4",
            CommitStatus.pending,
            "",
            "Building SRPM ...",
            templ.format(ver=v),
            trim=True,
        ).and_return().once()
    for v in ["29", "30", "31", "rawhide"]:
        flexmock(GitProject).should_receive("set_commit_status").with_args(
            "528b803be6f93e19ca4130bf4976f2800a3004c4",
            CommitStatus.failure,
            "https://test.url",
            "SRPM build failed, check the logs for details.",
            templ.format(ver=v),
            trim=True,
        ).and_return().once()
    flexmock(GitProject).should_receive("get_pr").and_return(flexmock())
    flexmock(SRPMBuildModel).should_receive("create").and_return(
        SRPMBuildModel(success=False, id=2))
    flexmock(CoprBuildModel).should_receive("get_or_create").and_return(
        CoprBuildModel(id=1))
    flexmock(sentry_integration).should_receive(
        "send_to_sentry").and_return().once()

    flexmock(PackitAPI).should_receive("create_srpm").and_raise(
        FailedCreateSRPM, "some error")
    flexmock(Pushgateway).should_receive("push_copr_build_created").never()

    flexmock(CoprBuildJobHelper).should_receive("run_build").never()

    assert not helper.run_copr_build()["success"]
Ejemplo n.º 15
0
def test_copr_build_fails_in_packit_gitlab(gitlab_mr_event):
    # status is set for each build-target (4x):
    #  - Building SRPM ...
    #  - Build failed, check latest comment for details.
    helper = build_helper(event=gitlab_mr_event)
    templ = "packit-stg/rpm-build-fedora-{ver}-x86_64"
    flexmock(copr_build).should_receive("get_srpm_log_url_from_flask").and_return(
        "https://test.url"
    )
    for v in ["29", "30", "31", "rawhide"]:
        flexmock(GitProject).should_receive("set_commit_status").with_args(
            "1f6a716aa7a618a9ffe56970d77177d99d100022",
            CommitStatus.pending,
            "",
            "Building SRPM ...",
            templ.format(ver=v),
            trim=True,
        ).and_return().once()
    for v in ["29", "30", "31", "rawhide"]:
        flexmock(GitProject).should_receive("set_commit_status").with_args(
            "1f6a716aa7a618a9ffe56970d77177d99d100022",
            CommitStatus.failure,
            "https://test.url",
            "SRPM build failed, check the logs for details.",
            templ.format(ver=v),
            trim=True,
        ).and_return().once()
    flexmock(GitProject).should_receive("get_pr").and_return(flexmock())
    flexmock(SRPMBuildModel).should_receive("create").and_return(
        SRPMBuildModel(success=False, id=2)
    )
    flexmock(CoprBuildModel).should_receive("get_or_create").and_return(
        CoprBuildModel(id=1)
    )
    flexmock(sentry_integration).should_receive("send_to_sentry").and_return().once()

    flexmock(PackitAPI).should_receive("create_srpm").and_raise(
        FailedCreateSRPM, "some error"
    )

    flexmock(CoprBuildJobHelper).should_receive("run_build").never()

    assert not helper.run_copr_build()["success"]
Ejemplo n.º 16
0
def test_copr_build_success_set_test_check(pull_request_event):
    # status is set for each build-target (4x):
    #  - Building SRPM ...
    #  - Building RPM ...
    # status is set for each test-target (4x):
    #  - Building SRPM ...
    #  - Building RPM ...
    test_job = JobConfig(type=JobType.tests, trigger=JobConfigTriggerType.pull_request,)
    flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(
        flexmock(job_config_trigger_type=JobConfigTriggerType.release)
    )
    helper = build_helper(jobs=[test_job], event=pull_request_event)
    flexmock(GitProject).should_receive("set_commit_status").and_return().times(16)
    flexmock(SRPMBuildModel).should_receive("create").and_return(SRPMBuildModel())
    flexmock(CoprBuildModel).should_receive("get_or_create").and_return(
        CoprBuildModel(id=1)
    )
    flexmock(PackitAPI).should_receive("run_copr_build").and_return(1, None).once()
    flexmock(Celery).should_receive("send_task").once()
    assert helper.run_copr_build()["success"]
Ejemplo n.º 17
0
def test_copr_build_success_gitlab(gitlab_mr_event):
    # status is set for each build-target (4x):
    #  - Building SRPM ...
    #  - Starting RPM build...
    helper = build_helper(
        event=gitlab_mr_event,
        db_trigger=flexmock(
            job_config_trigger_type=JobConfigTriggerType.pull_request, id=123),
    )
    flexmock(GitProject).should_receive(
        "set_commit_status").and_return().times(8)
    flexmock(GitProject).should_receive("get_pr").and_return(flexmock())
    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").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="the-owner")).mock(),
            mock_chroot_proxy=flexmock().should_receive("get_list").and_return(
                {target: ""
                 for target in DEFAULT_TARGETS}).mock(),
        ))
    flexmock(Pushgateway).should_receive("push_copr_build_created")

    flexmock(Celery).should_receive("send_task").once()
    assert helper.run_copr_build()["success"]
Ejemplo n.º 18
0
def test_get_logs(client):
    chroot = "foo-1-x86_64"
    state = "pending"
    build_id = 2

    project = GitProjectModel()
    project.namespace = "john-foo"
    project.repo_name = "bar"

    pr = PullRequestModel()
    pr.pr_id = 234
    pr.project = project

    c = CoprBuildModel()
    c.target = chroot
    c.build_id = str(build_id)
    c.srpm_build_id = 11
    c.status = state
    c.web_url = (
        "https://copr.fedorainfracloud.org/coprs/john-foo-bar/john-foo-bar/build/2/"
    )
    c.build_logs_url = "https://localhost:5000/build/2/foo-1-x86_64/logs"

    flexmock(CoprBuildModel).should_receive("get_by_id").and_return(c)
    flexmock(CoprBuildModel).should_receive("get_project").and_return(project)
    flexmock(CoprBuildModel).should_receive("job_trigger").and_return(
        flexmock(get_trigger_object=lambda: pr))

    url = "/copr-build/1"
    logs_url = get_copr_build_info_url_from_flask(1)
    assert logs_url.endswith(url)

    resp = client.get(url).data.decode()
    assert f"srpm-build/{c.srpm_build_id}/logs" in resp
    assert c.web_url in resp
    assert c.build_logs_url in resp
    assert c.target in resp
Ejemplo n.º 19
0
def test_copr_build_fails_to_update_copr_project(github_pr_event):
    # status is set for each build-target (4x):
    #  - Building SRPM ...
    #  - Build failed, check latest comment for details.
    helper = build_helper(event=github_pr_event)
    templ = "packit-stg/rpm-build-fedora-{ver}-x86_64"
    flexmock(copr_build).should_receive("get_srpm_log_url_from_flask").and_return(
        "https://test.url"
    )
    for v in ["29", "30", "31", "rawhide"]:
        flexmock(GitProject).should_receive("set_commit_status").with_args(
            "528b803be6f93e19ca4130bf4976f2800a3004c4",
            CommitStatus.pending,
            "",
            "Building SRPM ...",
            templ.format(ver=v),
            trim=True,
        ).and_return().once()
    for v in ["29", "30", "31", "rawhide"]:
        flexmock(GitProject).should_receive("set_commit_status").with_args(
            "528b803be6f93e19ca4130bf4976f2800a3004c4",
            CommitStatus.error,
            "",
            "Submit of the build failed: Copr project update failed.",
            templ.format(ver=v),
            trim=True,
        ).and_return().once()
    flexmock(GitProject).should_receive("get_pr").and_return(flexmock())
    flexmock(SRPMBuildModel).should_receive("create").and_return(
        SRPMBuildModel(success=True, id=2)
    )
    flexmock(CoprBuildModel).should_receive("get_or_create").and_return(
        CoprBuildModel(id=1)
    )

    flexmock(PackitAPI).should_receive("create_srpm").and_return("my.srpm")
    flexmock(GitProject).should_receive("pr_comment").with_args(
        pr_id=342,
        body="Based on your Packit configuration the settings of the "
        "nobody/the-example-namespace-the-example-repo-342-stg "
        "Copr project would need to be updated as follows:\n"
        "\n"
        "| field | old value | new value |\n"
        "| ----- | --------- | --------- |\n"
        "| chroots | ['f30', 'f31'] | ['f31', 'f32'] |\n"
        "| description | old | new |\n"
        "\n"
        "\n"
        "Packit was unable to update the settings above "
        "as it is missing `admin` permissions on the "
        "nobody/the-example-namespace-the-example-repo-342-stg Copr project.\n"
        "\n"
        "To fix this you can do one of the following:\n"
        "\n"
        "- Grant Packit `admin` permissions on the "
        "nobody/the-example-namespace-the-example-repo-342-stg Copr project on the "
        "[permissions page](https://copr.fedorainfracloud.org/"
        "coprs/nobody/the-example-namespace-the-example-repo-342-stg/permissions/).\n"
        "- Change the above Copr project settings manually on the "
        "[settings page](https://copr.fedorainfracloud.org/"
        "coprs/nobody/the-example-namespace-the-example-repo-342-stg/edit/) "
        "to match the Packit configuration.\n"
        "- Update the Packit configuration to match the Copr project settings.\n"
        "\n"
        "Please re-trigger the build, once the issue above is fixed.\n",
    ).and_return().once()

    flexmock(sentry_integration).should_receive("send_to_sentry").and_return().once()
    # copr build
    flexmock(CoprHelper).should_receive("get_copr_settings_url").with_args(
        "nobody",
        "the-example-namespace-the-example-repo-342-stg",
        section="permissions",
    ).and_return(
        "https://copr.fedorainfracloud.org/"
        "coprs/nobody/the-example-namespace-the-example-repo-342-stg/permissions/"
    ).once()

    flexmock(CoprHelper).should_receive("get_copr_settings_url").with_args(
        "nobody",
        "the-example-namespace-the-example-repo-342-stg",
    ).and_return(
        "https://copr.fedorainfracloud.org/"
        "coprs/nobody/the-example-namespace-the-example-repo-342-stg/edit/"
    ).once()

    flexmock(CoprHelper).should_receive("create_copr_project_if_not_exists").and_raise(
        PackitCoprSettingsException,
        "Copr project update failed.",
        fields_to_change={
            "chroots": (["f30", "f31"], ["f31", "f32"]),
            "description": ("old", "new"),
        },
    )

    assert not helper.run_copr_build()["success"]
Ejemplo n.º 20
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"]
Ejemplo n.º 21
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"]
Ejemplo n.º 22
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="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(),
        )
    )

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