test_useradd.py 8.3 KB

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