test_useradd.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import string
  5. import random
  6. # Import Salt Testing libs
  7. from tests.support.case import ModuleCase
  8. from tests.support.unit import skipIf
  9. from tests.support.helpers import (
  10. requires_system_grains
  11. )
  12. # Import Salt libs
  13. import salt.utils.platform
  14. # Import 3rd-party libs
  15. import pytest
  16. from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
  17. @pytest.mark.destructive_test
  18. @skipIf(not salt.utils.platform.is_linux(), 'These tests can only be run on linux')
  19. @pytest.mark.skip_if_not_root
  20. @pytest.mark.windows_whitelisted
  21. class UseraddModuleTestLinux(ModuleCase):
  22. def setUp(self):
  23. super(UseraddModuleTestLinux, self).setUp()
  24. os_grain = self.run_function('grains.item', ['kernel'])
  25. if os_grain['kernel'] not in ('Linux', 'Darwin'):
  26. self.skipTest(
  27. 'Test not applicable to \'{kernel}\' kernel'.format(
  28. **os_grain
  29. )
  30. )
  31. def __random_string(self, size=6):
  32. return 'RS-' + ''.join(
  33. random.choice(string.ascii_uppercase + string.digits)
  34. for x in range(size)
  35. )
  36. @requires_system_grains
  37. def test_groups_includes_primary(self, grains):
  38. # Let's create a user, which usually creates the group matching the
  39. # name
  40. uname = self.__random_string()
  41. if self.run_function('user.add', [uname]) is not True:
  42. # Skip because creating is not what we're testing here
  43. self.run_function('user.delete', [uname, True, True])
  44. self.skipTest('Failed to create user')
  45. try:
  46. uinfo = self.run_function('user.info', [uname])
  47. if grains['os_family'] in ('Suse',):
  48. self.assertIn('users', uinfo['groups'])
  49. else:
  50. self.assertIn(uname, uinfo['groups'])
  51. # This uid is available, store it
  52. uid = uinfo['uid']
  53. self.run_function('user.delete', [uname, True, True])
  54. # Now, a weird group id
  55. gname = self.__random_string()
  56. if self.run_function('group.add', [gname]) is not True:
  57. self.run_function('group.delete', [gname, True, True])
  58. self.skipTest('Failed to create group')
  59. ginfo = self.run_function('group.info', [gname])
  60. # And create the user with that gid
  61. if self.run_function('user.add', [uname, uid, ginfo['gid']]) is False:
  62. # Skip because creating is not what we're testing here
  63. self.run_function('user.delete', [uname, True, True])
  64. self.skipTest('Failed to create user')
  65. uinfo = self.run_function('user.info', [uname])
  66. self.assertIn(gname, uinfo['groups'])
  67. except AssertionError:
  68. self.run_function('user.delete', [uname, True, True])
  69. raise
  70. def test_user_primary_group(self):
  71. '''
  72. Tests the primary_group function
  73. '''
  74. name = 'saltyuser'
  75. # Create a user to test primary group function
  76. if self.run_function('user.add', [name]) is not True:
  77. self.run_function('user.delete', [name])
  78. self.skipTest('Failed to create a user')
  79. try:
  80. # Test useradd.primary_group
  81. primary_group = self.run_function('user.primary_group', [name])
  82. uid_info = self.run_function('user.info', [name])
  83. self.assertIn(primary_group, uid_info['groups'])
  84. except Exception:
  85. self.run_function('user.delete', [name])
  86. raise
  87. @pytest.mark.destructive_test
  88. @skipIf(not salt.utils.platform.is_windows(), 'These tests can only be run on Windows')
  89. @pytest.mark.skip_if_not_root
  90. @pytest.mark.windows_whitelisted
  91. class UseraddModuleTestWindows(ModuleCase):
  92. def __random_string(self, size=6):
  93. return 'RS-' + ''.join(
  94. random.choice(string.ascii_uppercase + string.digits)
  95. for x in range(size))
  96. def setUp(self):
  97. self.user_name = self.__random_string()
  98. self.group_name = self.__random_string()
  99. def tearDown(self):
  100. self.run_function('user.delete', [self.user_name, True, True])
  101. self.run_function('group.delete', [self.group_name])
  102. def _add_user(self):
  103. '''
  104. helper class to add user
  105. '''
  106. if self.run_function('user.add', [self.user_name]) is False:
  107. # Skip because creating is not what we're testing here
  108. self.skipTest('Failed to create user')
  109. def _add_group(self):
  110. '''
  111. helper class to add group
  112. '''
  113. if self.run_function('group.add', [self.group_name]) is False:
  114. # Skip because creating is not what we're testing here
  115. self.skipTest('Failed to create group')
  116. def test_add_user(self):
  117. '''
  118. Test adding a user
  119. '''
  120. self._add_user()
  121. user_list = self.run_function('user.list_users')
  122. self.assertIn(self.user_name, user_list)
  123. def test_add_group(self):
  124. '''
  125. Test adding a user
  126. '''
  127. self._add_group()
  128. group_list = self.run_function('group.list_groups')
  129. self.assertIn(self.group_name, group_list)
  130. def test_add_user_to_group(self):
  131. '''
  132. Test adding a user to a group
  133. '''
  134. self._add_group()
  135. # And create the user as a member of that group
  136. self.run_function('user.add', [self.user_name], groups=self.group_name)
  137. user_info = self.run_function('user.info', [self.user_name])
  138. self.assertIn(self.group_name, user_info['groups'])
  139. def test_add_user_addgroup(self):
  140. '''
  141. Test adding a user to a group with groupadd
  142. '''
  143. self._add_group()
  144. self._add_user()
  145. self.run_function('user.addgroup', [self.user_name, self.group_name])
  146. info = self.run_function('user.info', [self.user_name])
  147. self.assertEqual(info['groups'], [self.group_name])
  148. def test_user_chhome(self):
  149. '''
  150. Test changing a users home dir
  151. '''
  152. self._add_user()
  153. user_dir = r'c:\salt'
  154. self.run_function('user.chhome', [self.user_name, user_dir])
  155. info = self.run_function('user.info', [self.user_name])
  156. self.assertEqual(info['home'], user_dir)
  157. def test_user_chprofile(self):
  158. '''
  159. Test changing a users profile
  160. '''
  161. self._add_user()
  162. config = r'c:\salt\config'
  163. self.run_function('user.chprofile', [self.user_name, config])
  164. info = self.run_function('user.info', [self.user_name])
  165. self.assertEqual(info['profile'], config)
  166. def test_user_chfullname(self):
  167. '''
  168. Test changing a users fullname
  169. '''
  170. self._add_user()
  171. name = 'Salt Test'
  172. self.run_function('user.chfullname', [self.user_name, name])
  173. info = self.run_function('user.info', [self.user_name])
  174. self.assertEqual(info['fullname'], name)
  175. def test_user_delete(self):
  176. '''
  177. Test deleting a user
  178. '''
  179. self._add_user()
  180. self.assertTrue(self.run_function('user.info', [self.user_name])['active'])
  181. self.run_function('user.delete', [self.user_name])
  182. self.assertEqual({}, self.run_function('user.info', [self.user_name]))
  183. def test_user_removegroup(self):
  184. '''
  185. Test removing a group
  186. '''
  187. self._add_user()
  188. self._add_group()
  189. self.run_function('user.addgroup', [self.user_name, self.group_name])
  190. self.assertIn(self.group_name, self.run_function('user.list_groups', [self.user_name]))
  191. self.run_function('user.removegroup', [self.user_name, self.group_name])
  192. self.assertNotIn(self.group_name, self.run_function('user.list_groups', [self.user_name]))
  193. def test_user_rename(self):
  194. '''
  195. Test changing a users name
  196. '''
  197. self._add_user()
  198. name = 'newuser'
  199. self.run_function('user.rename', [self.user_name, name])
  200. info = self.run_function('user.info', [name])
  201. self.assertTrue(info['active'])
  202. #delete new user
  203. self.run_function('user.delete', [name, True, True])
  204. def test_user_setpassword(self):
  205. '''
  206. Test setting a password
  207. '''
  208. self._add_user()
  209. passwd = 'sup3rs3cr3T!'
  210. self.assertTrue(self.run_function('user.setpassword', [self.user_name, passwd]))