Merge pull request #1729 from cheater/develop

Adding the gid_from_name parameter to the user.present state
This commit is contained in:
Thomas S Hatch 2012-07-30 15:17:29 -07:00
commit 75229d1463
2 changed files with 45 additions and 0 deletions

View file

@ -101,6 +101,7 @@ def present(
name,
uid=None,
gid=None,
gid_from_name=False,
groups=None,
home=True,
password=None,
@ -126,6 +127,9 @@ def present(
gid
The default group id
gid_from_name
If True, the default group id will be set to the id of the group with the same name as the user.
groups
A list of groups to assign the user to, pass a list object
@ -178,6 +182,8 @@ def present(
'result': True,
'comment': 'User {0} is present and up to date'.format(name)}
if gid_from_name:
gid = __salt__['file.group_to_gid'](name)
changes = _changes(
name,
uid,

View file

@ -7,6 +7,7 @@ user present with custom homedir
import os
from saltunittest import skipIf
import integration
import grp
class UserTest(integration.ModuleCase):
@ -47,6 +48,44 @@ class UserTest(integration.ModuleCase):
self.assertTrue(os.stat('/var/lib/salt_test'))
ret = self.run_state('user.absent', name='salt_test')
@skipIf(not (os.geteuid()==0), 'you must be this root to run this test')
def test_user_present_gid_from_name_default(self):
"""
This is a DESTRUCTIVE TEST it creates a new user on the on the minion.
This is an integration test. Not all systems will automatically create
a group of the same name as the user, but I don't have access to any.
If you run the test and it fails, please fix the code it's testing to
work on your operating system.
"""
ret = self.run_state('user.present', name='salt_test',
gid_from_name=True, home='/var/lib/salt_test')
gid = self.run_function('user.info', ['salt_test'])['gid']
result = ret[next(iter(ret))]['result']
group_name = grp.getgrgid(gid).gr_name
self.assertTrue(result)
self.assertTrue(os.stat('/var/lib/salt_test'))
self.assertTrue(group_name == 'salt_test')
ret = self.run_state('user.absent', name='salt_test')
@skipIf(not (os.geteuid()==0), 'you must be this root to run this test')
def test_user_present_gid_from_name(self):
"""
This is a DESTRUCTIVE TEST it creates a new user on the on the minion.
This is a unit test, NOT an integration test. We create a group of the
same name as the user beforehand, so it should all run smoothly.
"""
ret = self.run_state('group.present', name='salt_test')
ret = self.run_state('user.present', name='salt_test',
gid_from_name=True, home='/var/lib/salt_test')
gid = self.run_function('user.info', ['salt_test'])['gid']
result = ret[next(iter(ret))]['result']
group_name = grp.getgrgid(gid).gr_name
self.assertTrue(result)
self.assertTrue(os.stat('/var/lib/salt_test'))
self.assertTrue(group_name == 'salt_test')
ret = self.run_state('user.absent', name='salt_test')
ret = self.run_state('group.absent', name='salt_test')
if __name__ == '__main__':
from integration import run_tests