Skip to content

Commit b5a47d9

Browse files
authored
Merge branch 'master' into cs/elasticsearch-module-deprecation
2 parents edea9e4 + a686ce0 commit b5a47d9

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

changelog/65458.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pip.installed state will now properly fail when a specified user does not exists

salt/states/pip_state.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,13 @@ def installed(
839839
ret["comment"] = "\n".join(comments)
840840
return ret
841841

842+
# If the user does not exist, stop here with error:
843+
if user and "user.info" in __salt__ and not __salt__["user.info"](user):
844+
# The user does not exists, exit with result set to False
845+
ret["result"] = False
846+
ret["comment"] = f"User {user} does not exist"
847+
return ret
848+
842849
# If a requirements file is specified, only install the contents of the
843850
# requirements file. Similarly, using the --editable flag with pip should
844851
# also ignore the "name" and "pkgs" parameters.

tests/unit/states/test_pip_state.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,24 @@ def test_install_in_editable_mode(self):
379379
self.assertSaltTrueReturn({"test": ret})
380380
self.assertInSaltComment("successfully installed", {"test": ret})
381381

382+
def test_install_with_specified_user(self):
383+
"""
384+
Check that if `user` parameter is set and the user does not exists
385+
it will fail with an error, see #65458
386+
"""
387+
user_info = MagicMock(return_value={})
388+
pip_version = MagicMock(return_value="10.0.1")
389+
with patch.dict(
390+
pip_state.__salt__,
391+
{
392+
"user.info": user_info,
393+
"pip.version": pip_version,
394+
},
395+
):
396+
ret = pip_state.installed("mypkg", user="fred")
397+
self.assertSaltFalseReturn({"test": ret})
398+
self.assertInSaltComment("User fred does not exist", {"test": ret})
399+
382400

383401
class PipStateUtilsTest(TestCase):
384402
def test_has_internal_exceptions_mod_function(self):
@@ -414,7 +432,7 @@ def test_importable_installation_error(self):
414432
extra_requirements = []
415433
for name, version in salt.version.dependency_information():
416434
if name in ["PyYAML", "packaging", "looseversion"]:
417-
extra_requirements.append("{}=={}".format(name, version))
435+
extra_requirements.append(f"{name}=={version}")
418436
failures = {}
419437
pip_version_requirements = [
420438
# Latest pip 18
@@ -453,7 +471,7 @@ def test_importable_installation_error(self):
453471
with VirtualEnv() as venv:
454472
venv.install(*extra_requirements)
455473
if requirement:
456-
venv.install("pip{}".format(requirement))
474+
venv.install(f"pip{requirement}")
457475
try:
458476
subprocess.check_output([venv.venv_python, "-c", code])
459477
except subprocess.CalledProcessError as exc:

0 commit comments

Comments
 (0)