test_mac_power.py 11 KB

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