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 3 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 gloabl logger `log` to `logger` inside pip_state
14 changes: 8 additions & 6 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 @@ -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,7 @@ 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 @@ -852,7 +852,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 Down
76 changes: 76 additions & 0 deletions tests/pytests/unit/states/test_pip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
: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:
# Observed behavior in #64169
assert False
vps-eric marked this conversation as resolved.
Show resolved Hide resolved
except:
vps-eric marked this conversation as resolved.
Show resolved Hide resolved
# Something went wrong, but it isn't what's being tested for here.
return

# Take 64169 further and actually confirm that the exception from pip.list got logged.
exc_msg_present = False
for log_line in caplog.messages:
# The exception must be somewhere in the log, but may optionally not be on a line by itself.
if exception_message in log_line:
exc_msg_present = True
break

assert exc_msg_present
vps-eric marked this conversation as resolved.
Show resolved Hide resolved

# 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