test_groupadd.py 8.7 KB

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