salt/tests/integration/states/test_service.py

118 lines
4.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
'''
2016-11-30 17:27:37 +00:00
Tests for the service state
'''
# Import python libs
from __future__ import absolute_import, print_function, unicode_literals
import re
# Import Salt Testing libs
from tests.support.case import ModuleCase
from tests.support.helpers import destructiveTest
2017-04-02 16:09:47 +00:00
from tests.support.mixins import SaltReturnAssertsMixin
# 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
import salt.utils.platform
INIT_DELAY = 5
2016-11-30 20:22:53 +00:00
@destructiveTest
class ServiceTest(ModuleCase, SaltReturnAssertsMixin):
'''
Validate the service state
'''
def setUp(self):
self.service_name = 'cron'
cmd_name = 'crontab'
os_family = self.run_function('grains.get', ['os_family'])
os_release = self.run_function('grains.get', ['osrelease'])
self.stopped = False
self.running = True
if os_family == 'RedHat':
self.service_name = 'crond'
elif os_family == 'Arch':
self.service_name = 'sshd'
cmd_name = 'systemctl'
elif os_family == 'MacOS':
self.service_name = 'org.ntp.ntpd'
if int(os_release.split('.')[1]) >= 13:
self.service_name = 'com.apple.AirPlayXPCHelper'
self.stopped = ''
self.running = '[0-9]'
2018-08-27 22:55:12 +00:00
elif os_family == 'Windows':
self.service_name = 'Spooler'
self.pre_srv_enabled = True if self.service_name in self.run_function('service.get_enabled') else False
self.post_srv_disable = False
if not self.pre_srv_enabled:
self.run_function('service.enable', name=self.service_name)
self.post_srv_disable = True
if os_family != 'Windows' and salt.utils.path.which(cmd_name) is None:
self.skipTest('{0} is not installed'.format(cmd_name))
def tearDown(self):
if self.post_srv_disable:
self.run_function('service.disable', name=self.service_name)
def check_service_status(self, exp_return):
'''
helper method to check status of service
'''
check_status = self.run_function('service.status',
name=self.service_name)
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')
def test_service_running(self):
'''
test service.running state module
'''
2018-05-30 19:35:05 +00:00
if self.run_function('service.status', name=self.service_name):
stop_service = self.run_function('service.stop', name=self.service_name)
self.assertTrue(stop_service)
self.check_service_status(self.stopped)
if salt.utils.platform.is_darwin():
# make sure the service is enabled on macosx
enable = self.run_function('service.enable', name=self.service_name)
start_service = self.run_state('service.running',
name=self.service_name)
self.assertTrue(start_service)
self.check_service_status(self.running)
def test_service_dead(self):
'''
test service.dead state module
'''
start_service = self.run_state('service.running',
name=self.service_name)
self.assertSaltTrueReturn(start_service)
self.check_service_status(self.running)
ret = self.run_state('service.dead', name=self.service_name)
self.assertSaltTrueReturn(ret)
self.check_service_status(self.stopped)
def test_service_dead_init_delay(self):
'''
test service.dead state module with init_delay arg
'''
start_service = self.run_state('service.running',
name=self.service_name)
self.assertSaltTrueReturn(start_service)
self.check_service_status(self.running)
ret = self.run_state('service.dead', name=self.service_name,
init_delay=INIT_DELAY)
self.assertSaltTrueReturn(ret)
self.check_service_status(self.stopped)