test_useradd.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. destructiveTest,
  11. skip_if_not_root,
  12. requires_system_grains
  13. )
  14. # Import Salt libs
  15. import salt.utils.platform
  16. # Import 3rd-party libs
  17. from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
  18. @destructiveTest
  19. @skipIf(not salt.utils.platform.is_linux(), 'These tests can only be run on linux')
  20. @skip_if_not_root
  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. @destructiveTest
  88. @skipIf(not salt.utils.platform.is_windows(), 'These tests can only be run on Windows')
  89. @skip_if_not_root
  90. class UseraddModuleTestWindows(ModuleCase):
  91. def __random_string(self, size=6):
  92. return 'RS-' + ''.join(
  93. random.choice(string.ascii_uppercase + string.digits)
  94. for x in range(size))
  95. def setUp(self):
  96. self.user_name = self.__random_string()
  97. self.group_name = self.__random_string()
  98. def tearDown(self):
  99. self.run_function('user.delete', [self.user_name, True, True])
  100. self.run_function('group.delete', [self.group_name])
  101. def _add_user(self):
  102. '''
  103. helper class to add user
  104. '''
  105. if self.run_function('user.add', [self.user_name]) is False:
  106. # Skip because creating is not what we're testing here
  107. self.skipTest('Failed to create user')
  108. def _add_group(self):
  109. '''
  110. helper class to add group
  111. '''
  112. if self.run_function('group.add', [self.group_name]) is False:
  113. # Skip because creating is not what we're testing here
  114. self.skipTest('Failed to create group')
  115. def test_add_user(self):
  116. '''
  117. Test adding a user
  118. '''
  119. self._add_user()
  120. user_list = self.run_function('user.list_users')
  121. self.assertIn(self.user_name, user_list)
  122. def test_add_group(self):
  123. '''
  124. Test adding a user
  125. '''
  126. self._add_group()
  127. group_list = self.run_function('group.list_groups')
  128. self.assertIn(self.group_name, group_list)
  129. def test_add_user_to_group(self):
  130. '''
  131. Test adding a user to a group
  132. '''
  133. self._add_group()
  134. # And create the user as a member of that group
  135. self.run_function('user.add', [self.user_name], groups=self.group_name)
  136. user_info = self.run_function('user.info', [self.user_name])
  137. self.assertIn(self.group_name, user_info['groups'])
  138. def test_add_user_addgroup(self):
  139. '''
  140. Test adding a user to a group with groupadd
  141. '''
  142. self._add_group()
  143. self._add_user()
  144. self.run_function('user.addgroup', [self.user_name, self.group_name])
  145. info = self.run_function('user.info', [self.user_name])
  146. self.assertEqual(info['groups'], [self.group_name])
  147. def test_user_chhome(self):
  148. '''
  149. Test changing a users home dir
  150. '''
  151. self._add_user()
  152. user_dir = r'c:\salt'
  153. self.run_function('user.chhome', [self.user_name, user_dir])
  154. info = self.run_function('user.info', [self.user_name])
  155. self.assertEqual(info['home'], user_dir)
  156. def test_user_chprofile(self):
  157. '''
  158. Test changing a users profile
  159. '''
  160. self._add_user()
  161. config = r'c:\salt\config'
  162. self.run_function('user.chprofile', [self.user_name, config])
  163. info = self.run_function('user.info', [self.user_name])
  164. self.assertEqual(info['profile'], config)
  165. def test_user_chfullname(self):
  166. '''
  167. Test changing a users fullname
  168. '''
  169. self._add_user()
  170. name = 'Salt Test'
  171. self.run_function('user.chfullname', [self.user_name, name])
  172. info = self.run_function('user.info', [self.user_name])
  173. self.assertEqual(info['fullname'], name)
  174. def test_user_delete(self):
  175. '''
  176. Test deleting a user
  177. '''
  178. self._add_user()
  179. self.assertTrue(self.run_function('user.info', [self.user_name])['active'])
  180. self.run_function('user.delete', [self.user_name])
  181. self.assertEqual({}, self.run_function('user.info', [self.user_name]))
  182. def test_user_removegroup(self):
  183. '''
  184. Test removing a group
  185. '''
  186. self._add_user()
  187. self._add_group()
  188. self.run_function('user.addgroup', [self.user_name, self.group_name])
  189. self.assertIn(self.group_name, self.run_function('user.list_groups', [self.user_name]))
  190. self.run_function('user.removegroup', [self.user_name, self.group_name])
  191. self.assertNotIn(self.group_name, self.run_function('user.list_groups', [self.user_name]))
  192. def test_user_rename(self):
  193. '''
  194. Test changing a users name
  195. '''
  196. self._add_user()
  197. name = 'newuser'
  198. self.run_function('user.rename', [self.user_name, name])
  199. info = self.run_function('user.info', [name])
  200. self.assertTrue(info['active'])
  201. #delete new user
  202. self.run_function('user.delete', [name, True, True])
  203. def test_user_setpassword(self):
  204. '''
  205. Test setting a password
  206. '''
  207. self._add_user()
  208. passwd = 'sup3rs3cr3T!'
  209. self.assertTrue(self.run_function('user.setpassword', [self.user_name, passwd]))