mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Fix the pwd.getpwnam
caching issue on macOS user module
This commit is contained in:
parent
9829b5f8a3
commit
84b5e4fa5b
4 changed files with 23 additions and 5 deletions
1
changelog/60676.fixed
Normal file
1
changelog/60676.fixed
Normal file
|
@ -0,0 +1 @@
|
|||
Fix the ``pwd.getpwnam`` caching issue on macOS user module
|
|
@ -382,8 +382,10 @@ def info(name):
|
|||
salt '*' user.info root
|
||||
"""
|
||||
try:
|
||||
data = pwd.getpwnam(name)
|
||||
except KeyError:
|
||||
# pwd.getpwnam seems to cache weirdly, after an account is
|
||||
# deleted, it still returns data. Let's not use it
|
||||
data = next(iter(x for x in pwd.getpwall() if x.pw_name == name))
|
||||
except StopIteration:
|
||||
return {}
|
||||
else:
|
||||
return _format_info(data)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import pathlib
|
||||
|
||||
import pytest
|
||||
import salt.utils.platform
|
||||
from saltfactories.utils import random_string
|
||||
|
||||
pytestmark = [
|
||||
|
@ -57,3 +58,17 @@ def test_delete_remove(user, account, remove):
|
|||
assert pathlib.Path(user_info["home"]).exists() is False
|
||||
else:
|
||||
assert pathlib.Path(user_info["home"]).exists() is True
|
||||
|
||||
|
||||
def test_info_after_deletion(user, account):
|
||||
"""
|
||||
This test targets a situation where, at least on macOS, the call to ``user.info(username)``
|
||||
returns data after the account has been deleted from the system.
|
||||
It's a weird caching issue with ``pwd.getpwnam``
|
||||
"""
|
||||
kwargs = {}
|
||||
if not salt.utils.platform.is_windows():
|
||||
kwargs["remove"] = True
|
||||
ret = user.delete(account.username, **kwargs)
|
||||
assert ret is True
|
||||
assert not user.info(account.username)
|
||||
|
|
|
@ -357,18 +357,18 @@ def test_info():
|
|||
Tests the return of user information
|
||||
"""
|
||||
mock_pwnam = pwd.struct_passwd(
|
||||
("test", "*", 0, 0, "TEST USER", "/var/test", "/bin/bash")
|
||||
("root", "*", 0, 0, "TEST USER", "/var/test", "/bin/bash")
|
||||
)
|
||||
ret = {
|
||||
"shell": "/bin/bash",
|
||||
"name": "test",
|
||||
"name": "root",
|
||||
"gid": 0,
|
||||
"groups": ["_TEST_GROUP"],
|
||||
"home": "/var/test",
|
||||
"fullname": "TEST USER",
|
||||
"uid": 0,
|
||||
}
|
||||
with patch("pwd.getpwnam", MagicMock(return_value=mock_pwnam)), patch(
|
||||
with patch("pwd.getpwall", MagicMock(return_value=[mock_pwnam])), patch(
|
||||
"salt.modules.mac_user.list_groups", MagicMock(return_value=["_TEST_GROUP"])
|
||||
):
|
||||
assert mac_user.info("root") == ret
|
||||
|
|
Loading…
Add table
Reference in a new issue