add tests for _getgrall and local vs remote group handling

This commit is contained in:
nicholasmhughes 2023-08-28 20:17:39 -04:00 committed by Gareth J. Greenaway
parent 0be8c475d9
commit 0af4490c0f
2 changed files with 65 additions and 0 deletions

View file

@ -0,0 +1,36 @@
from textwrap import dedent
import pytest
pytest.importorskip("grp")
import grp
import salt.utils.user
@pytest.fixture()
def etc_group(tmp_path):
etcgrp = tmp_path / "etc" / "group"
etcgrp.parent.mkdir()
etcgrp.write_text(
dedent(
"""games:x:50:
docker:x:959:debian,salt
salt:x:1000:"""
)
)
return etcgrp
def test__getgrall(etc_group):
group_lines = [
["games", "x", 50, []],
["docker", "x", 959, ["debian", "salt"]],
["salt", "x", 1000, []],
]
expected_grall = [grp.struct_group(comps) for comps in group_lines]
grall = salt.utils.user._getgrall(root=str(etc_group.parent.parent))
assert grall == expected_grall

View file

@ -0,0 +1,29 @@
from types import SimpleNamespace
import pytest
from tests.support.mock import MagicMock, patch
pytest.importorskip("grp")
import grp
import salt.utils.user
def test_get_group_list():
getpwname = SimpleNamespace(pw_gid=1000)
getgrgid = MagicMock(side_effect=[SimpleNamespace(gr_name="remote")])
group_lines = [
["games", "x", 50, []],
["salt", "x", 1000, []],
]
getgrall = [grp.struct_group(comps) for comps in group_lines]
with patch("os.getgrouplist", MagicMock(return_value=[50, 1000, 12000])), patch(
"pwd.getpwnam", MagicMock(return_value=getpwname)
), patch("salt.utils.user._getgrall", MagicMock(return_value=getgrall)), patch(
"grp.getgrgid", getgrgid
):
group_list = salt.utils.user.get_group_list("salt")
assert group_list == ["games", "remote", "salt"]
getgrgid.assert_called_once()