1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-01 01:46:18 +00:00

Move modules and module_utils unit tests to correct place (#81)

* Move modules and module_utils unit tests to correct place.

* Update ignore.txt

* Fix imports.

* Fix typos.

* Fix more typos.
This commit is contained in:
Felix Fontein 2020-03-31 10:42:38 +02:00 committed by GitHub
parent ab3c2120fb
commit be191cce6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
1170 changed files with 732 additions and 751 deletions

View file

@ -0,0 +1,124 @@
# Copyright (c) 2018-2019 Cisco and/or its affiliates.
#
# 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 __future__ import absolute_import
import pytest
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args, exit_json, fail_json, AnsibleFailJson, AnsibleExitJson
from ansible.module_utils import basic
from ansible_collections.community.general.plugins.module_utils.network.ftd.common import FtdConfigurationError, FtdServerError, FtdUnexpectedResponse
from ansible_collections.community.general.plugins.module_utils.network.ftd.configuration import FtdInvalidOperationNameError, CheckModeException
from ansible_collections.community.general.plugins.module_utils.network.ftd.fdm_swagger_client import ValidationError
from ansible_collections.community.general.plugins.modules.network.ftd import ftd_configuration
class TestFtdConfiguration(object):
module = ftd_configuration
@pytest.fixture(autouse=True)
def module_mock(self, mocker):
return mocker.patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json)
@pytest.fixture(autouse=True)
def connection_mock(self, mocker):
connection_class_mock = mocker.patch('ansible_collections.community.general.plugins.modules.network.ftd.ftd_configuration.Connection')
return connection_class_mock.return_value
@pytest.fixture
def resource_mock(self, mocker):
resource_class_mock = mocker.patch('ansible_collections.community.general.plugins.modules.network.ftd.ftd_configuration.BaseConfigurationResource')
resource_instance = resource_class_mock.return_value
return resource_instance.execute_operation
def test_module_should_fail_when_ftd_invalid_operation_name_error(self, resource_mock):
operation_name = 'test name'
resource_mock.side_effect = FtdInvalidOperationNameError(operation_name)
result = self._run_module_with_fail_json({'operation': operation_name})
assert result['failed']
assert 'Invalid operation name provided: %s' % operation_name == result['msg']
def test_module_should_fail_when_ftd_configuration_error(self, resource_mock):
operation_name = 'test name'
msg = 'Foo error.'
resource_mock.side_effect = FtdConfigurationError(msg)
result = self._run_module_with_fail_json({'operation': operation_name})
assert result['failed']
assert 'Failed to execute %s operation because of the configuration error: %s' % (operation_name, msg) == \
result['msg']
def test_module_should_fail_when_ftd_server_error(self, resource_mock):
operation_name = 'test name'
code = 500
response = {'error': 'foo'}
resource_mock.side_effect = FtdServerError(response, code)
result = self._run_module_with_fail_json({'operation': operation_name})
assert result['failed']
assert 'Server returned an error trying to execute %s operation. Status code: %s. ' \
'Server response: %s' % (operation_name, code, response) == \
result['msg']
def test_module_should_fail_when_validation_error(self, resource_mock):
operation_name = 'test name'
msg = 'Foo error.'
resource_mock.side_effect = ValidationError(msg)
result = self._run_module_with_fail_json({'operation': operation_name})
assert result['failed']
assert msg == result['msg']
def test_module_should_fail_when_unexpected_server_response(self, resource_mock):
operation_name = 'test name'
msg = 'Foo error.'
resource_mock.side_effect = FtdUnexpectedResponse(msg)
result = self._run_module_with_fail_json({'operation': operation_name})
assert result['failed']
assert msg == result['msg']
def test_module_should_fail_when_check_mode_exception(self, resource_mock):
operation_name = 'test name'
msg = 'Foo error.'
resource_mock.side_effect = CheckModeException(msg)
result = self._run_module({'operation': operation_name})
assert not result['changed']
def test_module_should_run_successful(self, resource_mock):
operation_name = 'test name'
resource_mock.return_value = {'result': 'ok'}
result = self._run_module({'operation': operation_name})
assert result['response'] == {'result': 'ok'}
def _run_module(self, module_args):
set_module_args(module_args)
with pytest.raises(AnsibleExitJson) as ex:
self.module.main()
return ex.value.args[0]
def _run_module_with_fail_json(self, module_args):
set_module_args(module_args)
with pytest.raises(AnsibleFailJson) as exc:
self.module.main()
result = exc.value.args[0]
return result

