salt/tests/unit/modules/test_deb_apache.py

358 lines
14 KiB
Python
Raw Normal View History

2015-01-16 09:30:52 +00:00
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
'''
2015-01-21 02:14:30 +00:00
# Import Python libs
2018-01-21 20:08:08 +00:00
from __future__ import absolute_import, print_function, unicode_literals
2015-01-21 02:14:30 +00:00
2015-01-16 09:30:52 +00:00
# Import Salt Testing Libs
2017-03-22 16:42:17 +00:00
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import (
2015-01-16 09:30:52 +00:00
MagicMock,
patch,
NO_MOCK,
NO_MOCK_REASON
)
# Import Salt Libs
import salt.modules.deb_apache as deb_apache
2018-01-22 14:59:17 +00:00
from salt.ext import six
2015-01-16 09:30:52 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2017-03-22 16:42:17 +00:00
class DebApacheTestCase(TestCase, LoaderModuleMockMixin):
2015-01-16 09:30:52 +00:00
'''
Test cases for salt.modules.deb_apache
'''
2017-03-22 16:42:17 +00:00
def setup_loader_modules(self):
return {deb_apache: {}}
2015-01-16 09:30:52 +00:00
# 'check_site_enabled' function tests: 3
def test_check_site_enabled(self):
'''
Test if the specific Site symlink is enabled.
'''
2017-04-10 13:00:57 +00:00
with patch('os.path.islink', MagicMock(return_value=True)):
self.assertTrue(deb_apache.check_site_enabled('saltstack.com'))
2015-01-16 09:30:52 +00:00
def test_check_site_enabled_default(self):
'''
Test if the specific Site symlink is enabled.
'''
2017-04-10 13:00:57 +00:00
with patch('os.path.islink', MagicMock(side_effect=[False, True])):
self.assertTrue(deb_apache.check_site_enabled('default'))
2015-01-16 09:30:52 +00:00
def test_check_site_enabled_false(self):
'''
Test if the specific Site symlink is enabled.
'''
2017-04-10 13:00:57 +00:00
with patch('os.path.islink', MagicMock(return_value=False)):
self.assertFalse(deb_apache.check_site_enabled('saltstack.com'))
2015-01-16 09:30:52 +00:00
# 'a2ensite' function tests: 4
def test_a2ensite_notfound(self):
'''
Test if it runs a2ensite for the given site.
'''
mock = MagicMock(return_value=1)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2ensite('saltstack.com'),
{'Name': 'Apache2 Enable Site',
'Site': 'saltstack.com',
'Status': 'Site saltstack.com Not found'})
def test_a2ensite_enabled(self):
'''
Test if it runs a2ensite for the given site.
'''
mock = MagicMock(return_value=0)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2ensite('saltstack.com'),
{'Name': 'Apache2 Enable Site',
'Site': 'saltstack.com',
'Status': 'Site saltstack.com enabled'})
def test_a2ensite(self):
'''
Test if it runs a2ensite for the given site.
'''
mock = MagicMock(return_value=2)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2ensite('saltstack.com'),
{'Name': 'Apache2 Enable Site',
'Site': 'saltstack.com',
'Status': 2})
def test_a2ensite_exception(self):
'''
Test if it runs a2ensite for the given site.
'''
mock = MagicMock(side_effect=Exception('error'))
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
2018-01-21 20:08:08 +00:00
self.assertEqual(six.text_type(deb_apache.a2ensite('saltstack.com')),
2015-01-16 12:09:32 +00:00
'error')
2015-01-16 09:30:52 +00:00
# 'a2dissite' function tests: 4
def test_a2dissite_notfound(self):
'''
Test if it runs a2dissite for the given site.
'''
mock = MagicMock(return_value=256)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2dissite('saltstack.com'),
{'Name': 'Apache2 Disable Site',
'Site': 'saltstack.com',
'Status': 'Site saltstack.com Not found'})
def test_a2dissite_disabled(self):
'''
Test if it runs a2dissite for the given site.
'''
mock = MagicMock(return_value=0)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2dissite('saltstack.com'),
{'Name': 'Apache2 Disable Site',
'Site': 'saltstack.com',
'Status': 'Site saltstack.com disabled'})
def test_a2dissite(self):
'''
Test if it runs a2dissite for the given site.
'''
mock = MagicMock(return_value=2)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2dissite('saltstack.com'),
{'Name': 'Apache2 Disable Site',
'Site': 'saltstack.com',
'Status': 2})
def test_a2dissite_exception(self):
'''
Test if it runs a2dissite for the given site.
'''
mock = MagicMock(side_effect=Exception('error'))
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
2018-01-21 20:08:08 +00:00
self.assertEqual(six.text_type(deb_apache.a2dissite('saltstack.com')),
2015-01-16 12:09:32 +00:00
'error')
2015-01-16 09:30:52 +00:00
# 'check_mod_enabled' function tests: 2
2015-01-16 09:30:52 +00:00
def test_check_mod_enabled(self):
'''
Test if the specific mod symlink is enabled.
'''
2017-04-10 13:00:57 +00:00
with patch('os.path.islink', MagicMock(return_value=True)):
self.assertTrue(deb_apache.check_mod_enabled('status.conf'))
2015-01-16 09:30:52 +00:00
def test_check_mod_enabled_false(self):
'''
Test if the specific mod symlink is enabled.
'''
2017-04-10 13:00:57 +00:00
with patch('os.path.islink', MagicMock(return_value=False)):
self.assertFalse(deb_apache.check_mod_enabled('status.conf'))
2015-01-16 09:30:52 +00:00
# 'a2enmod' function tests: 4
def test_a2enmod_notfound(self):
'''
Test if it runs a2enmod for the given module.
2015-01-16 09:30:52 +00:00
'''
mock = MagicMock(return_value=1)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2enmod('vhost_alias'),
{'Name': 'Apache2 Enable Mod',
'Mod': 'vhost_alias',
'Status': 'Mod vhost_alias Not found'})
def test_a2enmod_enabled(self):
'''
Test if it runs a2enmod for the given module.
2015-01-16 09:30:52 +00:00
'''
mock = MagicMock(return_value=0)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2enmod('vhost_alias'),
{'Name': 'Apache2 Enable Mod',
'Mod': 'vhost_alias',
'Status': 'Mod vhost_alias enabled'})
def test_a2enmod(self):
'''
Test if it runs a2enmod for the given module.
2015-01-16 09:30:52 +00:00
'''
mock = MagicMock(return_value=2)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2enmod('vhost_alias'),
{'Name': 'Apache2 Enable Mod',
'Mod': 'vhost_alias',
'Status': 2})
def test_a2enmod_exception(self):
'''
Test if it runs a2enmod for the given module.
2015-01-16 09:30:52 +00:00
'''
mock = MagicMock(side_effect=Exception('error'))
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
2018-01-21 20:08:08 +00:00
self.assertEqual(six.text_type(deb_apache.a2enmod('vhost_alias')),
2015-01-16 12:09:32 +00:00
'error')
2015-01-16 09:30:52 +00:00
# 'a2dismod' function tests: 4
def test_a2dismod_notfound(self):
'''
Test if it runs a2dismod for the given module.
2015-01-16 09:30:52 +00:00
'''
mock = MagicMock(return_value=256)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2dismod('vhost_alias'),
{'Name': 'Apache2 Disable Mod',
'Mod': 'vhost_alias',
'Status': 'Mod vhost_alias Not found'})
def test_a2dismod_disabled(self):
'''
Test if it runs a2dismod for the given module.
2015-01-16 09:30:52 +00:00
'''
mock = MagicMock(return_value=0)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2dismod('vhost_alias'),
{'Name': 'Apache2 Disable Mod',
'Mod': 'vhost_alias',
'Status': 'Mod vhost_alias disabled'})
def test_a2dismod(self):
'''
Test if it runs a2dismod for the given module.
2015-01-16 09:30:52 +00:00
'''
mock = MagicMock(return_value=2)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2dismod('vhost_alias'),
{'Name': 'Apache2 Disable Mod',
'Mod': 'vhost_alias',
'Status': 2})
def test_a2dismod_exception(self):
'''
Test if it runs a2dismod for the given module.
2015-01-16 09:30:52 +00:00
'''
mock = MagicMock(side_effect=Exception('error'))
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
2018-01-21 20:08:08 +00:00
self.assertEqual(six.text_type(deb_apache.a2dismod('vhost_alias')),
2015-01-16 12:09:32 +00:00
'error')
2015-01-16 09:30:52 +00:00
# 'check_conf_enabled' function tests: 2
def test_check_conf_enabled(self):
'''
Test if the specific conf symlink is enabled.
'''
2017-04-10 13:00:57 +00:00
with patch('os.path.islink', MagicMock(return_value=True)):
self.assertTrue(deb_apache.check_conf_enabled('security.conf'))
def test_check_conf_enabled_false(self):
'''
Test if the specific conf symlink is enabled.
'''
2017-04-10 13:00:57 +00:00
with patch('os.path.islink', MagicMock(return_value=False)):
self.assertFalse(deb_apache.check_conf_enabled('security.conf'))
# 'a2enconf' function tests: 4
def test_a2enconf_notfound(self):
'''
Test if it runs a2enconf for the given conf.
'''
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
with patch('salt.utils.path.which', MagicMock(return_value='a2enconf')):
2017-04-10 13:00:57 +00:00
mock = MagicMock(return_value=1)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2enconf('security'),
{'Name': 'Apache2 Enable Conf',
'Conf': 'security',
'Status': 'Conf security Not found'})
def test_a2enconf_enabled(self):
'''
Test if it runs a2enconf for the given conf.
'''
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
with patch('salt.utils.path.which', MagicMock(return_value='a2enconf')):
2017-04-10 13:00:57 +00:00
mock = MagicMock(return_value=0)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2enconf('security'),
{'Name': 'Apache2 Enable Conf',
'Conf': 'security',
'Status': 'Conf security enabled'})
def test_a2enconf(self):
'''
Test if it runs a2enconf for the given conf.
'''
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
with patch('salt.utils.path.which', MagicMock(return_value='a2enconf')):
2017-04-10 13:00:57 +00:00
mock = MagicMock(return_value=2)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2enconf('security'),
{'Name': 'Apache2 Enable Conf',
'Conf': 'security',
'Status': 2})
def test_a2enconf_exception(self):
'''
Test if it runs a2enconf for the given conf.
'''
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
with patch('salt.utils.path.which', MagicMock(return_value='a2enconf')):
2017-04-10 13:00:57 +00:00
mock = MagicMock(side_effect=Exception('error'))
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
2018-01-21 20:08:08 +00:00
self.assertEqual(six.text_type(deb_apache.a2enconf('security')),
2017-04-10 13:00:57 +00:00
'error')
# 'a2disconf' function tests: 4
def test_a2disconf_notfound(self):
'''
Test if it runs a2disconf for the given conf.
'''
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
with patch('salt.utils.path.which', MagicMock(return_value='a2disconf')):
2017-04-10 13:00:57 +00:00
mock = MagicMock(return_value=256)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2disconf('security'),
{'Name': 'Apache2 Disable Conf',
'Conf': 'security',
'Status': 'Conf security Not found'})
def test_a2disconf_disabled(self):
'''
Test if it runs a2disconf for the given conf.
'''
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
with patch('salt.utils.path.which', MagicMock(return_value='a2disconf')):
2017-04-10 13:00:57 +00:00
mock = MagicMock(return_value=0)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2disconf('security'),
{'Name': 'Apache2 Disable Conf',
'Conf': 'security',
'Status': 'Conf security disabled'})
def test_a2disconf(self):
'''
Test if it runs a2disconf for the given conf.
'''
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
with patch('salt.utils.path.which', MagicMock(return_value='a2disconf')):
2017-04-10 13:00:57 +00:00
mock = MagicMock(return_value=2)
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
self.assertEqual(deb_apache.a2disconf('security'),
{'Name': 'Apache2 Disable Conf',
'Conf': 'security',
'Status': 2})
def test_a2disconf_exception(self):
'''
Test if it runs a2disconf for the given conf.
'''
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
with patch('salt.utils.path.which', MagicMock(return_value='a2disconf')):
2017-04-10 13:00:57 +00:00
mock = MagicMock(side_effect=Exception('error'))
with patch.dict(deb_apache.__salt__, {'cmd.retcode': mock}):
2018-01-21 20:08:08 +00:00
self.assertEqual(six.text_type(deb_apache.a2disconf('security')),
2017-04-10 13:00:57 +00:00
'error')