From 3c552ecb907ee956250b0fd09bab96a34b3420af Mon Sep 17 00:00:00 2001 From: Eric Graham Date: Wed, 26 Apr 2023 18:27:11 -0500 Subject: [PATCH] Add unit test for #64169 --- tests/pytests/unit/states/test_pip.py | 69 +++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tests/pytests/unit/states/test_pip.py diff --git a/tests/pytests/unit/states/test_pip.py b/tests/pytests/unit/states/test_pip.py new file mode 100644 index 00000000000..7e04602ce44 --- /dev/null +++ b/tests/pytests/unit/states/test_pip.py @@ -0,0 +1,69 @@ +""" + :codeauthor: Eric Graham +""" +import logging + +import pytest + +import salt.states.pip_state as pip_state +from salt.exceptions import CommandExecutionError +from tests.support.mock import MagicMock, patch + + +@pytest.fixture +def configure_loader_modules(): + return { + pip_state: { + '__env__': 'base', + '__opts__': { + 'test': False + } + } + } + + +def test_issue_64169(caplog): + pkg_to_install = 'nonexistent_package' + exception_message = 'Invalid JSON (test_issue_64169)' + + mock_pip_list = MagicMock(side_effect=[ + CommandExecutionError(exception_message), # pre-cache the pip list (preinstall) + {}, # Checking if the pkg is already installed + {pkg_to_install: '100.10.1'} # Confirming successful installation + ]) + mock_pip_version = MagicMock(return_value='100.10.1') + mock_pip_install = MagicMock(return_value={"retcode": 0, "stdout": ""}) + + with patch.dict(pip_state.__salt__, { + "pip.list": mock_pip_list, + "pip.version": mock_pip_version, + "pip.install": mock_pip_install + }): + with caplog.at_level(logging.WARNING): + # Call pip.installed with a specifically 'broken' pip.list. + # pip.installed should continue, but log the exception from pip.list. + # pip.installed should NOT raise an exception itself. + # noinspection PyBroadException + try: + pip_state.installed( + name=pkg_to_install, + use_wheel=False, # Set False to simplify testing + no_use_wheel=False, # ' + no_binary=False, # ' + log=None # Regression will cause this function call to throw + # an AttributeError + ) + except AttributeError: + # Observed behavior in #64169 + assert False + except: + # Something went wrong, but it isn't what's being tested for here. + return + + # Take 64169 further and actually confirm that the targeted exception from pip.list got logged. + assert exception_message in caplog.messages + + # Confirm that the state continued to install the package as expected. + # Only check the 'pkgs' parameter of pip.install + mock_install_call_args, mock_install_call_kwargs = mock_pip_install.call_args + assert mock_install_call_kwargs['pkgs'] == pkg_to_install