2016-03-22 23:29:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
integration tests for mac_service
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import python libs
|
|
|
|
from __future__ import absolute_import, print_function
|
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.case import ModuleCase
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf
|
2017-04-04 17:57:27 +00:00
|
|
|
from tests.support.helpers import destructiveTest, skip_if_not_root
|
2016-03-22 23:29:32 +00:00
|
|
|
|
|
|
|
# Import salt libs
|
|
|
|
import salt.utils
|
|
|
|
|
2016-09-09 14:47:51 +00:00
|
|
|
|
2016-12-28 21:49:46 +00:00
|
|
|
@skipIf(not salt.utils.is_darwin(), 'Test only available on macOS')
|
2016-09-08 22:30:35 +00:00
|
|
|
@skipIf(not salt.utils.which('launchctl'), 'Test requires launchctl binary')
|
|
|
|
@skipIf(not salt.utils.which('plutil'), 'Test requires plutil binary')
|
2017-04-04 17:57:27 +00:00
|
|
|
@skip_if_not_root
|
2017-04-03 16:04:09 +00:00
|
|
|
class MacServiceModuleTest(ModuleCase):
|
2016-03-22 23:29:32 +00:00
|
|
|
'''
|
|
|
|
Validate the mac_service module
|
|
|
|
'''
|
2016-03-23 21:15:41 +00:00
|
|
|
SERVICE_NAME = 'com.apple.apsd'
|
|
|
|
SERVICE_ENABLED = False
|
2016-03-22 23:29:32 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
'''
|
2016-09-08 22:30:35 +00:00
|
|
|
Get current state of the test service
|
2016-03-22 23:29:32 +00:00
|
|
|
'''
|
2016-03-23 21:15:41 +00:00
|
|
|
self.SERVICE_ENABLED = self.run_function('service.enabled',
|
|
|
|
[self.SERVICE_NAME])
|
2016-03-23 17:13:55 +00:00
|
|
|
|
2016-03-22 23:29:32 +00:00
|
|
|
def tearDown(self):
|
|
|
|
'''
|
2016-09-08 22:30:35 +00:00
|
|
|
Reset the test service to the original state
|
2016-03-22 23:29:32 +00:00
|
|
|
'''
|
2016-03-23 21:15:41 +00:00
|
|
|
if self.SERVICE_ENABLED:
|
|
|
|
self.run_function('service.start', [self.SERVICE_NAME])
|
2016-03-23 17:13:55 +00:00
|
|
|
else:
|
2016-03-23 21:15:41 +00:00
|
|
|
self.run_function('service.stop', [self.SERVICE_NAME])
|
2016-03-22 23:29:32 +00:00
|
|
|
|
2016-03-23 15:50:56 +00:00
|
|
|
def test_show(self):
|
|
|
|
'''
|
|
|
|
Test service.show
|
|
|
|
'''
|
|
|
|
# Existing Service
|
2016-03-23 21:15:41 +00:00
|
|
|
service_info = self.run_function('service.show', [self.SERVICE_NAME])
|
2016-03-23 15:50:56 +00:00
|
|
|
self.assertIsInstance(service_info, dict)
|
2016-03-23 21:15:41 +00:00
|
|
|
self.assertEqual(service_info['plist']['Label'], self.SERVICE_NAME)
|
2016-03-23 15:50:56 +00:00
|
|
|
|
|
|
|
# Missing Service
|
|
|
|
self.assertIn(
|
|
|
|
'Service not found',
|
|
|
|
self.run_function('service.show', ['spongebob']))
|
|
|
|
|
|
|
|
def test_launchctl(self):
|
|
|
|
'''
|
|
|
|
Test service.launchctl
|
|
|
|
'''
|
|
|
|
# Expected Functionality
|
|
|
|
self.assertTrue(
|
2016-09-08 23:07:28 +00:00
|
|
|
self.run_function('service.launchctl',
|
2016-09-21 16:46:54 +00:00
|
|
|
['error', 'bootstrap', 64]))
|
2016-03-23 15:50:56 +00:00
|
|
|
self.assertEqual(
|
2016-03-23 17:52:49 +00:00
|
|
|
self.run_function('service.launchctl',
|
2016-09-21 16:46:54 +00:00
|
|
|
['error', 'bootstrap', 64],
|
2016-03-23 17:56:49 +00:00
|
|
|
return_stdout=True),
|
2016-03-23 15:50:56 +00:00
|
|
|
'64: unknown error code')
|
|
|
|
|
|
|
|
# Raise an error
|
|
|
|
self.assertIn(
|
2016-09-08 22:30:35 +00:00
|
|
|
'Failed to error service',
|
|
|
|
self.run_function('service.launchctl', ['error', 'bootstrap']))
|
2016-03-23 15:50:56 +00:00
|
|
|
|
2016-03-23 17:13:55 +00:00
|
|
|
def test_list(self):
|
|
|
|
'''
|
|
|
|
Test service.list
|
|
|
|
'''
|
|
|
|
# Expected Functionality
|
|
|
|
self.assertIn('PID', self.run_function('service.list'))
|
|
|
|
self.assertIn(
|
|
|
|
'{',
|
|
|
|
self.run_function('service.list', ['com.apple.coreservicesd']))
|
|
|
|
|
|
|
|
# Service not found
|
|
|
|
self.assertIn(
|
|
|
|
'Service not found',
|
|
|
|
self.run_function('service.list', ['spongebob']))
|
|
|
|
|
2016-04-18 14:40:20 +00:00
|
|
|
@destructiveTest
|
|
|
|
def test_enable(self):
|
|
|
|
'''
|
|
|
|
Test service.enable
|
|
|
|
'''
|
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('service.enable', [self.SERVICE_NAME]))
|
|
|
|
|
|
|
|
self.assertIn(
|
|
|
|
'Service not found',
|
|
|
|
self.run_function('service.enable', ['spongebob']))
|
|
|
|
|
|
|
|
@destructiveTest
|
|
|
|
def test_disable(self):
|
|
|
|
'''
|
|
|
|
Test service.disable
|
|
|
|
'''
|
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('service.disable', [self.SERVICE_NAME]))
|
|
|
|
|
|
|
|
self.assertIn(
|
|
|
|
'Service not found',
|
|
|
|
self.run_function('service.disable', ['spongebob']))
|
|
|
|
|
2016-03-23 17:13:55 +00:00
|
|
|
@destructiveTest
|
|
|
|
def test_start(self):
|
|
|
|
'''
|
|
|
|
Test service.start
|
|
|
|
Test service.stop
|
|
|
|
Test service.status
|
|
|
|
'''
|
2016-03-23 21:15:41 +00:00
|
|
|
self.assertTrue(self.run_function('service.start', [self.SERVICE_NAME]))
|
2016-03-23 17:13:55 +00:00
|
|
|
|
|
|
|
self.assertIn(
|
|
|
|
'Service not found',
|
|
|
|
self.run_function('service.start', ['spongebob']))
|
|
|
|
|
|
|
|
@destructiveTest
|
|
|
|
def test_stop(self):
|
|
|
|
'''
|
|
|
|
Test service.stop
|
|
|
|
'''
|
2016-03-23 21:15:41 +00:00
|
|
|
self.assertTrue(self.run_function('service.stop', [self.SERVICE_NAME]))
|
2016-03-23 17:13:55 +00:00
|
|
|
|
|
|
|
self.assertIn(
|
|
|
|
'Service not found',
|
|
|
|
self.run_function('service.stop', ['spongebob']))
|
|
|
|
|
|
|
|
@destructiveTest
|
|
|
|
def test_status(self):
|
|
|
|
'''
|
|
|
|
Test service.status
|
|
|
|
'''
|
|
|
|
# A running service
|
2016-03-23 21:15:41 +00:00
|
|
|
self.assertTrue(self.run_function('service.start', [self.SERVICE_NAME]))
|
2016-03-23 17:13:55 +00:00
|
|
|
self.assertTrue(
|
2016-03-23 21:15:41 +00:00
|
|
|
self.run_function('service.status', [self.SERVICE_NAME]).isdigit())
|
2016-03-23 17:13:55 +00:00
|
|
|
|
|
|
|
# A stopped service
|
2016-03-23 21:15:41 +00:00
|
|
|
self.assertTrue(self.run_function('service.stop', [self.SERVICE_NAME]))
|
2016-03-23 17:13:55 +00:00
|
|
|
self.assertEqual(
|
|
|
|
'',
|
2016-03-23 21:15:41 +00:00
|
|
|
self.run_function('service.status', [self.SERVICE_NAME]))
|
2016-03-23 17:13:55 +00:00
|
|
|
|
|
|
|
# Service not found
|
|
|
|
self.assertEqual('', self.run_function('service.status', ['spongebob']))
|
|
|
|
|
|
|
|
def test_available(self):
|
|
|
|
'''
|
|
|
|
Test service.available
|
|
|
|
'''
|
2016-03-23 21:15:41 +00:00
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('service.available', [self.SERVICE_NAME]))
|
2016-03-23 17:13:55 +00:00
|
|
|
self.assertFalse(self.run_function('service.available', ['spongebob']))
|
|
|
|
|
|
|
|
def test_missing(self):
|
|
|
|
'''
|
|
|
|
Test service.missing
|
|
|
|
'''
|
2016-03-23 21:15:41 +00:00
|
|
|
self.assertFalse(self.run_function('service.missing', [self.SERVICE_NAME]))
|
2016-03-23 17:13:55 +00:00
|
|
|
self.assertTrue(self.run_function('service.missing', ['spongebob']))
|
|
|
|
|
|
|
|
@destructiveTest
|
|
|
|
def test_enabled(self):
|
|
|
|
'''
|
|
|
|
Test service.enabled
|
|
|
|
'''
|
2016-03-23 21:15:41 +00:00
|
|
|
self.assertTrue(self.run_function('service.start', [self.SERVICE_NAME]))
|
|
|
|
self.assertTrue(
|
|
|
|
self.run_function('service.enabled', [self.SERVICE_NAME]))
|
2016-03-23 17:13:55 +00:00
|
|
|
|
2016-03-23 21:15:41 +00:00
|
|
|
self.assertTrue(self.run_function('service.stop', [self.SERVICE_NAME]))
|
|
|
|
self.assertFalse(
|
|
|
|
self.run_function('service.enabled', [self.SERVICE_NAME]))
|
2016-03-23 17:13:55 +00:00
|
|
|
|
|
|
|
self.assertFalse(self.run_function('service.enabled', ['spongebob']))
|
|
|
|
|
|
|
|
@destructiveTest
|
|
|
|
def test_disabled(self):
|
|
|
|
'''
|
|
|
|
Test service.disabled
|
|
|
|
'''
|
2016-11-15 16:33:01 +00:00
|
|
|
SERVICE_NAME = 'com.apple.nfsd'
|
2016-11-15 16:25:28 +00:00
|
|
|
self.assertTrue(self.run_function('service.start', [SERVICE_NAME]))
|
2016-03-23 21:15:41 +00:00
|
|
|
self.assertFalse(
|
2016-11-15 16:25:28 +00:00
|
|
|
self.run_function('service.disabled', [SERVICE_NAME]))
|
2016-03-23 17:13:55 +00:00
|
|
|
|
2018-07-19 18:15:58 +00:00
|
|
|
self.assertTrue(self.run_function('service.disable', [SERVICE_NAME]))
|
2016-03-23 21:15:41 +00:00
|
|
|
self.assertTrue(
|
2016-11-15 16:25:28 +00:00
|
|
|
self.run_function('service.disabled', [SERVICE_NAME]))
|
2018-07-19 18:15:58 +00:00
|
|
|
self.assertTrue(self.run_function('service.enable', [SERVICE_NAME]))
|
2016-03-23 17:13:55 +00:00
|
|
|
|
2018-07-19 18:15:58 +00:00
|
|
|
self.assertFalse(self.run_function('service.disabled', ['spongebob']))
|
2016-03-23 17:13:55 +00:00
|
|
|
|
|
|
|
def test_get_all(self):
|
|
|
|
'''
|
|
|
|
Test service.get_all
|
|
|
|
'''
|
|
|
|
services = self.run_function('service.get_all')
|
|
|
|
self.assertIsInstance(services, list)
|
2016-03-23 21:15:41 +00:00
|
|
|
self.assertIn(self.SERVICE_NAME, services)
|
2016-03-23 17:13:55 +00:00
|
|
|
|
|
|
|
def test_get_enabled(self):
|
|
|
|
'''
|
|
|
|
Test service.get_enabled
|
|
|
|
'''
|
|
|
|
services = self.run_function('service.get_enabled')
|
|
|
|
self.assertIsInstance(services, list)
|
|
|
|
self.assertIn('com.apple.coreservicesd', services)
|