From 472234fbd612bf20e7b1afc9182450c4669db3dc Mon Sep 17 00:00:00 2001 From: Avi Biton Date: Sun, 7 Jan 2024 12:40:27 +0200 Subject: [PATCH] chore(RHTAPWATCH-707): write status to file Signed-off-by: Avi Biton --- tests/test_rpm_verifier.py | 32 +++++++++++++++++++++++--------- verify_rpms/rpm_verifier.py | 15 ++++++++++++++- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/tests/test_rpm_verifier.py b/tests/test_rpm_verifier.py index e11ef1d..7359bd9 100644 --- a/tests/test_rpm_verifier.py +++ b/tests/test_rpm_verifier.py @@ -3,7 +3,7 @@ from subprocess import CalledProcessError from textwrap import dedent from typing import Callable -from unittest.mock import MagicMock, call, create_autospec, sentinel +from unittest.mock import MagicMock, call, create_autospec import pytest from pytest import MonkeyPatch @@ -295,7 +295,7 @@ def mock_image_processor(self, monkeypatch: MonkeyPatch) -> MagicMock: """Monkey-patched ImageProcessor""" mock: MagicMock = create_autospec( ImageProcessor, - return_value=MagicMock(return_value=sentinel.output), + return_value=MagicMock(return_value="some output"), ) monkeypatch.setattr(rpm_verifier, ImageProcessor.__name__, mock) return mock @@ -310,13 +310,18 @@ def create_generate_output_mock( def _mock_generate_output(with_failures: bool = False) -> MagicMock: """Monkey-patched generate_output""" mock = create_autospec( - generate_output, return_value=(with_failures, sentinel.output_gen_out) + generate_output, return_value=(with_failures, "some output") ) monkeypatch.setattr(rpm_verifier, generate_output.__name__, mock) return mock return _mock_generate_output + @pytest.fixture() + def status_path(self, tmp_path: Path) -> Path: + """Status temp file""" + return tmp_path / "status" + @pytest.mark.parametrize( "fail_unsigned, has_errors", [ @@ -333,12 +338,13 @@ def _mock_generate_output(with_failures: bool = False) -> MagicMock: ), ], ) - def test_main( + def test_main( # pylint: disable=too-many-arguments self, create_generate_output_mock: MagicMock, mock_image_processor: MagicMock, fail_unsigned: bool, has_errors: bool, + status_path: Path, ) -> None: """Test call to rpm_verifier.py main function""" generate_output_mock = create_generate_output_mock(with_failures=has_errors) @@ -350,25 +356,30 @@ def test_main( fail_unsigned, "--workdir", "some/path", + "--status-path", + str(status_path), ], obj={}, standalone_mode=False, ) - + assert status_path.read_text() == "SUCCESS" assert mock_image_processor.return_value.call_count == 1 - generate_output_mock.assert_called_once_with([sentinel.output]) + generate_output_mock.assert_called_once_with( + [mock_image_processor.return_value.return_value] + ) mock_image_processor.return_value.assert_has_calls([call("img1")]) def test_main_fail_on_unsigned_rpm_or_errors( self, create_generate_output_mock: MagicMock, - mock_image_processor: MagicMock, + mock_image_processor: MagicMock, # pylint: disable=unused-argument + status_path: Path, ) -> None: """Test call to rpm_verifier.py main function fails when whe 'fail-unsigned' flag is used and there are unsigned RPMs """ fail_unsigned: bool = True - create_generate_output_mock(with_failures=True) + mock = create_generate_output_mock(with_failures=True) with pytest.raises(SystemExit) as err: rpm_verifier.main( # pylint: disable=no-value-for-parameter args=[ @@ -378,8 +389,11 @@ def test_main_fail_on_unsigned_rpm_or_errors( fail_unsigned, "--workdir", "some/path", + "--status-path", + str(status_path), ], obj={}, standalone_mode=False, ) - assert err.value.code == sentinel.output_gen_out + assert status_path.read_text() == "ERROR" + assert err.value.code == mock.return_value[1] diff --git a/verify_rpms/rpm_verifier.py b/verify_rpms/rpm_verifier.py index 65183ba..fa4c2d5 100644 --- a/verify_rpms/rpm_verifier.py +++ b/verify_rpms/rpm_verifier.py @@ -155,7 +155,18 @@ def __call__(self, img: str) -> ProcessedImage: type=click.Path(path_type=Path), required=True, ) -def main(img_input: str, fail_unsigned: bool, workdir: Path) -> None: +@click.option( + "--status-path", + help="Path in which the status will be written to", + type=click.Path(path_type=Path), + required=True, +) +def main( + img_input: str, + fail_unsigned: bool, + workdir: Path, + status_path: Path, +) -> None: """Verify RPMs are signed""" container_images = parse_image_input(img_input) @@ -168,7 +179,9 @@ def main(img_input: str, fail_unsigned: bool, workdir: Path) -> None: failures_occured, output = generate_output(list(processed_images)) if failures_occured and fail_unsigned: + status_path.write_text("ERROR") sys.exit(output) + status_path.write_text("SUCCESS") print(output)