View file

@ -0,0 +1,98 @@
# Copyright (c) 2018 Cisco and/or its affiliates.
#
# 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 __future__ import absolute_import
import pytest
from ansible.module_utils import basic
from ansible_collections.community.general.plugins.module_utils.network.ftd.common import HTTPMethod
from ansible_collections.community.general.plugins.module_utils.network.ftd.fdm_swagger_client import FILE_MODEL_NAME, OperationField
from ansible_collections.community.general.plugins.modules.network.ftd import ftd_file_download
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args, exit_json, fail_json, AnsibleFailJson, AnsibleExitJson
class TestFtdFileDownload(object):
module = ftd_file_download
@pytest.fixture(autouse=True)
def module_mock(self, mocker):
return mocker.patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json)
@pytest.fixture
def connection_mock(self, mocker):
connection_class_mock = mocker.patch('ansible_collections.community.general.plugins.modules.network.ftd.ftd_file_download.Connection')
return connection_class_mock.return_value
@pytest.mark.parametrize("missing_arg", ['operation', 'destination'])
def test_module_should_fail_without_required_args(self, missing_arg):
module_args = {'operation': 'downloadFile', 'destination': '/tmp'}
del module_args[missing_arg]
set_module_args(module_args)
with pytest.raises(AnsibleFailJson) as ex:
self.module.main()
assert 'missing required arguments: %s' % missing_arg in str(ex.value)
def test_module_should_fail_when_no_operation_spec_found(self, connection_mock):
connection_mock.get_operation_spec.return_value = None
set_module_args({'operation': 'nonExistingDownloadOperation', 'destination': '/tmp'})
with pytest.raises(AnsibleFailJson) as ex:
self.module.main()
result = ex.value.args[0]
assert result['failed']
assert result['msg'] == 'Operation with specified name is not found: nonExistingDownloadOperation'
def test_module_should_fail_when_not_download_operation_specified(self, connection_mock):
connection_mock.get_operation_spec.return_value = {
OperationField.METHOD: HTTPMethod.GET,
OperationField.URL: '/object',
OperationField.MODEL_NAME: 'NetworkObject'
}
set_module_args({'operation': 'nonDownloadOperation', 'destination': '/tmp'})
with pytest.raises(AnsibleFailJson) as ex:
self.module.main()
result = ex.value.args[0]
assert result['failed']
assert result['msg'] == 'Invalid download operation: nonDownloadOperation. ' \
'The operation must make GET request and return a file.'
def test_module_should_call_download_and_return(self, connection_mock):
connection_mock.validate_path_params.return_value = (True, None)
connection_mock.get_operation_spec.return_value = {
OperationField.METHOD: HTTPMethod.GET,
OperationField.URL: '/file/{objId}',
OperationField.MODEL_NAME: FILE_MODEL_NAME
}
set_module_args({
'operation': 'downloadFile',
'path_params': {'objId': '12'},
'destination': '/tmp'
})
with pytest.raises(AnsibleExitJson) as ex:
self.module.main()
result = ex.value.args[0]
assert not result['changed']
connection_mock.download_file.assert_called_once_with('/file/{objId}', '/tmp', {'objId': '12'})

View file

@ -0,0 +1,80 @@
from __future__ import absolute_import
import pytest
from ansible.module_utils import basic
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args, exit_json, fail_json, AnsibleFailJson, AnsibleExitJson
from ansible_collections.community.general.plugins.modules.network.ftd import ftd_file_upload
from ansible_collections.community.general.plugins.module_utils.network.ftd.fdm_swagger_client import OperationField
from ansible_collections.community.general.plugins.module_utils.network.ftd.common import HTTPMethod
class TestFtdFileUpload(object):
module = ftd_file_upload
@pytest.fixture(autouse=True)
def module_mock(self, mocker):
return mocker.patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json)
@pytest.fixture
def connection_mock(self, mocker):
connection_class_mock = mocker.patch('ansible_collections.community.general.plugins.modules.network.ftd.ftd_file_upload.Connection')
return connection_class_mock.return_value
@pytest.mark.parametrize("missing_arg", ['operation', 'file_to_upload'])
def test_module_should_fail_without_required_args(self, missing_arg):
module_args = {'operation': 'uploadFile', 'file_to_upload': '/tmp/test.txt'}
del module_args[missing_arg]
set_module_args(module_args)
with pytest.raises(AnsibleFailJson) as ex:
self.module.main()
assert 'missing required arguments: %s' % missing_arg in str(ex.value)
def test_module_should_fail_when_no_operation_spec_found(self, connection_mock):
connection_mock.get_operation_spec.return_value = None
set_module_args({'operation': 'nonExistingUploadOperation', 'file_to_upload': '/tmp/test.txt'})
with pytest.raises(AnsibleFailJson) as ex:
self.module.main()
result = ex.value.args[0]
assert result['failed']
assert result['msg'] == 'Operation with specified name is not found: nonExistingUploadOperation'
def test_module_should_fail_when_not_upload_operation_specified(self, connection_mock):
connection_mock.get_operation_spec.return_value = {
OperationField.METHOD: HTTPMethod.GET,
OperationField.URL: '/object/network',
OperationField.MODEL_NAME: 'NetworkObject'
}
set_module_args({'operation': 'nonUploadOperation', 'file_to_upload': '/tmp/test.txt'})
with pytest.raises(AnsibleFailJson) as ex:
self.module.main()
result = ex.value.args[0]
assert result['failed']
assert result['msg'] == 'Invalid upload operation: nonUploadOperation. ' \
'The operation must make POST request and return UploadStatus model.'
def test_module_should_call_upload_and_return_response(self, connection_mock):
connection_mock.get_operation_spec.return_value = {
OperationField.METHOD: HTTPMethod.POST,
OperationField.URL: '/uploadFile',
OperationField.MODEL_NAME: 'FileUploadStatus'
}
connection_mock.upload_file.return_value = {'id': '123'}
set_module_args({
'operation': 'uploadFile',
'file_to_upload': '/tmp/test.txt'
})
with pytest.raises(AnsibleExitJson) as ex:
self.module.main()
result = ex.value.args[0]
assert result['changed']
assert {'id': '123'} == result['response']
connection_mock.upload_file.assert_called_once_with('/tmp/test.txt', '/uploadFile')

View file

