Merge pull request #45365 from meaksh/2017.7-issue-45345

Return an error when "gid_from_name" is set but group does not exist
This commit is contained in:
Nicole Thomas 2018-01-16 13:31:49 -05:00 committed by GitHub
commit 5f58a87e84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 15 deletions

View File

@ -240,7 +240,8 @@ def present(name,
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, Default is ``False``.
the same name as the user. If the group does not exist the state will
fail. Default is ``False``.
groups
A list of groups to assign the user to, pass a list object. If a group
@ -455,6 +456,10 @@ def present(name,
if gid_from_name:
gid = __salt__['file.group_to_gid'](name)
if gid == '':
ret['comment'] = 'Default group with name "{0}" is not present'.format(name)
ret['result'] = False
return ret
changes = _changes(name,
uid,

View File

@ -97,15 +97,25 @@ class UserTest(ModuleCase, SaltReturnAssertsMixin):
home=HOMEDIR)
self.assertSaltTrueReturn(ret)
def test_user_present_nondefault(self):
@requires_system_grains
def test_user_present_nondefault(self, grains=None):
'''
This is a DESTRUCTIVE TEST it creates a new user on the on the minion.
'''
ret = self.run_state('user.present', name=self.user_name,
home=self.user_home)
self.assertSaltTrueReturn(ret)
ret = self.run_function('user.info', [self.user_name])
self.assertReturnNonEmptySaltType(ret)
group_name = grp.getgrgid(ret['gid']).gr_name
if not salt.utils.is_darwin():
self.assertTrue(os.path.isdir(self.user_home))
if grains['os_family'] in ('Suse',):
self.assertEqual(group_name, 'users')
elif grains['os_family'] == 'MacOS':
self.assertEqual(group_name, 'staff')
else:
self.assertEqual(group_name, self.user_name)
@requires_system_grains
def test_user_present_gid_from_name_default(self, grains=None):
@ -120,22 +130,26 @@ class UserTest(ModuleCase, SaltReturnAssertsMixin):
# user
gid_from_name = False if grains['os_family'] == 'MacOS' else True
ret = self.run_state('user.present', name=self.user_name,
ret_user_present = self.run_state('user.present', name=self.user_name,
gid_from_name=gid_from_name, home=self.user_home)
self.assertSaltTrueReturn(ret)
ret = self.run_function('user.info', [self.user_name])
self.assertReturnNonEmptySaltType(ret)
group_name = grp.getgrgid(ret['gid']).gr_name
if not salt.utils.is_darwin():
self.assertTrue(os.path.isdir(self.user_home))
if grains['os_family'] in ('Suse',):
self.assertEqual(group_name, 'users')
elif grains['os_family'] == 'MacOS':
self.assertEqual(group_name, 'staff')
if gid_from_name:
self.assertSaltFalseReturn(ret_user_present)
ret_user_present = ret_user_present[next(iter(ret_user_present))]
self.assertTrue('is not present' in ret_user_present['comment'])
else:
self.assertEqual(group_name, self.user_name)
self.assertSaltTrueReturn(ret_user_present)
ret_user_info = self.run_function('user.info', [self.user_name])
self.assertReturnNonEmptySaltType(ret_user_info)
group_name = grp.getgrgid(ret_user_info['gid']).gr_name
if not salt.utils.is_darwin():
self.assertTrue(os.path.isdir(self.user_home))
if grains['os_family'] in ('Suse',):
self.assertEqual(group_name, 'users')
elif grains['os_family'] == 'MacOS':
self.assertEqual(group_name, 'staff')
else:
self.assertEqual(group_name, self.user_name)
def test_user_present_gid_from_name(self):
'''