1
0

test_useradd.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. self.run_function('user.delete', [self.user_name, True, True])
  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.run_function('group.delete', [self.group_name, True, True])
  115. self.skipTest('Failed to create group')
  116. def test_add_user(self):
  117. '''
  118. Test adding a user
  119. '''
  120. user_name = self.__random_string()
  121. try:
  122. if self.run_function('user.add', [user_name]) is False:
  123. self.run_function('user.delete', [user_name, True, True])
  124. self.skipTest('Failed to create user')
  125. user_list = self.run_function('user.list_users')
  126. self.assertIn(user_name, user_list)
  127. except AssertionError:
  128. raise
  129. finally:
  130. self.run_function('user.delete', [user_name, True, True])
  131. def test_add_group(self):
  132. '''
  133. Test adding a user
  134. '''
  135. group_name = self.__random_string()
  136. try:
  137. if self.run_function('group.add', [group_name]) is False:
  138. # Skip because creating is not what we're testing here
  139. self.run_function('group.delete', [group_name, True, True])
  140. self.skipTest('Failed to create group')
  141. group_list = self.run_function('group.list_groups')
  142. self.assertIn(group_name, group_list)
  143. except AssertionError:
  144. raise
  145. finally:
  146. self.run_function('group.delete', [group_name])
  147. def test_add_user_to_group(self):
  148. '''
  149. Test adding a user to a group
  150. '''
  151. group_name = self.__random_string()
  152. user_name = self.__random_string()
  153. try:
  154. # Let's create a group
  155. if self.run_function(
  156. 'group.add', [group_name])['result'] is not True:
  157. self.run_function('group.delete', [group_name, True, True])
  158. self.skipTest('Failed to create group')
  159. # And create the user as a member of that group
  160. if self.run_function(
  161. 'user.add', [user_name], groups=group_name) is False:
  162. self.run_function('user.delete', [user_name, True, True])
  163. self.skipTest('Failed to create user')
  164. user_info = self.run_function('user.info', [user_name])
  165. self.assertIn(group_name, user_info['groups'])
  166. except AssertionError:
  167. raise
  168. finally:
  169. self.run_function('user.delete', [user_name, True, True])
  170. self.run_function('group.delete', [group_name])
  171. def test_add_user_addgroup(self):
  172. '''
  173. Test adding a user to a group with groupadd
  174. '''
  175. self._add_group()
  176. self._add_user()
  177. self.run_function('user.addgroup', [self.user_name, self.group_name])
  178. info = self.run_function('user.info', [self.user_name])
  179. self.assertEqual(info['groups'], [self.group_name])
  180. def test_user_chhome(self):
  181. '''
  182. Test changing a users home dir
  183. '''
  184. self._add_user()
  185. user_dir = r'c:\salt'
  186. self.run_function('user.chhome', [self.user_name, user_dir])
  187. info = self.run_function('user.info', [self.user_name])
  188. self.assertEqual(info['home'], user_dir)
  189. def test_user_chprofile(self):
  190. '''
  191. Test changing a users profile
  192. '''
  193. self._add_user()
  194. config = r'c:\salt\config'
  195. self.run_function('user.chprofile', [self.user_name, config])
  196. info = self.run_function('user.info', [self.user_name])
  197. self.assertEqual(info['profile'], config)
  198. def test_user_chfullname(self):
  199. '''
  200. Test changing a users fullname
  201. '''
  202. self._add_user()
  203. name = 'Salt Test'
  204. self.run_function('user.chfullname', [self.user_name, name])
  205. info = self.run_function('user.info', [self.user_name])
  206. self.assertEqual(info['fullname'], name)
  207. def test_user_delete(self):
  208. '''
  209. Test deleting a user
  210. '''
  211. self._add_user()
  212. self.assertTrue(self.run_function('user.info', [self.user_name])['active'])
  213. self.run_function('user.delete', [self.user_name])
  214. self.assertEqual({}, self.run_function('user.info', [self.user_name]))
  215. def test_user_removegroup(self):
  216. '''
  217. Test removing a group
  218. '''
  219. self._add_user()
  220. self._add_group()
  221. self.run_function('user.addgroup', [self.user_name, self.group_name])
  222. self.assertIn(self.group_name, self.run_function('user.list_groups', [self.user_name]))
  223. self.run_function('user.removegroup', [self.user_name, self.group_name])
  224. self.assertNotIn(self.group_name, self.run_function('user.list_groups', [self.user_name]))
  225. def test_user_rename(self):
  226. '''
  227. Test changing a users name
  228. '''
  229. self._add_user()
  230. name = 'newuser'
  231. self.run_function('user.rename', [self.user_name, name])
  232. info = self.run_function('user.info', [name])
  233. self.assertTrue(info['active'])
  234. #delete new user
  235. self.run_function('user.delete', [name, True, True])
  236. def test_user_setpassword(self):
  237. '''
  238. Test setting a password
  239. '''
  240. self._add_user()
  241. passwd = 'sup3rs3cr3T!'
  242. self.assertTrue(self.run_function('user.setpassword', [self.user_name, passwd]))