mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Allow passing different loader_module_globals per loader_module
This commit is contained in:
parent
93ad2cc428
commit
e7768705f9
21 changed files with 176 additions and 151 deletions
|
@ -427,13 +427,11 @@ class _FixLoaderModuleMockMixinMroOrder(type):
|
|||
|
||||
@functools.wraps(setup_func)
|
||||
def wrapper(self):
|
||||
loader_modules = getattr(self, 'loader_module', None)
|
||||
if loader_modules is None:
|
||||
return
|
||||
|
||||
if NO_MOCK:
|
||||
self.skipTest(NO_MOCK_REASON)
|
||||
|
||||
loader_modules = self.loader_module
|
||||
|
||||
if not isinstance(loader_modules, (list, tuple)):
|
||||
loader_modules = [loader_modules]
|
||||
|
||||
|
@ -446,24 +444,42 @@ class _FixLoaderModuleMockMixinMroOrder(type):
|
|||
else:
|
||||
loader_module_globals = copy.deepcopy(loader_module_globals)
|
||||
|
||||
minion_funcs = None
|
||||
if '__salt__' in loader_module_globals and loader_module_globals['__salt__'] == 'autoload':
|
||||
if '__opts__' not in loader_module_globals:
|
||||
if isinstance(loader_module_globals, (list, tuple)):
|
||||
if len(loader_modules) != len(loader_module_globals):
|
||||
raise RuntimeError(
|
||||
'You must provide __opts__ in the loader_module_globals to auto load the minion functions'
|
||||
'Multiple loader_modules or multiple loader_module_globals '
|
||||
'were passed, however, the number of items on '
|
||||
'each must be the same. {} != {}'.format(len(loader_modules),
|
||||
len(loader_module_globals))
|
||||
)
|
||||
import salt.loader
|
||||
ctx = {}
|
||||
if '__utils__' not in loader_module_globals:
|
||||
utils = salt.loader.utils(loader_module_globals['__opts__'],
|
||||
context=loader_module_globals.get('__context__') or ctx)
|
||||
loader_module_globals['__utils__'] = utils
|
||||
minion_funcs = salt.loader.minion_mods(
|
||||
loader_module_globals['__opts__'],
|
||||
context=loader_module_globals.get('__context__') or ctx,
|
||||
utils=loader_module_globals.get('__utils__'),
|
||||
)
|
||||
loader_module_globals['__salt__'] = minion_funcs
|
||||
if not isinstance(loader_module_globals, (list, tuple)):
|
||||
loader_module_globals = [loader_module_globals]
|
||||
while len(loader_module_globals) < len(loader_modules):
|
||||
loader_module_globals.append(loader_module_globals[0])
|
||||
|
||||
minion_funcs = []
|
||||
for loader_module_globals_instance in loader_module_globals:
|
||||
if '__salt__' in loader_module_globals_instance and \
|
||||
loader_module_globals_instance['__salt__'] == 'autoload':
|
||||
if '__opts__' not in loader_module_globals_instance:
|
||||
raise RuntimeError(
|
||||
'You must provide __opts__ in the loader_module_globals to auto load the minion functions'
|
||||
)
|
||||
import salt.loader
|
||||
ctx = {}
|
||||
if '__utils__' not in loader_module_globals_instance:
|
||||
utils = salt.loader.utils(loader_module_globals_instance['__opts__'],
|
||||
context=loader_module_globals_instance.get('__context__') or ctx)
|
||||
loader_module_globals_instance['__utils__'] = utils
|
||||
minion_funcs.append((
|
||||
loader_module_globals_instance,
|
||||
salt.loader.minion_mods(
|
||||
loader_module_globals_instance['__opts__'],
|
||||
context=loader_module_globals_instance.get('__context__') or ctx,
|
||||
utils=loader_module_globals_instance.get('__utils__'),
|
||||
)
|
||||
))
|
||||
loader_module_globals_instance['__salt__'] = minion_funcs[-1][1]
|
||||
|
||||
salt_dunders = (
|
||||
'__opts__', '__salt__', '__runner__', '__context__', '__utils__',
|
||||
|
@ -474,38 +490,40 @@ class _FixLoaderModuleMockMixinMroOrder(type):
|
|||
# '__proxy__'
|
||||
)
|
||||
for dunder_name in salt_dunders:
|
||||
if dunder_name not in loader_module_globals:
|
||||
if dunder_name in loader_module_blacklisted_dunders:
|
||||
continue
|
||||
loader_module_globals[dunder_name] = {}
|
||||
for loader_module_globals_instance in loader_module_globals:
|
||||
if dunder_name not in loader_module_globals_instance:
|
||||
if dunder_name in loader_module_blacklisted_dunders:
|
||||
continue
|
||||
loader_module_globals_instance[dunder_name] = {}
|
||||
|
||||
for loader_module in loader_modules:
|
||||
for key in loader_module_globals:
|
||||
for idx, loader_module in enumerate(loader_modules):
|
||||
for key in loader_module_globals[idx]:
|
||||
if not hasattr(loader_module, key):
|
||||
if key in salt_dunders:
|
||||
setattr(loader_module, key, {})
|
||||
else:
|
||||
setattr(loader_module, key, None)
|
||||
|
||||
if loader_module_globals:
|
||||
patcher = patch.multiple(loader_module, **loader_module_globals)
|
||||
if loader_module_globals[idx]:
|
||||
patcher = patch.multiple(loader_module, **loader_module_globals[idx])
|
||||
patcher.start()
|
||||
|
||||
def cleanup(patcher, loader_module_globals):
|
||||
patcher.stop()
|
||||
del loader_module_globals
|
||||
|
||||
self.addCleanup(cleanup, patcher, loader_module_globals)
|
||||
if minion_funcs is not None:
|
||||
# Since we autoloaded the minion_funcs, let's namespace the functions with the globals
|
||||
# used to patch above
|
||||
import salt.utils
|
||||
for func in minion_funcs:
|
||||
minion_funcs[func] = salt.utils.namespaced_function(
|
||||
minion_funcs[func],
|
||||
loader_module_globals,
|
||||
preserve_context=True
|
||||
)
|
||||
self.addCleanup(cleanup, patcher, loader_module_globals[idx])
|
||||
if minion_funcs:
|
||||
for loader_module_globals_instance, minion_funcs_instance in minion_funcs:
|
||||
# Since we autoloaded the minion_funcs, let's namespace the functions with the globals
|
||||
# used to patch above
|
||||
import salt.utils
|
||||
for func in minion_funcs_instance:
|
||||
minion_funcs_instance[func] = salt.utils.namespaced_function(
|
||||
minion_funcs_instance[func],
|
||||
loader_module_globals_instance,
|
||||
preserve_context=True
|
||||
)
|
||||
|
||||
return setup_func(self)
|
||||
return wrapper
|
||||
|
|
|
@ -7,6 +7,7 @@ Unit tests for the docker module
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import skipIf, TestCase
|
||||
from tests.support.mock import (
|
||||
MagicMock,
|
||||
|
@ -21,10 +22,6 @@ from salt.ext.six.moves import range
|
|||
from salt.exceptions import CommandExecutionError
|
||||
import salt.modules.dockermod as docker_mod
|
||||
|
||||
docker_mod.__context__ = {'docker.docker_version': ''}
|
||||
docker_mod.__salt__ = {}
|
||||
docker_mod.__opts__ = {}
|
||||
|
||||
|
||||
def _docker_py_version():
|
||||
try:
|
||||
|
@ -37,10 +34,16 @@ def _docker_py_version():
|
|||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
@skipIf(docker_mod.HAS_DOCKER_PY is False, 'docker-py must be installed to run these tests. Skipping.')
|
||||
class DockerTestCase(TestCase):
|
||||
class DockerTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Validate docker module
|
||||
'''
|
||||
loader_module = docker_mod
|
||||
|
||||
def loader_module_globals(self):
|
||||
return {
|
||||
'__context__': {'docker.docker_version': ''}
|
||||
}
|
||||
|
||||
def test_ps_with_host_true(self):
|
||||
'''
|
||||
|
|
|
@ -4,18 +4,19 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import skipIf, TestCase
|
||||
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, Mock, patch
|
||||
|
||||
# Import salt libs
|
||||
import salt.modules.uwsgi as uwsgi
|
||||
|
||||
uwsgi.__salt__ = {}
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
@patch('salt.utils.which', Mock(return_value='/usr/bin/uwsgi'))
|
||||
class UwsgiTestCase(TestCase):
|
||||
class UwsgiTestCase(TestCase, LoaderModuleMockMixin):
|
||||
|
||||
loader_module = uwsgi
|
||||
|
||||
def test_uwsgi_stats(self):
|
||||
socket = "127.0.0.1:5050"
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import skipIf, TestCase
|
||||
from tests.support.mock import (
|
||||
NO_MOCK,
|
||||
|
@ -16,15 +17,14 @@ from tests.support.mock import (
|
|||
# Import Salt Libs
|
||||
import salt.modules.varnish as varnish
|
||||
|
||||
# Globals
|
||||
varnish.__salt__ = {}
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class VarnishTestCase(TestCase):
|
||||
class VarnishTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Test cases for salt.modules.varnish
|
||||
'''
|
||||
loader_module = varnish
|
||||
|
||||
def test_version(self):
|
||||
'''
|
||||
Test to return server version from varnishd -V
|
||||
|
|
|
@ -7,16 +7,17 @@ from __future__ import absolute_import
|
|||
import salt.modules.win_certutil as certutil
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import TestCase
|
||||
from tests.support.mock import (
|
||||
MagicMock,
|
||||
patch
|
||||
)
|
||||
|
||||
certutil.__salt__ = {}
|
||||
|
||||
class CertUtilTestCase(TestCase, LoaderModuleMockMixin):
|
||||
|
||||
class CertUtilTestCase(TestCase):
|
||||
loader_module = certutil
|
||||
|
||||
def test_get_serial(self):
|
||||
'''
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
from tests.support.mock import (
|
||||
NO_MOCK,
|
||||
|
@ -47,14 +48,16 @@ class MockCtypes(object):
|
|||
def __init__(self):
|
||||
self.windll = MockWindll()
|
||||
|
||||
win_disk.ctypes = MockCtypes()
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class WinDiskTestCase(TestCase):
|
||||
class WinDiskTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Test cases for salt.modules.win_disk
|
||||
'''
|
||||
loader_module = win_disk
|
||||
|
||||
def loader_module_globals(self):
|
||||
return {'ctypes': MockCtypes()}
|
||||
# 'usage' function tests: 1
|
||||
|
||||
def test_usage(self):
|
||||
|
|
|
@ -7,17 +7,17 @@ from __future__ import absolute_import
|
|||
import salt.modules.win_dism as dism
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import TestCase
|
||||
from tests.support.mock import (
|
||||
MagicMock,
|
||||
patch
|
||||
)
|
||||
|
||||
dism.__salt__ = {}
|
||||
dism.__grains__ = {}
|
||||
|
||||
class WinDismTestCase(TestCase, LoaderModuleMockMixin):
|
||||
|
||||
class WinDismTestCase(TestCase):
|
||||
loader_module = dism
|
||||
|
||||
def test_add_capability(self):
|
||||
'''
|
||||
|
|
|
@ -4,27 +4,27 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Libs
|
||||
import salt.modules.win_license as license
|
||||
import salt.modules.win_license as win_license
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import TestCase
|
||||
from tests.support.mock import (
|
||||
MagicMock,
|
||||
patch
|
||||
)
|
||||
|
||||
license.__salt__ = {}
|
||||
|
||||
|
||||
class LicenseTestCase(TestCase):
|
||||
class LicenseTestCase(TestCase, LoaderModuleMockMixin):
|
||||
loader_module = win_license
|
||||
|
||||
def test_installed(self):
|
||||
'''
|
||||
Test to see if the given license key is installed
|
||||
'''
|
||||
mock = MagicMock(return_value='Partial Product Key: ABCDE')
|
||||
with patch.dict(license.__salt__, {'cmd.run': mock}):
|
||||
out = license.installed('AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE')
|
||||
with patch.dict(win_license.__salt__, {'cmd.run': mock}):
|
||||
out = win_license.installed('AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE')
|
||||
mock.assert_called_once_with(r'cscript C:\Windows\System32\slmgr.vbs /dli')
|
||||
self.assertTrue(out)
|
||||
|
||||
|
@ -33,8 +33,8 @@ class LicenseTestCase(TestCase):
|
|||
Test to see if the given license key is installed when the key is different
|
||||
'''
|
||||
mock = MagicMock(return_value='Partial Product Key: 12345')
|
||||
with patch.dict(license.__salt__, {'cmd.run': mock}):
|
||||
out = license.installed('AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE')
|
||||
with patch.dict(win_license.__salt__, {'cmd.run': mock}):
|
||||
out = win_license.installed('AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE')
|
||||
mock.assert_called_once_with(r'cscript C:\Windows\System32\slmgr.vbs /dli')
|
||||
self.assertFalse(out)
|
||||
|
||||
|
@ -43,8 +43,8 @@ class LicenseTestCase(TestCase):
|
|||
Test installing the given product key
|
||||
'''
|
||||
mock = MagicMock()
|
||||
with patch.dict(license.__salt__, {'cmd.run': mock}):
|
||||
license.install('AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE')
|
||||
with patch.dict(win_license.__salt__, {'cmd.run': mock}):
|
||||
win_license.install('AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE')
|
||||
mock.assert_called_once_with(r'cscript C:\Windows\System32\slmgr.vbs /ipk '
|
||||
'AAAAA-AAAAA-AAAAA-AAAA-AAAAA-ABCDE')
|
||||
|
||||
|
@ -53,8 +53,8 @@ class LicenseTestCase(TestCase):
|
|||
Test uninstalling the given product key
|
||||
'''
|
||||
mock = MagicMock()
|
||||
with patch.dict(license.__salt__, {'cmd.run': mock}):
|
||||
license.uninstall()
|
||||
with patch.dict(win_license.__salt__, {'cmd.run': mock}):
|
||||
win_license.uninstall()
|
||||
mock.assert_called_once_with(r'cscript C:\Windows\System32\slmgr.vbs /upk')
|
||||
|
||||
def test_activate(self):
|
||||
|
@ -62,8 +62,8 @@ class LicenseTestCase(TestCase):
|
|||
Test activating the current product key
|
||||
'''
|
||||
mock = MagicMock()
|
||||
with patch.dict(license.__salt__, {'cmd.run': mock}):
|
||||
license.activate()
|
||||
with patch.dict(win_license.__salt__, {'cmd.run': mock}):
|
||||
win_license.activate()
|
||||
mock.assert_called_once_with(r'cscript C:\Windows\System32\slmgr.vbs /ato')
|
||||
|
||||
def test_licensed(self):
|
||||
|
@ -71,8 +71,8 @@ class LicenseTestCase(TestCase):
|
|||
Test checking if the minion is licensed
|
||||
'''
|
||||
mock = MagicMock(return_value='License Status: Licensed')
|
||||
with patch.dict(license.__salt__, {'cmd.run': mock}):
|
||||
license.licensed()
|
||||
with patch.dict(win_license.__salt__, {'cmd.run': mock}):
|
||||
win_license.licensed()
|
||||
mock.assert_called_once_with(r'cscript C:\Windows\System32\slmgr.vbs /dli')
|
||||
|
||||
def test_info(self):
|
||||
|
@ -88,7 +88,7 @@ class LicenseTestCase(TestCase):
|
|||
|
||||
mock = MagicMock(return_value='Name: Win7\r\nDescription: Prof\r\nPartial Product Key: 12345\r\n'
|
||||
'License Status: Licensed')
|
||||
with patch.dict(license.__salt__, {'cmd.run': mock}):
|
||||
out = license.info()
|
||||
with patch.dict(win_license.__salt__, {'cmd.run': mock}):
|
||||
out = win_license.info()
|
||||
mock.assert_called_once_with(r'cscript C:\Windows\System32\slmgr.vbs /dli')
|
||||
self.assertEqual(out, expected)
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
from tests.support.mock import (
|
||||
mock_open,
|
||||
|
@ -19,9 +20,6 @@ from tests.support.mock import (
|
|||
# Import Salt Libs
|
||||
import salt.modules.xapi as xapi
|
||||
|
||||
xapi.__grains__ = {}
|
||||
xapi.__salt__ = {}
|
||||
|
||||
|
||||
class Mockxapi(object):
|
||||
'''
|
||||
|
@ -67,10 +65,12 @@ class Mockxapi(object):
|
|||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class XapiTestCase(TestCase):
|
||||
class XapiTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Test cases for salt.modules.xapi
|
||||
'''
|
||||
loader_module = xapi
|
||||
|
||||
def test_list_domains(self):
|
||||
'''
|
||||
Test to return a list of domain names on the minion
|
||||
|
|
|
@ -10,9 +10,8 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import skipIf, TestCase
|
||||
|
||||
# Import Mock libraries
|
||||
from tests.support.mock import (
|
||||
MagicMock,
|
||||
patch,
|
||||
|
@ -24,16 +23,14 @@ from tests.support.mock import (
|
|||
import salt.modules.zfs as zfs
|
||||
from salt.utils.odict import OrderedDict
|
||||
|
||||
# Globals
|
||||
zfs.__salt__ = {}
|
||||
|
||||
|
||||
# Skip this test case if we don't have access to mock!
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class ZfsTestCase(TestCase):
|
||||
class ZfsTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
This class contains a set of functions that test salt.modules.zfs module
|
||||
'''
|
||||
loader_module = zfs
|
||||
|
||||
@patch('salt.modules.zfs._check_zfs', MagicMock(return_value='/sbin/zfs'))
|
||||
def test_exists_success(self):
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
from tests.support.mock import (
|
||||
MagicMock,
|
||||
|
@ -18,14 +19,13 @@ from tests.support.mock import (
|
|||
# Import Salt Libs
|
||||
import salt.modules.znc as znc
|
||||
|
||||
znc.__salt__ = {}
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class ZncTestCase(TestCase):
|
||||
class ZncTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
TestCase for salt.modules.znc
|
||||
'''
|
||||
loader_module = znc
|
||||
# 'buildmod' function tests: 1
|
||||
|
||||
@patch('os.path.exists', MagicMock(return_value=False))
|
||||
|
|
|
@ -10,9 +10,8 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import skipIf, TestCase
|
||||
|
||||
# Import Mock libraries
|
||||
from tests.support.mock import (
|
||||
MagicMock,
|
||||
patch,
|
||||
|
@ -26,16 +25,14 @@ import salt.modules.zpool as zpool
|
|||
# Import Salt Utils
|
||||
from salt.utils.odict import OrderedDict
|
||||
|
||||
# Globals
|
||||
zpool.__salt__ = {}
|
||||
|
||||
|
||||
# Skip this test case if we don't have access to mock!
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class ZpoolTestCase(TestCase):
|
||||
class ZpoolTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
This class contains a set of functions that test salt.modules.zpool module
|
||||
'''
|
||||
loader_module = zpool
|
||||
|
||||
@patch('salt.modules.zpool._check_zpool', MagicMock(return_value='/sbin/zpool'))
|
||||
def test_exists_success(self):
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import skipIf, TestCase
|
||||
from tests.support.mock import (
|
||||
NO_MOCK,
|
||||
|
@ -16,14 +17,14 @@ from tests.support.mock import (
|
|||
import salt.renderers.gpg as gpg
|
||||
from salt.exceptions import SaltRenderError
|
||||
|
||||
gpg.__salt__ = {}
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class GPGTestCase(TestCase):
|
||||
class GPGTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
unit test GPG renderer
|
||||
'''
|
||||
loader_module = gpg
|
||||
|
||||
def test__get_gpg_exec(self):
|
||||
'''
|
||||
test _get_gpg_exec
|
||||
|
|
|
@ -4,16 +4,17 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import TestCase
|
||||
|
||||
# Import Salt libs
|
||||
import salt.renderers.yaml as yaml
|
||||
|
||||
yaml.__salt__ = {}
|
||||
yaml.__opts__ = {}
|
||||
|
||||
class YAMLRendererTestCase(TestCase, LoaderModuleMockMixin):
|
||||
|
||||
loader_module = yaml
|
||||
|
||||
class YAMLRendererTestCase(TestCase):
|
||||
def test_yaml_render_string(self):
|
||||
data = "string"
|
||||
result = yaml.render(data)
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import skipIf, TestCase
|
||||
|
||||
# Import Salt libs
|
||||
|
@ -39,7 +40,10 @@ class RendererMixin(object):
|
|||
_state.opts['renderer_whitelist'])
|
||||
|
||||
|
||||
class RendererTests(TestCase, RendererMixin):
|
||||
class RendererTests(TestCase, RendererMixin, LoaderModuleMockMixin):
|
||||
|
||||
loader_module = yamlex
|
||||
|
||||
@skipIf(not yamlex.available, SKIP_MESSAGE % 'yamlex')
|
||||
def test_basic(self):
|
||||
sls_obj = self.render(basic_template)
|
||||
|
|
|
@ -11,15 +11,13 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
|
||||
|
||||
# Import salt libs
|
||||
import salt.returners.smtp_return as smtp
|
||||
|
||||
smtp.__salt__ = {}
|
||||
smtp.__opts__ = {}
|
||||
|
||||
try:
|
||||
import gnupg # pylint: disable=unused-import
|
||||
HAS_GNUPG = True
|
||||
|
@ -28,10 +26,12 @@ except ImportError:
|
|||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class SMTPReturnerTestCase(TestCase):
|
||||
class SMTPReturnerTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Test SMTP returner
|
||||
'''
|
||||
loader_module = smtp
|
||||
|
||||
def _test_returner(self, mocked_smtplib, *args): # pylint: disable=unused-argument
|
||||
'''
|
||||
Test to see if the SMTP returner sends a message
|
||||
|
@ -57,29 +57,27 @@ class SMTPReturnerTestCase(TestCase):
|
|||
smtp.returner(ret)
|
||||
self.assertTrue(mocked_smtplib.return_value.sendmail.called)
|
||||
|
||||
if HAS_GNUPG:
|
||||
@patch('salt.returners.smtp_return.gnupg')
|
||||
@patch('salt.returners.smtp_return.smtplib.SMTP')
|
||||
def test_returner(self, mocked_smtplib, *args):
|
||||
with patch.dict(smtp.__opts__, {'extension_modules': '',
|
||||
'renderer': 'jinja|yaml',
|
||||
'renderer_blacklist': [],
|
||||
'renderer_whitelist': [],
|
||||
'file_roots': [],
|
||||
'pillar_roots': [],
|
||||
'cachedir': '/'}):
|
||||
self._test_returner(mocked_smtplib, *args)
|
||||
if HAS_GNUPG:
|
||||
@patch('salt.returners.smtp_return.gnupg')
|
||||
@patch('salt.returners.smtp_return.smtplib.SMTP')
|
||||
def test_returner(self, mocked_smtplib, *args):
|
||||
with patch.dict(smtp.__opts__, {'extension_modules': '',
|
||||
'renderer': 'jinja|yaml',
|
||||
'renderer_blacklist': [],
|
||||
'renderer_whitelist': [],
|
||||
'file_roots': [],
|
||||
'pillar_roots': [],
|
||||
'cachedir': '/'}):
|
||||
self._test_returner(mocked_smtplib, *args)
|
||||
|
||||
else:
|
||||
@patch('salt.returners.smtp_return.smtplib.SMTP')
|
||||
def test_returner(self, mocked_smtplib, *args):
|
||||
with patch.dict(smtp.__opts__, {'extension_modules': '',
|
||||
'renderer': 'jinja|yaml',
|
||||
'renderer_blacklist': [],
|
||||
'renderer_whitelist': [],
|
||||
'file_roots': [],
|
||||
'pillar_roots': [],
|
||||
'cachedir': '/'}):
|
||||
self._test_returner(mocked_smtplib, *args)
|
||||
|
||||
SMTPReturnerTestCase.test_returner = test_returner
|
||||
else:
|
||||
@patch('salt.returners.smtp_return.smtplib.SMTP')
|
||||
def test_returner(self, mocked_smtplib, *args):
|
||||
with patch.dict(smtp.__opts__, {'extension_modules': '',
|
||||
'renderer': 'jinja|yaml',
|
||||
'renderer_blacklist': [],
|
||||
'renderer_whitelist': [],
|
||||
'file_roots': [],
|
||||
'pillar_roots': [],
|
||||
'cachedir': '/'}):
|
||||
self._test_returner(mocked_smtplib, *args)
|
||||
|
|
|
@ -8,6 +8,7 @@ from __future__ import absolute_import
|
|||
import logging
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import skipIf, TestCase
|
||||
from tests.support.mock import (
|
||||
MagicMock,
|
||||
|
@ -22,14 +23,14 @@ import salt.runners.vault as vault
|
|||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
vault.__opts__ = {}
|
||||
|
||||
|
||||
class VaultTest(TestCase):
|
||||
class VaultTest(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Tests for the runner module of the Vault integration
|
||||
'''
|
||||
|
||||
loader_module = vault
|
||||
|
||||
def setUp(self):
|
||||
self.grains = {
|
||||
'id': 'test-minion',
|
||||
|
@ -52,6 +53,9 @@ class VaultTest(TestCase):
|
|||
]
|
||||
}
|
||||
|
||||
def tearDown(self):
|
||||
del self.grains
|
||||
|
||||
def test_pattern_list_expander(self):
|
||||
'''
|
||||
Ensure _expand_pattern_lists works as intended:
|
||||
|
|
|
@ -7,6 +7,7 @@ unit tests for the alias state
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import skipIf, TestCase
|
||||
from tests.support.mock import (
|
||||
NO_MOCK,
|
||||
|
@ -18,15 +19,14 @@ from tests.support.mock import (
|
|||
# Import Salt Libs
|
||||
import salt.states.alias as alias
|
||||
|
||||
alias.__opts__ = {}
|
||||
alias.__salt__ = {}
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class AliasTest(TestCase):
|
||||
class AliasTest(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Validate the alias state
|
||||
'''
|
||||
loader_module = alias
|
||||
|
||||
def test_present_has_target(self):
|
||||
'''
|
||||
test alias.present has target already
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import skipIf, TestCase
|
||||
from tests.support.mock import (
|
||||
NO_MOCK,
|
||||
|
@ -17,15 +18,13 @@ from tests.support.mock import (
|
|||
# Import Salt Libs
|
||||
import salt.states.alternatives as alternatives
|
||||
|
||||
alternatives.__opts__ = {}
|
||||
alternatives.__salt__ = {}
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class AlternativesTestCase(TestCase):
|
||||
class AlternativesTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Test cases for salt.states.alternatives
|
||||
'''
|
||||
loader_module = alternatives
|
||||
# 'install' function tests: 1
|
||||
|
||||
def test_install(self):
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import skipIf, TestCase
|
||||
from tests.support.mock import (
|
||||
NO_MOCK,
|
||||
|
@ -18,15 +19,13 @@ from tests.support.mock import (
|
|||
import salt.states.apache as apache
|
||||
import salt.utils
|
||||
|
||||
apache.__opts__ = {}
|
||||
apache.__salt__ = {}
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class ApacheTestCase(TestCase):
|
||||
class ApacheTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Test cases for salt.states.apache
|
||||
'''
|
||||
loader_module = apache
|
||||
# 'configfile' function tests: 1
|
||||
|
||||
@patch('os.path.exists', MagicMock(return_value=True))
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import skipIf, TestCase
|
||||
from tests.support.mock import (
|
||||
NO_MOCK,
|
||||
|
@ -14,15 +15,13 @@ from tests.support.mock import (
|
|||
# Import Salt Libs
|
||||
import salt.states.apache_conf as apache_conf
|
||||
|
||||
apache_conf.__opts__ = {}
|
||||
apache_conf.__salt__ = {}
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class ApacheConfTestCase(TestCase):
|
||||
class ApacheConfTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Test cases for salt.states.apache_conf
|
||||
'''
|
||||
loader_module = apache_conf
|
||||
# 'enabled' function tests: 1
|
||||
|
||||
def test_enabled(self):
|
||||
|
|
Loading…
Add table
Reference in a new issue