1
0

test_mac_user.py 8.6 KB

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