-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
71 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Import python libs | ||
from __future__ import absolute_import, print_function, unicode_literals | ||
|
||
import grp | ||
import os | ||
import pwd | ||
|
||
# Import salt libs | ||
import salt.utils.platform | ||
import salt.utils.user | ||
from tests.support.mock import patch | ||
from tests.support.runtests import this_user | ||
|
||
# Import Salt Testing libs | ||
from tests.support.unit import TestCase, skipIf | ||
|
||
|
||
class TestUser(TestCase): | ||
@skipIf(salt.utils.platform.is_windows(), "Module not available on Windows") | ||
def test_chugid_and_umask(self): | ||
|
||
running_user = this_user() | ||
running_group = grp.getgrgid(os.getgid()).gr_name | ||
|
||
gids = {30: "expectedgroup", 20: running_group} | ||
getgrnams = { | ||
"expectedgroup": grp.struct_group( | ||
("expectedgroup", "*", 30, ["expecteduser"]) | ||
), | ||
running_group: grp.struct_group((running_group, "*", 20, [running_user])), | ||
} | ||
getpwnams = { | ||
"expecteduser": pwd.struct_passwd( | ||
("expecteduser", "x", 30, 30, "-", "-", "-") | ||
), | ||
running_user: pwd.struct_passwd((running_user, "x", 20, 20, "-", "-", "-")), | ||
} | ||
|
||
def getgrnam(group): | ||
return getgrnams[group] | ||
|
||
def getpwnam(user): | ||
return getpwnams[user] | ||
|
||
def getgrgid(gid): | ||
return getgrnams[gids[gid]] | ||
|
||
with patch("grp.getgrgid", getgrgid): | ||
with patch("grp.getgrnam", getgrnam): | ||
with patch("pwd.getpwnam", getpwnam): | ||
with patch("salt.utils.user.chugid") as chugid_mock: | ||
salt.utils.user.chugid_and_umask( | ||
"expecteduser", umask=None, group=running_group | ||
) | ||
chugid_mock.assert_called_with("expecteduser", running_group) | ||
salt.utils.user.chugid_and_umask( | ||
"expecteduser", umask=None, group=None | ||
) | ||
chugid_mock.assert_called_with("expecteduser", "expectedgroup") |