1
0

test_mac_power.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. # -*- coding: utf-8 -*-
  2. '''
  3. integration tests for mac_power
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import pytest
  8. # Import Salt Testing libs
  9. from tests.support.case import ModuleCase
  10. from tests.support.unit import skipIf
  11. from tests.support.helpers import flaky
  12. # Import Salt libs
  13. import salt.utils.path
  14. import salt.utils.platform
  15. @pytest.mark.skip_if_not_root
  16. @flaky(attempts=10)
  17. @skipIf(not salt.utils.platform.is_darwin(), 'Test only available on macOS')
  18. @skipIf(not salt.utils.path.which('systemsetup'), '\'systemsetup\' binary not found in $PATH')
  19. class MacPowerModuleTest(ModuleCase):
  20. '''
  21. Validate the mac_power module
  22. '''
  23. def setUp(self):
  24. '''
  25. Get current settings
  26. '''
  27. # Get current settings
  28. self.COMPUTER_SLEEP = self.run_function('power.get_computer_sleep')
  29. self.DISPLAY_SLEEP = self.run_function('power.get_display_sleep')
  30. self.HARD_DISK_SLEEP = self.run_function('power.get_harddisk_sleep')
  31. def tearDown(self):
  32. '''
  33. Reset to original settings
  34. '''
  35. self.run_function('power.set_computer_sleep', [self.COMPUTER_SLEEP])
  36. self.run_function('power.set_display_sleep', [self.DISPLAY_SLEEP])
  37. self.run_function('power.set_harddisk_sleep', [self.HARD_DISK_SLEEP])
  38. @pytest.mark.destructive_test
  39. def test_computer_sleep(self):
  40. '''
  41. Test power.get_computer_sleep
  42. Test power.set_computer_sleep
  43. '''
  44. # Normal Functionality
  45. self.assertTrue(self.run_function('power.set_computer_sleep', [90]))
  46. self.assertEqual(
  47. self.run_function('power.get_computer_sleep'), 'after 90 minutes')
  48. self.assertTrue(self.run_function('power.set_computer_sleep', ['Off']))
  49. self.assertEqual(self.run_function('power.get_computer_sleep'), 'Never')
  50. # Test invalid input
  51. self.assertIn(
  52. 'Invalid String Value for Minutes',
  53. self.run_function('power.set_computer_sleep', ['spongebob']))
  54. self.assertIn(
  55. 'Invalid Integer Value for Minutes',
  56. self.run_function('power.set_computer_sleep', [0]))
  57. self.assertIn(
  58. 'Invalid Integer Value for Minutes',
  59. self.run_function('power.set_computer_sleep', [181]))
  60. self.assertIn(
  61. 'Invalid Boolean Value for Minutes',
  62. self.run_function('power.set_computer_sleep', [True]))
  63. @pytest.mark.destructive_test
  64. def test_display_sleep(self):
  65. '''
  66. Test power.get_display_sleep
  67. Test power.set_display_sleep
  68. '''
  69. # Normal Functionality
  70. self.assertTrue(self.run_function('power.set_display_sleep', [90]))
  71. self.assertEqual(
  72. self.run_function('power.get_display_sleep'), 'after 90 minutes')
  73. self.assertTrue(self.run_function('power.set_display_sleep', ['Off']))
  74. self.assertEqual(self.run_function('power.get_display_sleep'), 'Never')
  75. # Test invalid input
  76. self.assertIn(
  77. 'Invalid String Value for Minutes',
  78. self.run_function('power.set_display_sleep', ['spongebob']))
  79. self.assertIn(
  80. 'Invalid Integer Value for Minutes',
  81. self.run_function('power.set_display_sleep', [0]))
  82. self.assertIn(
  83. 'Invalid Integer Value for Minutes',
  84. self.run_function('power.set_display_sleep', [181]))
  85. self.assertIn(
  86. 'Invalid Boolean Value for Minutes',
  87. self.run_function('power.set_display_sleep', [True]))
  88. @pytest.mark.destructive_test
  89. def test_harddisk_sleep(self):
  90. '''
  91. Test power.get_harddisk_sleep
  92. Test power.set_harddisk_sleep
  93. '''
  94. # Normal Functionality
  95. self.assertTrue(self.run_function('power.set_harddisk_sleep', [90]))
  96. self.assertEqual(
  97. self.run_function('power.get_harddisk_sleep'), 'after 90 minutes')
  98. self.assertTrue(self.run_function('power.set_harddisk_sleep', ['Off']))
  99. self.assertEqual(self.run_function('power.get_harddisk_sleep'), 'Never')
  100. # Test invalid input
  101. self.assertIn(
  102. 'Invalid String Value for Minutes',
  103. self.run_function('power.set_harddisk_sleep', ['spongebob']))
  104. self.assertIn(
  105. 'Invalid Integer Value for Minutes',
  106. self.run_function('power.set_harddisk_sleep', [0]))
  107. self.assertIn(
  108. 'Invalid Integer Value for Minutes',
  109. self.run_function('power.set_harddisk_sleep', [181]))
  110. self.assertIn(
  111. 'Invalid Boolean Value for Minutes',
  112. self.run_function('power.set_harddisk_sleep', [True]))
  113. def test_restart_freeze(self):
  114. '''
  115. Test power.get_restart_freeze
  116. Test power.set_restart_freeze
  117. '''
  118. # Normal Functionality
  119. self.assertTrue(self.run_function('power.set_restart_freeze', ['on']))
  120. self.assertTrue(self.run_function('power.get_restart_freeze'))
  121. # This will return False because mac fails to actually make the change
  122. self.assertFalse(
  123. self.run_function('power.set_restart_freeze', ['off']))
  124. # Even setting to off returns true, it actually is never set
  125. # This is an apple bug
  126. self.assertTrue(self.run_function('power.get_restart_freeze'))
  127. @pytest.mark.skip_if_not_root
  128. @flaky(attempts=10)
  129. @skipIf(not salt.utils.platform.is_darwin(), 'Test only available on macOS')
  130. @skipIf(not salt.utils.path.which('systemsetup'), '\'systemsetup\' binary not found in $PATH')
  131. class MacPowerModuleTestSleepOnPowerButton(ModuleCase):
  132. '''
  133. Test power.get_sleep_on_power_button
  134. Test power.set_sleep_on_power_button
  135. '''
  136. SLEEP_ON_BUTTON = None
  137. def setUp(self):
  138. '''
  139. Check if function is available
  140. Get existing value
  141. '''
  142. # Is the function available
  143. ret = self.run_function('power.get_sleep_on_power_button')
  144. if isinstance(ret, bool):
  145. self.SLEEP_ON_BUTTON = self.run_function(
  146. 'power.get_sleep_on_power_button')
  147. def tearDown(self):
  148. '''
  149. Reset to original value
  150. '''
  151. if self.SLEEP_ON_BUTTON is not None:
  152. self.run_function(
  153. 'power.set_sleep_on_power_button', [self.SLEEP_ON_BUTTON])
  154. def test_sleep_on_power_button(self):
  155. '''
  156. Test power.get_sleep_on_power_button
  157. Test power.set_sleep_on_power_button
  158. '''
  159. # If available on this system, test it
  160. if self.SLEEP_ON_BUTTON is None:
  161. # Check for not available
  162. ret = self.run_function('power.get_sleep_on_power_button')
  163. self.assertIn('Error', ret)
  164. else:
  165. self.assertTrue(
  166. self.run_function('power.set_sleep_on_power_button', ['on']))
  167. self.assertTrue(
  168. self.run_function('power.get_sleep_on_power_button'))
  169. self.assertTrue(
  170. self.run_function('power.set_sleep_on_power_button', ['off']))
  171. self.assertFalse(
  172. self.run_function('power.get_sleep_on_power_button'))
  173. @pytest.mark.skip_if_not_root
  174. @flaky(attempts=10)
  175. @skipIf(not salt.utils.platform.is_darwin(), 'Test only available on macOS')
  176. @skipIf(not salt.utils.path.which('systemsetup'), '\'systemsetup\' binary not found in $PATH')
  177. class MacPowerModuleTestRestartPowerFailure(ModuleCase):
  178. '''
  179. Test power.get_restart_power_failure
  180. Test power.set_restart_power_failure
  181. '''
  182. RESTART_POWER = None
  183. def setUp(self):
  184. '''
  185. Check if function is available
  186. Get existing value
  187. '''
  188. # Is the function available
  189. ret = self.run_function('power.get_restart_power_failure')
  190. if isinstance(ret, bool):
  191. self.RESTART_POWER = ret
  192. def tearDown(self):
  193. '''
  194. Reset to original value
  195. '''
  196. if self.RESTART_POWER is not None:
  197. self.run_function(
  198. 'power.set_sleep_on_power_button', [self.SLEEP_ON_BUTTON])
  199. def test_restart_power_failure(self):
  200. '''
  201. Test power.get_restart_power_failure
  202. Test power.set_restart_power_failure
  203. '''
  204. # If available on this system, test it
  205. if self.RESTART_POWER is None:
  206. # Check for not available
  207. ret = self.run_function('power.get_restart_power_failure')
  208. self.assertIn('Error', ret)
  209. else:
  210. self.assertTrue(
  211. self.run_function('power.set_restart_power_failure', ['on']))
  212. self.assertTrue(
  213. self.run_function('power.get_restart_power_failure'))
  214. self.assertTrue(
  215. self.run_function('power.set_restart_power_failure', ['off']))
  216. self.assertFalse(
  217. self.run_function('power.get_restart_power_failure'))
  218. @pytest.mark.skip_if_not_root
  219. @flaky(attempts=10)
  220. @skipIf(not salt.utils.platform.is_darwin(), 'Test only available on macOS')
  221. @skipIf(not salt.utils.path.which('systemsetup'), '\'systemsetup\' binary not found in $PATH')
  222. class MacPowerModuleTestWakeOnNet(ModuleCase):
  223. '''
  224. Test power.get_wake_on_network
  225. Test power.set_wake_on_network
  226. '''
  227. WAKE_ON_NET = None
  228. def setUp(self):
  229. '''
  230. Check if function is available
  231. Get existing value
  232. '''
  233. # Is the function available
  234. ret = self.run_function('power.get_wake_on_network')
  235. if isinstance(ret, bool):
  236. self.WAKE_ON_NET = ret
  237. def tearDown(self):
  238. '''
  239. Reset to original value
  240. '''
  241. if self.WAKE_ON_NET is not None:
  242. self.run_function('power.set_wake_on_network', [self.WAKE_ON_NET])
  243. def test_wake_on_network(self):
  244. '''
  245. Test power.get_wake_on_network
  246. Test power.set_wake_on_network
  247. '''
  248. # If available on this system, test it
  249. if self.WAKE_ON_NET is None:
  250. # Check for not available
  251. ret = self.run_function('power.get_wake_on_network')
  252. self.assertIn('Error', ret)
  253. else:
  254. self.assertTrue(
  255. self.run_function('power.set_wake_on_network', ['on']))
  256. self.assertTrue(self.run_function('power.get_wake_on_network'))
  257. self.assertTrue(
  258. self.run_function('power.set_wake_on_network', ['off']))
  259. self.assertFalse(self.run_function('power.get_wake_on_network'))
  260. @pytest.mark.skip_if_not_root
  261. @flaky(attempts=10)
  262. @skipIf(not salt.utils.platform.is_darwin(), 'Test only available on macOS')
  263. @skipIf(not salt.utils.path.which('systemsetup'), '\'systemsetup\' binary not found in $PATH')
  264. class MacPowerModuleTestWakeOnModem(ModuleCase):
  265. '''
  266. Test power.get_wake_on_modem
  267. Test power.set_wake_on_modem
  268. '''
  269. WAKE_ON_MODEM = None
  270. def setUp(self):
  271. '''
  272. Check if function is available
  273. Get existing value
  274. '''
  275. # Is the function available
  276. ret = self.run_function('power.get_wake_on_modem')
  277. if isinstance(ret, bool):
  278. self.WAKE_ON_MODEM = ret
  279. def tearDown(self):
  280. '''
  281. Reset to original value
  282. '''
  283. if self.WAKE_ON_MODEM is not None:
  284. self.run_function('power.set_wake_on_modem', [self.WAKE_ON_MODEM])
  285. def test_wake_on_modem(self):
  286. '''
  287. Test power.get_wake_on_modem
  288. Test power.set_wake_on_modem
  289. '''
  290. # If available on this system, test it
  291. if self.WAKE_ON_MODEM is None:
  292. # Check for not available
  293. ret = self.run_function('power.get_wake_on_modem')
  294. self.assertIn('Error', ret)
  295. else:
  296. self.assertTrue(
  297. self.run_function('power.set_wake_on_modem', ['on']))
  298. self.assertTrue(self.run_function('power.get_wake_on_modem'))
  299. self.assertTrue(
  300. self.run_function('power.set_wake_on_modem', ['off']))
  301. self.assertFalse(self.run_function('power.get_wake_on_modem'))