mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
New module: manage Citrix Netscaler service configuration (network/netscaler/netscaler_service) (#25129)
* netscaler_service initial implementation * Changes as requested by reviewers * Skip some tests if under python2.6 and importing requests library * Change option "operation" to "state" * Remove print statements from netscaler module utils * Catch all exceptions during login * Fix fail message * Add common option save_config
This commit is contained in:
parent
2220362a5f
commit
a00089c341
24 changed files with 2328 additions and 0 deletions
0
test/units/modules/network/netscaler/__init__.py
Normal file
0
test/units/modules/network/netscaler/__init__.py
Normal file
69
test/units/modules/network/netscaler/netscaler_module.py
Normal file
69
test/units/modules/network/netscaler/netscaler_module.py
Normal file
|
@ -0,0 +1,69 @@
|
|||
import sys
|
||||
|
||||
from ansible.compat.tests.mock import patch, Mock
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.module_utils import basic
|
||||
import json
|
||||
from ansible.module_utils._text import to_bytes
|
||||
|
||||
base_modules_mock = Mock()
|
||||
nitro_service_mock = Mock()
|
||||
nitro_exception_mock = Mock()
|
||||
|
||||
|
||||
base_modules_to_mock = {
|
||||
'nssrc': base_modules_mock,
|
||||
'nssrc.com': base_modules_mock,
|
||||
'nssrc.com.citrix': base_modules_mock,
|
||||
'nssrc.com.citrix.netscaler': base_modules_mock,
|
||||
'nssrc.com.citrix.netscaler.nitro': base_modules_mock,
|
||||
'nssrc.com.citrix.netscaler.nitro.resource': base_modules_mock,
|
||||
'nssrc.com.citrix.netscaler.nitro.resource.config': base_modules_mock,
|
||||
'nssrc.com.citrix.netscaler.nitro.exception': base_modules_mock,
|
||||
'nssrc.com.citrix.netscaler.nitro.exception.nitro_exception': base_modules_mock,
|
||||
'nssrc.com.citrix.netscaler.nitro.exception.nitro_exception.nitro_exception': nitro_exception_mock,
|
||||
'nssrc.com.citrix.netscaler.nitro.service': base_modules_mock,
|
||||
'nssrc.com.citrix.netscaler.nitro.service.nitro_service': base_modules_mock,
|
||||
'nssrc.com.citrix.netscaler.nitro.service.nitro_service.nitro_service': nitro_service_mock,
|
||||
}
|
||||
|
||||
nitro_base_patcher = patch.dict(sys.modules, base_modules_to_mock)
|
||||
|
||||
|
||||
def set_module_args(args):
|
||||
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
|
||||
basic._ANSIBLE_ARGS = to_bytes(args)
|
||||
|
||||
|
||||
class AnsibleExitJson(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class AnsibleFailJson(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class TestModule(unittest.TestCase):
|
||||
def failed(self):
|
||||
def fail_json(*args, **kwargs):
|
||||
kwargs['failed'] = True
|
||||
raise AnsibleFailJson(kwargs)
|
||||
|
||||
with patch.object(basic.AnsibleModule, 'fail_json', fail_json):
|
||||
with self.assertRaises(AnsibleFailJson) as exc:
|
||||
self.module.main()
|
||||
|
||||
result = exc.exception.args[0]
|
||||
self.assertTrue(result['failed'], result)
|
||||
return result
|
||||
|
||||
def exited(self, changed=False):
|
||||
def exit_json(*args, **kwargs):
|
||||
raise AnsibleExitJson(kwargs)
|
||||
|
||||
with patch.object(basic.AnsibleModule, 'exit_json', exit_json):
|
||||
with self.assertRaises(AnsibleExitJson) as exc:
|
||||
self.module.main()
|
||||
|
||||
result = exc.exception.args[0]
|
||||
return result
|
|
@ -0,0 +1,175 @@
|
|||
|
||||
# Copyright (c) 2017 Citrix Systems
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import Mock
|
||||
|
||||
|
||||
from ansible.module_utils.netscaler import ConfigProxy, get_immutables_intersection, ensure_feature_is_enabled, log, loglines
|
||||
|
||||
|
||||
class TestNetscalerConfigProxy(unittest.TestCase):
|
||||
|
||||
def test_values_copied_to_actual(self):
|
||||
actual = Mock()
|
||||
client = Mock()
|
||||
values = {
|
||||
'some_key': 'some_value',
|
||||
}
|
||||
ConfigProxy(
|
||||
actual=actual,
|
||||
client=client,
|
||||
attribute_values_dict=values,
|
||||
readwrite_attrs=['some_key']
|
||||
)
|
||||
self.assertEqual(actual.some_key, values['some_key'], msg='Failed to pass correct value from values dict')
|
||||
|
||||
def test_none_values_not_copied_to_actual(self):
|
||||
actual = Mock()
|
||||
client = Mock()
|
||||
actual.key_for_none = 'initial'
|
||||
print('actual %s' % actual.key_for_none)
|
||||
values = {
|
||||
'key_for_none': None,
|
||||
}
|
||||
print('value %s' % actual.key_for_none)
|
||||
ConfigProxy(
|
||||
actual=actual,
|
||||
client=client,
|
||||
attribute_values_dict=values,
|
||||
readwrite_attrs=['key_for_none']
|
||||
)
|
||||
self.assertEqual(actual.key_for_none, 'initial')
|
||||
|
||||
def test_missing_from_values_dict_not_copied_to_actual(self):
|
||||
actual = Mock()
|
||||
client = Mock()
|
||||
values = {
|
||||
'irrelevant_key': 'irrelevant_value',
|
||||
}
|
||||
print('value %s' % actual.key_for_none)
|
||||
ConfigProxy(
|
||||
actual=actual,
|
||||
client=client,
|
||||
attribute_values_dict=values,
|
||||
readwrite_attrs=['key_for_none']
|
||||
)
|
||||
print('none %s' % getattr(actual, 'key_for_none'))
|
||||
self.assertIsInstance(actual.key_for_none, Mock)
|
||||
|
||||
def test_bool_yes_no_transform(self):
|
||||
actual = Mock()
|
||||
client = Mock()
|
||||
values = {
|
||||
'yes_key': True,
|
||||
'no_key': False,
|
||||
}
|
||||
transforms = {
|
||||
'yes_key': ['bool_yes_no'],
|
||||
'no_key': ['bool_yes_no']
|
||||
}
|
||||
ConfigProxy(
|
||||
actual=actual,
|
||||
client=client,
|
||||
attribute_values_dict=values,
|
||||
readwrite_attrs=['yes_key', 'no_key'],
|
||||
transforms=transforms,
|
||||
)
|
||||
actual_values = [actual.yes_key, actual.no_key]
|
||||
self.assertListEqual(actual_values, ['YES', 'NO'])
|
||||
|
||||
def test_bool_on_off_transform(self):
|
||||
actual = Mock()
|
||||
client = Mock()
|
||||
values = {
|
||||
'on_key': True,
|
||||
'off_key': False,
|
||||
}
|
||||
transforms = {
|
||||
'on_key': ['bool_on_off'],
|
||||
'off_key': ['bool_on_off']
|
||||
}
|
||||
ConfigProxy(
|
||||
actual=actual,
|
||||
client=client,
|
||||
attribute_values_dict=values,
|
||||
readwrite_attrs=['on_key', 'off_key'],
|
||||
transforms=transforms,
|
||||
)
|
||||
actual_values = [actual.on_key, actual.off_key]
|
||||
self.assertListEqual(actual_values, ['ON', 'OFF'])
|
||||
|
||||
def test_callable_transform(self):
|
||||
actual = Mock()
|
||||
client = Mock()
|
||||
values = {
|
||||
'transform_key': 'hello',
|
||||
'transform_chain': 'hello',
|
||||
}
|
||||
transforms = {
|
||||
'transform_key': [lambda v: v.upper()],
|
||||
'transform_chain': [lambda v: v.upper(), lambda v: v[:4]]
|
||||
}
|
||||
ConfigProxy(
|
||||
actual=actual,
|
||||
client=client,
|
||||
attribute_values_dict=values,
|
||||
readwrite_attrs=['transform_key', 'transform_chain'],
|
||||
transforms=transforms,
|
||||
)
|
||||
actual_values = [actual.transform_key, actual.transform_chain]
|
||||
self.assertListEqual(actual_values, ['HELLO', 'HELL'])
|
||||
|
||||
|
||||
class TestNetscalerModuleUtils(unittest.TestCase):
|
||||
|
||||
def test_immutables_intersection(self):
|
||||
actual = Mock()
|
||||
client = Mock()
|
||||
values = {
|
||||
'mutable_key': 'some value',
|
||||
'immutable_key': 'some other value',
|
||||
}
|
||||
proxy = ConfigProxy(
|
||||
actual=actual,
|
||||
client=client,
|
||||
attribute_values_dict=values,
|
||||
readwrite_attrs=['mutable_key', 'immutable_key'],
|
||||
immutable_attrs=['immutable_key'],
|
||||
)
|
||||
keys_to_check = ['mutable_key', 'immutable_key', 'non_existant_key']
|
||||
result = get_immutables_intersection(proxy, keys_to_check)
|
||||
self.assertListEqual(result, ['immutable_key'])
|
||||
|
||||
def test_ensure_feature_is_enabled(self):
|
||||
client = Mock()
|
||||
attrs = {'get_enabled_features.return_value': ['GSLB']}
|
||||
client.configure_mock(**attrs)
|
||||
ensure_feature_is_enabled(client, 'GSLB')
|
||||
ensure_feature_is_enabled(client, 'LB')
|
||||
client.enable_features.assert_called_once_with('LB')
|
||||
|
||||
def test_log_function(self):
|
||||
messages = [
|
||||
'First message',
|
||||
'Second message',
|
||||
]
|
||||
log(messages[0])
|
||||
log(messages[1])
|
||||
self.assertListEqual(messages, loglines, msg='Log messages not recorded correctly')
|
343
test/units/modules/network/netscaler/test_netscaler_service.py
Normal file
343
test/units/modules/network/netscaler/test_netscaler_service.py
Normal file
|
@ -0,0 +1,343 @@
|
|||
|
||||
# Copyright (c) 2017 Citrix Systems
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
from ansible.compat.tests.mock import patch, Mock, MagicMock, call
|
||||
|
||||
import sys
|
||||
|
||||
if sys.version_info[:2] != (2, 6):
|
||||
import requests
|
||||
|
||||
|
||||
from .netscaler_module import TestModule, nitro_base_patcher, set_module_args
|
||||
|
||||
|
||||
class TestNetscalerServiceModule(TestModule):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
m = MagicMock()
|
||||
cls.service_mock = MagicMock()
|
||||
cls.service_mock.__class__ = MagicMock()
|
||||
cls.service_lbmonitor_binding_mock = MagicMock()
|
||||
cls.lbmonitor_service_binding_mock = MagicMock()
|
||||
nssrc_modules_mock = {
|
||||
'nssrc.com.citrix.netscaler.nitro.resource.config.basic': m,
|
||||
'nssrc.com.citrix.netscaler.nitro.resource.config.basic.service': m,
|
||||
'nssrc.com.citrix.netscaler.nitro.resource.config.basic.service.service': cls.service_mock,
|
||||
'nssrc.com.citrix.netscaler.nitro.resource.config.basic.service_lbmonitor_binding': cls.service_lbmonitor_binding_mock,
|
||||
'nssrc.com.citrix.netscaler.nitro.resource.config.basic.service_lbmonitor_binding.service_lbmonitor_binding': m,
|
||||
'nssrc.com.citrix.netscaler.nitro.resource.config.lb': m,
|
||||
'nssrc.com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_service_binding': m,
|
||||
'nssrc.com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_service_binding.lbmonitor_service_binding': cls.lbmonitor_service_binding_mock,
|
||||
}
|
||||
|
||||
cls.nitro_specific_patcher = patch.dict(sys.modules, nssrc_modules_mock)
|
||||
cls.nitro_base_patcher = nitro_base_patcher
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.nitro_base_patcher.stop()
|
||||
cls.nitro_specific_patcher.stop()
|
||||
|
||||
def set_module_state(self, state):
|
||||
set_module_args(dict(
|
||||
nitro_user='user',
|
||||
nitro_pass='pass',
|
||||
nsip='1.1.1.1',
|
||||
state=state,
|
||||
))
|
||||
|
||||
def setUp(self):
|
||||
self.nitro_base_patcher.start()
|
||||
self.nitro_specific_patcher.start()
|
||||
|
||||
# Setup minimal required arguments to pass AnsibleModule argument parsing
|
||||
|
||||
def tearDown(self):
|
||||
self.nitro_base_patcher.stop()
|
||||
self.nitro_specific_patcher.stop()
|
||||
|
||||
def test_graceful_nitro_api_import_error(self):
|
||||
# Stop nitro api patching to cause ImportError
|
||||
self.set_module_state('present')
|
||||
self.nitro_base_patcher.stop()
|
||||
self.nitro_specific_patcher.stop()
|
||||
from ansible.modules.network.netscaler import netscaler_service
|
||||
self.module = netscaler_service
|
||||
result = self.failed()
|
||||
self.assertEqual(result['msg'], 'Could not load nitro python sdk')
|
||||
|
||||
def test_graceful_nitro_error_on_login(self):
|
||||
self.set_module_state('present')
|
||||
from ansible.modules.network.netscaler import netscaler_service
|
||||
|
||||
class MockException(Exception):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.errorcode = 0
|
||||
self.message = ''
|
||||
|
||||
client_mock = Mock()
|
||||
client_mock.login = Mock(side_effect=MockException)
|
||||
m = Mock(return_value=client_mock)
|
||||
with patch('ansible.modules.network.netscaler.netscaler_service.get_nitro_client', m):
|
||||
with patch('ansible.modules.network.netscaler.netscaler_service.nitro_exception', MockException):
|
||||
self.module = netscaler_service
|
||||
result = self.failed()
|
||||
self.assertTrue(result['msg'].startswith('nitro exception'), msg='nitro exception during login not handled properly')
|
||||
|
||||
def test_graceful_no_connection_error(self):
|
||||
|
||||
if sys.version_info[:2] == (2, 6):
|
||||
self.skipTest('requests library not available under python2.6')
|
||||
self.set_module_state('present')
|
||||
from ansible.modules.network.netscaler import netscaler_service
|
||||
|
||||
class MockException(Exception):
|
||||
pass
|
||||
client_mock = Mock()
|
||||
attrs = {'login.side_effect': requests.exceptions.ConnectionError}
|
||||
client_mock.configure_mock(**attrs)
|
||||
m = Mock(return_value=client_mock)
|
||||
with patch.multiple(
|
||||
'ansible.modules.network.netscaler.netscaler_service',
|
||||
get_nitro_client=m,
|
||||
nitro_exception=MockException,
|
||||
):
|
||||
self.module = netscaler_service
|
||||
result = self.failed()
|
||||
self.assertTrue(result['msg'].startswith('Connection error'), msg='Connection error was not handled gracefully')
|
||||
|
||||
def test_graceful_login_error(self):
|
||||
self.set_module_state('present')
|
||||
from ansible.modules.network.netscaler import netscaler_service
|
||||
|
||||
if sys.version_info[:2] == (2, 6):
|
||||
self.skipTest('requests library not available under python2.6')
|
||||
|
||||
class MockException(Exception):
|
||||
pass
|
||||
client_mock = Mock()
|
||||
attrs = {'login.side_effect': requests.exceptions.SSLError}
|
||||
client_mock.configure_mock(**attrs)
|
||||
m = Mock(return_value=client_mock)
|
||||
with patch.multiple(
|
||||
'ansible.modules.network.netscaler.netscaler_service',
|
||||
get_nitro_client=m,
|
||||
nitro_exception=MockException,
|
||||
):
|
||||
self.module = netscaler_service
|
||||
result = self.failed()
|
||||
self.assertTrue(result['msg'].startswith('SSL Error'), msg='SSL Error was not handled gracefully')
|
||||
|
||||
def test_create_non_existing_service(self):
|
||||
self.set_module_state('present')
|
||||
from ansible.modules.network.netscaler import netscaler_service
|
||||
service_proxy_mock = MagicMock()
|
||||
attrs = {
|
||||
'diff_object.return_value': {},
|
||||
}
|
||||
service_proxy_mock.configure_mock(**attrs)
|
||||
|
||||
m = MagicMock(return_value=service_proxy_mock)
|
||||
service_exists_mock = Mock(side_effect=[False, True])
|
||||
|
||||
with patch.multiple(
|
||||
'ansible.modules.network.netscaler.netscaler_service',
|
||||
ConfigProxy=m,
|
||||
service_exists=service_exists_mock,
|
||||
):
|
||||
self.module = netscaler_service
|
||||
result = self.exited()
|
||||
service_proxy_mock.assert_has_calls([call.add()])
|
||||
self.assertTrue(result['changed'], msg='Change not recorded')
|
||||
|
||||
def test_update_service_when_service_differs(self):
|
||||
self.set_module_state('present')
|
||||
from ansible.modules.network.netscaler import netscaler_service
|
||||
service_proxy_mock = MagicMock()
|
||||
attrs = {
|
||||
'diff_object.return_value': {},
|
||||
}
|
||||
service_proxy_mock.configure_mock(**attrs)
|
||||
|
||||
m = MagicMock(return_value=service_proxy_mock)
|
||||
service_exists_mock = Mock(side_effect=[True, True])
|
||||
service_identical_mock = Mock(side_effect=[False, True])
|
||||
monitor_bindings_identical_mock = Mock(side_effect=[True, True])
|
||||
all_identical_mock = Mock(side_effect=[False])
|
||||
|
||||
with patch.multiple(
|
||||
'ansible.modules.network.netscaler.netscaler_service',
|
||||
ConfigProxy=m,
|
||||
service_exists=service_exists_mock,
|
||||
service_identical=service_identical_mock,
|
||||
monitor_bindings_identical=monitor_bindings_identical_mock,
|
||||
all_identical=all_identical_mock,
|
||||
):
|
||||
self.module = netscaler_service
|
||||
result = self.exited()
|
||||
service_proxy_mock.assert_has_calls([call.update()])
|
||||
self.assertTrue(result['changed'], msg='Change not recorded')
|
||||
|
||||
def test_update_service_when_monitor_bindings_differ(self):
|
||||
self.set_module_state('present')
|
||||
from ansible.modules.network.netscaler import netscaler_service
|
||||
service_proxy_mock = MagicMock()
|
||||
attrs = {
|
||||
'diff_object.return_value': {},
|
||||
}
|
||||
service_proxy_mock.configure_mock(**attrs)
|
||||
|
||||
m = MagicMock(return_value=service_proxy_mock)
|
||||
service_exists_mock = Mock(side_effect=[True, True])
|
||||
service_identical_mock = Mock(side_effect=[True, True])
|
||||
monitor_bindings_identical_mock = Mock(side_effect=[False, True])
|
||||
all_identical_mock = Mock(side_effect=[False])
|
||||
sync_monitor_bindings_mock = Mock()
|
||||
|
||||
with patch.multiple(
|
||||
'ansible.modules.network.netscaler.netscaler_service',
|
||||
ConfigProxy=m,
|
||||
service_exists=service_exists_mock,
|
||||
service_identical=service_identical_mock,
|
||||
monitor_bindings_identical=monitor_bindings_identical_mock,
|
||||
all_identical=all_identical_mock,
|
||||
sync_monitor_bindings=sync_monitor_bindings_mock,
|
||||
):
|
||||
self.module = netscaler_service
|
||||
result = self.exited()
|
||||
# poor man's assert_called_once since python3.5 does not implement that mock method
|
||||
self.assertEqual(len(sync_monitor_bindings_mock.mock_calls), 1, msg='sync monitor bindings not called once')
|
||||
self.assertTrue(result['changed'], msg='Change not recorded')
|
||||
|
||||
def test_no_change_to_module_when_all_identical(self):
|
||||
self.set_module_state('present')
|
||||
from ansible.modules.network.netscaler import netscaler_service
|
||||
service_proxy_mock = MagicMock()
|
||||
attrs = {
|
||||
'diff_object.return_value': {},
|
||||
}
|
||||
service_proxy_mock.configure_mock(**attrs)
|
||||
|
||||
m = MagicMock(return_value=service_proxy_mock)
|
||||
service_exists_mock = Mock(side_effect=[True, True])
|
||||
service_identical_mock = Mock(side_effect=[True, True])
|
||||
monitor_bindings_identical_mock = Mock(side_effect=[True, True])
|
||||
|
||||
with patch.multiple(
|
||||
'ansible.modules.network.netscaler.netscaler_service',
|
||||
ConfigProxy=m,
|
||||
service_exists=service_exists_mock,
|
||||
service_identical=service_identical_mock,
|
||||
monitor_bindings_identical=monitor_bindings_identical_mock,
|
||||
):
|
||||
self.module = netscaler_service
|
||||
result = self.exited()
|
||||
self.assertFalse(result['changed'], msg='Erroneous changed status update')
|
||||
|
||||
def test_absent_operation(self):
|
||||
self.set_module_state('absent')
|
||||
from ansible.modules.network.netscaler import netscaler_service
|
||||
service_proxy_mock = MagicMock()
|
||||
attrs = {
|
||||
'diff_object.return_value': {},
|
||||
}
|
||||
service_proxy_mock.configure_mock(**attrs)
|
||||
|
||||
m = MagicMock(return_value=service_proxy_mock)
|
||||
service_exists_mock = Mock(side_effect=[True, False])
|
||||
|
||||
with patch.multiple(
|
||||
'ansible.modules.network.netscaler.netscaler_service',
|
||||
ConfigProxy=m,
|
||||
service_exists=service_exists_mock,
|
||||
|
||||
):
|
||||
self.module = netscaler_service
|
||||
result = self.exited()
|
||||
service_proxy_mock.assert_has_calls([call.delete()])
|
||||
self.assertTrue(result['changed'], msg='Changed status not set correctly')
|
||||
|
||||
def test_absent_operation_no_change(self):
|
||||
self.set_module_state('absent')
|
||||
from ansible.modules.network.netscaler import netscaler_service
|
||||
service_proxy_mock = MagicMock()
|
||||
attrs = {
|
||||
'diff_object.return_value': {},
|
||||
}
|
||||
service_proxy_mock.configure_mock(**attrs)
|
||||
|
||||
m = MagicMock(return_value=service_proxy_mock)
|
||||
service_exists_mock = Mock(side_effect=[False, False])
|
||||
|
||||
with patch.multiple(
|
||||
'ansible.modules.network.netscaler.netscaler_service',
|
||||
ConfigProxy=m,
|
||||
service_exists=service_exists_mock,
|
||||
|
||||
):
|
||||
self.module = netscaler_service
|
||||
result = self.exited()
|
||||
service_proxy_mock.assert_not_called()
|
||||
self.assertFalse(result['changed'], msg='Changed status not set correctly')
|
||||
|
||||
def test_graceful_nitro_exception_operation_present(self):
|
||||
self.set_module_state('present')
|
||||
from ansible.modules.network.netscaler import netscaler_service
|
||||
|
||||
class MockException(Exception):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.errorcode = 0
|
||||
self.message = ''
|
||||
|
||||
m = Mock(side_effect=MockException)
|
||||
with patch.multiple(
|
||||
'ansible.modules.network.netscaler.netscaler_service',
|
||||
service_exists=m,
|
||||
nitro_exception=MockException
|
||||
):
|
||||
self.module = netscaler_service
|
||||
result = self.failed()
|
||||
self.assertTrue(
|
||||
result['msg'].startswith('nitro exception'),
|
||||
msg='Nitro exception not caught on operation present'
|
||||
)
|
||||
|
||||
def test_graceful_nitro_exception_operation_absent(self):
|
||||
self.set_module_state('absent')
|
||||
from ansible.modules.network.netscaler import netscaler_service
|
||||
|
||||
class MockException(Exception):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.errorcode = 0
|
||||
self.message = ''
|
||||
|
||||
m = Mock(side_effect=MockException)
|
||||
with patch.multiple(
|
||||
'ansible.modules.network.netscaler.netscaler_service',
|
||||
service_exists=m,
|
||||
nitro_exception=MockException
|
||||
):
|
||||
self.module = netscaler_service
|
||||
result = self.failed()
|
||||
self.assertTrue(
|
||||
result['msg'].startswith('nitro exception'),
|
||||
msg='Nitro exception not caught on operation absent'
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue