1
0

test_mac_service.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # -*- coding: utf-8 -*-
  2. """
  3. integration tests for mac_service
  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. runs_on,
  10. skip_if_binaries_missing,
  11. skip_if_not_root,
  12. slowTest,
  13. )
  14. @skip_if_not_root
  15. @runs_on(kernel="Darwin")
  16. @skip_if_binaries_missing("launchctl", "plutil")
  17. class MacServiceModuleTest(ModuleCase):
  18. """
  19. Validate the mac_service module
  20. """
  21. SERVICE_NAME = "com.apple.apsd"
  22. SERVICE_ENABLED = False
  23. def setUp(self):
  24. """
  25. Get current state of the test service
  26. """
  27. self.SERVICE_ENABLED = self.run_function("service.enabled", [self.SERVICE_NAME])
  28. def tearDown(self):
  29. """
  30. Reset the test service to the original state
  31. """
  32. if self.SERVICE_ENABLED:
  33. self.run_function("service.start", [self.SERVICE_NAME])
  34. self.run_function("service.enable", [self.SERVICE_NAME])
  35. else:
  36. self.run_function("service.stop", [self.SERVICE_NAME])
  37. self.run_function("service.disable", [self.SERVICE_NAME])
  38. @slowTest
  39. def test_show(self):
  40. """
  41. Test service.show
  42. """
  43. # Existing Service
  44. service_info = self.run_function("service.show", [self.SERVICE_NAME])
  45. self.assertIsInstance(service_info, dict)
  46. self.assertEqual(service_info["plist"]["Label"], self.SERVICE_NAME)
  47. # Missing Service
  48. self.assertIn(
  49. "Service not found", self.run_function("service.show", ["spongebob"])
  50. )
  51. @slowTest
  52. def test_launchctl(self):
  53. """
  54. Test service.launchctl
  55. """
  56. # Expected Functionality
  57. self.assertTrue(
  58. self.run_function("service.launchctl", ["error", "bootstrap", 64])
  59. )
  60. self.assertEqual(
  61. self.run_function(
  62. "service.launchctl", ["error", "bootstrap", 64], return_stdout=True
  63. ),
  64. "64: unknown error code",
  65. )
  66. # Raise an error
  67. self.assertIn(
  68. "Failed to error service",
  69. self.run_function("service.launchctl", ["error", "bootstrap"]),
  70. )
  71. @slowTest
  72. def test_list(self):
  73. """
  74. Test service.list
  75. """
  76. # Expected Functionality
  77. self.assertIn("PID", self.run_function("service.list"))
  78. self.assertIn(
  79. "{", self.run_function("service.list", ["com.apple.coreservicesd"])
  80. )
  81. # Service not found
  82. self.assertIn(
  83. "Service not found", self.run_function("service.list", ["spongebob"])
  84. )
  85. @destructiveTest
  86. @slowTest
  87. def test_enable(self):
  88. """
  89. Test service.enable
  90. """
  91. self.assertTrue(self.run_function("service.enable", [self.SERVICE_NAME]))
  92. self.assertIn(
  93. "Service not found", self.run_function("service.enable", ["spongebob"])
  94. )
  95. @destructiveTest
  96. @slowTest
  97. def test_disable(self):
  98. """
  99. Test service.disable
  100. """
  101. self.assertTrue(self.run_function("service.disable", [self.SERVICE_NAME]))
  102. self.assertIn(
  103. "Service not found", self.run_function("service.disable", ["spongebob"])
  104. )
  105. @destructiveTest
  106. @slowTest
  107. def test_start(self):
  108. """
  109. Test service.start
  110. Test service.stop
  111. Test service.status
  112. """
  113. self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME]))
  114. self.assertIn(
  115. "Service not found", self.run_function("service.start", ["spongebob"])
  116. )
  117. @destructiveTest
  118. @slowTest
  119. def test_stop(self):
  120. """
  121. Test service.stop
  122. """
  123. self.assertTrue(self.run_function("service.stop", [self.SERVICE_NAME]))
  124. self.assertIn(
  125. "Service not found", self.run_function("service.stop", ["spongebob"])
  126. )
  127. @destructiveTest
  128. @slowTest
  129. def test_status(self):
  130. """
  131. Test service.status
  132. """
  133. # A running service
  134. self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME]))
  135. self.assertTrue(
  136. self.run_function("service.status", [self.SERVICE_NAME]).isdigit()
  137. )
  138. # A stopped service
  139. self.assertTrue(self.run_function("service.stop", [self.SERVICE_NAME]))
  140. self.assertEqual("", self.run_function("service.status", [self.SERVICE_NAME]))
  141. # Service not found
  142. self.assertEqual("", self.run_function("service.status", ["spongebob"]))
  143. @slowTest
  144. def test_available(self):
  145. """
  146. Test service.available
  147. """
  148. self.assertTrue(self.run_function("service.available", [self.SERVICE_NAME]))
  149. self.assertFalse(self.run_function("service.available", ["spongebob"]))
  150. @slowTest
  151. def test_missing(self):
  152. """
  153. Test service.missing
  154. """
  155. self.assertFalse(self.run_function("service.missing", [self.SERVICE_NAME]))
  156. self.assertTrue(self.run_function("service.missing", ["spongebob"]))
  157. @destructiveTest
  158. @slowTest
  159. def test_enabled(self):
  160. """
  161. Test service.enabled
  162. """
  163. self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME]))
  164. self.assertTrue(self.run_function("service.enabled", [self.SERVICE_NAME]))
  165. self.assertTrue(self.run_function("service.stop", [self.SERVICE_NAME]))
  166. self.assertFalse(self.run_function("service.enabled", [self.SERVICE_NAME]))
  167. self.assertFalse(self.run_function("service.enabled", ["spongebob"]))
  168. @destructiveTest
  169. @slowTest
  170. def test_disabled(self):
  171. """
  172. Test service.disabled
  173. """
  174. SERVICE_NAME = "com.apple.nfsd"
  175. self.assertTrue(self.run_function("service.start", [SERVICE_NAME]))
  176. self.assertFalse(self.run_function("service.disabled", [SERVICE_NAME]))
  177. self.assertTrue(self.run_function("service.disable", [SERVICE_NAME]))
  178. self.assertTrue(self.run_function("service.disabled", [SERVICE_NAME]))
  179. self.assertTrue(self.run_function("service.enable", [SERVICE_NAME]))
  180. self.assertFalse(self.run_function("service.disabled", ["spongebob"]))
  181. @slowTest
  182. def test_get_all(self):
  183. """
  184. Test service.get_all
  185. """
  186. services = self.run_function("service.get_all")
  187. self.assertIsInstance(services, list)
  188. self.assertIn(self.SERVICE_NAME, services)
  189. @slowTest
  190. def test_get_enabled(self):
  191. """
  192. Test service.get_enabled
  193. """
  194. services = self.run_function("service.get_enabled")
  195. self.assertIsInstance(services, list)
  196. self.assertIn("com.apple.coreservicesd", services)