1
0

test_mac_service.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. """
  2. integration tests for mac_service
  3. """
  4. import plistlib
  5. import salt.utils.files
  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.salt.integration.test"
  22. SERVICE_PATH = "/Library/LaunchDaemons/com.salt.integration.test.plist"
  23. def setUp(self):
  24. """
  25. setup our test launch service.
  26. """
  27. service_data = {
  28. "KeepAlive": True,
  29. "Label": self.SERVICE_NAME,
  30. "ProgramArguments": ["/bin/sleep", "1000"],
  31. "RunAtLoad": True,
  32. }
  33. with salt.utils.files.fopen(self.SERVICE_PATH, "wb") as fp:
  34. plistlib.dump(service_data, fp)
  35. self.run_function("service.enable", [self.SERVICE_NAME])
  36. self.run_function("service.start", [self.SERVICE_NAME])
  37. def tearDown(self):
  38. """
  39. stop and remove our test service.
  40. """
  41. self.run_function("service.stop", [self.SERVICE_NAME])
  42. salt.utils.files.safe_rm(self.SERVICE_PATH)
  43. @slowTest
  44. def test_show(self):
  45. """
  46. Test service.show
  47. """
  48. # Existing Service
  49. service_info = self.run_function("service.show", [self.SERVICE_NAME])
  50. self.assertIsInstance(service_info, dict)
  51. self.assertEqual(service_info["plist"]["Label"], self.SERVICE_NAME)
  52. # Missing Service
  53. self.assertIn(
  54. "Service not found", self.run_function("service.show", ["spongebob"])
  55. )
  56. @slowTest
  57. def test_launchctl(self):
  58. """
  59. Test service.launchctl
  60. """
  61. # Expected Functionality
  62. self.assertTrue(
  63. self.run_function("service.launchctl", ["error", "bootstrap", 64])
  64. )
  65. self.assertEqual(
  66. self.run_function(
  67. "service.launchctl", ["error", "bootstrap", 64], return_stdout=True
  68. ),
  69. "64: unknown error code",
  70. )
  71. # Raise an error
  72. self.assertIn(
  73. "Failed to error service",
  74. self.run_function("service.launchctl", ["error", "bootstrap"]),
  75. )
  76. @slowTest
  77. def test_list(self):
  78. """
  79. Test service.list
  80. """
  81. # Expected Functionality
  82. self.assertIn("PID", self.run_function("service.list"))
  83. self.assertIn("{", self.run_function("service.list", [self.SERVICE_NAME]))
  84. # Service not found
  85. self.assertIn(
  86. "Service not found", self.run_function("service.list", ["spongebob"])
  87. )
  88. @destructiveTest
  89. @slowTest
  90. def test_enable(self):
  91. """
  92. Test service.enable
  93. """
  94. self.assertTrue(self.run_function("service.enable", [self.SERVICE_NAME]))
  95. self.assertIn(
  96. "Service not found", self.run_function("service.enable", ["spongebob"])
  97. )
  98. @destructiveTest
  99. @slowTest
  100. def test_disable(self):
  101. """
  102. Test service.disable
  103. """
  104. self.assertTrue(self.run_function("service.disable", [self.SERVICE_NAME]))
  105. self.assertIn(
  106. "Service not found", self.run_function("service.disable", ["spongebob"])
  107. )
  108. @destructiveTest
  109. @slowTest
  110. def test_start(self):
  111. """
  112. Test service.start
  113. Test service.stop
  114. Test service.status
  115. """
  116. self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME]))
  117. self.assertIn(
  118. "Service not found", self.run_function("service.start", ["spongebob"])
  119. )
  120. @destructiveTest
  121. @slowTest
  122. def test_stop(self):
  123. """
  124. Test service.stop
  125. """
  126. self.assertTrue(self.run_function("service.stop", [self.SERVICE_NAME]))
  127. self.assertIn(
  128. "Service not found", self.run_function("service.stop", ["spongebob"])
  129. )
  130. @destructiveTest
  131. @slowTest
  132. def test_status(self):
  133. """
  134. Test service.status
  135. """
  136. # A running service
  137. self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME]))
  138. self.assertTrue(self.run_function("service.status", [self.SERVICE_NAME]))
  139. # A stopped service
  140. self.assertTrue(self.run_function("service.stop", [self.SERVICE_NAME]))
  141. self.assertFalse(self.run_function("service.status", [self.SERVICE_NAME]))
  142. # Service not found
  143. self.assertFalse(self.run_function("service.status", ["spongebob"]))
  144. @slowTest
  145. def test_available(self):
  146. """
  147. Test service.available
  148. """
  149. self.assertTrue(self.run_function("service.available", [self.SERVICE_NAME]))
  150. self.assertFalse(self.run_function("service.available", ["spongebob"]))
  151. @slowTest
  152. def test_missing(self):
  153. """
  154. Test service.missing
  155. """
  156. self.assertFalse(self.run_function("service.missing", [self.SERVICE_NAME]))
  157. self.assertTrue(self.run_function("service.missing", ["spongebob"]))
  158. @destructiveTest
  159. @slowTest
  160. def test_enabled(self):
  161. """
  162. Test service.enabled
  163. """
  164. self.assertTrue(self.run_function("service.enabled", [self.SERVICE_NAME]))
  165. self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME]))
  166. self.assertTrue(self.run_function("service.enabled", [self.SERVICE_NAME]))
  167. self.assertTrue(self.run_function("service.stop", [self.SERVICE_NAME]))
  168. self.assertTrue(self.run_function("service.enabled", ["spongebob"]))
  169. @destructiveTest
  170. @slowTest
  171. def test_disabled(self):
  172. """
  173. Test service.disabled
  174. """
  175. self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME]))
  176. self.assertFalse(self.run_function("service.disabled", [self.SERVICE_NAME]))
  177. self.assertTrue(self.run_function("service.disable", [self.SERVICE_NAME]))
  178. self.assertTrue(self.run_function("service.disabled", [self.SERVICE_NAME]))
  179. self.assertTrue(self.run_function("service.enable", [self.SERVICE_NAME]))
  180. self.assertIn(
  181. "Service not found", self.run_function("service.stop", ["spongebob"])
  182. )
  183. @slowTest
  184. def test_get_all(self):
  185. """
  186. Test service.get_all
  187. """
  188. services = self.run_function("service.get_all")
  189. self.assertIsInstance(services, list)
  190. self.assertIn(self.SERVICE_NAME, services)
  191. @slowTest
  192. def test_get_enabled(self):
  193. """
  194. Test service.get_enabled
  195. """
  196. services = self.run_function("service.get_enabled")
  197. self.assertIsInstance(services, list)
  198. self.assertIn(self.SERVICE_NAME, services)
  199. @slowTest
  200. def test_service_laoded(self):
  201. """
  202. Test service.get_enabled
  203. """
  204. self.assertTrue(self.run_function("service.loaded", [self.SERVICE_NAME]))