def test_pr_comment_build_build_and_test_handler(mock_pr_comment_functionality, pr_build_comment_event): flexmock(PullRequestModel).should_receive("get_or_create").with_args( pr_id=9, namespace="packit-service", repo_name="hello-world", project_url="https://github.com/packit-service/hello-world", ).and_return( flexmock(id=9, job_config_trigger_type=JobConfigTriggerType.pull_request)) flexmock(GithubProject, get_files="foo.spec") flexmock(GithubProject).should_receive("is_private").and_return(False) flexmock(copr_build).should_receive("get_valid_build_targets").and_return( set()) flexmock(CoprBuildJobHelper).should_receive( "report_status_to_build").with_args( description=TASK_ACCEPTED, state=BaseCommitStatus.pending, url="", ).once() flexmock(CoprBuildJobHelper).should_receive( "report_status_to_tests").with_args( description=TASK_ACCEPTED, state=BaseCommitStatus.pending, url="", ).once() flexmock(Signature).should_receive("apply_async").twice() flexmock(Pushgateway).should_receive("push").times(3).and_return() pr = flexmock(head_commit="12345") flexmock(GithubProject).should_receive("get_pr").and_return(pr) comment = flexmock() flexmock(pr).should_receive("get_comment").and_return(comment) flexmock(comment).should_receive("add_reaction").with_args("+1").once() processing_results = SteveJobs().process_message(pr_build_comment_event) assert len(processing_results) == 2 copr_build_job = [ item for item in processing_results if item["details"]["job"] == "copr_build" ] assert copr_build_job test_job = [ item for item in processing_results if item["details"]["job"] == "tests" ] assert test_job event_dict, job, job_config, package_config = get_parameters_from_results( test_job) assert json.dumps(event_dict) results = run_testing_farm_handler( package_config=package_config, event=event_dict, job_config=job_config, ) assert first_dict_value(results["job"])["success"] assert "already handled" in first_dict_value( results["job"])["details"]["msg"]
def test_retry_propose_downstream_task(github_release_webhook): packit_yaml = ( "{'specfile_path': 'hello-world.spec', 'synced_files': []" ", jobs: [{trigger: release, job: propose_downstream, metadata: {targets:[]}}]}" ) flexmock(Github, get_repo=lambda full_name_or_id: None) project = flexmock( get_file_content=lambda path, ref: packit_yaml, full_repo_name="packit-service/hello-world", repo="hello-world", get_files=lambda ref, filter_regex: [], get_sha_from_tag=lambda tag_name: "123456", get_web_url=lambda: "https://github.com/packit/hello-world", is_private=lambda: False, default_branch="main", ) lp = flexmock(LocalProject, refresh_the_arguments=lambda: None) lp.git_project = project flexmock(DistGit).should_receive("local_project").and_return(lp) flexmock(Allowlist, check_and_report=True) config = ServiceConfig() config.command_handler_work_dir = SANDCASTLE_WORK_DIR config.get_project = lambda url: project flexmock(ServiceConfig).should_receive("get_service_config").and_return( config) # it would make sense to make LocalProject offline flexmock(AddReleaseDbTrigger).should_receive("db_trigger").and_return( flexmock(job_config_trigger_type=JobConfigTriggerType.release, id=123)) flexmock(Signature).should_receive("apply_async").once() flexmock(PackitAPI).should_receive("sync_release").with_args( dist_git_branch="main", tag="0.3.0").and_raise( RebaseHelperError, "Failed to download file from URL example.com").once() flexmock(Task).should_receive("retry").once().and_return() processing_results = SteveJobs().process_message(github_release_webhook) event_dict, job, job_config, package_config = get_parameters_from_results( processing_results) results = run_propose_downstream_handler(event_dict, package_config, job_config) assert not first_dict_value(results["job"])["success"] assert "not able to download" in first_dict_value( results["job"])["details"]["msg"]
def test_pr_embedded_command_handler(mock_pr_comment_functionality, pr_embedded_command_comment_event, comments_list): flexmock(PullRequestModel).should_receive("get_or_create").with_args( pr_id=9, namespace="packit-service", repo_name="hello-world", project_url="https://github.com/packit-service/hello-world", ).and_return( flexmock(id=9, job_config_trigger_type=JobConfigTriggerType.pull_request)) pr_embedded_command_comment_event["comment"]["body"] = comments_list flexmock(CoprBuildJobHelper).should_receive("run_copr_build").and_return( TaskResults(success=True, details={})) flexmock(GithubProject, get_files="foo.spec") flexmock(GithubProject).should_receive("is_private").and_return(False) flexmock(Signature).should_receive("apply_async").once() processing_results = SteveJobs().process_message( pr_embedded_command_comment_event) event_dict, job, job_config, package_config = get_parameters_from_results( processing_results) results = run_copr_build_handler( package_config=package_config, event=event_dict, job_config=job_config, ) assert first_dict_value(results["job"])["success"]
def test_dist_git_push_release_handle(github_release_webhook): packit_yaml = ( "{'specfile_path': 'hello-world.spec', 'synced_files': []" ", jobs: [{trigger: release, job: propose_downstream, metadata: {targets:[]}}]}" ) flexmock(Github, get_repo=lambda full_name_or_id: None) project = flexmock( get_file_content=lambda path, ref: packit_yaml, full_repo_name="packit-service/hello-world", repo="hello-world", get_files=lambda ref, filter_regex: [], get_sha_from_tag=lambda tag_name: "123456", get_web_url=lambda: "https://github.com/packit-service/hello-world", is_private=lambda: False, ) flexmock(LocalProject, refresh_the_arguments=lambda: None) flexmock(Whitelist, check_and_report=True) config = ServiceConfig() config.command_handler_work_dir = SANDCASTLE_WORK_DIR config.get_project = lambda url: project flexmock(ServiceConfig).should_receive("get_service_config").and_return(config) # it would make sense to make LocalProject offline flexmock(PackitAPI).should_receive("sync_release").with_args( dist_git_branch="master", version="0.3.0" ).once() flexmock(AddReleaseDbTrigger).should_receive("db_trigger").and_return( flexmock(job_config_trigger_type=JobConfigTriggerType.release) ) results = SteveJobs().process_message(github_release_webhook) assert first_dict_value(results["jobs"])["success"] assert results["event"]["trigger"] == "release"
def test_process_message(event): packit_yaml = { "specfile_path": "bar.spec", "synced_files": [], "jobs": [{ "trigger": "release", "job": "propose_downstream" }], } flexmock(Github, get_repo=lambda full_name_or_id: None) flexmock( GithubProject, get_file_content=lambda path, ref: dumps(packit_yaml), full_repo_name="foo/bar", get_files=lambda ref, filter_regex: [], get_sha_from_tag=lambda tag_name: "12345", get_web_url=lambda: "https://github.com/the-namespace/the-repo", is_private=lambda: False, ) flexmock(LocalProject, refresh_the_arguments=lambda: None) config = ServiceConfig() config.command_handler_work_dir = SANDCASTLE_WORK_DIR flexmock(ServiceConfig).should_receive("get_service_config").and_return( config) flexmock(PackitAPI).should_receive("sync_release").with_args( dist_git_branch="master", version="1.2.3").once() flexmock(AddReleaseDbTrigger).should_receive("db_trigger").and_return( flexmock(job_config_trigger_type=JobConfigTriggerType.release)) flexmock(Whitelist, check_and_report=True) results = SteveJobs().process_message(event) j = first_dict_value(results["jobs"]) assert "propose_downstream" in next(iter(results["jobs"])) assert j["success"] assert results["event"]["trigger"] == "release"
def test_installation(): config = ServiceConfig() config.command_handler_work_dir = SANDCASTLE_WORK_DIR flexmock(ServiceConfig).should_receive("get_service_config").and_return( config) flexmock(InstallationModel).should_receive("create").once() flexmock(Allowlist).should_receive("add_namespace").with_args( "github.com/packit-service", "jpopelka").and_return(False) flexmock(GithubProject).should_receive("create_issue").once() flexmock(Signature).should_receive("apply_async").once() flexmock(Pushgateway).should_receive("push").once().and_return() processing_results = SteveJobs().process_message(installation_event()) event_dict, job, job_config, package_config = get_parameters_from_results( processing_results) assert json.dumps(event_dict) results = run_installation_handler( package_config=package_config, event=event_dict, job_config=job_config, ) assert first_dict_value(results["job"])["success"]
def test_issue_comment_propose_update_handler( mock_issue_comment_functionality, issue_comment_propose_update_event ): flexmock(PackitAPI).should_receive("sync_release").and_return( PullRequest( title="foo", description="bar", target_branch="baz", source_branch="yet", id=1, status=PRStatus.open, url="https://xyz", author="me", created=datetime.now(), ) ) flexmock( GithubProject, get_files=lambda ref, filter_regex: [], get_web_url=lambda: "https://github.com/the-namespace/the-repo", is_private=lambda: False, ) flexmock(IssueCommentEvent, db_trigger=ProjectReleaseModel()) results = SteveJobs().process_message(issue_comment_propose_update_event) assert first_dict_value(results["jobs"])["success"]
def test_downstream_koji_build(): packit_yaml = ( "{'specfile_path': 'buildah.spec', 'synced_files': []," "'jobs': [{'trigger': 'commit', 'job': 'koji_build'}]," "'downstream_package_name': 'buildah'}" ) pagure_project = flexmock( PagureProject, full_repo_name="rpms/buildah", get_web_url=lambda: "https://src.fedoraproject.org/rpms/buildah", default_branch="main", ) pagure_project.should_receive("get_files").with_args( ref="abcd", filter_regex=r".+\.spec$" ).and_return(["buildah.spec"]) pagure_project.should_receive("get_file_content").with_args( path=".distro/source-git.yaml", ref="abcd" ).and_raise(FileNotFoundError, "Not found.") pagure_project.should_receive("get_file_content").with_args( path=".packit.yaml", ref="abcd" ).and_return(packit_yaml) flexmock(GitBranchModel).should_receive("get_or_create").with_args( branch_name="main", namespace="rpms", repo_name="buildah", project_url="https://src.fedoraproject.org/rpms/buildah", ).and_return(flexmock(id=9, job_config_trigger_type=JobConfigTriggerType.commit)) flexmock(ServiceConfig).should_receive("get_service_config").and_return( ServiceConfig( command_handler_work_dir=SANDCASTLE_WORK_DIR, repository_cache="/tmp/repository-cache", add_repositories_to_repository_cache=False, ) ) flexmock(LocalProject, refresh_the_arguments=lambda: None) flexmock(RepositoryCache).should_call("__init__").once() flexmock(Signature).should_receive("apply_async").once() flexmock(Pushgateway).should_receive("push").once().and_return() flexmock(PackitAPI).should_receive("build").with_args( dist_git_branch="main", scratch=False, nowait=True, from_upstream=False, ) processing_results = SteveJobs().process_message(distgit_commit_event()) event_dict, job, job_config, package_config = get_parameters_from_results( processing_results ) assert json.dumps(event_dict) results = run_downstream_koji_build( package_config=package_config, event=event_dict, job_config=job_config, ) assert first_dict_value(results["job"])["success"]
def test_bodhi_update_fedora_stable_by_default(koji_build_completed_f35): """(Known build scenario.)""" packit_yaml = ("{'specfile_path': 'python-ogr.spec', 'synced_files': []," "'jobs': [{'trigger': 'commit', 'job': 'bodhi_update'}]," "'downstream_package_name': 'python-ogr'}") pagure_project = flexmock( PagureProject, full_repo_name="rpms/python-ogr", get_web_url=lambda: "https://src.fedoraproject.org/rpms/python-ogr", default_branch="main", ) pagure_project.should_receive("get_files").with_args( ref="51b57ec04f5e6e9066ac859a1408cfbf1ead307e", filter_regex=r".+\.spec$").and_return(["packit.spec"]) pagure_project.should_receive("get_file_content").with_args( path=".distro/source-git.yaml", ref="51b57ec04f5e6e9066ac859a1408cfbf1ead307e").and_raise( FileNotFoundError, "Not found.") pagure_project.should_receive("get_file_content").with_args( path=".packit.yaml", ref="51b57ec04f5e6e9066ac859a1408cfbf1ead307e").and_return(packit_yaml) flexmock(ServiceConfig).should_receive("get_service_config").and_return( ServiceConfig( command_handler_work_dir=SANDCASTLE_WORK_DIR, repository_cache="/tmp/repository-cache", add_repositories_to_repository_cache=False, )) flexmock(LocalProject, refresh_the_arguments=lambda: None) flexmock(RepositoryCache).should_call("__init__").once() # 1*CreateBodhiUpdateHandler + 1*KojiBuildReportHandler flexmock(Signature).should_receive("apply_async").times(2) flexmock(Pushgateway).should_receive("push").once().and_return() flexmock(PackitAPI).should_receive("create_update").with_args( dist_git_branch="f35", update_type="enhancement", update_notes=DEFAULT_BODHI_NOTE, koji_builds=["1874070"], ).once() # Database not touched flexmock(KojiBuildModel).should_receive("get_by_build_id").with_args( build_id=1874070).times(0) processing_results = SteveJobs().process_message(koji_build_completed_f35) # 1*CreateBodhiUpdateHandler + 1*KojiBuildReportHandler assert len(processing_results) == 2 processing_results.pop() event_dict, job, job_config, package_config = get_parameters_from_results( processing_results) assert json.dumps(event_dict) results = run_bodhi_update( package_config=package_config, event=event_dict, job_config=job_config, ) assert first_dict_value(results["job"])["success"]
def test_dist_git_push_release_handle_one_failed( github_release_webhook, fedora_branches ): packit_yaml = ( "{'specfile_path': 'hello-world.spec', 'synced_files': []" ", jobs: [{trigger: release, job: propose_downstream, " "metadata: {targets:[], dist-git-branch: fedora-all}}]}" ) flexmock(Github, get_repo=lambda full_name_or_id: None) project = ( flexmock( get_file_content=lambda path, ref: packit_yaml, full_repo_name="packit-service/hello-world", repo="hello-world", get_files=lambda ref, filter_regex: [], get_sha_from_tag=lambda tag_name: "123456", get_web_url=lambda: "https://github.com/packit/hello-world", is_private=lambda: False, ) .should_receive("create_issue") .once() .mock() ) flexmock(LocalProject, refresh_the_arguments=lambda: None) flexmock(Whitelist, check_and_report=True) config = ServiceConfig() config.command_handler_work_dir = SANDCASTLE_WORK_DIR config.get_project = lambda url: project flexmock(ServiceConfig).should_receive("get_service_config").and_return(config) # it would make sense to make LocalProject offline for i, branch in enumerate(fedora_branches): sync_release = ( flexmock(PackitAPI) .should_receive("sync_release") .with_args(dist_git_branch=branch, version="0.3.0") .once() ) if i == 1: sync_release.and_raise(Exception, f"Failed {branch}").once() flexmock(FedPKG).should_receive("clone").and_return(None) flexmock(sentry_integration).should_receive("send_to_sentry").and_return().once() flexmock(AddReleaseDbTrigger).should_receive("db_trigger").and_return( flexmock(job_config_trigger_type=JobConfigTriggerType.release, id=123) ) flexmock(Signature).should_receive("apply_async").once() processing_results = SteveJobs().process_message(github_release_webhook) assert processing_results["details"]["event"]["trigger"] == "release" event_dict, package_config, job = get_parameters_from_results(processing_results) results = run_propose_downstream_handler( package_config=package_config, event=event_dict, job_config=job, ) assert not first_dict_value(results["job"])["success"]
def test_dist_git_push_release_handle_all_failed(github_release_webhook, fedora_branches): packit_yaml = ("{'specfile_path': 'hello-world.spec', 'synced_files': []" ", jobs: [{trigger: release, job: propose_downstream, " "metadata: {targets:[], dist-git-branch: fedora-all}}]}") flexmock(Github, get_repo=lambda full_name_or_id: None) table_content = "" for branch in fedora_branches: table_content += f"| `{branch}` | `Failed` |\n" project = (flexmock( get_file_content=lambda path, ref: packit_yaml, full_repo_name="packit-service/hello-world", repo="hello-world", get_files=lambda ref, filter_regex: [], get_sha_from_tag=lambda tag_name: "123456", get_web_url=lambda: "https://github.com/packit/hello-world", is_private=lambda: False, default_branch="main", ).should_receive("create_issue").with_args( title="[packit] Propose downstream failed for release 0.3.0", body="Packit failed on creating pull-requests in dist-git:\n\n" "| dist-git branch | error |\n" "| --------------- | ----- |\n" f"{table_content}\n\n" "You can retrigger the update by adding a comment (`/packit propose-downstream`)" " into this issue.\n", ).once().mock()) lp = flexmock(LocalProject, refresh_the_arguments=lambda: None) lp.git_project = project flexmock(DistGit).should_receive("local_project").and_return(lp) flexmock(Allowlist, check_and_report=True) config = ServiceConfig() config.command_handler_work_dir = SANDCASTLE_WORK_DIR config.get_project = lambda url: project flexmock(ServiceConfig).should_receive("get_service_config").and_return( config) # it would make sense to make LocalProject offline flexmock(PackitAPI).should_receive("sync_release").and_raise( Exception, "Failed").times(len(fedora_branches)) flexmock(AddReleaseDbTrigger).should_receive("db_trigger").and_return( flexmock(job_config_trigger_type=JobConfigTriggerType.release, id=123)) flexmock(sentry_integration).should_receive( "send_to_sentry").and_return().times(len(fedora_branches)) flexmock(Signature).should_receive("apply_async").once() processing_results = SteveJobs().process_message(github_release_webhook) event_dict, job, job_config, package_config = get_parameters_from_results( processing_results) results = run_propose_downstream_handler( package_config=package_config, event=event_dict, job_config=job_config, ) assert not first_dict_value(results["job"])["success"]
def test_process_message(event, private, enabled_private_namespaces, success): packit_yaml = { "specfile_path": "bar.spec", "synced_files": [], "jobs": [{ "trigger": "release", "job": "propose_downstream" }], } flexmock(Github, get_repo=lambda full_name_or_id: None) gh_project = flexmock( GithubProject, get_file_content=lambda path, ref: dumps(packit_yaml), full_repo_name="the-namespace/the-repo", get_files=lambda ref, filter_regex: [], get_sha_from_tag=lambda tag_name: "12345", get_web_url=lambda: "https://github.com/the-namespace/the-repo", is_private=lambda: private, ) gh_project.default_branch = "main" lp = flexmock(LocalProject, refresh_the_arguments=lambda: None) lp.git_project = gh_project flexmock(DistGit).should_receive("local_project").and_return(lp) config = ServiceConfig( enabled_private_namespaces=enabled_private_namespaces) config.command_handler_work_dir = SANDCASTLE_WORK_DIR flexmock(ServiceConfig).should_receive("get_service_config").and_return( config) flexmock(PackitAPI).should_receive("sync_release").with_args( dist_git_branch="main", tag="1.2.3").times(1 if success else 0) flexmock(AddReleaseDbTrigger).should_receive("db_trigger").and_return( flexmock(job_config_trigger_type=JobConfigTriggerType.release, id=1)) flexmock(Allowlist, check_and_report=True) flexmock(Signature).should_receive("apply_async").times( 1 if success else 0) processing_results = SteveJobs().process_message(event) if not success: assert processing_results == [] return event_dict, job, job_config, package_config = get_parameters_from_results( processing_results) results = run_propose_downstream_handler( package_config=package_config, event=event_dict, job_config=job_config, ) assert "propose_downstream" in next(iter(results["job"])) assert first_dict_value(results["job"])["success"]
def test_rebuild_failed( mock_pr_comment_functionality, pr_embedded_command_comment_event ): flexmock(PullRequestModel).should_receive("get_or_create").with_args( pr_id=9, namespace="packit-service", repo_name="hello-world", project_url="https://github.com/packit-service/hello-world", ).and_return( flexmock(id=9, job_config_trigger_type=JobConfigTriggerType.pull_request) ) pr_embedded_command_comment_event["comment"]["body"] = "/packit rebuild-failed" flexmock(CoprBuildJobHelper).should_receive("run_copr_build").and_return( TaskResults(success=True, details={}) ) flexmock(GithubProject, get_files="foo.spec") flexmock(GithubProject).should_receive("is_private").and_return(False) pr = flexmock(head_commit="12345") flexmock(GithubProject).should_receive("get_pr").and_return(pr) comment = flexmock() flexmock(pr).should_receive("get_comment").and_return(comment) flexmock(comment).should_receive("add_reaction").with_args("+1").once() flexmock(copr_build).should_receive("get_valid_build_targets").and_return(set()) model = flexmock(CoprBuildModel, status="failed", target="target") flexmock(model).should_receive("get_all_by").and_return(flexmock()) flexmock(AbstractForgeIndependentEvent).should_receive( "get_all_build_failed_targets" ).and_return({"target"}) flexmock(CoprBuildJobHelper).should_receive("report_status_to_build").with_args( description=TASK_ACCEPTED, state=BaseCommitStatus.pending, url="", ).once() flexmock(Signature).should_receive("apply_async").once() flexmock(Pushgateway).should_receive("push").twice().and_return() processing_results = SteveJobs().process_message(pr_embedded_command_comment_event) event_dict, job, job_config, package_config = get_parameters_from_results( processing_results ) assert event_dict["targets_override"] == ["target"] assert json.dumps(event_dict) results = run_copr_build_handler( package_config=package_config, event=event_dict, job_config=job_config, ) assert first_dict_value(results["job"])["success"]
def test_issue_comment_propose_downstream_handler( mock_comment, event_type, ): project_class, comment_event = mock_comment flexmock(PackitAPI).should_receive("sync_release").and_return( PullRequestReadOnly( title="foo", description="bar", target_branch="baz", source_branch="yet", id=1, status=PRStatus.open, url="https://xyz", author="me", created=datetime.now(), ) ) flexmock( project_class, get_files=lambda ref, filter_regex: [], is_private=lambda: False, ) flexmock(LocalProject).should_receive("reset").with_args("HEAD").once() flexmock(IssueCommentGitlabEvent).should_receive("db_trigger").and_return( flexmock(id=123, job_config_trigger_type=JobConfigTriggerType.release) ) flexmock(IssueModel).should_receive("get_or_create").and_return( flexmock(id=123, job_config_trigger_type=JobConfigTriggerType.release) ) flexmock(Signature).should_receive("apply_async").once() flexmock(Pushgateway).should_receive("push").once().and_return() flexmock(shutil).should_receive("rmtree").with_args("") processing_results = SteveJobs().process_message(comment_event) event_dict, job, job_config, package_config = get_parameters_from_results( processing_results ) assert json.dumps(event_dict) results = run_propose_downstream_handler( package_config=package_config, event=event_dict, job_config=job_config, ) assert first_dict_value(results["job"])["success"]
def test_srpm_build_end_failure(srpm_build_end, pc_build_pr, srpm_build_model): srpm_build_end["status"] = COPR_API_FAIL_STATE pr = flexmock(source_project=flexmock()) flexmock(GithubProject).should_receive("is_private").and_return(False) flexmock(GithubProject).should_receive("get_pr").and_return(pr) flexmock(AbstractCoprBuildEvent).should_receive( "get_package_config").and_return(pc_build_pr) flexmock(CoprBuildModel).should_receive("get_all_by_build_id").and_return( [flexmock(target="fedora-33-x86_64")]) (flexmock(CoprBuildJobHelper).should_receive("get_build").with_args( 3122876).and_return( flexmock(source_package={"url": "https://my.host/my.srpm" })).at_least().once()) flexmock(Pushgateway).should_receive("push").once().and_return() flexmock(CoprBuildJobHelper).should_receive( "monitor_not_submitted_copr_builds") flexmock(SRPMBuildModel).should_receive("get_by_copr_build_id").and_return( srpm_build_model) srpm_build_model.should_call("set_status").with_args("failure").once() srpm_build_model.should_receive("set_end_time").once() url = get_srpm_build_info_url(1) flexmock(StatusReporter).should_receive("report").with_args( state=BaseCommitStatus.failure, description="SRPM build failed, check the logs for details.", url=url, check_names=["rpm-build:fedora-33-x86_64"], markdown_content=None, ).once() flexmock(Signature).should_receive("apply_async").once() flexmock(srpm_build_model).should_receive("set_url").with_args( "https://my.host/my.srpm").mock() processing_results = SteveJobs().process_message(srpm_build_end) event_dict, job, job_config, package_config = get_parameters_from_results( processing_results) assert json.dumps(event_dict) results = run_copr_build_end_handler( package_config=package_config, event=event_dict, job_config=job_config, ) assert not first_dict_value(results["job"])["success"]
def test_koji_build_start(koji_build_scratch_start, pc_koji_build_pr, koji_build_pr): koji_build_pr.target = "rawhide" flexmock(GithubProject).should_receive("is_private").and_return(False) flexmock(KojiTaskEvent).should_receive("get_package_config").and_return( pc_koji_build_pr) flexmock(KojiBuildModel).should_receive("get_by_build_id").and_return( koji_build_pr) url = get_koji_build_info_url(1) flexmock(requests).should_receive("get").and_return(requests.Response()) flexmock( requests.Response).should_receive("raise_for_status").and_return(None) koji_build_pr.should_receive("set_build_start_time").once() koji_build_pr.should_receive("set_build_finished_time").with_args( None).once() koji_build_pr.should_receive("set_status").with_args("running").once() koji_build_pr.should_receive("set_build_logs_url") koji_build_pr.should_receive("set_web_url") # check if packit-service set correct PR status flexmock(StatusReporter).should_receive("report").with_args( state=BaseCommitStatus.running, description="RPM build is in progress...", url=url, check_names="production-build:rawhide", markdown_content=None, ).once() flexmock(Signature).should_receive("apply_async").once() flexmock(Pushgateway).should_receive("push").once().and_return() processing_results = SteveJobs().process_message(koji_build_scratch_start) event_dict, job, job_config, package_config = get_parameters_from_results( processing_results) assert json.dumps(event_dict) results = run_koji_build_report_handler( package_config=package_config, event=event_dict, job_config=job_config, ) assert first_dict_value(results["job"])["success"]
def test_pr_comment_build_handler(mock_pr_comment_functionality, pr_build_comment_event): flexmock(CoprBuildJobHelper).should_receive("run_copr_build").and_return( TaskResults(success=True, details={})) (flexmock(GithubProject).should_receive("can_merge_pr").with_args( "phracek").and_return(True).once()) flexmock(GithubProject, get_files="foo.spec") flexmock(GithubProject).should_receive("is_private").and_return(False) flexmock(Signature).should_receive("apply_async").once() processing_results = SteveJobs().process_message(pr_build_comment_event) event_dict, package_config, job = get_parameters_from_results( processing_results) results = run_pr_comment_copr_build_handler( package_config=package_config, event=event_dict, job_config=job, ) assert first_dict_value(results["job"])["success"]
def test_check_rerun_push_testing_farm_handler( mock_push_functionality, check_rerun_event_testing_farm ): flexmock(TestingFarmJobHelper).should_receive("run_testing_farm").once().and_return( TaskResults(success=True, details={}) ) flexmock(GithubProject).should_receive("get_files").and_return(["foo.spec"]) flexmock(GithubProject).should_receive("get_web_url").and_return( "https://github.com/the-namespace/the-repo" ) flexmock(GithubProject).should_receive("is_private").and_return(False) flexmock(TestingFarmJobHelper).should_receive("get_latest_copr_build").and_return( flexmock(status=PG_BUILD_STATUS_SUCCESS) ) flexmock(copr_build).should_receive("get_valid_build_targets").and_return( {"fedora-rawhide-x86_64", "fedora-34-x86_64"} ) flexmock(StatusReporterGithubChecks).should_receive("set_status").with_args( state=BaseCommitStatus.pending, description=TASK_ACCEPTED, check_name="testing-farm:fedora-rawhide-x86_64", url="", links_to_external_services=None, markdown_content=None, ).once() flexmock(Signature).should_receive("apply_async").once() flexmock(Pushgateway).should_receive("push").twice().and_return() processing_results = SteveJobs().process_message(check_rerun_event_testing_farm) event_dict, job, job_config, package_config = get_parameters_from_results( processing_results ) assert event_dict["targets_override"] == ["fedora-rawhide-x86_64"] assert json.dumps(event_dict) results = run_testing_farm_handler( package_config=package_config, event=event_dict, job_config=job_config, ) assert first_dict_value(results["job"])["success"]
def test_koji_build_end(koji_build_scratch_end, pc_koji_build_pr, koji_build_pr): koji_build_pr.target = "rawhide" flexmock(GithubProject).should_receive("is_private").and_return(False) flexmock(KojiBuildEvent).should_receive("get_package_config").and_return( pc_koji_build_pr) flexmock(KojiBuildModel).should_receive("get_by_build_id").and_return( koji_build_pr) url = get_koji_build_info_url_from_flask(1) flexmock(requests).should_receive("get").and_return(requests.Response()) flexmock( requests.Response).should_receive("raise_for_status").and_return(None) koji_build_pr.should_receive("set_build_start_time").once() koji_build_pr.should_receive("set_build_finished_time").once() koji_build_pr.should_receive("set_status").with_args("success").once() koji_build_pr.should_receive("set_build_logs_url") koji_build_pr.should_receive("set_web_url") # 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="packit-stg/production-build-rawhide", ).once() flexmock(Signature).should_receive("apply_async").once() processing_results = SteveJobs().process_message(koji_build_scratch_end) event_dict, package_config, job = get_parameters_from_results( processing_results) results = run_koji_build_report_handler( package_config=package_config, event=event_dict, job_config=job, ) assert first_dict_value(results["job"])["success"]
def test_issue_comment_propose_downstream_handler( mock_issue_comment_functionality, issue_comment_propose_downstream_event): flexmock(PackitAPI).should_receive("sync_release").and_return( PullRequest( title="foo", description="bar", target_branch="baz", source_branch="yet", id=1, status=PRStatus.open, url="https://xyz", author="me", created=datetime.now(), )) flexmock( GithubProject, get_files=lambda ref, filter_regex: [], get_web_url=lambda: "https://github.com/the-namespace/the-repo", is_private=lambda: False, ) flexmock(IssueCommentEvent, db_trigger=IssueModel(id=123)) flexmock(IssueModel).should_receive("get_by_id").with_args(123).and_return( flexmock(issue_id=12345)) flexmock(Signature).should_receive("apply_async").once() processing_results = SteveJobs().process_message( issue_comment_propose_downstream_event) event_dict, job, job_config, package_config = get_parameters_from_results( processing_results) results = run_propose_downstream_handler( package_config=package_config, event=event_dict, job_config=job_config, ) assert first_dict_value(results["job"])["success"]
def test_srpm_build_start(srpm_build_start, pc_build_pr, srpm_build_model): pr = flexmock(source_project=flexmock()) flexmock(GithubProject).should_receive("is_private").and_return(False) flexmock(GithubProject).should_receive("get_pr").and_return(pr) flexmock(AbstractCoprBuildEvent).should_receive( "get_package_config").and_return(pc_build_pr) flexmock(CoprBuildModel).should_receive("get_all_by_build_id").and_return( [flexmock(target="fedora-33-x86_64")]) flexmock(Pushgateway).should_receive("push").once().and_return() flexmock(SRPMBuildModel).should_receive("get_by_copr_build_id").and_return( srpm_build_model) flexmock(SRPMBuildModel).should_receive("set_start_time") flexmock(SRPMBuildModel).should_receive("set_build_logs_url") url = get_srpm_build_info_url(1) flexmock(StatusReporter).should_receive("report").with_args( state=BaseCommitStatus.running, description="SRPM build is in progress...", url=url, check_names=["rpm-build:fedora-33-x86_64"], markdown_content=None, ).once() flexmock(Signature).should_receive("apply_async").once() processing_results = SteveJobs().process_message(srpm_build_start) event_dict, job, job_config, package_config = get_parameters_from_results( processing_results) assert json.dumps(event_dict) results = run_copr_build_start_handler( package_config=package_config, event=event_dict, job_config=job_config, ) assert first_dict_value(results["job"])["success"]
def test_pr_comment_production_build_handler(pr_production_build_comment_event): packit_yaml = str( { "specfile_path": "the-specfile.spec", "synced_files": [], "jobs": [ { "trigger": "pull_request", "job": "production_build", "metadata": {"targets": "fedora-rawhide-x86_64", "scratch": "true"}, } ], } ) comment = flexmock(add_reaction=lambda reaction: None) flexmock( GithubProject, full_repo_name="packit-service/hello-world", get_file_content=lambda path, ref: packit_yaml, get_files=lambda ref, filter_regex: ["the-specfile.spec"], get_web_url=lambda: "https://github.com/the-namespace/the-repo", get_pr=lambda pr_id: flexmock( head_commit="12345", get_comment=lambda comment_id: comment ), ) flexmock(Github, get_repo=lambda full_name_or_id: None) config = ServiceConfig() config.command_handler_work_dir = SANDCASTLE_WORK_DIR flexmock(ServiceConfig).should_receive("get_service_config").and_return(config) trigger = flexmock( job_config_trigger_type=JobConfigTriggerType.pull_request, id=123 ) flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(trigger) flexmock(PullRequestModel).should_receive("get_by_id").with_args(123).and_return( trigger ) flexmock(LocalProject, refresh_the_arguments=lambda: None) flexmock(Allowlist, check_and_report=True) flexmock(PullRequestModel).should_receive("get_or_create").with_args( pr_id=9, namespace="packit-service", repo_name="hello-world", project_url="https://github.com/packit-service/hello-world", ).and_return( flexmock(id=9, job_config_trigger_type=JobConfigTriggerType.pull_request) ) flexmock(KojiBuildJobHelper).should_receive("run_koji_build").and_return( TaskResults(success=True, details={}) ) flexmock(GithubProject, get_files="foo.spec") flexmock(GithubProject).should_receive("is_private").and_return(False) flexmock(KojiBuildJobHelper).should_receive("report_status_to_build").with_args( description=TASK_ACCEPTED, state=BaseCommitStatus.pending, url="", ).once() flexmock(Signature).should_receive("apply_async").once() flexmock(Pushgateway).should_receive("push").twice().and_return() pr = flexmock(head_commit="12345") flexmock(GithubProject).should_receive("get_pr").and_return(pr) comment = flexmock() flexmock(pr).should_receive("get_comment").and_return(comment) flexmock(comment).should_receive("add_reaction").with_args("+1").once() processing_results = SteveJobs().process_message(pr_production_build_comment_event) event_dict, job, job_config, package_config = get_parameters_from_results( processing_results ) assert json.dumps(event_dict) results = run_koji_build_handler( package_config=package_config, event=event_dict, job_config=job_config, ) assert first_dict_value(results["job"])["success"]
def test_bodhi_update_for_unknown_koji_build(koji_build_completed_old_format): packit_yaml = ("{'specfile_path': 'packit.spec', 'synced_files': []," "'jobs': [{'trigger': 'commit', 'job': 'bodhi_update'," "'metadata': {'dist_git_branches': ['rawhide']}}]," "'downstream_package_name': 'packit'}") pagure_project = flexmock( PagureProject, full_repo_name="rpms/packit", get_web_url=lambda: "https://src.fedoraproject.org/rpms/packit", default_branch="main", ) pagure_project.should_receive("get_files").with_args( ref="0eb3e12005cb18f15d3054020f7ac934c01eae08", filter_regex=r".+\.spec$").and_return(["packit.spec"]) pagure_project.should_receive("get_file_content").with_args( path=".distro/source-git.yaml", ref="0eb3e12005cb18f15d3054020f7ac934c01eae08").and_raise( FileNotFoundError, "Not found.") pagure_project.should_receive("get_file_content").with_args( path=".packit.yaml", ref="0eb3e12005cb18f15d3054020f7ac934c01eae08").and_return(packit_yaml) flexmock(ServiceConfig).should_receive("get_service_config").and_return( ServiceConfig( command_handler_work_dir=SANDCASTLE_WORK_DIR, repository_cache="/tmp/repository-cache", add_repositories_to_repository_cache=False, )) flexmock(LocalProject, refresh_the_arguments=lambda: None) flexmock(RepositoryCache).should_call("__init__").once() # 1*CreateBodhiUpdateHandler + 1*KojiBuildReportHandler flexmock(Signature).should_receive("apply_async").times(2) flexmock(Pushgateway).should_receive("push").once().and_return() flexmock(PackitAPI).should_receive("create_update").with_args( dist_git_branch="rawhide", update_type="enhancement", update_notes=DEFAULT_BODHI_NOTE, koji_builds=["1864700"], ) # Database structure run_model_flexmock = flexmock() git_branch_model_flexmock = flexmock( id=1, job_config_trigger_type=JobConfigTriggerType.commit) flexmock(KojiBuildModel).should_receive("get_by_build_id").with_args( build_id=1864700).and_return(None) flexmock(GitBranchModel).should_receive("get_or_create").and_return( git_branch_model_flexmock) flexmock(RunModel).should_receive("create").and_return(run_model_flexmock) flexmock(KojiBuildModel).should_receive("create").with_args( build_id="1864700", commit_sha="0eb3e12005cb18f15d3054020f7ac934c01eae08", web_url="https://koji.fedoraproject.org/koji/taskinfo?taskID=79721403", target="noarch", status="COMPLETE", run_model=run_model_flexmock, ).and_return( flexmock(get_trigger_object=lambda: git_branch_model_flexmock)) processing_results = SteveJobs().process_message( koji_build_completed_old_format) # 1*CreateBodhiUpdateHandler + 1*KojiBuildReportHandler assert len(processing_results) == 2 processing_results.pop() event_dict, job, job_config, package_config = get_parameters_from_results( processing_results) assert json.dumps(event_dict) results = run_bodhi_update( package_config=package_config, event=event_dict, job_config=job_config, ) assert first_dict_value(results["job"])["success"]
def test_downstream_koji_build_report_unknown_build(koji_build_fixture, request): koji_build_event = request.getfixturevalue(koji_build_fixture.__name__) packit_yaml = ("{'specfile_path': 'python-ogr.spec', 'synced_files': []," "'jobs': [{'trigger': 'commit', 'job': 'koji_build'}]," "'downstream_package_name': 'python-ogr'}") pagure_project = flexmock( PagureProject, full_repo_name="rpms/python-ogr", get_web_url=lambda: "https://src.fedoraproject.org/rpms/python-ogr", default_branch="main", ) pagure_project.should_receive("get_files").with_args( ref="e029dd5250dde9a37a2cdddb6d822d973b09e5da", filter_regex=r".+\.spec$").and_return(["python-ogr.spec"]) pagure_project.should_receive("get_file_content").with_args( path=".distro/source-git.yaml", ref="e029dd5250dde9a37a2cdddb6d822d973b09e5da").and_raise( FileNotFoundError, "Not found.") pagure_project.should_receive("get_file_content").with_args( path=".packit.yaml", ref="e029dd5250dde9a37a2cdddb6d822d973b09e5da").and_return(packit_yaml) flexmock(GitBranchModel).should_receive("get_or_create").with_args( branch_name="main", namespace="rpms", repo_name="python-ogr", project_url="https://src.fedoraproject.org/rpms/python-ogr", ).and_return( flexmock(id=9, job_config_trigger_type=JobConfigTriggerType.commit)) flexmock(ServiceConfig).should_receive("get_service_config").and_return( ServiceConfig( command_handler_work_dir=SANDCASTLE_WORK_DIR, repository_cache="/tmp/repository-cache", add_repositories_to_repository_cache=False, )) # 1*KojiBuildReportHandler flexmock(Signature).should_receive("apply_async").once() flexmock(Pushgateway).should_receive("push").once().and_return() # Database run_model_flexmock = flexmock() git_branch_model_flexmock = flexmock( id=1, job_config_trigger_type=JobConfigTriggerType.commit) flexmock(KojiBuildModel).should_receive("get_by_build_id").with_args( build_id=1864700).and_return(None) flexmock(GitBranchModel).should_receive("get_or_create").and_return( git_branch_model_flexmock) flexmock(RunModel).should_receive("create").and_return(run_model_flexmock) flexmock(KojiBuildModel).should_receive("create").with_args( build_id="1864700", commit_sha="0eb3e12005cb18f15d3054020f7ac934c01eae08", web_url="https://koji.fedoraproject.org/koji/taskinfo?taskID=79721403", target="noarch", status="BUILDING", run_model=run_model_flexmock, ).and_return( flexmock(get_trigger_object=lambda: git_branch_model_flexmock)) flexmock(KojiBuildModel).should_receive("get_by_build_id").with_args( build_id=1874074).and_return( flexmock( target="noarch", status="BUILDING", web_url= "https://koji.fedoraproject.org/koji/taskinfo?taskID=79721403", build_logs_url=None, get_trigger_object=lambda: flexmock( id=1, job_config_trigger_type=JobConfigTriggerType.commit), ).should_receive("set_build_logs_url").with_args().and_return(). mock()).once() # only when running a handler processing_results = SteveJobs().process_message(koji_build_event) # 1*KojiBuildReportHandler event_dict, job, job_config, package_config = get_parameters_from_results( processing_results) assert json.dumps(event_dict) results = run_downstream_koji_build_report( package_config=package_config, event=event_dict, job_config=job_config, ) assert first_dict_value(results["job"])["success"]