test_mac_system.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 Salt Testing libs
  10. from tests.support.case import ModuleCase
  11. from tests.support.unit import skipIf
  12. from tests.support.helpers import destructiveTest, skip_if_not_root, flaky
  13. # Import salt libs
  14. import salt.utils.path
  15. import salt.utils.platform
  16. from salt.ext.six.moves import range
  17. def __random_string(size=6):
  18. '''
  19. Generates a random username
  20. '''
  21. return 'RS-' + ''.join(
  22. random.choice(string.ascii_uppercase + string.digits)
  23. for x in range(size)
  24. )
  25. SET_COMPUTER_NAME = __random_string()
  26. SET_SUBNET_NAME = __random_string()
  27. @skip_if_not_root
  28. @flaky
  29. @skipIf(not salt.utils.platform.is_darwin(), 'Test only available on macOS')
  30. @skipIf(not salt.utils.path.which('systemsetup'), '\'systemsetup\' binary not found in $PATH')
  31. class MacSystemModuleTest(ModuleCase):
  32. '''
  33. Validate the mac_system module
  34. '''
  35. ATRUN_ENABLED = False
  36. REMOTE_LOGIN_ENABLED = False
  37. REMOTE_EVENTS_ENABLED = False
  38. COMPUTER_NAME = ''
  39. SUBNET_NAME = ''
  40. KEYBOARD_DISABLED = False
  41. def setUp(self):
  42. '''
  43. Get current settings
  44. '''
  45. self.ATRUN_ENABLED = self.run_function('service.enabled', ['com.apple.atrun'])
  46. self.REMOTE_LOGIN_ENABLED = self.run_function('system.get_remote_login')
  47. self.REMOTE_EVENTS_ENABLED = self.run_function('system.get_remote_events')
  48. self.COMPUTER_NAME = self.run_function('system.get_computer_name')
  49. self.SUBNET_NAME = self.run_function('system.get_subnet_name')
  50. self.KEYBOARD_DISABLED = self.run_function('system.get_disable_keyboard_on_lock')
  51. def tearDown(self):
  52. '''
  53. Reset to original settings
  54. '''
  55. if not self.ATRUN_ENABLED:
  56. atrun = '/System/Library/LaunchDaemons/com.apple.atrun.plist'
  57. self.run_function('service.stop', [atrun])
  58. self.run_function('system.set_remote_login', [self.REMOTE_LOGIN_ENABLED])
  59. self.run_function('system.set_remote_events', [self.REMOTE_EVENTS_ENABLED])
  60. self.run_function('system.set_computer_name', [self.COMPUTER_NAME])
  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. @destructiveTest
  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. @destructiveTest
  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. @destructiveTest
  113. def test_get_set_computer_name(self):
  114. '''
  115. Test system.get_computer_name
  116. Test system.set_computer_name
  117. '''
  118. self.assertTrue(
  119. self.run_function('system.set_computer_name', [SET_COMPUTER_NAME]))
  120. self.assertEqual(
  121. self.run_function('system.get_computer_name'),
  122. SET_COMPUTER_NAME)
  123. @destructiveTest
  124. def test_get_set_subnet_name(self):
  125. '''
  126. Test system.get_subnet_name
  127. Test system.set_subnet_name
  128. '''
  129. self.assertTrue(
  130. self.run_function('system.set_subnet_name', [SET_SUBNET_NAME]))
  131. self.assertEqual(
  132. self.run_function('system.get_subnet_name'),
  133. SET_SUBNET_NAME)
  134. def test_get_list_startup_disk(self):
  135. '''
  136. Test system.get_startup_disk
  137. Test system.list_startup_disks
  138. Don't know how to test system.set_startup_disk as there's usually only
  139. one startup disk available on a system
  140. '''
  141. # Test list and get
  142. ret = self.run_function('system.list_startup_disks')
  143. self.assertIsInstance(ret, list)
  144. self.assertIn(self.run_function('system.get_startup_disk'), ret)
  145. # Test passing set a bad disk
  146. self.assertIn(
  147. 'Invalid value passed for path.',
  148. self.run_function('system.set_startup_disk', ['spongebob']))
  149. @skipIf(True, 'Skip this test until mac fixes it.')
  150. def test_get_set_restart_delay(self):
  151. '''
  152. Test system.get_restart_delay
  153. Test system.set_restart_delay
  154. system.set_restart_delay does not work due to an apple bug, see docs
  155. may need to disable this test as we can't control the delay value
  156. '''
  157. # Normal Functionality
  158. self.assertTrue(self.run_function('system.set_restart_delay', [90]))
  159. self.assertEqual(
  160. self.run_function('system.get_restart_delay'),
  161. '90 seconds')
  162. # Pass set bad value for seconds
  163. self.assertIn(
  164. 'Invalid value passed for seconds.',
  165. self.run_function('system.set_restart_delay', [70]))
  166. def test_get_set_disable_keyboard_on_lock(self):
  167. '''
  168. Test system.get_disable_keyboard_on_lock
  169. Test system.set_disable_keyboard_on_lock
  170. '''
  171. # Normal Functionality
  172. self.assertTrue(
  173. self.run_function('system.set_disable_keyboard_on_lock', [True]))
  174. self.assertTrue(
  175. self.run_function('system.get_disable_keyboard_on_lock'))
  176. self.assertTrue(
  177. self.run_function('system.set_disable_keyboard_on_lock', [False]))
  178. self.assertFalse(
  179. self.run_function('system.get_disable_keyboard_on_lock'))
  180. # Test valid input
  181. self.assertTrue(
  182. self.run_function('system.set_disable_keyboard_on_lock', [True]))
  183. self.assertTrue(
  184. self.run_function('system.set_disable_keyboard_on_lock', [False]))
  185. self.assertTrue(
  186. self.run_function('system.set_disable_keyboard_on_lock', ['yes']))
  187. self.assertTrue(
  188. self.run_function('system.set_disable_keyboard_on_lock', ['no']))
  189. self.assertTrue(
  190. self.run_function('system.set_disable_keyboard_on_lock', ['On']))
  191. self.assertTrue(
  192. self.run_function('system.set_disable_keyboard_on_lock', ['Off']))
  193. self.assertTrue(
  194. self.run_function('system.set_disable_keyboard_on_lock', [1]))
  195. self.assertTrue(
  196. self.run_function('system.set_disable_keyboard_on_lock', [0]))
  197. # Test invalid input
  198. self.assertIn(
  199. 'Invalid String Value for Enabled',
  200. self.run_function('system.set_disable_keyboard_on_lock',
  201. ['spongebob']))
  202. @skipIf(True, 'Skip this test until mac fixes it.')
  203. def test_get_set_boot_arch(self):
  204. '''
  205. Test system.get_boot_arch
  206. Test system.set_boot_arch
  207. system.set_boot_arch does not work due to an apple bug, see docs
  208. may need to disable this test as we can't set the boot architecture
  209. '''
  210. # Normal Functionality
  211. self.assertTrue(self.run_function('system.set_boot_arch', ['i386']))
  212. self.assertEqual(self.run_function('system.get_boot_arch'), 'i386')
  213. self.assertTrue(self.run_function('system.set_boot_arch', ['default']))
  214. self.assertEqual(self.run_function('system.get_boot_arch'), 'default')
  215. # Test invalid input
  216. self.assertIn(
  217. 'Invalid value passed for arch',
  218. self.run_function('system.set_boot_arch', ['spongebob']))