mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
Code cleanup. Proper mocking.
This commit is contained in:
parent
f23596cbd1
commit
cb532fb9ef
@ -4,8 +4,10 @@
|
||||
'''
|
||||
# Import Python Libs
|
||||
from __future__ import absolute_import
|
||||
import os
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
from tests.support.mock import (
|
||||
MagicMock,
|
||||
@ -16,18 +18,15 @@ from tests.support.mock import (
|
||||
|
||||
# Import Salt Libs
|
||||
import salt.modules.nagios as nagios
|
||||
import os
|
||||
|
||||
|
||||
# Globals
|
||||
nagios.__salt__ = {}
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class NagiosTestCase(TestCase):
|
||||
class NagiosTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Test cases for salt.modules.nagios
|
||||
'''
|
||||
loader_module = nagios
|
||||
|
||||
def test_run(self):
|
||||
'''
|
||||
Test for Run nagios plugin and return all
|
||||
|
@ -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,8 +19,6 @@ from tests.support.mock import (
|
||||
# Import Salt Libs
|
||||
import salt.modules.netscaler as netscaler
|
||||
|
||||
netscaler.__salt__ = {}
|
||||
|
||||
|
||||
class MockJson(Exception):
|
||||
'''
|
||||
@ -555,22 +554,26 @@ class MockNSSSLVServerSSLCertKeyBinding(object):
|
||||
raise MockNSNitroError
|
||||
return MockNSSSLVServerSSLCertKeyBinding()
|
||||
|
||||
netscaler.NSNitro = MockNSNitro
|
||||
netscaler.NSServiceGroup = MockNSServiceGroup
|
||||
netscaler.NSServiceGroupServerBinding = MockNSServiceGroupServerBinding
|
||||
netscaler.NSLBVServerServiceGroupBinding = MockNSLBVServerServiceGroupBinding
|
||||
netscaler.NSService = MockNSService
|
||||
netscaler.NSServer = MockNSServer
|
||||
netscaler.NSLBVServer = MockNSLBVServer
|
||||
netscaler.NSNitroError = MockNSNitroError
|
||||
netscaler.NSSSLVServerSSLCertKeyBinding = MockNSSSLVServerSSLCertKeyBinding
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class NetscalerTestCase(TestCase):
|
||||
class NetscalerTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
TestCase for salt.modules.netscaler
|
||||
'''
|
||||
loader_module = netscaler
|
||||
|
||||
def loader_module_globals(self):
|
||||
return {
|
||||
'NSNitro': MockNSNitro,
|
||||
'NSServiceGroup': MockNSServiceGroup,
|
||||
'NSServiceGroupServerBinding': MockNSServiceGroupServerBinding,
|
||||
'NSLBVServerServiceGroupBinding': MockNSLBVServerServiceGroupBinding,
|
||||
'NSService': MockNSService,
|
||||
'NSServer': MockNSServer,
|
||||
'NSLBVServer': MockNSLBVServer,
|
||||
'NSNitroError': MockNSNitroError,
|
||||
'NSSSLVServerSSLCertKeyBinding': MockNSSSLVServerSSLCertKeyBinding,
|
||||
}
|
||||
# 'servicegroup_exists' function tests: 1
|
||||
|
||||
def test_servicegroup_exists(self):
|
||||
|
@ -9,6 +9,7 @@ import socket
|
||||
import os.path
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
from tests.support.mock import (
|
||||
mock_open,
|
||||
@ -24,18 +25,23 @@ import salt.utils
|
||||
import salt.modules.network as network
|
||||
from salt.exceptions import CommandExecutionError
|
||||
if six.PY2:
|
||||
import salt.ext.ipaddress
|
||||
|
||||
# Globals
|
||||
network.__grains__ = {}
|
||||
network.__salt__ = {}
|
||||
import salt.ext.ipaddress as ipaddress
|
||||
HAS_IPADDRESS = True
|
||||
else:
|
||||
try:
|
||||
import ipaddress
|
||||
HAS_IPADDRESS = True
|
||||
except ImportError:
|
||||
HAS_IPADDRESS = False
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class NetworkTestCase(TestCase):
|
||||
class NetworkTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Test cases for salt.modules.network
|
||||
'''
|
||||
loader_module = network
|
||||
|
||||
def test_wol_bad_mac(self):
|
||||
'''
|
||||
tests network.wol with bad mac
|
||||
@ -119,6 +125,7 @@ class NetworkTestCase(TestCase):
|
||||
MagicMock(return_value="")}):
|
||||
self.assertListEqual(network.traceroute('host'), [])
|
||||
|
||||
@patch('salt.utils.which', MagicMock(return_value='dig'))
|
||||
def test_dig(self):
|
||||
'''
|
||||
Test for Performs a DNS lookup with dig
|
||||
@ -268,27 +275,27 @@ class NetworkTestCase(TestCase):
|
||||
self.assertDictEqual(network.connect('host', 'port'),
|
||||
{'comment': ret, 'result': True})
|
||||
|
||||
@skipIf(not six.PY2, 'test applies only to python 2')
|
||||
@skipIf(HAS_IPADDRESS is False, 'unable to import \'ipaddress\'')
|
||||
def test_is_private(self):
|
||||
'''
|
||||
Test for Check if the given IP address is a private address
|
||||
'''
|
||||
with patch.object(salt.ext.ipaddress.IPv4Address, 'is_private',
|
||||
with patch.object(ipaddress.IPv4Address, 'is_private',
|
||||
return_value=True):
|
||||
self.assertTrue(network.is_private('0.0.0.0'))
|
||||
with patch.object(salt.ext.ipaddress.IPv6Address, 'is_private',
|
||||
with patch.object(ipaddress.IPv6Address, 'is_private',
|
||||
return_value=True):
|
||||
self.assertTrue(network.is_private('::1'))
|
||||
|
||||
@skipIf(not six.PY2, 'test applies only to python 2')
|
||||
@skipIf(HAS_IPADDRESS is False, 'unable to import \'ipaddress\'')
|
||||
def test_is_loopback(self):
|
||||
'''
|
||||
Test for Check if the given IP address is a loopback address
|
||||
'''
|
||||
with patch.object(salt.ext.ipaddress.IPv4Address, 'is_loopback',
|
||||
with patch.object(ipaddress.IPv4Address, 'is_loopback',
|
||||
return_value=True):
|
||||
self.assertTrue(network.is_loopback('127.0.0.1'))
|
||||
with patch.object(salt.ext.ipaddress.IPv6Address, 'is_loopback',
|
||||
with patch.object(ipaddress.IPv6Address, 'is_loopback',
|
||||
return_value=True):
|
||||
self.assertTrue(network.is_loopback('::1'))
|
||||
|
||||
|
@ -7,8 +7,9 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.unit import skipIf
|
||||
from tests.unit import ModuleTestCase, hasDependency
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import skipIf
|
||||
from tests.support.mock import (
|
||||
patch,
|
||||
NO_MOCK,
|
||||
@ -17,7 +18,6 @@ from tests.support.mock import (
|
||||
import salt.modules.servicenow as servicenow
|
||||
|
||||
SERVICE_NAME = 'servicenow'
|
||||
servicenow.__salt__ = {}
|
||||
|
||||
|
||||
class MockServiceNowClient(object):
|
||||
@ -31,10 +31,14 @@ class MockServiceNowClient(object):
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
@patch('servicenow_rest.api.Client', MockServiceNowClient)
|
||||
class ServiceNowModuleTestCase(ModuleTestCase):
|
||||
class ServiceNowModuleTestCase(ModuleTestCase, LoaderModuleMockMixin):
|
||||
loader_module = servicenow
|
||||
|
||||
def loader_module_globals(self):
|
||||
return {'Client': MockServiceNowClient}
|
||||
|
||||
def setUp(self):
|
||||
hasDependency('servicenow_rest')
|
||||
servicenow.Client = MockServiceNowClient
|
||||
|
||||
def get_config(service):
|
||||
if service == SERVICE_NAME:
|
||||
|
@ -14,6 +14,7 @@ import salt.modules.vsphere as vsphere
|
||||
from salt.exceptions import CommandExecutionError, VMwareSaltError
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
from tests.support.mock import (
|
||||
MagicMock,
|
||||
@ -29,17 +30,14 @@ PASSWORD = 'SuperSecret!'
|
||||
ERROR = 'Some Testing Error Message'
|
||||
mock_si = MagicMock()
|
||||
|
||||
# Inject empty dunders do they can be patched
|
||||
vsphere.__pillar__ = {}
|
||||
vsphere.__salt__ = {}
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
@patch('salt.modules.vsphere.__virtual__', MagicMock(return_value='vsphere'))
|
||||
class VsphereTestCase(TestCase):
|
||||
class VsphereTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Unit TestCase for the salt.modules.vsphere module.
|
||||
'''
|
||||
loader_module = vsphere
|
||||
|
||||
# Tests for get_coredump_network_config function
|
||||
|
||||
@ -557,8 +555,9 @@ class VsphereTestCase(TestCase):
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
@patch('salt.modules.vsphere.__virtual__', MagicMock(return_value='vsphere'))
|
||||
class GetProxyTypeTestCase(TestCase):
|
||||
class GetProxyTypeTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''Tests for salt.modules.vsphere.get_proxy_type'''
|
||||
loader_module = vsphere
|
||||
|
||||
def test_output(self):
|
||||
with patch.dict(vsphere.__pillar__,
|
||||
@ -569,8 +568,9 @@ class GetProxyTypeTestCase(TestCase):
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
@patch('salt.modules.vsphere.__virtual__', MagicMock(return_value='vsphere'))
|
||||
class SupportsProxiesTestCase(TestCase):
|
||||
class SupportsProxiesTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''Tests for salt.modules.vsphere.supports_proxies decorator'''
|
||||
loader_module = vsphere
|
||||
|
||||
def test_supported_proxy(self):
|
||||
@vsphere.supports_proxies('supported')
|
||||
@ -598,8 +598,9 @@ class SupportsProxiesTestCase(TestCase):
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
@patch('salt.modules.vsphere.__virtual__', MagicMock(return_value='vsphere'))
|
||||
class _GetProxyConnectionDetailsTestCase(TestCase):
|
||||
class _GetProxyConnectionDetailsTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''Tests for salt.modules.vsphere._get_proxy_connection_details'''
|
||||
loader_module = vsphere
|
||||
|
||||
def setUp(self):
|
||||
self.esxi_host_details = {'host': 'fake_host',
|
||||
@ -619,6 +620,13 @@ class _GetProxyConnectionDetailsTestCase(TestCase):
|
||||
'principal': 'fake_principal',
|
||||
'domain': 'fake_domain'}
|
||||
|
||||
def tearDown(self):
|
||||
for attrname in ('esxi_host_details', 'esxi_vcenter_details'):
|
||||
try:
|
||||
delattr(self, attrname)
|
||||
except AttributeError:
|
||||
continue
|
||||
|
||||
def test_esxi_proxy_host_details(self):
|
||||
with patch('salt.modules.vsphere.get_proxy_type',
|
||||
MagicMock(return_value='esxi')):
|
||||
@ -656,17 +664,25 @@ class _GetProxyConnectionDetailsTestCase(TestCase):
|
||||
@patch('salt.modules.vsphere._get_proxy_connection_details', MagicMock())
|
||||
@patch('salt.utils.vmware.get_service_instance', MagicMock())
|
||||
@patch('salt.utils.vmware.disconnect', MagicMock())
|
||||
class GetsServiceInstanceViaProxyTestCase(TestCase):
|
||||
class GetsServiceInstanceViaProxyTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Tests for salt.modules.vsphere.gets_service_instance_via_proxy
|
||||
decorator
|
||||
'''
|
||||
loader_module = vsphere
|
||||
|
||||
def setUp(self):
|
||||
self.mock_si = MagicMock()
|
||||
self.mock_details1 = MagicMock()
|
||||
self.mock_details2 = MagicMock()
|
||||
|
||||
def tearDown(self):
|
||||
for attrname in ('mock_si', 'mock_details1', 'mock_details2'):
|
||||
try:
|
||||
delattr(self, attrname)
|
||||
except AttributeError:
|
||||
continue
|
||||
|
||||
def test_no_service_instance_or_kwargs_parameters(self):
|
||||
@vsphere.gets_service_instance_via_proxy
|
||||
def mock_function():
|
||||
@ -814,8 +830,9 @@ class GetsServiceInstanceViaProxyTestCase(TestCase):
|
||||
# Function mocks
|
||||
@patch('salt.modules.vsphere._get_proxy_connection_details', MagicMock())
|
||||
@patch('salt.utils.vmware.get_service_instance', MagicMock())
|
||||
class GetServiceInstanceViaProxyTestCase(TestCase):
|
||||
class GetServiceInstanceViaProxyTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''Tests for salt.modules.vsphere.get_service_instance_via_proxy'''
|
||||
loader_module = vsphere
|
||||
|
||||
def test_supported_proxes(self):
|
||||
supported_proxies = ['esxi']
|
||||
@ -850,8 +867,9 @@ class GetServiceInstanceViaProxyTestCase(TestCase):
|
||||
# Function mocks
|
||||
@patch('salt.modules.vsphere._get_proxy_connection_details', MagicMock())
|
||||
@patch('salt.utils.vmware.disconnect', MagicMock())
|
||||
class DisconnectTestCase(TestCase):
|
||||
class DisconnectTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''Tests for salt.modules.vsphere.disconnect'''
|
||||
loader_module = vsphere
|
||||
|
||||
def test_supported_proxes(self):
|
||||
supported_proxies = ['esxi']
|
||||
@ -881,8 +899,9 @@ class DisconnectTestCase(TestCase):
|
||||
@patch('salt.utils.vmware.disconnect', MagicMock())
|
||||
# Function mocks
|
||||
@patch('salt.utils.vmware.is_connection_to_a_vcenter', MagicMock())
|
||||
class TestVcenterConnectionTestCase(TestCase):
|
||||
class TestVcenterConnectionTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''Tests for salt.modules.vsphere.test_vcenter_connection'''
|
||||
loader_module = vsphere
|
||||
|
||||
def test_supported_proxes(self):
|
||||
supported_proxies = ['esxi']
|
||||
|
Loading…
Reference in New Issue
Block a user