salt/tests/integration/modules/test_mac_service.py
Erik Johnson 3184168365 Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.

A summary of the changes in this PR is as follows:

* Moves the following functions from `salt.utils` to new locations
  (including a deprecation warning if invoked from `salt.utils`):
  `to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
  `dequote`, `is_hex`, `is_bin_str`, `rand_string`,
  `contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
  `which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
  `is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
  `is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
  `is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
  `salt/utils/__init__.py` for better organization, so we can keep the
  deprecated ones separate from the ones yet to be deprecated as we
  continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
  unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
  import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
  accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
  such that it returns a string matching the passed size. Previously
  this function would get `size` bytes from `os.urandom()`,
  base64-encode it, and return the result, which would in most cases not
  be equal to the passed size.
2017-08-08 13:33:43 -05:00

222 lines
6.7 KiB
Python

# -*- coding: utf-8 -*-
'''
integration tests for mac_service
'''
# Import Python libs
from __future__ import absolute_import, print_function
# Import Salt Testing libs
from tests.support.case import ModuleCase
from tests.support.unit import skipIf
from tests.support.helpers import destructiveTest, skip_if_not_root
# Import Salt libs
import salt.utils.path
import salt.utils.platform
@skipIf(not salt.utils.platform.is_darwin(), 'Test only available on macOS')
@skipIf(not salt.utils.path.which('launchctl'), 'Test requires launchctl binary')
@skipIf(not salt.utils.path.which('plutil'), 'Test requires plutil binary')
@skip_if_not_root
class MacServiceModuleTest(ModuleCase):
'''
Validate the mac_service module
'''
SERVICE_NAME = 'com.apple.apsd'
SERVICE_ENABLED = False
def setUp(self):
'''
Get current state of the test service
'''
self.SERVICE_ENABLED = self.run_function('service.enabled',
[self.SERVICE_NAME])
def tearDown(self):
'''
Reset the test service to the original state
'''
if self.SERVICE_ENABLED:
self.run_function('service.start', [self.SERVICE_NAME])
else:
self.run_function('service.stop', [self.SERVICE_NAME])
def test_show(self):
'''
Test service.show
'''
# Existing Service
service_info = self.run_function('service.show', [self.SERVICE_NAME])
self.assertIsInstance(service_info, dict)
self.assertEqual(service_info['plist']['Label'], self.SERVICE_NAME)
# Missing Service
self.assertIn(
'Service not found',
self.run_function('service.show', ['spongebob']))
def test_launchctl(self):
'''
Test service.launchctl
'''
# Expected Functionality
self.assertTrue(
self.run_function('service.launchctl',
['error', 'bootstrap', 64]))
self.assertEqual(
self.run_function('service.launchctl',
['error', 'bootstrap', 64],
return_stdout=True),
'64: unknown error code')
# Raise an error
self.assertIn(
'Failed to error service',
self.run_function('service.launchctl', ['error', 'bootstrap']))
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']))
@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']))
@destructiveTest
def test_start(self):
'''
Test service.start
Test service.stop
Test service.status
'''
self.assertTrue(self.run_function('service.start', [self.SERVICE_NAME]))
self.assertIn(
'Service not found',
self.run_function('service.start', ['spongebob']))
@destructiveTest
def test_stop(self):
'''
Test service.stop
'''
self.assertTrue(self.run_function('service.stop', [self.SERVICE_NAME]))
self.assertIn(
'Service not found',
self.run_function('service.stop', ['spongebob']))
@destructiveTest
def test_status(self):
'''
Test service.status
'''
# A running service
self.assertTrue(self.run_function('service.start', [self.SERVICE_NAME]))
self.assertTrue(
self.run_function('service.status', [self.SERVICE_NAME]).isdigit())
# A stopped service
self.assertTrue(self.run_function('service.stop', [self.SERVICE_NAME]))
self.assertEqual(
'',
self.run_function('service.status', [self.SERVICE_NAME]))
# Service not found
self.assertEqual('', self.run_function('service.status', ['spongebob']))
def test_available(self):
'''
Test service.available
'''
self.assertTrue(
self.run_function('service.available', [self.SERVICE_NAME]))
self.assertFalse(self.run_function('service.available', ['spongebob']))
def test_missing(self):
'''
Test service.missing
'''
self.assertFalse(self.run_function('service.missing', [self.SERVICE_NAME]))
self.assertTrue(self.run_function('service.missing', ['spongebob']))
@destructiveTest
def test_enabled(self):
'''
Test service.enabled
'''
self.assertTrue(self.run_function('service.start', [self.SERVICE_NAME]))
self.assertTrue(
self.run_function('service.enabled', [self.SERVICE_NAME]))
self.assertTrue(self.run_function('service.stop', [self.SERVICE_NAME]))
self.assertFalse(
self.run_function('service.enabled', [self.SERVICE_NAME]))
self.assertFalse(self.run_function('service.enabled', ['spongebob']))
@destructiveTest
def test_disabled(self):
'''
Test service.disabled
'''
SERVICE_NAME = 'com.apple.nfsd'
self.assertTrue(self.run_function('service.start', [SERVICE_NAME]))
self.assertFalse(
self.run_function('service.disabled', [SERVICE_NAME]))
self.assertTrue(self.run_function('service.stop', [SERVICE_NAME]))
self.assertTrue(
self.run_function('service.disabled', [SERVICE_NAME]))
self.assertTrue(self.run_function('service.disabled', ['spongebob']))
def test_get_all(self):
'''
Test service.get_all
'''
services = self.run_function('service.get_all')
self.assertIsInstance(services, list)
self.assertIn(self.SERVICE_NAME, services)
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)