test_mac_power.py 12 KB

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