|
| 1 | +# Copyright Contributors to the Packit project. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import datetime |
| 5 | +import json |
| 6 | + |
| 7 | +import pytest |
| 8 | +from celery.canvas import group as celery_group |
| 9 | +from flexmock import flexmock |
| 10 | +from packit.api import PackitAPI |
| 11 | +from packit.config import ( |
| 12 | + CommonPackageConfig, |
| 13 | + JobConfig, |
| 14 | + JobConfigTriggerType, |
| 15 | + JobType, |
| 16 | + PackageConfig, |
| 17 | +) |
| 18 | + |
| 19 | +from packit_service.events import ( |
| 20 | + koji, |
| 21 | + openscanhub, |
| 22 | +) |
| 23 | +from packit_service.models import ( |
| 24 | + BuildStatus, |
| 25 | + KojiBuildTargetModel, |
| 26 | + OSHScanModel, |
| 27 | + ProjectEventModelType, |
| 28 | +) |
| 29 | +from packit_service.worker.handlers import KojiOpenScanHubTaskFinishedHandler |
| 30 | +from packit_service.worker.handlers.koji import KojiOpenScanHubHelper |
| 31 | +from packit_service.worker.helpers import open_scan_hub |
| 32 | +from packit_service.worker.helpers.build import KojiBuildJobHelper |
| 33 | +from packit_service.worker.jobs import SteveJobs |
| 34 | +from packit_service.worker.monitoring import Pushgateway |
| 35 | +from packit_service.worker.reporting import BaseCommitStatus |
| 36 | +from packit_service.worker.tasks import ( |
| 37 | + run_openscanhub_task_finished_handler, |
| 38 | + run_openscanhub_task_started_handler, |
| 39 | +) |
| 40 | +from tests.spellbook import DATA_DIR, get_parameters_from_results |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture() |
| 44 | +def openscanhub_task_finished_event(): |
| 45 | + with open(DATA_DIR / "fedmsg" / "open_scan_hub_task_finished.json") as outfile: |
| 46 | + return json.load(outfile) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture() |
| 50 | +def openscanhub_task_started_event(): |
| 51 | + with open(DATA_DIR / "fedmsg" / "open_scan_hub_task_started.json") as outfile: |
| 52 | + return json.load(outfile) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture() |
| 56 | +def prepare_openscanhub_db_and_handler( |
| 57 | + add_pull_request_event_with_sha_123456, |
| 58 | +): |
| 59 | + db_project_object, db_project_event = add_pull_request_event_with_sha_123456 |
| 60 | + db_build = ( |
| 61 | + flexmock( |
| 62 | + build_id="55", |
| 63 | + identifier=None, |
| 64 | + status="success", |
| 65 | + build_submitted_time=datetime.datetime.utcnow(), |
| 66 | + target="the-target", |
| 67 | + owner="the-owner", |
| 68 | + project_name="the-namespace-repo_name-5", |
| 69 | + commit_sha="123456", |
| 70 | + project_event=flexmock(), |
| 71 | + srpm_build=flexmock(url=None) |
| 72 | + .should_receive("set_url") |
| 73 | + .with_args("https://some.host/my.srpm") |
| 74 | + .mock(), |
| 75 | + ) |
| 76 | + .should_receive("get_project_event_object") |
| 77 | + .and_return(db_project_object) |
| 78 | + .mock() |
| 79 | + .should_receive("get_project_event_model") |
| 80 | + .and_return(db_project_event) |
| 81 | + .mock() |
| 82 | + ) |
| 83 | + |
| 84 | + flexmock(celery_group).should_receive("apply_async") |
| 85 | + scan_mock = flexmock( |
| 86 | + id=123, |
| 87 | + koji_build_target=db_build, |
| 88 | + url="https://openscanhub.fedoraproject.org/task/17514/", |
| 89 | + set_issues_added_url=lambda _: None, |
| 90 | + set_issues_fixed_url=lambda _: None, |
| 91 | + set_scan_results_url=lambda _: None, |
| 92 | + ) |
| 93 | + flexmock(OSHScanModel).should_receive("get_by_task_id").and_return(scan_mock) |
| 94 | + flexmock(Pushgateway).should_receive("push").and_return() |
| 95 | + yield scan_mock |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.parametrize( |
| 99 | + "build_models", |
| 100 | + [ |
| 101 | + [ |
| 102 | + ( |
| 103 | + "abcdef", |
| 104 | + [flexmock(identifier=None, get_srpm_build=lambda: flexmock(url="base-srpm-url"))], |
| 105 | + ) |
| 106 | + ], |
| 107 | + [ |
| 108 | + ("abcdef", []), |
| 109 | + ( |
| 110 | + "fedcba", |
| 111 | + [flexmock(identifier=None, get_srpm_build=lambda: flexmock(url="base-srpm-url"))], |
| 112 | + ), |
| 113 | + ], |
| 114 | + ], |
| 115 | +) |
| 116 | +def test_handle_scan(build_models): |
| 117 | + srpm_mock = flexmock(url="https://some-url/my-srpm.src.rpm") |
| 118 | + # flexmock(koji.KojiBuild).should_receive("from_event_dict").and_return( |
| 119 | + # flexmock(chroot="fedora-rawhide-x86_64", build_id="123", pr_id=12), |
| 120 | + # ) |
| 121 | + # flexmock(open_scan_hub).should_receive("download_file").twice().and_return(True) |
| 122 | + |
| 123 | + # for commit_sha, models in build_models: |
| 124 | + # flexmock(KojiBuildTargetModel).should_receive("get_all_by").with_args( |
| 125 | + # commit_sha=commit_sha, |
| 126 | + # project_name="commit-project", |
| 127 | + # owner="user-123", |
| 128 | + # target="fedora-rawhide-x86_64", |
| 129 | + # status=BuildStatus.success, |
| 130 | + # ).and_return(models).once() |
| 131 | + |
| 132 | + flexmock(PackitAPI).should_receive("run_osh_build").once().and_return( |
| 133 | + 'some\nmultiline\noutput\n{"id": 123}\nand\nmore\n{"url": "scan-url"}\n', |
| 134 | + ) |
| 135 | + |
| 136 | + flexmock(KojiBuildJobHelper).should_receive("_report") |
| 137 | + package_config = flexmock( |
| 138 | + get_job_views=lambda: [ |
| 139 | + flexmock( |
| 140 | + type=JobType.koji_build, |
| 141 | + trigger=JobConfigTriggerType.commit, |
| 142 | + branch="main", |
| 143 | + project="commit-project", |
| 144 | + owner="user-123", |
| 145 | + identifier=None, |
| 146 | + ), |
| 147 | + ], |
| 148 | + ) |
| 149 | + |
| 150 | + project = flexmock( |
| 151 | + get_pr=lambda pr_id: flexmock( |
| 152 | + target_branch="main", |
| 153 | + target_branch_head_commit="abcdef", |
| 154 | + ), |
| 155 | + get_commits=lambda ref: ["abcdef", "fedcba"], |
| 156 | + ) |
| 157 | + |
| 158 | + KojiOpenScanHubHelper( |
| 159 | + build=flexmock( |
| 160 | + id=1, |
| 161 | + get_srpm_build=lambda: srpm_mock, |
| 162 | + target="fedora-rawhide-x86_64", |
| 163 | + scan=None, |
| 164 | + get_project_event_model=lambda: flexmock( |
| 165 | + type=ProjectEventModelType.pull_request, |
| 166 | + get_project_event_object=lambda: flexmock(), |
| 167 | + ), |
| 168 | + ) |
| 169 | + .should_receive("add_scan_transaction") |
| 170 | + .once() |
| 171 | + .and_return(flexmock()) |
| 172 | + .mock(), |
| 173 | + koji_build_helper=KojiBuildJobHelper( |
| 174 | + service_config=flexmock(), |
| 175 | + package_config=package_config, |
| 176 | + project=project, |
| 177 | + metadata=flexmock(pr_id=12), |
| 178 | + db_project_event=flexmock(get_project_event_object=lambda: None), |
| 179 | + job_config=flexmock(identifier=None), |
| 180 | + ), |
| 181 | + ).handle_scan() |
| 182 | + |
| 183 | + |
| 184 | +@pytest.mark.parametrize( |
| 185 | + "job_config_type,job_config_trigger,job_config_targets,scan_status,num_of_handlers", |
| 186 | + [ |
| 187 | + ( |
| 188 | + JobType.koji_build, |
| 189 | + JobConfigTriggerType.commit, |
| 190 | + ["fedora-rawhide-x86_64"], |
| 191 | + openscanhub.task.Status.success, |
| 192 | + 0, |
| 193 | + ), |
| 194 | + ( |
| 195 | + JobType.koji_build, |
| 196 | + JobConfigTriggerType.pull_request, |
| 197 | + ["fedora-stable"], |
| 198 | + openscanhub.task.Status.success, |
| 199 | + 0, |
| 200 | + ), |
| 201 | +# ( |
| 202 | +# JobType.koji_build, |
| 203 | +# JobConfigTriggerType.pull_request, |
| 204 | +# ["fedora-rawhide-x86_64"], |
| 205 | +# openscanhub.task.Status.success, |
| 206 | +# 1, |
| 207 | +# ), |
| 208 | +# ( |
| 209 | +# JobType.koji_build, |
| 210 | +# JobConfigTriggerType.pull_request, |
| 211 | +# ["fedora-rawhide-x86_64"], |
| 212 | +# openscanhub.task.Status.fail, |
| 213 | +# 1, |
| 214 | +# ), |
| 215 | +# ( |
| 216 | +# JobType.koji_build, |
| 217 | +# JobConfigTriggerType.pull_request, |
| 218 | +# ["fedora-rawhide-x86_64"], |
| 219 | +# openscanhub.task.Status.cancel, |
| 220 | +# 1, |
| 221 | +# ), |
| 222 | + ( |
| 223 | + JobType.koji_build, |
| 224 | + JobConfigTriggerType.commit, |
| 225 | + ["fedora-rawhide-x86_64"], |
| 226 | + openscanhub.task.Status.interrupt, |
| 227 | + 0, |
| 228 | + ), |
| 229 | + ], |
| 230 | +) |
| 231 | +def test_handle_scan_task_finished( |
| 232 | + openscanhub_task_finished_event, |
| 233 | + prepare_openscanhub_db_and_handler, |
| 234 | + job_config_type, |
| 235 | + job_config_trigger, |
| 236 | + job_config_targets, |
| 237 | + scan_status, |
| 238 | + num_of_handlers, |
| 239 | +): |
| 240 | + flexmock(openscanhub.task.Finished).should_receive( |
| 241 | + "get_packages_config", |
| 242 | + ).and_return( |
| 243 | + PackageConfig( |
| 244 | + jobs=[ |
| 245 | + JobConfig( |
| 246 | + type=job_config_type, |
| 247 | + trigger=job_config_trigger, |
| 248 | + packages={ |
| 249 | + "package": CommonPackageConfig( |
| 250 | + _targets=job_config_targets, |
| 251 | + specfile_path="test.spec", |
| 252 | + ), |
| 253 | + }, |
| 254 | + ), |
| 255 | + ], |
| 256 | + packages={"package": CommonPackageConfig()}, |
| 257 | + ), |
| 258 | + ) |
| 259 | + |
| 260 | + scan_mock = prepare_openscanhub_db_and_handler |
| 261 | + openscanhub_task_finished_event["status"] = scan_status |
| 262 | + processing_results = SteveJobs().process_message(openscanhub_task_finished_event) |
| 263 | + assert len(processing_results) == num_of_handlers |
| 264 | + |
| 265 | + if processing_results: |
| 266 | + links_to_external_services = { |
| 267 | + "OpenScanHub task": "https://openscanhub.fedoraproject.org/task/17514/" |
| 268 | + } |
| 269 | + if scan_status == openscanhub.task.Status.success: |
| 270 | + state = BaseCommitStatus.success |
| 271 | + description = "Scan in OpenScanHub is finished. 2 new findings identified." |
| 272 | + flexmock(scan_mock).should_receive("set_status").with_args( |
| 273 | + "succeeded", |
| 274 | + ).once() |
| 275 | + flexmock(scan_mock).should_receive("set_issues_added_count").with_args(2).once() |
| 276 | + flexmock(KojiOpenScanHubTaskFinishedHandler).should_receive( |
| 277 | + "get_number_of_new_findings_identified" |
| 278 | + ).and_return(2) |
| 279 | + links_to_external_services.update( |
| 280 | + { |
| 281 | + "Added issues": ( |
| 282 | + "https://openscanhub.fedoraproject.org/task/15649/log/added.html" |
| 283 | + ), |
| 284 | + } |
| 285 | + ) |
| 286 | + elif scan_status == openscanhub.task.Status.cancel: |
| 287 | + state = BaseCommitStatus.neutral |
| 288 | + description = f"Scan in OpenScanHub is finished in a {scan_status} state." |
| 289 | + flexmock(scan_mock).should_receive("set_status").with_args( |
| 290 | + "canceled", |
| 291 | + ).once() |
| 292 | + else: |
| 293 | + state = BaseCommitStatus.neutral |
| 294 | + description = f"Scan in OpenScanHub is finished in a {scan_status} state." |
| 295 | + flexmock(scan_mock).should_receive("set_status").with_args("failed").once() |
| 296 | + if num_of_handlers == 1: |
| 297 | + # one handler is always skipped because it is for fedora-stable -> |
| 298 | + # no rawhide build |
| 299 | + flexmock(KojiOpenScanHubHelper).should_receive("report").with_args( |
| 300 | + state=state, |
| 301 | + description=description, |
| 302 | + url="/jobs/openscanhub/123", |
| 303 | + links_to_external_services=links_to_external_services, |
| 304 | + ).once().and_return() |
| 305 | + |
| 306 | + for sub_results in processing_results: |
| 307 | + event_dict, job, job_config, package_config = get_parameters_from_results( |
| 308 | + [sub_results], |
| 309 | + ) |
| 310 | + assert json.dumps(event_dict) |
| 311 | + |
| 312 | + run_openscanhub_task_finished_handler( |
| 313 | + package_config=package_config, |
| 314 | + event=event_dict, |
| 315 | + job_config=job_config, |
| 316 | + ) |
| 317 | + |
| 318 | + |
| 319 | +@pytest.mark.parametrize( |
| 320 | + "job_config_type,job_config_trigger,job_config_targets,num_of_handlers", |
| 321 | + [ |
| 322 | + ( |
| 323 | + JobType.koji_build, |
| 324 | + JobConfigTriggerType.commit, |
| 325 | + ["fedora-rawhide-x86_64"], |
| 326 | + 0, |
| 327 | + ), |
| 328 | + ( |
| 329 | + JobType.koji_build, |
| 330 | + JobConfigTriggerType.pull_request, |
| 331 | + ["fedora-stable"], |
| 332 | + 0, |
| 333 | + ), |
| 334 | +# ( |
| 335 | +# JobType.koji_build, |
| 336 | +# JobConfigTriggerType.pull_request, |
| 337 | +# ["fedora-rawhide-x86_64"], |
| 338 | +# 1, |
| 339 | +# ), |
| 340 | + ], |
| 341 | +) |
| 342 | +def test_handle_scan_task_started( |
| 343 | + openscanhub_task_started_event, |
| 344 | + prepare_openscanhub_db_and_handler, |
| 345 | + job_config_type, |
| 346 | + job_config_trigger, |
| 347 | + job_config_targets, |
| 348 | + num_of_handlers, |
| 349 | +): |
| 350 | + flexmock(openscanhub.task.Started).should_receive( |
| 351 | + "get_packages_config", |
| 352 | + ).and_return( |
| 353 | + PackageConfig( |
| 354 | + jobs=[ |
| 355 | + JobConfig( |
| 356 | + type=job_config_type, |
| 357 | + trigger=job_config_trigger, |
| 358 | + packages={ |
| 359 | + "package": CommonPackageConfig( |
| 360 | + _targets=job_config_targets, |
| 361 | + specfile_path="test.spec", |
| 362 | + ), |
| 363 | + }, |
| 364 | + ), |
| 365 | + ], |
| 366 | + packages={"package": CommonPackageConfig()}, |
| 367 | + ), |
| 368 | + ) |
| 369 | + |
| 370 | + scan_mock = prepare_openscanhub_db_and_handler |
| 371 | + processing_results = SteveJobs().process_message(openscanhub_task_started_event) |
| 372 | + assert len(processing_results) == num_of_handlers |
| 373 | + |
| 374 | + if processing_results: |
| 375 | + if num_of_handlers == 1: |
| 376 | + flexmock(scan_mock).should_receive("set_status").with_args("running").once() |
| 377 | + flexmock(KojiOpenScanHubHelper).should_receive("report").with_args( |
| 378 | + state=BaseCommitStatus.running, |
| 379 | + description="Scan in OpenScanHub has started.", |
| 380 | + url="https://openscanhub.fedoraproject.org/task/17514/", |
| 381 | + ).once().and_return() |
| 382 | + |
| 383 | + for sub_results in processing_results: |
| 384 | + event_dict, job, job_config, package_config = get_parameters_from_results( |
| 385 | + [sub_results], |
| 386 | + ) |
| 387 | + assert json.dumps(event_dict) |
| 388 | + |
| 389 | + run_openscanhub_task_started_handler( |
| 390 | + package_config=package_config, |
| 391 | + event=event_dict, |
| 392 | + job_config=job_config, |
| 393 | + ) |
0 commit comments