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

[3006.x] Fix 64169 #64171

Merged
merged 8 commits into from
May 10, 2023
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
2 changes: 2 additions & 0 deletions changelog/64169.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Call global logger when catching pip.list exceptions in states.pip.installed
Rename global logger `log` to `logger` inside pip_state
26 changes: 15 additions & 11 deletions salt/states/pip_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def pip_has_exceptions_mod(ver):

# pylint: enable=import-error

log = logging.getLogger(__name__)
logger = logging.getLogger(__name__)

# Define the module's virtual name
__virtualname__ = "pip"
Expand Down Expand Up @@ -189,10 +189,10 @@ def _check_pkg_version_format(pkg):
# vcs+URL urls are not properly parsed.
# The next line is meant to trigger an AttributeError and
# handle lower pip versions
log.debug("Installed pip version: %s", pip.__version__)
logger.debug("Installed pip version: %s", pip.__version__)
install_req = _from_line(pkg)
except AttributeError:
log.debug("Installed pip version is lower than 1.2")
logger.debug("Installed pip version is lower than 1.2")
supported_vcs = ("git", "svn", "hg", "bzr")
if pkg.startswith(supported_vcs):
for vcs in supported_vcs:
Expand Down Expand Up @@ -251,7 +251,7 @@ def _check_if_installed(
index_url,
extra_index_url,
pip_list=False,
**kwargs
**kwargs,
):
"""
Takes a package name and version specification (if any) and checks it is
Expand Down Expand Up @@ -351,7 +351,7 @@ def _pep440_version_cmp(pkg1, pkg2, ignore_epoch=False):
making the comparison.
"""
if HAS_PKG_RESOURCES is False:
log.warning(
logger.warning(
"The pkg_resources packages was not loaded. Please install setuptools."
)
return None
Expand All @@ -367,7 +367,9 @@ def _pep440_version_cmp(pkg1, pkg2, ignore_epoch=False):
if pkg_resources.parse_version(pkg1) > pkg_resources.parse_version(pkg2):
return 1
except Exception as exc: # pylint: disable=broad-except
log.exception(exc)
logger.exception(
f'Comparison of package versions "{pkg1}" and "{pkg2}" failed: {exc}'
)
return None


Expand Down Expand Up @@ -418,7 +420,7 @@ def installed(
cache_dir=None,
no_binary=None,
extra_args=None,
**kwargs
**kwargs,
):
"""
Make sure the package is installed
Expand Down Expand Up @@ -852,7 +854,9 @@ def installed(
)
# If we fail, then just send False, and we'll try again in the next function call
except Exception as exc: # pylint: disable=broad-except
log.exception(exc)
logger.exception(
f"Pre-caching of PIP packages during states.pip.installed failed by exception from pip.list: {exc}"
)
pip_list = False

for prefix, state_pkg_name, version_spec in pkgs_details:
Expand All @@ -872,7 +876,7 @@ def installed(
index_url,
extra_index_url,
pip_list,
**kwargs
**kwargs,
)
# If _check_if_installed result is None, something went wrong with
# the command running. This way we keep stateful output.
Expand Down Expand Up @@ -978,7 +982,7 @@ def installed(
no_cache_dir=no_cache_dir,
extra_args=extra_args,
disable_version_check=True,
**kwargs
**kwargs,
)

if pip_install_call and pip_install_call.get("retcode", 1) == 0:
Expand Down Expand Up @@ -1043,7 +1047,7 @@ def installed(
user=user,
cwd=cwd,
env_vars=env_vars,
**kwargs
**kwargs,
)
)

Expand Down
71 changes: 71 additions & 0 deletions tests/pytests/unit/states/test_pip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
:codeauthor: Eric Graham <eric.graham@vantagepnt.com>
"""
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 as exc:
# Observed behavior in #64169
pytest.fail(
"Regression on #64169: pip_state.installed seems to be throwing an unexpected AttributeException: "
f"{exc}"
)

# Take 64169 further and actually confirm that the exception from pip.list got logged.
assert (
"Pre-caching of PIP packages during states.pip.installed failed by exception "
f"from pip.list: {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