diff --git a/tests/test_rpm_verifier.py b/tests/test_rpm_verifier.py index dc6f3c0..60f1e6a 100644 --- a/tests/test_rpm_verifier.py +++ b/tests/test_rpm_verifier.py @@ -2,6 +2,7 @@ from pathlib import Path from subprocess import CalledProcessError from textwrap import dedent +from typing import Callable from unittest.mock import MagicMock, call, create_autospec, sentinel import pytest @@ -355,24 +356,34 @@ def mock_image_processor(self, monkeypatch: MonkeyPatch) -> MagicMock: return mock @pytest.fixture() - def mock_generate_output(self, monkeypatch: MonkeyPatch) -> MagicMock: - """Monkey-patched generate_output""" - mock = create_autospec(generate_output, return_value=(False, "")) - monkeypatch.setattr(rpm_verifier, generate_output.__name__, mock) - return mock + def create_generate_output_mock( + self, monkeypatch: MonkeyPatch + ) -> Callable[[bool], MagicMock]: + """Create a generate_output mock with different results according to + the `fail_unsigned` flag""" + + def _mock_generate_output(fail: bool = False) -> MagicMock: + """Monkey-patched generate_output""" + mock = create_autospec(generate_output, return_value=(fail, "")) + monkeypatch.setattr(rpm_verifier, generate_output.__name__, mock) + return mock + + return _mock_generate_output def test_main( self, - mock_generate_output: MagicMock, + create_generate_output_mock: MagicMock, mock_image_processor: MagicMock, ) -> None: """Test call to rpm_verifier.py main function""" + fail_on_unsigned_rpms: bool = False + generate_output_mock = create_generate_output_mock(fail=fail_on_unsigned_rpms) rpm_verifier.main( # pylint: disable=no-value-for-parameter args=[ "--input", "img1", "--fail-unsigned", - "true", + fail_on_unsigned_rpms, "--workdir", "some/path", ], @@ -381,5 +392,32 @@ def test_main( ) assert mock_image_processor.return_value.call_count == 1 + generate_output_mock.assert_called_once_with( + [sentinel.output], fail_on_unsigned_rpms + ) mock_image_processor.return_value.assert_has_calls([call("img1")]) - mock_generate_output.assert_called_once_with([sentinel.output], True) + + def test_main_fail_on_unsigned_rpm( + self, + create_generate_output_mock: MagicMock, + mock_image_processor: MagicMock, + ) -> None: + """Test call to rpm_verifier.py main function fails + when whe 'fail-unsigned' flag is used and there are unsigned RPMs + """ + fail_on_unsigned_rpms: bool = True + create_generate_output_mock(fail=fail_on_unsigned_rpms) + with pytest.raises(SystemExit): + rpm_verifier.main( # pylint: disable=no-value-for-parameter + args=[ + "--input", + "img1", + "--fail-unsigned", + fail_on_unsigned_rpms, + "--workdir", + "some/path", + ], + obj={}, + standalone_mode=False, + ) + assert mock_image_processor.return_value.call_count == 1