1
0

test_mac_service.py 6.9 KB

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