1
0

test_mac_power.py 11 KB

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