1
0

test_mac_system.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # -*- coding: utf-8 -*-
  2. """
  3. integration tests for mac_system
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import logging
  7. import pytest
  8. from tests.support.case import ModuleCase
  9. from tests.support.helpers import random_string
  10. from tests.support.unit import skipIf
  11. log = logging.getLogger(__name__)
  12. SET_COMPUTER_NAME = random_string("RS-", lowercase=False)
  13. SET_SUBNET_NAME = random_string("RS-", lowercase=False)
  14. @pytest.mark.skip_if_not_root
  15. @pytest.mark.flaky(max_runs=10)
  16. @pytest.mark.skip_unless_on_darwin
  17. @pytest.mark.skip_if_binaries_missing("systemsetup")
  18. class MacSystemModuleTest(ModuleCase):
  19. """
  20. Validate the mac_system module
  21. """
  22. ATRUN_ENABLED = False
  23. REMOTE_LOGIN_ENABLED = False
  24. REMOTE_EVENTS_ENABLED = False
  25. SUBNET_NAME = ""
  26. KEYBOARD_DISABLED = False
  27. def setUp(self):
  28. """
  29. Get current settings
  30. """
  31. self.ATRUN_ENABLED = self.run_function("service.enabled", ["com.apple.atrun"])
  32. self.REMOTE_LOGIN_ENABLED = self.run_function("system.get_remote_login")
  33. self.REMOTE_EVENTS_ENABLED = self.run_function("system.get_remote_events")
  34. self.SUBNET_NAME = self.run_function("system.get_subnet_name")
  35. self.KEYBOARD_DISABLED = self.run_function(
  36. "system.get_disable_keyboard_on_lock"
  37. )
  38. def tearDown(self):
  39. """
  40. Reset to original settings
  41. """
  42. if not self.ATRUN_ENABLED:
  43. atrun = "/System/Library/LaunchDaemons/com.apple.atrun.plist"
  44. self.run_function("service.stop", [atrun])
  45. self.run_function("system.set_remote_login", [self.REMOTE_LOGIN_ENABLED])
  46. self.run_function("system.set_remote_events", [self.REMOTE_EVENTS_ENABLED])
  47. self.run_function("system.set_subnet_name", [self.SUBNET_NAME])
  48. self.run_function(
  49. "system.set_disable_keyboard_on_lock", [self.KEYBOARD_DISABLED]
  50. )
  51. @pytest.mark.destructive_test
  52. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  53. def test_get_set_remote_login(self):
  54. """
  55. Test system.get_remote_login
  56. Test system.set_remote_login
  57. """
  58. # Normal Functionality
  59. self.assertTrue(self.run_function("system.set_remote_login", [True]))
  60. self.assertTrue(self.run_function("system.get_remote_login"))
  61. self.assertTrue(self.run_function("system.set_remote_login", [False]))
  62. self.assertFalse(self.run_function("system.get_remote_login"))
  63. # Test valid input
  64. self.assertTrue(self.run_function("system.set_remote_login", [True]))
  65. self.assertTrue(self.run_function("system.set_remote_login", [False]))
  66. self.assertTrue(self.run_function("system.set_remote_login", ["yes"]))
  67. self.assertTrue(self.run_function("system.set_remote_login", ["no"]))
  68. self.assertTrue(self.run_function("system.set_remote_login", ["On"]))
  69. self.assertTrue(self.run_function("system.set_remote_login", ["Off"]))
  70. self.assertTrue(self.run_function("system.set_remote_login", [1]))
  71. self.assertTrue(self.run_function("system.set_remote_login", [0]))
  72. # Test invalid input
  73. self.assertIn(
  74. "Invalid String Value for Enabled",
  75. self.run_function("system.set_remote_login", ["spongebob"]),
  76. )
  77. @pytest.mark.destructive_test
  78. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  79. def test_get_set_remote_events(self):
  80. """
  81. Test system.get_remote_events
  82. Test system.set_remote_events
  83. """
  84. # Normal Functionality
  85. self.assertTrue(self.run_function("system.set_remote_events", [True]))
  86. self.assertTrue(self.run_function("system.get_remote_events"))
  87. self.assertTrue(self.run_function("system.set_remote_events", [False]))
  88. self.assertFalse(self.run_function("system.get_remote_events"))
  89. # Test valid input
  90. self.assertTrue(self.run_function("system.set_remote_events", [True]))
  91. self.assertTrue(self.run_function("system.set_remote_events", [False]))
  92. self.assertTrue(self.run_function("system.set_remote_events", ["yes"]))
  93. self.assertTrue(self.run_function("system.set_remote_events", ["no"]))
  94. self.assertTrue(self.run_function("system.set_remote_events", ["On"]))
  95. self.assertTrue(self.run_function("system.set_remote_events", ["Off"]))
  96. self.assertTrue(self.run_function("system.set_remote_events", [1]))
  97. self.assertTrue(self.run_function("system.set_remote_events", [0]))
  98. # Test invalid input
  99. self.assertIn(
  100. "Invalid String Value for Enabled",
  101. self.run_function("system.set_remote_events", ["spongebob"]),
  102. )
  103. @pytest.mark.destructive_test
  104. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  105. def test_get_set_subnet_name(self):
  106. """
  107. Test system.get_subnet_name
  108. Test system.set_subnet_name
  109. """
  110. self.assertTrue(self.run_function("system.set_subnet_name", [SET_SUBNET_NAME]))
  111. self.assertEqual(self.run_function("system.get_subnet_name"), SET_SUBNET_NAME)
  112. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  113. def test_get_list_startup_disk(self):
  114. """
  115. Test system.get_startup_disk
  116. Test system.list_startup_disks
  117. Don't know how to test system.set_startup_disk as there's usually only
  118. one startup disk available on a system
  119. """
  120. # Test list and get
  121. ret = self.run_function("system.list_startup_disks")
  122. self.assertIsInstance(ret, list)
  123. self.assertIn(self.run_function("system.get_startup_disk"), ret)
  124. # Test passing set a bad disk
  125. self.assertIn(
  126. "Invalid value passed for path.",
  127. self.run_function("system.set_startup_disk", ["spongebob"]),
  128. )
  129. @skipIf(True, "Skip this test until mac fixes it.")
  130. def test_get_set_restart_delay(self):
  131. """
  132. Test system.get_restart_delay
  133. Test system.set_restart_delay
  134. system.set_restart_delay does not work due to an apple bug, see docs
  135. may need to disable this test as we can't control the delay value
  136. """
  137. # Normal Functionality
  138. self.assertTrue(self.run_function("system.set_restart_delay", [90]))
  139. self.assertEqual(self.run_function("system.get_restart_delay"), "90 seconds")
  140. # Pass set bad value for seconds
  141. self.assertIn(
  142. "Invalid value passed for seconds.",
  143. self.run_function("system.set_restart_delay", [70]),
  144. )
  145. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  146. def test_get_set_disable_keyboard_on_lock(self):
  147. """
  148. Test system.get_disable_keyboard_on_lock
  149. Test system.set_disable_keyboard_on_lock
  150. """
  151. # Normal Functionality
  152. self.assertTrue(
  153. self.run_function("system.set_disable_keyboard_on_lock", [True])
  154. )
  155. self.assertTrue(self.run_function("system.get_disable_keyboard_on_lock"))
  156. self.assertTrue(
  157. self.run_function("system.set_disable_keyboard_on_lock", [False])
  158. )
  159. self.assertFalse(self.run_function("system.get_disable_keyboard_on_lock"))
  160. # Test valid input
  161. self.assertTrue(
  162. self.run_function("system.set_disable_keyboard_on_lock", [True])
  163. )
  164. self.assertTrue(
  165. self.run_function("system.set_disable_keyboard_on_lock", [False])
  166. )
  167. self.assertTrue(
  168. self.run_function("system.set_disable_keyboard_on_lock", ["yes"])
  169. )
  170. self.assertTrue(
  171. self.run_function("system.set_disable_keyboard_on_lock", ["no"])
  172. )
  173. self.assertTrue(
  174. self.run_function("system.set_disable_keyboard_on_lock", ["On"])
  175. )
  176. self.assertTrue(
  177. self.run_function("system.set_disable_keyboard_on_lock", ["Off"])
  178. )
  179. self.assertTrue(self.run_function("system.set_disable_keyboard_on_lock", [1]))
  180. self.assertTrue(self.run_function("system.set_disable_keyboard_on_lock", [0]))
  181. # Test invalid input
  182. self.assertIn(
  183. "Invalid String Value for Enabled",
  184. self.run_function("system.set_disable_keyboard_on_lock", ["spongebob"]),
  185. )
  186. @skipIf(True, "Skip this test until mac fixes it.")
  187. def test_get_set_boot_arch(self):
  188. """
  189. Test system.get_boot_arch
  190. Test system.set_boot_arch
  191. system.set_boot_arch does not work due to an apple bug, see docs
  192. may need to disable this test as we can't set the boot architecture
  193. """
  194. # Normal Functionality
  195. self.assertTrue(self.run_function("system.set_boot_arch", ["i386"]))
  196. self.assertEqual(self.run_function("system.get_boot_arch"), "i386")
  197. self.assertTrue(self.run_function("system.set_boot_arch", ["default"]))
  198. self.assertEqual(self.run_function("system.get_boot_arch"), "default")
  199. # Test invalid input
  200. self.assertIn(
  201. "Invalid value passed for arch",
  202. self.run_function("system.set_boot_arch", ["spongebob"]),
  203. )
  204. @pytest.mark.skip_if_not_root
  205. @pytest.mark.skip_unless_on_darwin
  206. class MacSystemComputerNameTest(ModuleCase):
  207. def setUp(self):
  208. self.COMPUTER_NAME = self.run_function("system.get_computer_name")
  209. self.wait_for_all_jobs()
  210. def tearDown(self):
  211. self.run_function("system.set_computer_name", [self.COMPUTER_NAME])
  212. self.wait_for_all_jobs()
  213. # A similar test used to be skipped on py3 due to 'hanging', if we see
  214. # something similar again we may want to skip this gain until we
  215. # investigate
  216. # @skipIf(salt.utils.platform.is_darwin() and six.PY3, 'This test hangs on OS X on Py3. Skipping until #53566 is merged.')
  217. @pytest.mark.destructive_test
  218. @pytest.mark.slow_test(seconds=10) # Test takes >5 and <=10 seconds
  219. def test_get_set_computer_name(self):
  220. """
  221. Test system.get_computer_name
  222. Test system.set_computer_name
  223. """
  224. log.debug("Set name is %s", SET_COMPUTER_NAME)
  225. self.assertTrue(
  226. self.run_function("system.set_computer_name", [SET_COMPUTER_NAME])
  227. )
  228. self.assertEqual(
  229. self.run_function("system.get_computer_name"), SET_COMPUTER_NAME
  230. )