def test_process_message(event): packit_yaml = { "specfile_path": "", "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", ) flexmock(LocalProject, refresh_the_arguments=lambda: None) config = Config() config.command_handler_work_dir = SANDCASTLE_WORK_DIR flexmock(Config).should_receive("get_service_config").and_return(config) flexmock(PackitAPI).should_receive("sync_release").with_args( dist_git_branch="master", version="1.2.3", create_pr=False ).once() flexmock(Whitelist, check_and_report=True) flexmock(SteveJobs, _is_private=False) results = SteveJobs().process_message(event) assert "propose_downstream" in results.get("jobs", {}) assert results["trigger"] == str(JobTriggerType.release)
def test_dist_git_push_release_handle(release_event): packit_yaml = ( "{'specfile_path': '', 'synced_files': []" ", jobs: [{trigger: release, job: propose_downstream, metadata: {targets:[]}}]}" ) flexmock(Github, get_repo=lambda full_name_or_id: None) flexmock( GithubProject, get_file_content=lambda path, ref: packit_yaml, full_repo_name="packit-service/hello-world", ) flexmock(LocalProject, refresh_the_arguments=lambda: None) flexmock(Whitelist, check_and_report=True) steve = SteveJobs() flexmock(SteveJobs, _is_private=False) config = Config() config.command_handler_work_dir = SANDCASTLE_WORK_DIR flexmock(Config).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", create_pr=False).once() results = steve.process_message(release_event) assert "propose_downstream" in results.get("jobs", {}) assert results.get("jobs", {}).get("propose_downstream", {}).get("success") assert results["trigger"] == str(JobTriggerType.release)
def process_message(event: dict, topic: str = None): try: steve = SteveJobs() steve.process_message(event=event, topic=topic) except Exception as ex: logger.error("There was an exception while processing the event.") logger.exception(ex)
def test_copr_build_not_comment_on_success(copr_build_end, pc_build_pr, copr_build_pr): steve = SteveJobs() flexmock(GithubProject).should_receive("is_private").and_return(False) 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_packit_comment_with_congratulation").and_return(True) 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") copr_build_pr.should_receive("set_end_time").once() url = get_copr_build_log_url_from_flask(1) flexmock(requests).should_receive("get").and_return(requests.Response()) flexmock( requests.Response).should_receive("raise_for_status").and_return(None) # check if packit-service set correct PR status flexmock(StatusReporter).should_receive("report").with_args( state=CommitStatus.success, description="RPMs were built successfully.", url=url, check_names=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)
def test_copr_build_start(copr_build_start, pc_build_pr, copr_build_pr): steve = SteveJobs() flexmock(GithubProject).should_receive("is_private").and_return(False) 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_copr_build_log_url_from_flask(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_start_time").once() 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 is in progress...", url=url, check_names=EXPECTED_BUILD_CHECK_NAME, ).once() steve.process_message(copr_build_start)
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)
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)
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_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)
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)
def test_github_app_installation(installation): steve = SteveJobs() github_app_response = steve.get_job_input_from_github_app_installation( installation) assert github_app_response trigger, github_app = github_app_response github_app.installation_id = 1173510 github_app.account_login = "******" github_app.account_id = 26160778 github_app.account_url = "https://api.github.com/users/rpitonak" github_app.account_type = "User" github_app.created_at = 1560941425 github_app.sender_id = 26160778 github_app.sender_login = "******"
def test_pr_comment_wrong_packit_command_handler( mock_pr_comment_functionality, pr_wrong_packit_comment_event): flexmock(GithubProject).should_receive("is_private").and_return(False) flexmock(GithubProject).should_receive("can_merge_pr").and_return(True) results = SteveJobs().process_message(pr_wrong_packit_comment_event) assert results == []
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_pr_comment_copr_build_handler(mock_pr_comment_functionality, pr_copr_build_comment_event): flexmock(CoprBuildHandler).should_receive("run_copr_build").and_return( HandlerResults(success=True, details={})) steve = SteveJobs() flexmock(SteveJobs, _is_private=False) results = steve.process_message(pr_copr_build_comment_event) assert results.get("jobs", {}) assert "pull_request_action" in results.get("jobs", {}) assert "created" in results.get("event", {}).get("action", None) assert results.get("event", {}).get("pr_id", None) == 9 assert "comment" in results.get("trigger", None) assert results.get("event", {}).get("comment", None) == "/packit copr-build" assert results.get("jobs", {}).get("pull_request_action", {}).get("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_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_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", ) 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) flexmock(SteveJobs, _is_private=False) results = SteveJobs().process_message(event) assert "propose_downstream" in results["jobs"] assert results["event"]["trigger"] == "release"
def test_issue_comment_propose_update_handler( mock_issue_comment_functionality, issue_comment_propose_update_event): flexmock(PackitAPI).should_receive("sync_release").and_return( HandlerResults(success=True, details={})) flexmock(SteveJobs, _is_private=False) results = SteveJobs().process_message(issue_comment_propose_update_event) assert results["jobs"]["pull_request_action"]["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( HandlerResults(success=True, details={})) steve = SteveJobs() flexmock(SteveJobs, _is_private=False) results = steve.process_message(issue_comment_propose_update_event) assert results.get("jobs", {}) assert "pull_request_action" in results.get("jobs", {}) assert "created" in results.get("event", {}).get("action", None) assert results.get("event", {}).get("issue_id", None) == 512 assert "comment" in results.get("trigger", None) assert results.get("event", {}).get("comment", None) == "/packit propose-update" assert results.get("jobs", {}).get("pull_request_action", {}).get("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_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_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_pr_comment_empty_handler(mock_pr_comment_functionality, pr_empty_comment_event): flexmock(GithubProject).should_receive("is_private").and_return(False) results = SteveJobs().process_message(pr_empty_comment_event) msg = "comment '' is empty." one_job_finished_with_msg(results, msg)
def test_pr_comment_wrong_packit_command_handler( mock_pr_comment_functionality, pr_wrong_packit_comment_event): flexmock(GithubProject).should_receive("is_private").and_return(False) results = SteveJobs().process_message(pr_wrong_packit_comment_event) msg = "comment '/packit foobar' does not contain a valid packit-service command." one_job_finished_with_msg(results, msg)
def test_trigger_packit_command_without_config( pr_embedded_command_comment_event, comments ): flexmock( GithubProject, full_repo_name="namespace/repo", # just throwing an exception get_file_content=lambda path, ref: (_ for _ in ()).throw(FileNotFoundError), get_files=lambda ref, filter_regex: ["specfile.spec"], get_web_url=lambda: "https://github.com/namespace/repo", ) pr_embedded_command_comment_event["comment"]["body"] = comments flexmock(GithubProject).should_receive("is_private").and_return(False) pr = flexmock(head_commit="12345") flexmock(GithubProject).should_receive("get_pr").and_return(pr) err_msg = ( "No config file for packit (e.g. `.packit.yaml`) found in namespace/repo on commit 12345" "\n\nFor more info, please check out the documentation: " "https://packit.dev/docs/packit-service or contact us - [Packit team]" "(https://github.com/orgs/packit/teams/the-packit-team)" ) flexmock(pr).should_receive("comment").with_args(err_msg) with pytest.raises(PackitConfigException) as exc: SteveJobs().process_message(pr_embedded_command_comment_event) assert "No config file found in " in exc.value
def test_dist_git_push_release_handle_multiple_branches(release_event): 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) flexmock( GithubProject, get_file_content=lambda path, ref: packit_yaml, full_repo_name="packit-service/hello-world", get_files=lambda filter_regex: [], ) flexmock(LocalProject, refresh_the_arguments=lambda: None) flexmock(Whitelist, check_and_report=True) flexmock(SteveJobs, _is_private=False) config = ServiceConfig() config.command_handler_work_dir = SANDCASTLE_WORK_DIR 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(PackitAPI).should_receive("sync_release").with_args( dist_git_branch="f32", version="0.3.0").once() flexmock(PackitAPI).should_receive("sync_release").with_args( dist_git_branch="f31", version="0.3.0").once() flexmock(PackitAPI).should_receive("sync_release").with_args( dist_git_branch="f30", version="0.3.0").once() flexmock(FedPKG).should_receive("clone").and_return(None) results = SteveJobs().process_message(release_event) assert results["jobs"]["propose_downstream"]["success"] assert results["event"]["trigger"] == "release"
def test_copr_build_not_comment_on_success(copr_build_end): steve = SteveJobs() flexmock(SteveJobs, _is_private=False) flexmock(CoprBuildEvent).should_receive("get_package_config").and_return( flexmock()) flexmock(PRCheckName).should_receive("get_build_check").and_return( PACKIT_STG_CHECK) flexmock(CoprBuildEndHandler).should_receive( "was_last_build_successful").and_return(True) flexmock(GithubProject).should_receive("pr_comment").never() 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 = (f"https://copr-be.cloud.fedoraproject.org/results/" f"packit/packit-service-hello-world-24-stg/fedora-rawhide-x86_64/" f"01044215-hello/builder-live.log.gz") 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(BuildStatusReporter).should_receive("report").with_args( state="success", description="RPMs were built successfully.", url=url, check_names=PACKIT_STG_CHECK, ).once() # skip testing farm flexmock(CoprBuildEndHandler).should_receive( "get_tests_for_build").and_return(None) steve.process_message(copr_build_end)
def test_pr_test_command_handler(pr_embedded_command_comment_event): jobs = [{ "trigger": "pull_request", "job": "tests", "metadata": { "targets": "fedora-rawhide-x86_64" }, }] packit_yaml = ( "{'specfile_path': 'the-specfile.spec', 'synced_files': [], 'jobs': " + str(jobs) + "}") flexmock( GithubProject, full_repo_name="packit-service/hello-world", get_file_content=lambda path, ref: packit_yaml, get_files=lambda ref, filter_regex: ["the-specfile.spec"], get_web_url=lambda: "https://github.com/the-namespace/the-repo", get_pr=lambda pr_id: flexmock(head_commit="12345"), ) flexmock(Github, get_repo=lambda full_name_or_id: None) config = ServiceConfig() config.command_handler_work_dir = SANDCASTLE_WORK_DIR flexmock(ServiceConfig).should_receive("get_service_config").and_return( config) trigger = flexmock( job_config_trigger_type=JobConfigTriggerType.pull_request, id=123) flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return( trigger) flexmock(PullRequestModel).should_receive("get_by_id").with_args( 123).and_return(trigger) flexmock(LocalProject, refresh_the_arguments=lambda: None) flexmock(Allowlist, check_and_report=True) flexmock(PullRequestModel).should_receive("get_or_create").with_args( pr_id=9, namespace="packit-service", repo_name="hello-world", project_url="https://github.com/packit-service/hello-world", ).and_return( flexmock(id=9, job_config_trigger_type=JobConfigTriggerType.pull_request)) pr_embedded_command_comment_event["comment"]["body"] = "/packit test" flexmock(GithubProject, get_files="foo.spec") flexmock(GithubProject).should_receive("is_private").and_return(False) flexmock(Signature).should_receive("apply_async").once() flexmock(TestingFarmJobHelper).should_receive( "run_testing_farm_on_all").and_return( TaskResults(success=True, details={})) processing_results = SteveJobs().process_message( pr_embedded_command_comment_event) event_dict, job, job_config, package_config = get_parameters_from_results( processing_results) run_testing_farm_handler( package_config=package_config, event=event_dict, job_config=job_config, )
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_pr_comment_wrong_packit_command_handler( mock_pr_comment_functionality, pr_wrong_packit_comment_event ): flexmock(SteveJobs, _is_private=False) results = SteveJobs().process_message(pr_wrong_packit_comment_event) assert results["jobs"]["pull_request_action"]["success"] msg = "comment '/packit foobar' does not contain a valid packit-service command." assert results["jobs"]["pull_request_action"]["details"]["msg"] == msg