test_mac_user.py 8.8 KB

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