1
0

test_groupadd.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import random
  5. import string
  6. # Import Salt Testing libs
  7. from tests.support.case import ModuleCase
  8. from tests.support.helpers import destructiveTest, skip_if_not_root
  9. from tests.support.unit import skipIf
  10. # Import Salt libs
  11. from salt.ext import six
  12. from salt.ext.six.moves import range
  13. import salt.utils.files
  14. import salt.utils.platform
  15. import salt.utils.stringutils
  16. if not salt.utils.platform.is_windows():
  17. import grp
  18. @skip_if_not_root
  19. @destructiveTest
  20. class GroupModuleTest(ModuleCase):
  21. '''
  22. Validate the linux group system module
  23. '''
  24. def setUp(self):
  25. '''
  26. Get current settings
  27. '''
  28. super(GroupModuleTest, self).setUp()
  29. self._user = self.__random_string()
  30. self._user1 = self.__random_string()
  31. self._no_user = self.__random_string()
  32. self._group = self.__random_string()
  33. self._no_group = self.__random_string()
  34. self.os_grain = self.run_function('grains.item', ['kernel'])
  35. self._gid = 64989 if 'Windows' not in self.os_grain['kernel'] else None
  36. self._new_gid = 64998 if 'Windows' not in self.os_grain['kernel'] else None
  37. if self.os_grain['kernel'] not in ('Linux', 'Windows'):
  38. self.skipTest(
  39. 'Test not applicable to \'{kernel}\' kernel'.format(
  40. **self.os_grain
  41. )
  42. )
  43. def tearDown(self):
  44. '''
  45. Reset to original settings
  46. '''
  47. self.run_function('user.delete', [self._user])
  48. self.run_function('user.delete', [self._user1])
  49. self.run_function('group.delete', [self._group])
  50. def __random_string(self, size=6):
  51. '''
  52. Generates a random names
  53. '''
  54. return 'tg-' + ''.join(
  55. random.choice(string.ascii_lowercase + string.digits)
  56. for x in range(size)
  57. )
  58. def __get_system_group_gid_range(self):
  59. '''
  60. Returns (SYS_GID_MIN, SYS_GID_MAX)
  61. '''
  62. try:
  63. login_defs = {}
  64. with salt.utils.files.fopen('/etc/login.defs') as defs_fd:
  65. for line in defs_fd:
  66. line = salt.utils.stringutils.to_unicode(line).strip()
  67. if line.startswith('#'):
  68. continue
  69. try:
  70. key, val = line.split()
  71. except ValueError:
  72. pass
  73. else:
  74. login_defs[key] = val
  75. except OSError:
  76. login_defs = {'SYS_GID_MIN': 101,
  77. 'SYS_GID_MAX': 999}
  78. gid_min = login_defs.get('SYS_GID_MIN', 101)
  79. gid_max = login_defs.get('SYS_GID_MAX',
  80. int(login_defs.get('GID_MIN', 1000)) - 1)
  81. return int(gid_min), int(gid_max)
  82. def __get_free_system_gid(self):
  83. '''
  84. Find a free system gid
  85. '''
  86. gid_min, gid_max = self.__get_system_group_gid_range()
  87. busy_gids = [x.gr_gid
  88. for x in grp.getgrall()
  89. if gid_min <= x.gr_gid <= gid_max]
  90. # find free system gid
  91. for gid in range(gid_min, gid_max + 1):
  92. if gid not in busy_gids:
  93. return gid
  94. @destructiveTest
  95. def test_add(self):
  96. '''
  97. Test the add group function
  98. '''
  99. # add a new group
  100. self.assertTrue(self.run_function('group.add', [self._group], gid=self._gid))
  101. group_info = self.run_function('group.info', [self._group])
  102. self.assertEqual(group_info['gid'], self._gid)
  103. self.assertEqual(group_info['name'], self._group)
  104. # try adding the group again
  105. self.assertFalse(self.run_function('group.add', [self._group], gid=self._gid))
  106. @destructiveTest
  107. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows')
  108. def test_add_system_group(self):
  109. '''
  110. Test the add group function with system=True
  111. '''
  112. gid_min, gid_max = self.__get_system_group_gid_range()
  113. # add a new system group
  114. self.assertTrue(self.run_function('group.add',
  115. [self._group, None, True]))
  116. group_info = self.run_function('group.info', [self._group])
  117. self.assertEqual(group_info['name'], self._group)
  118. self.assertTrue(gid_min <= group_info['gid'] <= gid_max)
  119. # try adding the group again
  120. self.assertFalse(self.run_function('group.add',
  121. [self._group]))
  122. @destructiveTest
  123. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows')
  124. def test_add_system_group_gid(self):
  125. '''
  126. Test the add group function with system=True and a specific gid
  127. '''
  128. gid = self.__get_free_system_gid()
  129. # add a new system group
  130. self.assertTrue(self.run_function('group.add',
  131. [self._group, gid, True]))
  132. group_info = self.run_function('group.info', [self._group])
  133. self.assertEqual(group_info['name'], self._group)
  134. self.assertEqual(group_info['gid'], gid)
  135. # try adding the group again
  136. self.assertFalse(self.run_function('group.add',
  137. [self._group, gid]))
  138. @destructiveTest
  139. def test_delete(self):
  140. '''
  141. Test the delete group function
  142. '''
  143. self.assertTrue(self.run_function('group.add', [self._group]))
  144. # correct functionality
  145. self.assertTrue(self.run_function('group.delete', [self._group]))
  146. # group does not exist
  147. self.assertFalse(self.run_function('group.delete', [self._no_group]))
  148. def test_info(self):
  149. '''
  150. Test the info group function
  151. '''
  152. self.run_function('group.add', [self._group], gid=self._gid)
  153. self.run_function('user.add', [self._user])
  154. self.run_function('group.adduser', [self._group, self._user])
  155. group_info = self.run_function('group.info', [self._group])
  156. self.assertEqual(group_info['name'], self._group)
  157. self.assertEqual(group_info['gid'], self._gid)
  158. self.assertIn(self._user, str(group_info['members']))
  159. @skipIf(salt.utils.platform.is_windows(), 'gid test skipped on windows')
  160. def test_chgid(self):
  161. '''
  162. Test the change gid function
  163. '''
  164. self.run_function('group.add', [self._group], gid=self._gid)
  165. self.assertTrue(self.run_function('group.chgid', [self._group, self._new_gid]))
  166. group_info = self.run_function('group.info', [self._group])
  167. self.assertEqual(group_info['gid'], self._new_gid)
  168. def test_adduser(self):
  169. '''
  170. Test the add user to group function
  171. '''
  172. self.run_function('group.add', [self._group], gid=self._gid)
  173. self.run_function('user.add', [self._user])
  174. self.assertTrue(self.run_function('group.adduser', [self._group, self._user]))
  175. group_info = self.run_function('group.info', [self._group])
  176. self.assertIn(self._user, str(group_info['members']))
  177. # try add a non existing user
  178. self.assertFalse(self.run_function('group.adduser', [self._group, self._no_user]))
  179. # try add a user to non existing group
  180. self.assertFalse(self.run_function('group.adduser', [self._no_group, self._user]))
  181. # try add a non existing user to a non existing group
  182. self.assertFalse(self.run_function('group.adduser', [self._no_group, self._no_user]))
  183. def test_deluser(self):
  184. '''
  185. Test the delete user from group function
  186. '''
  187. self.run_function('group.add', [self._group], gid=self._gid)
  188. self.run_function('user.add', [self._user])
  189. self.run_function('group.adduser', [self._group, self._user])
  190. self.assertTrue(self.run_function('group.deluser', [self._group, self._user]))
  191. group_info = self.run_function('group.info', [self._group])
  192. self.assertNotIn(self._user, str(group_info['members']))
  193. def test_members(self):
  194. '''
  195. Test the members function
  196. '''
  197. self.run_function('group.add', [self._group], gid=self._gid)
  198. self.run_function('user.add', [self._user])
  199. self.run_function('user.add', [self._user1])
  200. m = '{0},{1}'.format(self._user, self._user1)
  201. ret = self.run_function('group.members', [self._group, m])
  202. self.assertTrue(ret)
  203. group_info = self.run_function('group.info', [self._group])
  204. self.assertIn(self._user, str(group_info['members']))
  205. self.assertIn(self._user1, str(group_info['members']))
  206. def test_getent(self):
  207. '''
  208. Test the getent function
  209. '''
  210. self.run_function('group.add', [self._group], gid=self._gid)
  211. self.run_function('user.add', [self._user])
  212. self.run_function('group.adduser', [self._group, self._user])
  213. ginfo = self.run_function('user.getent')
  214. self.assertIn(self._group, six.text_type(ginfo))
  215. self.assertIn(self._user, six.text_type(ginfo))
  216. self.assertNotIn(self._no_group, six.text_type(ginfo))
  217. self.assertNotIn(self._no_user, six.text_type(ginfo))