From 071a65fb10a72e23b9e22d11f7da6957b2c05f7c Mon Sep 17 00:00:00 2001 From: Eric Graham Date: Mon, 1 May 2023 10:55:29 -0500 Subject: [PATCH] Rename Global Logger log to logger in pip_state.py --- changelog/64169.fixed.md | 1 + salt/states/pip_state.py | 14 ++++++++------ tests/pytests/unit/states/test_pip.py | 11 +++++++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/changelog/64169.fixed.md b/changelog/64169.fixed.md index 499b94b693b..fe80eff1e94 100644 --- a/changelog/64169.fixed.md +++ b/changelog/64169.fixed.md @@ -1 +1,2 @@ Call global logger when catching pip.list exceptions in states.pip.installed +Rename gloabl logger `log` to `logger` inside pip_state \ No newline at end of file diff --git a/salt/states/pip_state.py b/salt/states/pip_state.py index fd99d6bd626..cc5d877c06e 100644 --- a/salt/states/pip_state.py +++ b/salt/states/pip_state.py @@ -114,7 +114,7 @@ if HAS_PIP is True: # pylint: enable=import-error -log = logging.getLogger(__name__) +logger = logging.getLogger(__name__) # Define the module's virtual name __virtualname__ = "pip" @@ -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: @@ -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 @@ -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 @@ -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 - globals().get("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: diff --git a/tests/pytests/unit/states/test_pip.py b/tests/pytests/unit/states/test_pip.py index 7e04602ce44..1b6d8afb364 100644 --- a/tests/pytests/unit/states/test_pip.py +++ b/tests/pytests/unit/states/test_pip.py @@ -60,8 +60,15 @@ def test_issue_64169(caplog): # 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 + # 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 # Confirm that the state continued to install the package as expected. # Only check the 'pkgs' parameter of pip.install