Fix the pwd.getpwnam caching issue on macOS user module

This commit is contained in:
Pedro Algarvio 2021-08-06 16:52:37 +01:00 committed by Megan Wilhite
parent 76cd13a9d9
commit 5e7b9b6be0
2 changed files with 18 additions and 3 deletions

View file

@ -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)

View file

@ -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