2016-08-31 19:21:07 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Import python libs
|
2018-01-18 22:13:08 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2016-08-31 19:21:07 +00:00
|
|
|
import random
|
2017-10-17 21:55:18 +00:00
|
|
|
import string
|
2016-08-31 19:21:07 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.case import ModuleCase
|
2017-04-04 17:57:27 +00:00
|
|
|
from tests.support.helpers import destructiveTest, skip_if_not_root
|
2018-03-23 17:30:22 +00:00
|
|
|
from tests.support.unit import skipIf
|
2016-08-31 19:21:07 +00:00
|
|
|
|
2017-10-17 21:55:18 +00:00
|
|
|
# Import Salt libs
|
2018-01-18 22:13:08 +00:00
|
|
|
from salt.ext import six
|
2016-08-31 19:26:14 +00:00
|
|
|
from salt.ext.six.moves import range
|
2017-10-17 21:55:18 +00:00
|
|
|
import salt.utils.files
|
2018-04-03 18:46:56 +00:00
|
|
|
import salt.utils.platform
|
2018-01-27 22:49:02 +00:00
|
|
|
import salt.utils.stringutils
|
2016-08-31 19:21:07 +00:00
|
|
|
|
2018-04-25 15:36:32 +00:00
|
|
|
if not salt.utils.platform.is_windows():
|
2018-04-25 16:39:58 +00:00
|
|
|
import grp
|
2018-04-25 07:18:08 +00:00
|
|
|
|
2016-08-31 19:21:07 +00:00
|
|
|
|
2017-04-04 17:57:27 +00:00
|
|
|
@skip_if_not_root
|
2017-12-01 19:29:07 +00:00
|
|
|
@destructiveTest
|
2017-04-03 16:04:09 +00:00
|
|
|
class GroupModuleTest(ModuleCase):
|
2016-08-31 19:21:07 +00:00
|
|
|
'''
|
|
|
|
Validate the linux group system module
|
|
|
|
'''
|
|
|
|
|
2017-04-04 17:57:27 +00:00
|
|
|
def setUp(self):
|
|
|
|
'''
|
|
|
|
Get current settings
|
|
|
|
'''
|
|
|
|
super(GroupModuleTest, self).setUp()
|
2016-08-31 19:21:07 +00:00
|
|
|
self._user = self.__random_string()
|
|
|
|
self._user1 = self.__random_string()
|
|
|
|
self._no_user = self.__random_string()
|
|
|
|
self._group = self.__random_string()
|
|
|
|
self._no_group = self.__random_string()
|
2018-03-23 17:30:22 +00:00
|
|
|
self.os_grain = self.run_function('grains.item', ['kernel'])
|
|
|
|
self._gid = 64989 if 'Windows' not in self.os_grain['kernel'] else None
|
|
|
|
self._new_gid = 64998 if 'Windows' not in self.os_grain['kernel'] else None
|
|
|
|
if self.os_grain['kernel'] not in ('Linux', 'Windows'):
|
2016-08-31 19:21:07 +00:00
|
|
|
self.skipTest(
|
|
|
|
'Test not applicable to \'{kernel}\' kernel'.format(
|
2018-03-23 17:30:22 +00:00
|
|
|
**self.os_grain
|
2016-08-31 19:21:07 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
'''
|
|
|
|
Reset to original settings
|
|
|
|
'''
|
|
|
|
self.run_function('user.delete', [self._user])
|
|
|
|
self.run_function('user.delete', [self._user1])
|
|
|
|
self.run_function('group.delete', [self._group])
|
|
|
|
|
|
|
|
def __random_string(self, size=6):
|
|
|
|
'''
|
|
|
|
Generates a random names
|
|
|
|
'''
|
|
|
|
return 'tg-' + ''.join(
|
|
|
|
random.choice(string.ascii_lowercase + string.digits)
|
|
|
|
for x in range(size)
|
|
|
|
)
|
|
|
|
|
2017-08-23 10:30:50 +00:00
|
|
|
def __get_system_group_gid_range(self):
|
|
|
|
'''
|
|
|
|
Returns (SYS_GID_MIN, SYS_GID_MAX)
|
|
|
|
'''
|
2018-01-27 22:49:02 +00:00
|
|
|
try:
|
|
|
|
login_defs = {}
|
|
|
|
with salt.utils.files.fopen('/etc/login.defs') as defs_fd:
|
|
|
|
for line in defs_fd:
|
|
|
|
line = salt.utils.stringutils.to_unicode(line).strip()
|
|
|
|
if line.startswith('#'):
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
key, val = line.split()
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
login_defs[key] = val
|
|
|
|
except OSError:
|
2017-08-23 10:30:50 +00:00
|
|
|
login_defs = {'SYS_GID_MIN': 101,
|
|
|
|
'SYS_GID_MAX': 999}
|
|
|
|
|
|
|
|
gid_min = login_defs.get('SYS_GID_MIN', 101)
|
|
|
|
gid_max = login_defs.get('SYS_GID_MAX',
|
|
|
|
int(login_defs.get('GID_MIN', 1000)) - 1)
|
|
|
|
|
2017-11-14 20:32:39 +00:00
|
|
|
return int(gid_min), int(gid_max)
|
2017-08-23 10:30:50 +00:00
|
|
|
|
|
|
|
def __get_free_system_gid(self):
|
|
|
|
'''
|
|
|
|
Find a free system gid
|
|
|
|
'''
|
|
|
|
|
|
|
|
gid_min, gid_max = self.__get_system_group_gid_range()
|
|
|
|
|
|
|
|
busy_gids = [x.gr_gid
|
|
|
|
for x in grp.getgrall()
|
|
|
|
if gid_min <= x.gr_gid <= gid_max]
|
|
|
|
|
|
|
|
# find free system gid
|
|
|
|
for gid in range(gid_min, gid_max + 1):
|
|
|
|
if gid not in busy_gids:
|
|
|
|
return gid
|
|
|
|
|
2016-08-31 19:21:07 +00:00
|
|
|
@destructiveTest
|
|
|
|
def test_add(self):
|
|
|
|
'''
|
|
|
|
Test the add group function
|
|
|
|
'''
|
2017-10-17 21:55:18 +00:00
|
|
|
# add a new group
|
2018-03-23 17:30:22 +00:00
|
|
|
self.assertTrue(self.run_function('group.add', [self._group], gid=self._gid))
|
2016-08-31 19:21:07 +00:00
|
|
|
group_info = self.run_function('group.info', [self._group])
|
|
|
|
self.assertEqual(group_info['gid'], self._gid)
|
2018-03-23 17:30:22 +00:00
|
|
|
self.assertEqual(group_info['name'], self._group)
|
2017-10-17 21:55:18 +00:00
|
|
|
# try adding the group again
|
2018-03-23 17:30:22 +00:00
|
|
|
if self.os_grain['kernel'] == 'Windows':
|
|
|
|
add_group = self.run_function('group.add', [self._group], gid=self._gid)
|
|
|
|
self.assertEqual(add_group['result'], None)
|
|
|
|
self.assertEqual(add_group['comment'], 'The group {0} already exists.'.format(self._group))
|
|
|
|
self.assertEqual(add_group['changes'], [])
|
|
|
|
else:
|
|
|
|
self.assertFalse(self.run_function('group.add', [self._group], gid=self._gid))
|
2016-08-31 19:21:07 +00:00
|
|
|
|
2017-08-23 10:30:50 +00:00
|
|
|
@destructiveTest
|
2018-04-25 22:26:13 +00:00
|
|
|
@skipIf(salt.utils.platform.is_windows(), 'Skip on Windows')
|
2017-08-23 10:30:50 +00:00
|
|
|
def test_add_system_group(self):
|
|
|
|
'''
|
|
|
|
Test the add group function with system=True
|
|
|
|
'''
|
|
|
|
|
|
|
|
gid_min, gid_max = self.__get_system_group_gid_range()
|
|
|
|
|
|
|
|
# add a new system group
|
|
|
|
self.assertTrue(self.run_function('group.add',
|
|
|
|
[self._group, None, True]))
|
|
|
|
group_info = self.run_function('group.info', [self._group])
|
|
|
|
self.assertEqual(group_info['name'], self._group)
|
|
|
|
self.assertTrue(gid_min <= group_info['gid'] <= gid_max)
|
2017-10-17 21:55:18 +00:00
|
|
|
# try adding the group again
|
2017-08-23 10:30:50 +00:00
|
|
|
self.assertFalse(self.run_function('group.add',
|
|
|
|
[self._group]))
|
|
|
|
|
|
|
|
@destructiveTest
|
2018-04-25 22:26:13 +00:00
|
|
|
@skipIf(salt.utils.platform.is_windows(), 'Skip on Windows')
|
2017-08-23 10:30:50 +00:00
|
|
|
def test_add_system_group_gid(self):
|
|
|
|
'''
|
|
|
|
Test the add group function with system=True and a specific gid
|
|
|
|
'''
|
|
|
|
|
|
|
|
gid = self.__get_free_system_gid()
|
|
|
|
|
|
|
|
# add a new system group
|
|
|
|
self.assertTrue(self.run_function('group.add',
|
|
|
|
[self._group, gid, True]))
|
|
|
|
group_info = self.run_function('group.info', [self._group])
|
|
|
|
self.assertEqual(group_info['name'], self._group)
|
|
|
|
self.assertEqual(group_info['gid'], gid)
|
2017-10-17 21:55:18 +00:00
|
|
|
# try adding the group again
|
2017-08-23 10:30:50 +00:00
|
|
|
self.assertFalse(self.run_function('group.add',
|
|
|
|
[self._group, gid]))
|
|
|
|
|
2016-08-31 19:21:07 +00:00
|
|
|
@destructiveTest
|
|
|
|
def test_delete(self):
|
|
|
|
'''
|
|
|
|
Test the delete group function
|
|
|
|
'''
|
|
|
|
self.assertTrue(self.run_function('group.add', [self._group]))
|
|
|
|
|
2017-10-17 21:55:18 +00:00
|
|
|
# correct functionality
|
2016-08-31 19:21:07 +00:00
|
|
|
self.assertTrue(self.run_function('group.delete', [self._group]))
|
|
|
|
|
2017-10-17 21:55:18 +00:00
|
|
|
# group does not exist
|
2018-03-23 17:30:22 +00:00
|
|
|
if self.os_grain['kernel'] == 'Windows':
|
|
|
|
del_group = self.run_function('group.delete', [self._no_group])
|
|
|
|
self.assertEqual(del_group['changes'], [])
|
|
|
|
self.assertEqual(del_group['comment'], 'The group {0} does not exists.'.format(self._no_group))
|
|
|
|
else:
|
|
|
|
self.assertFalse(self.run_function('group.delete', [self._no_group]))
|
2016-08-31 19:21:07 +00:00
|
|
|
|
|
|
|
def test_info(self):
|
|
|
|
'''
|
|
|
|
Test the info group function
|
|
|
|
'''
|
2018-03-23 17:30:22 +00:00
|
|
|
self.run_function('group.add', [self._group], gid=self._gid)
|
2016-08-31 19:21:07 +00:00
|
|
|
self.run_function('user.add', [self._user])
|
|
|
|
self.run_function('group.adduser', [self._group, self._user])
|
|
|
|
group_info = self.run_function('group.info', [self._group])
|
|
|
|
|
|
|
|
self.assertEqual(group_info['name'], self._group)
|
|
|
|
self.assertEqual(group_info['gid'], self._gid)
|
2018-03-23 17:30:22 +00:00
|
|
|
self.assertIn(self._user, str(group_info['members']))
|
2016-08-31 19:21:07 +00:00
|
|
|
|
2018-04-03 18:46:56 +00:00
|
|
|
@skipIf(salt.utils.platform.is_windows(), 'gid test skipped on windows')
|
2016-08-31 19:21:07 +00:00
|
|
|
def test_chgid(self):
|
|
|
|
'''
|
|
|
|
Test the change gid function
|
|
|
|
'''
|
2018-03-23 17:30:22 +00:00
|
|
|
self.run_function('group.add', [self._group], gid=self._gid)
|
2016-08-31 19:21:07 +00:00
|
|
|
self.assertTrue(self.run_function('group.chgid', [self._group, self._new_gid]))
|
|
|
|
group_info = self.run_function('group.info', [self._group])
|
|
|
|
self.assertEqual(group_info['gid'], self._new_gid)
|
|
|
|
|
|
|
|
def test_adduser(self):
|
|
|
|
'''
|
|
|
|
Test the add user to group function
|
|
|
|
'''
|
2018-03-23 17:30:22 +00:00
|
|
|
self.run_function('group.add', [self._group], gid=self._gid)
|
2016-08-31 19:21:07 +00:00
|
|
|
self.run_function('user.add', [self._user])
|
|
|
|
self.assertTrue(self.run_function('group.adduser', [self._group, self._user]))
|
|
|
|
group_info = self.run_function('group.info', [self._group])
|
2018-03-23 17:30:22 +00:00
|
|
|
self.assertIn(self._user, str(group_info['members']))
|
|
|
|
if self.os_grain['kernel'] == 'Windows':
|
|
|
|
no_group = self.run_function('group.adduser', [self._no_group, self._no_user])
|
|
|
|
no_user = self.run_function('group.adduser', [self._group, self._no_user])
|
|
|
|
funcs = [no_group, no_user]
|
|
|
|
for func in funcs:
|
|
|
|
self.assertIn('Fail', func['comment'])
|
|
|
|
self.assertFalse(func['result'])
|
|
|
|
else:
|
|
|
|
# try add a non existing user
|
|
|
|
self.assertFalse(self.run_function('group.adduser', [self._group, self._no_user]))
|
|
|
|
# try add a user to non existing group
|
|
|
|
self.assertFalse(self.run_function('group.adduser', [self._no_group, self._user]))
|
|
|
|
# try add a non existing user to a non existing group
|
|
|
|
self.assertFalse(self.run_function('group.adduser', [self._no_group, self._no_user]))
|
2016-08-31 19:21:07 +00:00
|
|
|
|
|
|
|
def test_deluser(self):
|
|
|
|
'''
|
|
|
|
Test the delete user from group function
|
|
|
|
'''
|
2018-03-23 17:30:22 +00:00
|
|
|
self.run_function('group.add', [self._group], gid=self._gid)
|
2016-08-31 19:21:07 +00:00
|
|
|
self.run_function('user.add', [self._user])
|
|
|
|
self.run_function('group.adduser', [self._group, self._user])
|
|
|
|
self.assertTrue(self.run_function('group.deluser', [self._group, self._user]))
|
|
|
|
group_info = self.run_function('group.info', [self._group])
|
2018-03-23 17:30:22 +00:00
|
|
|
self.assertNotIn(self._user, str(group_info['members']))
|
2016-08-31 19:21:07 +00:00
|
|
|
|
|
|
|
def test_members(self):
|
|
|
|
'''
|
|
|
|
Test the members function
|
|
|
|
'''
|
2018-03-23 17:30:22 +00:00
|
|
|
self.run_function('group.add', [self._group], gid=self._gid)
|
2016-08-31 19:21:07 +00:00
|
|
|
self.run_function('user.add', [self._user])
|
|
|
|
self.run_function('user.add', [self._user1])
|
|
|
|
m = '{0},{1}'.format(self._user, self._user1)
|
|
|
|
self.assertTrue(self.run_function('group.members', [self._group, m]))
|
|
|
|
group_info = self.run_function('group.info', [self._group])
|
2018-03-23 17:30:22 +00:00
|
|
|
self.assertIn(self._user, str(group_info['members']))
|
|
|
|
self.assertIn(self._user1, str(group_info['members']))
|
2016-08-31 19:21:07 +00:00
|
|
|
|
|
|
|
def test_getent(self):
|
|
|
|
'''
|
|
|
|
Test the getent function
|
|
|
|
'''
|
2018-03-23 17:30:22 +00:00
|
|
|
self.run_function('group.add', [self._group], gid=self._gid)
|
2016-08-31 19:21:07 +00:00
|
|
|
self.run_function('user.add', [self._user])
|
|
|
|
self.run_function('group.adduser', [self._group, self._user])
|
|
|
|
ginfo = self.run_function('user.getent')
|
2018-01-18 22:13:08 +00:00
|
|
|
self.assertIn(self._group, six.text_type(ginfo))
|
|
|
|
self.assertIn(self._user, six.text_type(ginfo))
|
|
|
|
self.assertNotIn(self._no_group, six.text_type(ginfo))
|
|
|
|
self.assertNotIn(self._no_user, six.text_type(ginfo))
|