1
0

test_mac_system.py 10 KB

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