Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(RHTAPWATCH-707): write status to file #44

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 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,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",
[
Expand All @@ -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)
Expand All @@ -350,25 +356,33 @@ def test_main(
fail_unsigned,
"--workdir",
"some/path",
"--status-path",
str(status_path),
],
obj={},
standalone_mode=False,
)

if has_errors:
assert status_path.read_text() == "ERROR"
else:
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=[
Expand All @@ -378,8 +392,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]
21 changes: 18 additions & 3 deletions verify_rpms/rpm_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -166,8 +177,12 @@ def main(img_input: str, fail_unsigned: bool, workdir: Path) -> None:
processor, container_images
)

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

Expand Down