test_groupadd.py 9.3 KB

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