1
0

test_groupadd.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. if self.os_grain['kernel'] == 'Windows':
  106. add_group = self.run_function('group.add', [self._group], gid=self._gid)
  107. self.assertEqual(add_group['result'], None)
  108. self.assertEqual(add_group['comment'], 'The group {0} already exists.'.format(self._group))
  109. self.assertEqual(add_group['changes'], [])
  110. else:
  111. self.assertFalse(self.run_function('group.add', [self._group], gid=self._gid))
  112. @destructiveTest
  113. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows')
  114. def test_add_system_group(self):
  115. '''
  116. Test the add group function with system=True
  117. '''
  118. gid_min, gid_max = self.__get_system_group_gid_range()
  119. # add a new system group
  120. self.assertTrue(self.run_function('group.add',
  121. [self._group, None, True]))
  122. group_info = self.run_function('group.info', [self._group])
  123. self.assertEqual(group_info['name'], self._group)
  124. self.assertTrue(gid_min <= group_info['gid'] <= gid_max)
  125. # try adding the group again
  126. self.assertFalse(self.run_function('group.add',
  127. [self._group]))
  128. @destructiveTest
  129. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows')
  130. def test_add_system_group_gid(self):
  131. '''
  132. Test the add group function with system=True and a specific gid
  133. '''
  134. gid = self.__get_free_system_gid()
  135. # add a new system group
  136. self.assertTrue(self.run_function('group.add',
  137. [self._group, gid, True]))
  138. group_info = self.run_function('group.info', [self._group])
  139. self.assertEqual(group_info['name'], self._group)
  140. self.assertEqual(group_info['gid'], gid)
  141. # try adding the group again
  142. self.assertFalse(self.run_function('group.add',
  143. [self._group, gid]))
  144. @destructiveTest
  145. def test_delete(self):
  146. '''
  147. Test the delete group function
  148. '''
  149. self.assertTrue(self.run_function('group.add', [self._group]))
  150. # correct functionality
  151. self.assertTrue(self.run_function('group.delete', [self._group]))
  152. # group does not exist
  153. if self.os_grain['kernel'] == 'Windows':
  154. del_group = self.run_function('group.delete', [self._no_group])
  155. self.assertEqual(del_group['changes'], [])
  156. self.assertEqual(del_group['comment'], 'The group {0} does not exists.'.format(self._no_group))
  157. else:
  158. self.assertFalse(self.run_function('group.delete', [self._no_group]))
  159. def test_info(self):
  160. '''
  161. Test the info group function
  162. '''
  163. self.run_function('group.add', [self._group], gid=self._gid)
  164. self.run_function('user.add', [self._user])
  165. self.run_function('group.adduser', [self._group, self._user])
  166. group_info = self.run_function('group.info', [self._group])
  167. self.assertEqual(group_info['name'], self._group)
  168. self.assertEqual(group_info['gid'], self._gid)
  169. self.assertIn(self._user, str(group_info['members']))
  170. @skipIf(salt.utils.platform.is_windows(), 'gid test skipped on windows')
  171. def test_chgid(self):
  172. '''
  173. Test the change gid function
  174. '''
  175. self.run_function('group.add', [self._group], gid=self._gid)
  176. self.assertTrue(self.run_function('group.chgid', [self._group, self._new_gid]))
  177. group_info = self.run_function('group.info', [self._group])
  178. self.assertEqual(group_info['gid'], self._new_gid)
  179. def test_adduser(self):
  180. '''
  181. Test the add user to group function
  182. '''
  183. self.run_function('group.add', [self._group], gid=self._gid)
  184. self.run_function('user.add', [self._user])
  185. self.assertTrue(self.run_function('group.adduser', [self._group, self._user]))
  186. group_info = self.run_function('group.info', [self._group])
  187. self.assertIn(self._user, str(group_info['members']))
  188. if self.os_grain['kernel'] == 'Windows':
  189. no_group = self.run_function('group.adduser', [self._no_group, self._no_user])
  190. no_user = self.run_function('group.adduser', [self._group, self._no_user])
  191. funcs = [no_group, no_user]
  192. for func in funcs:
  193. self.assertIn('Fail', func['comment'])
  194. self.assertFalse(func['result'])
  195. else:
  196. # try add a non existing user
  197. self.assertFalse(self.run_function('group.adduser', [self._group, self._no_user]))
  198. # try add a user to non existing group
  199. self.assertFalse(self.run_function('group.adduser', [self._no_group, self._user]))
  200. # try add a non existing user to a non existing group
  201. self.assertFalse(self.run_function('group.adduser', [self._no_group, self._no_user]))
  202. def test_deluser(self):
  203. '''
  204. Test the delete user from group function
  205. '''
  206. self.run_function('group.add', [self._group], gid=self._gid)
  207. self.run_function('user.add', [self._user])
  208. self.run_function('group.adduser', [self._group, self._user])
  209. self.assertTrue(self.run_function('group.deluser', [self._group, self._user]))
  210. group_info = self.run_function('group.info', [self._group])
  211. self.assertNotIn(self._user, str(group_info['members']))
  212. def test_members(self):
  213. '''
  214. Test the members function
  215. '''
  216. self.run_function('group.add', [self._group], gid=self._gid)
  217. self.run_function('user.add', [self._user])
  218. self.run_function('user.add', [self._user1])
  219. m = '{0},{1}'.format(self._user, self._user1)
  220. ret = self.run_function('group.members', [self._group, m])
  221. if salt.utils.platform.is_windows():
  222. self.assertTrue(ret['result'])
  223. else:
  224. self.assertTrue(ret)
  225. group_info = self.run_function('group.info', [self._group])
  226. self.assertIn(self._user, str(group_info['members']))
  227. self.assertIn(self._user1, str(group_info['members']))
  228. def test_getent(self):
  229. '''
  230. Test the getent function
  231. '''
  232. self.run_function('group.add', [self._group], gid=self._gid)
  233. self.run_function('user.add', [self._user])
  234. self.run_function('group.adduser', [self._group, self._user])
  235. ginfo = self.run_function('user.getent')
  236. self.assertIn(self._group, six.text_type(ginfo))
  237. self.assertIn(self._user, six.text_type(ginfo))
  238. self.assertNotIn(self._no_group, six.text_type(ginfo))
  239. self.assertNotIn(self._no_user, six.text_type(ginfo))