2016-11-30 17:22:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2016-11-30 17:27:37 +00:00
|
|
|
Tests for the service state
|
2016-11-30 17:22:05 +00:00
|
|
|
'''
|
|
|
|
# Import python libs
|
2018-01-19 05:49:04 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2018-01-05 17:25:44 +00:00
|
|
|
import re
|
2016-11-30 17:22:05 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.case import ModuleCase
|
2017-02-27 15:59:04 +00:00
|
|
|
from tests.support.helpers import destructiveTest
|
2017-04-02 16:09:47 +00:00
|
|
|
from tests.support.mixins import SaltReturnAssertsMixin
|
2016-11-30 17:22:05 +00:00
|
|
|
|
|
|
|
# Import salt libs
|
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-07-25 01:47:15 +00:00
|
|
|
import salt.utils.path
|
2016-11-30 17:22:05 +00:00
|
|
|
|
|
|
|
INIT_DELAY = 5
|
|
|
|
|
2016-11-30 20:22:53 +00:00
|
|
|
|
2016-11-30 17:22:05 +00:00
|
|
|
@destructiveTest
|
2017-04-03 16:04:09 +00:00
|
|
|
class ServiceTest(ModuleCase, SaltReturnAssertsMixin):
|
2016-11-30 17:22:05 +00:00
|
|
|
'''
|
|
|
|
Validate the service state
|
|
|
|
'''
|
2017-11-16 19:12:59 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.service_name = 'cron'
|
|
|
|
cmd_name = 'crontab'
|
|
|
|
os_family = self.run_function('grains.get', ['os_family'])
|
2018-01-23 15:07:10 +00:00
|
|
|
os_release = self.run_function('grains.get', ['osrelease'])
|
2018-01-05 17:25:44 +00:00
|
|
|
self.stopped = False
|
|
|
|
self.running = True
|
2017-11-16 19:12:59 +00:00
|
|
|
if os_family == 'RedHat':
|
|
|
|
self.service_name = 'crond'
|
|
|
|
elif os_family == 'Arch':
|
2018-01-19 22:16:01 +00:00
|
|
|
self.service_name = 'sshd'
|
2017-11-16 19:12:59 +00:00
|
|
|
cmd_name = 'systemctl'
|
2018-01-05 17:25:44 +00:00
|
|
|
elif os_family == 'MacOS':
|
|
|
|
self.service_name = 'org.ntp.ntpd'
|
2018-01-23 15:07:10 +00:00
|
|
|
if int(os_release.split('.')[1]) >= 13:
|
|
|
|
self.service_name = 'com.apple.AirPlayXPCHelper'
|
2018-01-05 17:25:44 +00:00
|
|
|
self.stopped = ''
|
|
|
|
self.running = '[0-9]'
|
2017-11-16 19:12:59 +00:00
|
|
|
|
2017-11-22 17:44:36 +00:00
|
|
|
if salt.utils.path.which(cmd_name) is None:
|
2017-11-16 19:12:59 +00:00
|
|
|
self.skipTest('{0} is not installed'.format(cmd_name))
|
|
|
|
|
2016-11-30 17:22:05 +00:00
|
|
|
def check_service_status(self, exp_return):
|
|
|
|
'''
|
|
|
|
helper method to check status of service
|
|
|
|
'''
|
2017-11-16 19:12:59 +00:00
|
|
|
check_status = self.run_function('service.status',
|
|
|
|
name=self.service_name)
|
2018-01-05 17:25:44 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
if not re.match(exp_return, check_status):
|
|
|
|
self.fail('status of service is not returning correctly')
|
|
|
|
except TypeError:
|
|
|
|
if check_status is not exp_return:
|
|
|
|
self.fail('status of service is not returning correctly')
|
2016-11-30 17:22:05 +00:00
|
|
|
|
2017-11-16 19:12:59 +00:00
|
|
|
def test_service_running(self):
|
|
|
|
'''
|
|
|
|
test service.running state module
|
|
|
|
'''
|
|
|
|
stop_service = self.run_function('service.stop', self.service_name)
|
|
|
|
self.assertTrue(stop_service)
|
2018-01-05 17:25:44 +00:00
|
|
|
self.check_service_status(self.stopped)
|
2017-11-16 19:12:59 +00:00
|
|
|
|
|
|
|
start_service = self.run_state('service.running',
|
|
|
|
name=self.service_name)
|
|
|
|
self.assertTrue(start_service)
|
2018-01-05 17:25:44 +00:00
|
|
|
self.check_service_status(self.running)
|
2017-11-16 19:12:59 +00:00
|
|
|
|
2016-11-30 17:22:05 +00:00
|
|
|
def test_service_dead(self):
|
|
|
|
'''
|
|
|
|
test service.dead state module
|
|
|
|
'''
|
2017-11-16 19:12:59 +00:00
|
|
|
start_service = self.run_state('service.running',
|
|
|
|
name=self.service_name)
|
2016-11-30 17:22:05 +00:00
|
|
|
self.assertSaltTrueReturn(start_service)
|
2018-01-05 17:25:44 +00:00
|
|
|
self.check_service_status(self.running)
|
2016-11-30 17:22:05 +00:00
|
|
|
|
2017-11-16 19:12:59 +00:00
|
|
|
ret = self.run_state('service.dead', name=self.service_name)
|
2016-11-30 17:22:05 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2018-01-05 17:25:44 +00:00
|
|
|
self.check_service_status(self.stopped)
|
2016-11-30 17:22:05 +00:00
|
|
|
|
|
|
|
def test_service_dead_init_delay(self):
|
|
|
|
'''
|
|
|
|
test service.dead state module with init_delay arg
|
|
|
|
'''
|
2017-11-16 19:12:59 +00:00
|
|
|
start_service = self.run_state('service.running',
|
|
|
|
name=self.service_name)
|
2016-11-30 17:22:05 +00:00
|
|
|
self.assertSaltTrueReturn(start_service)
|
2018-01-05 17:25:44 +00:00
|
|
|
self.check_service_status(self.running)
|
2016-11-30 17:22:05 +00:00
|
|
|
|
2017-11-16 19:12:59 +00:00
|
|
|
ret = self.run_state('service.dead', name=self.service_name,
|
2016-11-30 17:22:05 +00:00
|
|
|
init_delay=INIT_DELAY)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2018-01-05 17:25:44 +00:00
|
|
|
self.check_service_status(self.stopped)
|