@ -0,0 +1,248 @@
# Copyright (c) 2019 Cisco and/or its affiliates.
#
# 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 __future__ import absolute_import
import pytest
from ansible_collections.community.general.tests.unit.compat.mock import PropertyMock
from ansible.module_utils import basic
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args, exit_json, fail_json, AnsibleFailJson, AnsibleExitJson
from ansible_collections.community.general.plugins.modules.network.ftd import ftd_install
from ansible_collections.community.general.plugins.module_utils.network.ftd.device import FtdModel
DEFAULT_MODULE_PARAMS = dict(
device_hostname="firepower",
device_username="admin",
device_password="pass",
device_new_password="newpass",
device_sudo_password="sudopass",
device_ip="192.168.0.1",
device_netmask="255.255.255.0",
device_gateway="192.168.0.254",
device_model=FtdModel.FTD_ASA5516_X,
dns_server="8.8.8.8",
console_ip="10.89.0.0",
console_port="2004",
console_username="console_user",
console_password="console_pass",
rommon_file_location="tftp://10.0.0.1/boot/ftd-boot-1.9.2.0.lfbff",
image_file_location="http://10.0.0.1/Release/ftd-6.2.3-83.pkg",
image_version="6.2.3-83",
search_domains="cisco.com",
force_install=False
)
class TestFtdInstall(object):
module = ftd_install
@pytest.fixture(autouse=True)
def module_mock(self, mocker):
mocker.patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json)
mocker.patch.object(basic.AnsibleModule, '_socket_path', new_callable=PropertyMock, create=True,
return_value=mocker.MagicMock())
@pytest.fixture(autouse=True)
def connection_mock(self, mocker):
connection_class_mock = mocker.patch('ansible_collections.community.general.plugins.modules.network.ftd.ftd_install.Connection')
return connection_class_mock.return_value
@pytest.fixture
def config_resource_mock(self, mocker):
resource_class_mock = mocker.patch('ansible_collections.community.general.plugins.modules.network.ftd.ftd_install.BaseConfigurationResource')
return resource_class_mock.return_value
@pytest.fixture(autouse=True)
def ftd_factory_mock(self, mocker):
return mocker.patch('ansible_collections.community.general.plugins.modules.network.ftd.ftd_install.FtdPlatformFactory')
@pytest.fixture(autouse=True)
def has_kick_mock(self, mocker):
return mocker.patch('ansible_collections.community.general.plugins.module_utils.network.ftd.device.HAS_KICK', True)
def test_module_should_fail_when_kick_is_not_installed(self, mocker):
mocker.patch('ansible_collections.community.general.plugins.module_utils.network.ftd.device.HAS_KICK', False)
set_module_args(dict(DEFAULT_MODULE_PARAMS))
with pytest.raises(AnsibleFailJson) as ex:
self.module.main()
result = ex.value.args[0]
assert result['failed']
assert "Firepower-kickstart library is required to run this module" in result['msg']
def test_module_should_fail_when_platform_is_not_supported(self, config_resource_mock):
config_resource_mock.execute_operation.return_value = {'platformModel': 'nonSupportedModel'}
module_params = dict(DEFAULT_MODULE_PARAMS)
del module_params['device_model']
set_module_args(module_params)
with pytest.raises(AnsibleFailJson) as ex:
self.module.main()
result = ex.value.args[0]
assert result['failed']
assert result['msg'] == "Platform model 'nonSupportedModel' is not supported by this module."
def test_module_should_fail_when_device_model_is_missing_with_local_connection(self, mocker):
mocker.patch.object(basic.AnsibleModule, '_socket_path', create=True, return_value=None)
module_params = dict(DEFAULT_MODULE_PARAMS)
del module_params['device_model']
set_module_args(module_params)
with pytest.raises(AnsibleFailJson) as ex:
self.module.main()
result = ex.value.args[0]
assert result['failed']
expected_msg = \
"The following parameters are mandatory when the module is used with 'local' connection: device_model."
assert expected_msg == result['msg']
def test_module_should_fail_when_management_ip_values_are_missing_with_local_connection(self, mocker):
mocker.patch.object(basic.AnsibleModule, '_socket_path', create=True, return_value=None)
module_params = dict(DEFAULT_MODULE_PARAMS)
del module_params['device_ip']
del module_params['device_netmask']
del module_params['device_gateway']
set_module_args(module_params)
with pytest.raises(AnsibleFailJson) as ex:
self.module.main()
result = ex.value.args[0]
assert result['failed']
expected_msg = "The following parameters are mandatory when the module is used with 'local' connection: " \
"device_gateway, device_ip, device_netmask."
assert expected_msg == result['msg']
def test_module_should_return_when_software_is_already_installed(self, config_resource_mock):
config_resource_mock.execute_operation.return_value = {
'softwareVersion': '6.3.0-11',
'platformModel': 'Cisco ASA5516-X Threat Defense'
}
module_params = dict(DEFAULT_MODULE_PARAMS)
module_params['image_version'] = '6.3.0-11'
set_module_args(module_params)
with pytest.raises(AnsibleExitJson) as ex:
self.module.main()
result = ex.value.args[0]
assert not result['changed']
assert result['msg'] == 'FTD already has 6.3.0-11 version of software installed.'
def test_module_should_proceed_if_software_is_already_installed_and_force_param_given(self, config_resource_mock):
config_resource_mock.execute_operation.return_value = {
'softwareVersion': '6.3.0-11',
'platformModel': 'Cisco ASA5516-X Threat Defense'
}
module_params = dict(DEFAULT_MODULE_PARAMS)
module_params['image_version'] = '6.3.0-11'
module_params['force_install'] = True
set_module_args(module_params)
with pytest.raises(AnsibleExitJson) as ex:
self.module.main()
result = ex.value.args[0]
assert result['changed']
assert result['msg'] == 'Successfully installed FTD image 6.3.0-11 on the firewall device.'
def test_module_should_install_ftd_image(self, config_resource_mock, ftd_factory_mock):
config_resource_mock.execute_operation.side_effect = [
{
'softwareVersion': '6.2.3-11',
'platformModel': 'Cisco ASA5516-X Threat Defense'
}
]
module_params = dict(DEFAULT_MODULE_PARAMS)
set_module_args(module_params)
with pytest.raises(AnsibleExitJson) as ex:
self.module.main()
result = ex.value.args[0]
assert result['changed']
assert result['msg'] == 'Successfully installed FTD image 6.2.3-83 on the firewall device.'
ftd_factory_mock.create.assert_called_once_with('Cisco ASA5516-X Threat Defense', DEFAULT_MODULE_PARAMS)
ftd_factory_mock.create.return_value.install_ftd_image.assert_called_once_with(DEFAULT_MODULE_PARAMS)
def test_module_should_fill_management_ip_values_when_missing(self, config_resource_mock, ftd_factory_mock):
config_resource_mock.execute_operation.side_effect = [
{
'softwareVersion': '6.3.0-11',
'platformModel': 'Cisco ASA5516-X Threat Defense'
},
{
'items': [{
'ipv4Address': '192.168.1.1',
'ipv4NetMask': '255.255.255.0',
'ipv4Gateway': '192.168.0.1'
}]
}
]
module_params = dict(DEFAULT_MODULE_PARAMS)
expected_module_params = dict(module_params)
del module_params['device_ip']
del module_params['device_netmask']
del module_params['device_gateway']
expected_module_params.update(
device_ip='192.168.1.1',
device_netmask='255.255.255.0',
device_gateway='192.168.0.1'
)
set_module_args(module_params)
with pytest.raises(AnsibleExitJson):
self.module.main()
ftd_factory_mock.create.assert_called_once_with('Cisco ASA5516-X Threat Defense', expected_module_params)
ftd_factory_mock.create.return_value.install_ftd_image.assert_called_once_with(expected_module_params)
def test_module_should_fill_dns_server_when_missing(self, config_resource_mock, ftd_factory_mock):
config_resource_mock.execute_operation.side_effect = [
{
'softwareVersion': '6.3.0-11',
'platformModel': 'Cisco ASA5516-X Threat Defense'
},
{
'items': [{
'dnsServerGroup': {
'id': '123'
}
}]
},
{
'dnsServers': [{
'ipAddress': '8.8.9.9'
}]
}
]
module_params = dict(DEFAULT_MODULE_PARAMS)
expected_module_params = dict(module_params)
del module_params['dns_server']
expected_module_params['dns_server'] = '8.8.9.9'
set_module_args(module_params)
with pytest.raises(AnsibleExitJson):
self.module.main()
ftd_factory_mock.create.assert_called_once_with('Cisco ASA5516-X Threat Defense', expected_module_params)
ftd_factory_mock.create.return_value.install_ftd_image.assert_called_once_with(expected_module_params)