test_mac_user.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Nicole Thomas <nicole@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import random
  8. import string
  9. import os
  10. # Import Salt Testing Libs
  11. from tests.support.case import ModuleCase
  12. # Import Salt Libs
  13. import salt.utils.files
  14. from salt.exceptions import CommandExecutionError
  15. # Import 3rd-party libs
  16. import pytest
  17. from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
  18. import salt.ext.six as six
  19. def __random_string(size=6):
  20. '''
  21. Generates a random username
  22. '''
  23. return 'RS-' + ''.join(
  24. random.choice(string.ascii_uppercase + string.digits)
  25. for x in range(size)
  26. )
  27. # Create user strings for tests
  28. ADD_USER = __random_string()
  29. DEL_USER = __random_string()
  30. PRIMARY_GROUP_USER = __random_string()
  31. CHANGE_USER = __random_string()
  32. @pytest.mark.destructive_test
  33. @pytest.mark.skip_if_not_root
  34. class MacUserModuleTest(ModuleCase):
  35. '''
  36. Integration tests for the mac_user module
  37. '''
  38. def setUp(self):
  39. '''
  40. Sets up test requirements
  41. '''
  42. super(MacUserModuleTest, self).setUp()
  43. os_grain = self.run_function('grains.item', ['kernel'])
  44. if os_grain['kernel'] not in 'Darwin':
  45. self.skipTest(
  46. 'Test not applicable to \'{kernel}\' kernel'.format(
  47. **os_grain
  48. )
  49. )
  50. def test_mac_user_add(self):
  51. '''
  52. Tests the add function
  53. '''
  54. try:
  55. self.run_function('user.add', [ADD_USER])
  56. user_info = self.run_function('user.info', [ADD_USER])
  57. self.assertEqual(ADD_USER, user_info['name'])
  58. except CommandExecutionError:
  59. self.run_function('user.delete', [ADD_USER])
  60. raise
  61. def test_mac_user_delete(self):
  62. '''
  63. Tests the delete function
  64. '''
  65. # Create a user to delete - If unsuccessful, skip the test
  66. if self.run_function('user.add', [DEL_USER]) is not True:
  67. self.run_function('user.delete', [DEL_USER])
  68. self.skipTest('Failed to create a user to delete')
  69. try:
  70. # Now try to delete the added user
  71. ret = self.run_function('user.delete', [DEL_USER])
  72. self.assertTrue(ret)
  73. except CommandExecutionError:
  74. raise
  75. def test_mac_user_primary_group(self):
  76. '''
  77. Tests the primary_group function
  78. '''
  79. # Create a user to test primary group function
  80. if self.run_function('user.add', [PRIMARY_GROUP_USER]) is not True:
  81. self.run_function('user.delete', [PRIMARY_GROUP_USER])
  82. self.skipTest('Failed to create a user')
  83. try:
  84. # Test mac_user.primary_group
  85. primary_group = self.run_function('user.primary_group', [PRIMARY_GROUP_USER])
  86. uid_info = self.run_function('user.info', [PRIMARY_GROUP_USER])
  87. self.assertIn(primary_group, uid_info['groups'])
  88. except AssertionError:
  89. self.run_function('user.delete', [PRIMARY_GROUP_USER])
  90. raise
  91. def test_mac_user_changes(self):
  92. '''
  93. Tests mac_user functions that change user properties
  94. '''
  95. # Create a user to manipulate - if unsuccessful, skip the test
  96. if self.run_function('user.add', [CHANGE_USER]) is not True:
  97. self.run_function('user.delete', [CHANGE_USER])
  98. self.skipTest('Failed to create a user')
  99. try:
  100. # Test mac_user.chuid
  101. self.run_function('user.chuid', [CHANGE_USER, 4376])
  102. uid_info = self.run_function('user.info', [CHANGE_USER])
  103. self.assertEqual(uid_info['uid'], 4376)
  104. # Test mac_user.chgid
  105. self.run_function('user.chgid', [CHANGE_USER, 4376])
  106. gid_info = self.run_function('user.info', [CHANGE_USER])
  107. self.assertEqual(gid_info['gid'], 4376)
  108. # Test mac.user.chshell
  109. self.run_function('user.chshell', [CHANGE_USER, '/bin/zsh'])
  110. shell_info = self.run_function('user.info', [CHANGE_USER])
  111. self.assertEqual(shell_info['shell'], '/bin/zsh')
  112. # Test mac_user.chhome
  113. self.run_function('user.chhome', [CHANGE_USER, '/Users/foo'])
  114. home_info = self.run_function('user.info', [CHANGE_USER])
  115. self.assertEqual(home_info['home'], '/Users/foo')
  116. # Test mac_user.chfullname
  117. self.run_function('user.chfullname', [CHANGE_USER, 'Foo Bar'])
  118. fullname_info = self.run_function('user.info', [CHANGE_USER])
  119. self.assertEqual(fullname_info['fullname'], 'Foo Bar')
  120. # Test mac_user.chgroups
  121. self.run_function('user.chgroups', [CHANGE_USER, 'wheel'])
  122. groups_info = self.run_function('user.info', [CHANGE_USER])
  123. self.assertEqual(groups_info['groups'], ['wheel'])
  124. except AssertionError:
  125. self.run_function('user.delete', [CHANGE_USER])
  126. raise
  127. def test_mac_user_enable_auto_login(self):
  128. '''
  129. Tests mac_user functions that enable auto login
  130. '''
  131. # Make sure auto login is disabled before we start
  132. if self.run_function('user.get_auto_login'):
  133. self.skipTest('Auto login already enabled')
  134. try:
  135. # Does enable return True
  136. self.assertTrue(
  137. self.run_function('user.enable_auto_login',
  138. ['Spongebob', 'Squarepants']))
  139. # Did it set the user entry in the plist file
  140. self.assertEqual(
  141. self.run_function('user.get_auto_login'),
  142. 'Spongebob')
  143. # Did it generate the `/etc/kcpassword` file
  144. self.assertTrue(os.path.exists('/etc/kcpassword'))
  145. # Are the contents of the file correct
  146. if six.PY2:
  147. test_data = b'.\xf8\'B\xa0\xd9\xad\x8b\xcd\xcdl'
  148. else:
  149. test_data = b".\xc3\xb8'B\xc2\xa0\xc3\x99\xc2\xad\xc2\x8b\xc3\x8d\xc3\x8dl"
  150. with salt.utils.files.fopen('/etc/kcpassword', 'r' if six.PY2 else 'rb') as f:
  151. file_data = f.read()
  152. self.assertEqual(test_data, file_data)
  153. # Does disable return True
  154. self.assertTrue(self.run_function('user.disable_auto_login'))
  155. # Does it remove the user entry in the plist file
  156. self.assertFalse(self.run_function('user.get_auto_login'))
  157. # Is the `/etc/kcpassword` file removed
  158. self.assertFalse(os.path.exists('/etc/kcpassword'))
  159. finally:
  160. # Make sure auto_login is disabled
  161. self.assertTrue(self.run_function('user.disable_auto_login'))
  162. # Make sure autologin is disabled
  163. if self.run_function('user.get_auto_login'):
  164. raise Exception('Failed to disable auto login')
  165. def test_mac_user_disable_auto_login(self):
  166. '''
  167. Tests mac_user functions that disable auto login
  168. '''
  169. # Make sure auto login is enabled before we start
  170. # Is there an existing setting
  171. if self.run_function('user.get_auto_login'):
  172. self.skipTest('Auto login already enabled')
  173. try:
  174. # Enable auto login for the test
  175. self.run_function('user.enable_auto_login',
  176. ['Spongebob', 'Squarepants'])
  177. # Make sure auto login got set up
  178. if not self.run_function('user.get_auto_login') == 'Spongebob':
  179. raise Exception('Failed to enable auto login')
  180. # Does disable return True
  181. self.assertTrue(self.run_function('user.disable_auto_login'))
  182. # Does it remove the user entry in the plist file
  183. self.assertFalse(self.run_function('user.get_auto_login'))
  184. # Is the `/etc/kcpassword` file removed
  185. self.assertFalse(os.path.exists('/etc/kcpassword'))
  186. finally:
  187. # Make sure auto login is disabled
  188. self.assertTrue(self.run_function('user.disable_auto_login'))
  189. # Make sure auto login is disabled
  190. if self.run_function('user.get_auto_login'):
  191. raise Exception('Failed to disable auto login')
  192. def tearDown(self):
  193. '''
  194. Clean up after tests
  195. '''
  196. # Delete ADD_USER
  197. add_info = self.run_function('user.info', [ADD_USER])
  198. if add_info:
  199. self.run_function('user.delete', [ADD_USER])
  200. # Delete DEL_USER if something failed
  201. del_info = self.run_function('user.info', [DEL_USER])
  202. if del_info:
  203. self.run_function('user.delete', [DEL_USER])
  204. # Delete CHANGE_USER
  205. change_info = self.run_function('user.info', [CHANGE_USER])
  206. if change_info:
  207. self.run_function('user.delete', [CHANGE_USER])