Skip to content

Commit

Permalink
chore(RHTAPWATCH-707): write output to file
Browse files Browse the repository at this point in the history
Signed-off-by: Avi Biton <[email protected]>
  • Loading branch information
avi-biton committed Jan 7, 2024
1 parent 9bde6cb commit 4c25eff
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
24 changes: 17 additions & 7 deletions tests/test_rpm_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -310,7 +310,7 @@ 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
Expand Down Expand Up @@ -350,25 +350,31 @@ def test_main(
fail_unsigned,
"--workdir",
"some/path",
"--output-path",
"a",
"--status-path",
"a",
],
obj={},
standalone_mode=False,
)

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
) -> 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=[
Expand All @@ -378,8 +384,12 @@ def test_main_fail_on_unsigned_rpm_or_errors(
fail_unsigned,
"--workdir",
"some/path",
"--output-path",
"a",
"--status-path",
"a",
],
obj={},
standalone_mode=False,
)
assert err.value.code == sentinel.output_gen_out
assert err.value.code == mock.return_value[1]
23 changes: 22 additions & 1 deletion verify_rpms/rpm_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,25 @@ 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(
"--output-path",
help="Path in which the output will be written to",
type=click.Path(path_type=Path),
required=True,
)
@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,
output_path: Path,
status_path: Path,
) -> None:
"""Verify RPMs are signed"""

container_images = parse_image_input(img_input)
Expand All @@ -167,8 +185,11 @@ def main(img_input: str, fail_unsigned: bool, workdir: Path) -> None:
)

failures_occured, output = generate_output(list(processed_images))
output_path.write_text(output)
if failures_occured and fail_unsigned:
status_path.write_text("ERROR")
sys.exit(output)
status_path.write_text("SUCCESS")
print(output)


Expand Down

0 comments on commit 4c25eff

Please sign in to comment.