mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 21:32:49 +00:00
New modules and updated HTTP API plugin for FTD devices (#44578)
* Add common and Swagger client utils for FTD modules * Update FTD HTTP API plugin and add unit tests for it * Add configuration layer handling object idempotency * Add ftd_configuration module with unit tests * Add ftd_file_download and ftd_file_upload modules with unit tests * Validate operation data and parameters * Fix ansible-doc, boilerplate and import errors * Fix pip8 sanity errors * Update object comparison to work recursively * Add copyright
This commit is contained in:
committed by
Ricardo Carrillo Cruz
parent
1c42198f1e
commit
40a97d43d1
0
test/units/module_utils/network/ftd/__init__.py
Normal file
0
test/units/module_utils/network/ftd/__init__.py
Normal file
241
test/units/module_utils/network/ftd/test_common.py
Normal file
241
test/units/module_utils/network/ftd/test_common.py
Normal file
@@ -0,0 +1,241 @@
|
||||
# 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 ansible.module_utils.network.ftd.common import equal_objects
|
||||
|
||||
|
||||
# simple objects
|
||||
|
||||
def test_equal_objects_return_false_with_different_length():
|
||||
assert not equal_objects(
|
||||
{'foo': 1},
|
||||
{'foo': 1, 'bar': 2}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_false_with_different_fields():
|
||||
assert not equal_objects(
|
||||
{'foo': 1},
|
||||
{'bar': 1}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_false_with_different_value_types():
|
||||
assert not equal_objects(
|
||||
{'foo': 1},
|
||||
{'foo': '1'}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_false_with_different_values():
|
||||
assert not equal_objects(
|
||||
{'foo': 1},
|
||||
{'foo': 2}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_false_with_different_nested_values():
|
||||
assert not equal_objects(
|
||||
{'foo': {'bar': 1}},
|
||||
{'foo': {'bar': 2}}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_false_with_different_list_length():
|
||||
assert not equal_objects(
|
||||
{'foo': []},
|
||||
{'foo': ['bar']}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_true_with_equal_objects():
|
||||
assert equal_objects(
|
||||
{'foo': 1, 'bar': 2},
|
||||
{'bar': 2, 'foo': 1}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_true_with_equal_nested_dicts():
|
||||
assert equal_objects(
|
||||
{'foo': {'bar': 1, 'buz': 2}},
|
||||
{'foo': {'buz': 2, 'bar': 1}}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_true_with_equal_lists():
|
||||
assert equal_objects(
|
||||
{'foo': ['bar']},
|
||||
{'foo': ['bar']}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_true_with_ignored_fields():
|
||||
assert equal_objects(
|
||||
{'foo': 1, 'version': '123', 'id': '123123'},
|
||||
{'foo': 1}
|
||||
)
|
||||
|
||||
|
||||
# objects with object references
|
||||
|
||||
def test_equal_objects_return_true_with_different_ref_ids():
|
||||
assert not equal_objects(
|
||||
{'foo': {'id': '1', 'type': 'network', 'ignored_field': 'foo'}},
|
||||
{'foo': {'id': '2', 'type': 'network', 'ignored_field': 'bar'}}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_true_with_different_ref_types():
|
||||
assert not equal_objects(
|
||||
{'foo': {'id': '1', 'type': 'network', 'ignored_field': 'foo'}},
|
||||
{'foo': {'id': '1', 'type': 'accessRule', 'ignored_field': 'bar'}}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_true_with_same_object_refs():
|
||||
assert equal_objects(
|
||||
{'foo': {'id': '1', 'type': 'network', 'ignored_field': 'foo'}},
|
||||
{'foo': {'id': '1', 'type': 'network', 'ignored_field': 'bar'}}
|
||||
)
|
||||
|
||||
|
||||
# objects with array of object references
|
||||
|
||||
def test_equal_objects_return_false_with_different_array_length():
|
||||
assert not equal_objects(
|
||||
{'foo': [
|
||||
{'id': '1', 'type': 'network', 'ignored_field': 'foo'}
|
||||
]},
|
||||
{'foo': []}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_false_with_different_array_order():
|
||||
assert not equal_objects(
|
||||
{'foo': [
|
||||
{'id': '1', 'type': 'network', 'ignored_field': 'foo'},
|
||||
{'id': '2', 'type': 'network', 'ignored_field': 'bar'}
|
||||
]},
|
||||
{'foo': [
|
||||
{'id': '2', 'type': 'network', 'ignored_field': 'foo'},
|
||||
{'id': '1', 'type': 'network', 'ignored_field': 'bar'}
|
||||
]}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_true_with_equal_ref_arrays():
|
||||
assert equal_objects(
|
||||
{'foo': [
|
||||
{'id': '1', 'type': 'network', 'ignored_field': 'foo'}
|
||||
]},
|
||||
{'foo': [
|
||||
{'id': '1', 'type': 'network', 'ignored_field': 'bar'}
|
||||
]}
|
||||
)
|
||||
|
||||
|
||||
# objects with nested structures and object references
|
||||
|
||||
def test_equal_objects_return_true_with_equal_nested_object_references():
|
||||
assert equal_objects(
|
||||
{
|
||||
'name': 'foo',
|
||||
'config': {
|
||||
'version': '1',
|
||||
'port': {
|
||||
'name': 'oldPortName',
|
||||
'type': 'port',
|
||||
'id': '123'
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'foo',
|
||||
'config': {
|
||||
'version': '1',
|
||||
'port': {
|
||||
'name': 'newPortName',
|
||||
'type': 'port',
|
||||
'id': '123'
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_false_with_different_nested_object_references():
|
||||
assert not equal_objects(
|
||||
{
|
||||
'name': 'foo',
|
||||
'config': {
|
||||
'version': '1',
|
||||
'port': {
|
||||
'name': 'oldPortName',
|
||||
'type': 'port',
|
||||
'id': '123'
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'foo',
|
||||
'config': {
|
||||
'version': '1',
|
||||
'port': {
|
||||
'name': 'oldPortName',
|
||||
'type': 'port',
|
||||
'id': '234'
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_equal_objects_return_true_with_equal_nested_list_of_object_references():
|
||||
assert equal_objects(
|
||||
{
|
||||
'name': 'foo',
|
||||
'config': {
|
||||
'version': '1',
|
||||
'ports': [{
|
||||
'name': 'oldPortName',
|
||||
'type': 'port',
|
||||
'id': '123'
|
||||
}, {
|
||||
'name': 'oldPortName2',
|
||||
'type': 'port',
|
||||
'id': '234'
|
||||
}]
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'foo',
|
||||
'config': {
|
||||
'version': '1',
|
||||
'ports': [{
|
||||
'name': 'newPortName',
|
||||
'type': 'port',
|
||||
'id': '123'
|
||||
}, {
|
||||
'name': 'newPortName2',
|
||||
'type': 'port',
|
||||
'id': '234',
|
||||
'extraField': 'foo'
|
||||
}]
|
||||
}
|
||||
}
|
||||
)
|
||||
147
test/units/module_utils/network/ftd/test_configuration.py
Normal file
147
test/units/module_utils/network/ftd/test_configuration.py
Normal file
@@ -0,0 +1,147 @@
|
||||
# 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 ansible.compat.tests import mock
|
||||
from ansible.compat.tests.mock import call, patch
|
||||
from ansible.module_utils.network.ftd.configuration import iterate_over_pageable_resource, BaseConfigurationResource
|
||||
|
||||
|
||||
class TestBaseConfigurationResource(object):
|
||||
|
||||
@patch.object(BaseConfigurationResource, 'send_request')
|
||||
def test_get_objects_by_filter_with_multiple_filters(self, send_request_mock):
|
||||
objects = [
|
||||
{'name': 'obj1', 'type': 1, 'foo': {'bar': 'buzz'}},
|
||||
{'name': 'obj2', 'type': 1, 'foo': {'bar': 'buz'}},
|
||||
{'name': 'obj3', 'type': 2, 'foo': {'bar': 'buzz'}}
|
||||
]
|
||||
resource = BaseConfigurationResource(None)
|
||||
|
||||
send_request_mock.side_effect = [{'items': objects}, {'items': []}]
|
||||
assert objects == resource.get_objects_by_filter('/objects', {})
|
||||
|
||||
send_request_mock.side_effect = [{'items': objects}, {'items': []}]
|
||||
assert [objects[0]] == resource.get_objects_by_filter('/objects', {'name': 'obj1'})
|
||||
|
||||
send_request_mock.side_effect = [{'items': objects}, {'items': []}]
|
||||
assert [objects[1]] == resource.get_objects_by_filter('/objects',
|
||||
{'type': 1, 'foo': {'bar': 'buz'}})
|
||||
|
||||
@patch.object(BaseConfigurationResource, 'send_request')
|
||||
def test_get_objects_by_filter_with_multiple_responses(self, send_request_mock):
|
||||
send_request_mock.side_effect = [
|
||||
{'items': [
|
||||
{'name': 'obj1', 'type': 'foo'},
|
||||
{'name': 'obj2', 'type': 'bar'}
|
||||
]},
|
||||
{'items': [
|
||||
{'name': 'obj3', 'type': 'foo'}
|
||||
]},
|
||||
{'items': []}
|
||||
]
|
||||
|
||||
resource = BaseConfigurationResource(None)
|
||||
|
||||
assert [{'name': 'obj1', 'type': 'foo'}, {'name': 'obj3', 'type': 'foo'}] == resource.get_objects_by_filter(
|
||||
'/objects', {'type': 'foo'})
|
||||
|
||||
|
||||
class TestIterateOverPageableResource(object):
|
||||
|
||||
def test_iterate_over_pageable_resource_with_no_items(self):
|
||||
resource_func = mock.Mock(return_value={'items': []})
|
||||
|
||||
items = iterate_over_pageable_resource(resource_func)
|
||||
|
||||
assert [] == list(items)
|
||||
|
||||
def test_iterate_over_pageable_resource_with_one_page(self):
|
||||
resource_func = mock.Mock(side_effect=[
|
||||
{'items': ['foo', 'bar']},
|
||||
{'items': []},
|
||||
])
|
||||
|
||||
items = iterate_over_pageable_resource(resource_func)
|
||||
|
||||
assert ['foo', 'bar'] == list(items)
|
||||
resource_func.assert_has_calls([
|
||||
call(query_params={'offset': 0, 'limit': 10}),
|
||||
call(query_params={'offset': 10, 'limit': 10})
|
||||
])
|
||||
|
||||
def test_iterate_over_pageable_resource_with_multiple_pages(self):
|
||||
resource_func = mock.Mock(side_effect=[
|
||||
{'items': ['foo']},
|
||||
{'items': ['bar']},
|
||||
{'items': ['buzz']},
|
||||
{'items': []},
|
||||
])
|
||||
|
||||
items = iterate_over_pageable_resource(resource_func)
|
||||
|
||||
assert ['foo', 'bar', 'buzz'] == list(items)
|
||||
|
||||
def test_iterate_over_pageable_resource_should_preserve_query_params(self):
|
||||
resource_func = mock.Mock(return_value={'items': []})
|
||||
|
||||
items = iterate_over_pageable_resource(resource_func, {'filter': 'name:123'})
|
||||
|
||||
assert [] == list(items)
|
||||
resource_func.assert_called_once_with(query_params={'filter': 'name:123', 'offset': 0, 'limit': 10})
|
||||
|
||||
def test_iterate_over_pageable_resource_should_preserve_limit(self):
|
||||
resource_func = mock.Mock(side_effect=[
|
||||
{'items': ['foo']},
|
||||
{'items': []},
|
||||
])
|
||||
|
||||
items = iterate_over_pageable_resource(resource_func, {'limit': 1})
|
||||
|
||||
assert ['foo'] == list(items)
|
||||
resource_func.assert_has_calls([
|
||||
call(query_params={'offset': 0, 'limit': 1}),
|
||||
call(query_params={'offset': 1, 'limit': 1})
|
||||
])
|
||||
|
||||
def test_iterate_over_pageable_resource_should_preserve_offset(self):
|
||||
resource_func = mock.Mock(side_effect=[
|
||||
{'items': ['foo']},
|
||||
{'items': []},
|
||||
])
|
||||
|
||||
items = iterate_over_pageable_resource(resource_func, {'offset': 3})
|
||||
|
||||
assert ['foo'] == list(items)
|
||||
resource_func.assert_has_calls([
|
||||
call(query_params={'offset': 3, 'limit': 10}),
|
||||
call(query_params={'offset': 13, 'limit': 10})
|
||||
])
|
||||
|
||||
def test_iterate_over_pageable_resource_should_pass_with_string_offset_and_limit(self):
|
||||
resource_func = mock.Mock(side_effect=[
|
||||
{'items': ['foo']},
|
||||
{'items': []},
|
||||
])
|
||||
|
||||
items = iterate_over_pageable_resource(resource_func, {'offset': '1', 'limit': '1'})
|
||||
|
||||
assert ['foo'] == list(items)
|
||||
resource_func.assert_has_calls([
|
||||
call(query_params={'offset': '1', 'limit': '1'}),
|
||||
call(query_params={'offset': 2, 'limit': '1'})
|
||||
])
|
||||
196
test/units/module_utils/network/ftd/test_fdm_swagger_parser.py
Normal file
196
test/units/module_utils/network/ftd/test_fdm_swagger_parser.py
Normal file
@@ -0,0 +1,196 @@
|
||||
# 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/>.
|
||||
#
|
||||
|
||||
import copy
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from ansible.module_utils.network.ftd.common import HTTPMethod
|
||||
from ansible.module_utils.network.ftd.fdm_swagger_client import FdmSwaggerParser
|
||||
|
||||
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
|
||||
TEST_DATA_FOLDER = os.path.join(DIR_PATH, 'test_data')
|
||||
|
||||
base = {
|
||||
'basePath': "/api/fdm/v2",
|
||||
'definitions': {"NetworkObject": {"type": "object",
|
||||
"properties": {"version": {"type": "string"}, "name": {"type": "string"},
|
||||
"description": {"type": "string"},
|
||||
"subType": {"type": "object",
|
||||
"$ref": "#/definitions/NetworkObjectType"},
|
||||
"value": {"type": "string"},
|
||||
"isSystemDefined": {"type": "boolean"},
|
||||
"dnsResolution": {"type": "object",
|
||||
"$ref": "#/definitions/FQDNDNSResolution"},
|
||||
"id": {"type": "string"},
|
||||
"type": {"type": "string", "default": "networkobject"}},
|
||||
"required": ["subType", "type", "value"]},
|
||||
"NetworkObjectWrapper": {
|
||||
"allOf": [{"$ref": "#/definitions/NetworkObject"}, {"$ref": "#/definitions/LinksWrapper"}]}
|
||||
},
|
||||
'paths': {
|
||||
"/object/networks": {
|
||||
"get": {"tags": ["NetworkObject"], "operationId": "getNetworkObjectList",
|
||||
"responses": {"200": {"description": "", "schema": {"type": "object",
|
||||
"title": "NetworkObjectList",
|
||||
"properties": {"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/NetworkObjectWrapper"}},
|
||||
"paging": {
|
||||
"$ref": "#/definitions/Paging"}},
|
||||
"required": ["items",
|
||||
"paging"]}}},
|
||||
"parameters": [
|
||||
{"name": "offset", "in": "query", "required": False, "type": "integer"},
|
||||
{"name": "limit", "in": "query", "required": False, "type": "integer"},
|
||||
{"name": "sort", "in": "query", "required": False, "type": "string"},
|
||||
{"name": "filter", "in": "query", "required": False, "type": "string"}]},
|
||||
"post": {"tags": ["NetworkObject"], "operationId": "addNetworkObject",
|
||||
"responses": {
|
||||
"200": {"description": "",
|
||||
"schema": {"type": "object",
|
||||
"$ref": "#/definitions/NetworkObjectWrapper"}},
|
||||
"422": {"description": "",
|
||||
"schema": {"type": "object", "$ref": "#/definitions/ErrorWrapper"}}},
|
||||
"parameters": [{"in": "body", "name": "body",
|
||||
"required": True,
|
||||
"schema": {"$ref": "#/definitions/NetworkObject"}}]}
|
||||
},
|
||||
"/object/networks/{objId}": {
|
||||
"get": {"tags": ["NetworkObject"], "operationId": "getNetworkObject",
|
||||
"responses": {"200": {"description": "",
|
||||
"schema": {"type": "object",
|
||||
"$ref": "#/definitions/NetworkObjectWrapper"}},
|
||||
"404": {"description": "",
|
||||
"schema": {"type": "object",
|
||||
"$ref": "#/definitions/ErrorWrapper"}}},
|
||||
"parameters": [{"name": "objId", "in": "path", "required": True,
|
||||
"type": "string"}]},
|
||||
|
||||
"put": {"tags": ["NetworkObject"], "operationId": "editNetworkObject",
|
||||
"responses": {"200": {"description": "",
|
||||
"schema": {"type": "object",
|
||||
"$ref": "#/definitions/NetworkObjectWrapper"}},
|
||||
"422": {"description": "",
|
||||
"schema": {"type": "object",
|
||||
"$ref": "#/definitions/ErrorWrapper"}}},
|
||||
"parameters": [{"name": "objId", "in": "path", "required": True,
|
||||
"type": "string"},
|
||||
{"in": "body", "name": "body", "required": True,
|
||||
"schema": {"$ref": "#/definitions/NetworkObject"}}]},
|
||||
"delete": {"tags": ["NetworkObject"], "operationId": "deleteNetworkObject",
|
||||
"responses": {"204": {"description": ""},
|
||||
"422": {"description": "",
|
||||
"schema": {"type": "object",
|
||||
"$ref": "#/definitions/ErrorWrapper"}}},
|
||||
"parameters": [{"name": "objId", "in": "path", "required": True,
|
||||
"type": "string"}]}}}
|
||||
}
|
||||
|
||||
|
||||
def _get_objects(base_object, key_names):
|
||||
return dict((_key, base_object[_key]) for _key in key_names)
|
||||
|
||||
|
||||
class TestFdmSwaggerParser(unittest.TestCase):
|
||||
|
||||
def test_simple_object(self):
|
||||
self._data = copy.deepcopy(base)
|
||||
|
||||
self.fdm_data = FdmSwaggerParser().parse_spec(self._data)
|
||||
|
||||
expected_operations = {
|
||||
'getNetworkObjectList': {
|
||||
'method': HTTPMethod.GET,
|
||||
'url': '/api/fdm/v2/object/networks',
|
||||
'modelName': 'NetworkObject',
|
||||
'parameters': {
|
||||
'path': {},
|
||||
'query': {
|
||||
'offset': {
|
||||
'required': False,
|
||||
'type': 'integer'
|
||||
},
|
||||
'limit': {
|
||||
'required': False,
|
||||
'type': 'integer'
|
||||
},
|
||||
'sort': {
|
||||
'required': False,
|
||||
'type': 'string'
|
||||
},
|
||||
'filter': {
|
||||
'required': False,
|
||||
'type': 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
'addNetworkObject': {
|
||||
'method': HTTPMethod.POST,
|
||||
'url': '/api/fdm/v2/object/networks',
|
||||
'modelName': 'NetworkObject',
|
||||
'parameters': {'path': {},
|
||||
'query': {}}
|
||||
},
|
||||
'getNetworkObject': {
|
||||
'method': HTTPMethod.GET,
|
||||
'url': '/api/fdm/v2/object/networks/{objId}',
|
||||
'modelName': 'NetworkObject',
|
||||
'parameters': {
|
||||
'path': {
|
||||
'objId': {
|
||||
'required': True,
|
||||
'type': "string"
|
||||
}
|
||||
},
|
||||
'query': {}
|
||||
}
|
||||
},
|
||||
'editNetworkObject': {
|
||||
'method': HTTPMethod.PUT,
|
||||
'url': '/api/fdm/v2/object/networks/{objId}',
|
||||
'modelName': 'NetworkObject',
|
||||
'parameters': {
|
||||
'path': {
|
||||
'objId': {
|
||||
'required': True,
|
||||
'type': "string"
|
||||
}
|
||||
},
|
||||
'query': {}
|
||||
}
|
||||
},
|
||||
'deleteNetworkObject': {
|
||||
'method': HTTPMethod.DELETE,
|
||||
'url': '/api/fdm/v2/object/networks/{objId}',
|
||||
'modelName': None,
|
||||
'parameters': {
|
||||
'path': {
|
||||
'objId': {
|
||||
'required': True,
|
||||
'type': "string"
|
||||
}
|
||||
},
|
||||
'query': {}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert sorted(['NetworkObject', 'NetworkObjectWrapper']) == sorted(self.fdm_data['models'].keys())
|
||||
assert expected_operations == self.fdm_data['operations']
|
||||
1082
test/units/module_utils/network/ftd/test_fdm_swagger_validator.py
Normal file
1082
test/units/module_utils/network/ftd/test_fdm_swagger_validator.py
Normal file
File diff suppressed because it is too large
Load Diff
0
test/units/modules/network/ftd/__init__.py
Normal file
0
test/units/modules/network/ftd/__init__.py
Normal file
345
test/units/modules/network/ftd/test_ftd_configuration.py
Normal file
345
test/units/modules/network/ftd/test_ftd_configuration.py
Normal file
@@ -0,0 +1,345 @@
|
||||
# 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 json
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible.module_utils import basic
|
||||
from ansible.module_utils.network.ftd.common import HTTPMethod, FtdConfigurationError, FtdServerError
|
||||
from ansible.modules.network.ftd import ftd_configuration
|
||||
from units.modules.utils import set_module_args, exit_json, fail_json, AnsibleFailJson, AnsibleExitJson
|
||||
|
||||
ADD_RESPONSE = {'status': 'Object added'}
|
||||
EDIT_RESPONSE = {'status': 'Object edited'}
|
||||
DELETE_RESPONSE = {'status': 'Object deleted'}
|
||||
GET_BY_FILTER_RESPONSE = [{'name': 'foo', 'description': 'bar'}]
|
||||
ARBITRARY_RESPONSE = {'status': 'Arbitrary request sent'}
|
||||
|
||||
|
||||
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
|
||||
def connection_mock(self, mocker):
|
||||
connection_class_mock = mocker.patch('ansible.modules.network.ftd.ftd_configuration.Connection')
|
||||
connection_instance = connection_class_mock.return_value
|
||||
connection_instance.validate_data.return_value = True, None
|
||||
connection_instance.validate_query_params.return_value = True, None
|
||||
connection_instance.validate_path_params.return_value = True, None
|
||||
|
||||
return connection_instance
|
||||
|
||||
@pytest.fixture
|
||||
def resource_mock(self, mocker):
|
||||
resource_class_mock = mocker.patch('ansible.modules.network.ftd.ftd_configuration.BaseConfigurationResource')
|
||||
resource_instance = resource_class_mock.return_value
|
||||
resource_instance.add_object.return_value = ADD_RESPONSE
|
||||
resource_instance.edit_object.return_value = EDIT_RESPONSE
|
||||
resource_instance.delete_object.return_value = DELETE_RESPONSE
|
||||
resource_instance.send_request.return_value = ARBITRARY_RESPONSE
|
||||
resource_instance.get_objects_by_filter.return_value = GET_BY_FILTER_RESPONSE
|
||||
return resource_instance
|
||||
|
||||
def test_module_should_fail_without_operation_arg(self):
|
||||
set_module_args({})
|
||||
|
||||
with pytest.raises(AnsibleFailJson) as ex:
|
||||
self.module.main()
|
||||
|
||||
assert 'missing required arguments: operation' in str(ex)
|
||||
|
||||
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': 'nonExistingOperation'})
|
||||
|
||||
with pytest.raises(AnsibleFailJson) as ex:
|
||||
self.module.main()
|
||||
|
||||
assert 'Invalid operation name provided: nonExistingOperation' in str(ex)
|
||||
|
||||
def test_module_should_add_object_when_add_operation(self, connection_mock, resource_mock):
|
||||
connection_mock.get_operation_spec.return_value = {
|
||||
'method': HTTPMethod.POST,
|
||||
'url': '/object'
|
||||
}
|
||||
|
||||
params = {
|
||||
'operation': 'addObject',
|
||||
'data': {'name': 'testObject', 'type': 'object'}
|
||||
}
|
||||
result = self._run_module(params)
|
||||
|
||||
assert ADD_RESPONSE == result['response']
|
||||
resource_mock.add_object.assert_called_with(connection_mock.get_operation_spec.return_value['url'],
|
||||
params['data'], None, None)
|
||||
|
||||
def test_module_should_edit_object_when_edit_operation(self, connection_mock, resource_mock):
|
||||
connection_mock.get_operation_spec.return_value = {
|
||||
'method': HTTPMethod.PUT,
|
||||
'url': '/object/{objId}'
|
||||
}
|
||||
|
||||
params = {
|
||||
'operation': 'editObject',
|
||||
'data': {'id': '123', 'name': 'testObject', 'type': 'object'},
|
||||
'path_params': {'objId': '123'}
|
||||
}
|
||||
result = self._run_module(params)
|
||||
|
||||
assert EDIT_RESPONSE == result['response']
|
||||
resource_mock.edit_object.assert_called_with(connection_mock.get_operation_spec.return_value['url'],
|
||||
params['data'],
|
||||
params['path_params'], None)
|
||||
|
||||
def test_module_should_delete_object_when_delete_operation(self, connection_mock, resource_mock):
|
||||
connection_mock.get_operation_spec.return_value = {
|
||||
'method': HTTPMethod.DELETE,
|
||||
'url': '/object/{objId}'
|
||||
}
|
||||
|
||||
params = {
|
||||
'operation': 'deleteObject',
|
||||
'path_params': {'objId': '123'}
|
||||
}
|
||||
result = self._run_module(params)
|
||||
|
||||
assert DELETE_RESPONSE == result['response']
|
||||
resource_mock.delete_object.assert_called_with(connection_mock.get_operation_spec.return_value['url'],
|
||||
params['path_params'])
|
||||
|
||||
def test_module_should_get_objects_by_filter_when_find_by_filter_operation(self, connection_mock, resource_mock):
|
||||
connection_mock.get_operation_spec.return_value = {
|
||||
'method': HTTPMethod.GET,
|
||||
'url': '/objects'
|
||||
}
|
||||
|
||||
params = {
|
||||
'operation': 'getObjectList',
|
||||
'filters': {'name': 'foo'}
|
||||
}
|
||||
result = self._run_module(params)
|
||||
|
||||
assert GET_BY_FILTER_RESPONSE == result['response']
|
||||
resource_mock.get_objects_by_filter.assert_called_with(connection_mock.get_operation_spec.return_value['url'],
|
||||
params['filters'],
|
||||
None, None)
|
||||
|
||||
def test_module_should_send_request_when_arbitrary_operation(self, connection_mock, resource_mock):
|
||||
connection_mock.get_operation_spec.return_value = {
|
||||
'method': HTTPMethod.GET,
|
||||
'url': '/object/status/{objId}'
|
||||
}
|
||||
|
||||
params = {
|
||||
'operation': 'checkStatus',
|
||||
'path_params': {'objId': '123'}
|
||||
}
|
||||
result = self._run_module(params)
|
||||
|
||||
assert ARBITRARY_RESPONSE == result['response']
|
||||
resource_mock.send_request.assert_called_with(connection_mock.get_operation_spec.return_value['url'],
|
||||
HTTPMethod.GET, None,
|
||||
params['path_params'], None)
|
||||
|
||||
def test_module_should_fail_when_operation_raises_configuration_error(self, connection_mock, resource_mock):
|
||||
connection_mock.get_operation_spec.return_value = {'method': HTTPMethod.GET, 'url': '/test'}
|
||||
resource_mock.send_request.side_effect = FtdConfigurationError('Foo error.')
|
||||
|
||||
result = self._run_module_with_fail_json({'operation': 'failure'})
|
||||
assert result['failed']
|
||||
assert 'Failed to execute failure operation because of the configuration error: Foo error.' == result['msg']
|
||||
|
||||
def test_module_should_fail_when_operation_raises_server_error(self, connection_mock, resource_mock):
|
||||
connection_mock.get_operation_spec.return_value = {'method': HTTPMethod.GET, 'url': '/test'}
|
||||
resource_mock.send_request.side_effect = FtdServerError({'error': 'foo'}, 500)
|
||||
|
||||
result = self._run_module_with_fail_json({'operation': 'failure'})
|
||||
assert result['failed']
|
||||
assert 'Server returned an error trying to execute failure operation. Status code: 500. ' \
|
||||
'Server response: {\'error\': \'foo\'}' == result['msg']
|
||||
|
||||
def test_module_should_fail_if_validation_error_in_data(self, connection_mock):
|
||||
connection_mock.get_operation_spec.return_value = {'method': HTTPMethod.POST, 'url': '/test'}
|
||||
report = {
|
||||
'required': ['objects[0].type'],
|
||||
'invalid_type': [
|
||||
{
|
||||
'path': 'objects[3].id',
|
||||
'expected_type': 'string',
|
||||
'actually_value': 1
|
||||
}
|
||||
]
|
||||
}
|
||||
connection_mock.validate_data.return_value = (False, json.dumps(report, sort_keys=True, indent=4))
|
||||
|
||||
result = self._run_module_with_fail_json({
|
||||
'operation': 'test',
|
||||
'data': {}
|
||||
})
|
||||
key = 'Invalid data provided'
|
||||
assert result['msg'][key]
|
||||
result['msg'][key] = json.loads(result['msg'][key])
|
||||
assert result == {
|
||||
'msg':
|
||||
{key: {
|
||||
'invalid_type': [{'actually_value': 1, 'expected_type': 'string', 'path': 'objects[3].id'}],
|
||||
'required': ['objects[0].type']
|
||||
}},
|
||||
'failed': True}
|
||||
|
||||
def test_module_should_fail_if_validation_error_in_query_params(self, connection_mock):
|
||||
connection_mock.get_operation_spec.return_value = {'method': HTTPMethod.GET, 'url': '/test'}
|
||||
report = {
|
||||
'required': ['objects[0].type'],
|
||||
'invalid_type': [
|
||||
{
|
||||
'path': 'objects[3].id',
|
||||
'expected_type': 'string',
|
||||
'actually_value': 1
|
||||
}
|
||||
]
|
||||
}
|
||||
connection_mock.validate_query_params.return_value = (False, json.dumps(report, sort_keys=True, indent=4))
|
||||
|
||||
result = self._run_module_with_fail_json({
|
||||
'operation': 'test',
|
||||
'data': {}
|
||||
})
|
||||
key = 'Invalid query_params provided'
|
||||
assert result['msg'][key]
|
||||
result['msg'][key] = json.loads(result['msg'][key])
|
||||
|
||||
assert result == {'msg': {key: {
|
||||
'invalid_type': [{'actually_value': 1, 'expected_type': 'string', 'path': 'objects[3].id'}],
|
||||
'required': ['objects[0].type']}}, 'failed': True}
|
||||
|
||||
def test_module_should_fail_if_validation_error_in_path_params(self, connection_mock):
|
||||
connection_mock.get_operation_spec.return_value = {'method': HTTPMethod.GET, 'url': '/test'}
|
||||
report = {
|
||||
'path_params': {
|
||||
'required': ['objects[0].type'],
|
||||
'invalid_type': [
|
||||
{
|
||||
'path': 'objects[3].id',
|
||||
'expected_type': 'string',
|
||||
'actually_value': 1
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
connection_mock.validate_path_params.return_value = (False, json.dumps(report, sort_keys=True, indent=4))
|
||||
|
||||
result = self._run_module_with_fail_json({
|
||||
'operation': 'test',
|
||||
'data': {}
|
||||
})
|
||||
key = 'Invalid path_params provided'
|
||||
assert result['msg'][key]
|
||||
result['msg'][key] = json.loads(result['msg'][key])
|
||||
|
||||
assert result == {'msg': {key: {
|
||||
'path_params': {
|
||||
'invalid_type': [{'actually_value': 1, 'expected_type': 'string', 'path': 'objects[3].id'}],
|
||||
'required': ['objects[0].type']}}}, 'failed': True}
|
||||
|
||||
def test_module_should_fail_if_validation_error_in_all_params(self, connection_mock):
|
||||
connection_mock.get_operation_spec.return_value = {'method': HTTPMethod.POST, 'url': '/test'}
|
||||
report = {
|
||||
'data': {
|
||||
'required': ['objects[0].type'],
|
||||
'invalid_type': [
|
||||
{
|
||||
'path': 'objects[3].id',
|
||||
'expected_type': 'string',
|
||||
'actually_value': 1
|
||||
}
|
||||
]
|
||||
},
|
||||
'path_params': {
|
||||
'required': ['some_param'],
|
||||
'invalid_type': [
|
||||
{
|
||||
'path': 'name',
|
||||
'expected_type': 'string',
|
||||
'actually_value': True
|
||||
}
|
||||
]
|
||||
},
|
||||
'query_params': {
|
||||
'required': ['other_param'],
|
||||
'invalid_type': [
|
||||
{
|
||||
'path': 'f_integer',
|
||||
'expected_type': 'integer',
|
||||
'actually_value': "test"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
connection_mock.validate_data.return_value = (False, json.dumps(report['data'], sort_keys=True, indent=4))
|
||||
connection_mock.validate_query_params.return_value = (False,
|
||||
json.dumps(report['query_params'], sort_keys=True,
|
||||
indent=4))
|
||||
connection_mock.validate_path_params.return_value = (False,
|
||||
json.dumps(report['path_params'], sort_keys=True,
|
||||
indent=4))
|
||||
|
||||
result = self._run_module_with_fail_json({
|
||||
'operation': 'test',
|
||||
'data': {}
|
||||
})
|
||||
key_data = 'Invalid data provided'
|
||||
assert result['msg'][key_data]
|
||||
result['msg'][key_data] = json.loads(result['msg'][key_data])
|
||||
|
||||
key_path_params = 'Invalid path_params provided'
|
||||
assert result['msg'][key_path_params]
|
||||
result['msg'][key_path_params] = json.loads(result['msg'][key_path_params])
|
||||
|
||||
key_query_params = 'Invalid query_params provided'
|
||||
assert result['msg'][key_query_params]
|
||||
result['msg'][key_query_params] = json.loads(result['msg'][key_query_params])
|
||||
|
||||
assert result == {'msg': {
|
||||
key_data: {'invalid_type': [{'actually_value': 1, 'expected_type': 'string', 'path': 'objects[3].id'}],
|
||||
'required': ['objects[0].type']},
|
||||
key_path_params: {'invalid_type': [{'actually_value': True, 'expected_type': 'string', 'path': 'name'}],
|
||||
'required': ['some_param']},
|
||||
key_query_params: {
|
||||
'invalid_type': [{'actually_value': 'test', 'expected_type': 'integer', 'path': 'f_integer'}],
|
||||
'required': ['other_param']}}, 'failed': True}
|
||||
|
||||
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
|
||||
98
test/units/modules/network/ftd/test_ftd_file_download.py
Normal file
98
test/units/modules/network/ftd/test_ftd_file_download.py
Normal 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.module_utils.network.ftd.common import HTTPMethod
|
||||
from ansible.module_utils.network.ftd.fdm_swagger_client import FILE_MODEL_NAME, OperationField
|
||||
from ansible.modules.network.ftd import ftd_file_download
|
||||
from units.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.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)
|
||||
|
||||
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'})
|
||||
98
test/units/modules/network/ftd/test_ftd_file_upload.py
Normal file
98
test/units/modules/network/ftd/test_ftd_file_upload.py
Normal 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.module_utils.network.ftd.common import HTTPMethod
|
||||
from ansible.module_utils.network.ftd.fdm_swagger_client import OperationField
|
||||
from ansible.modules.network.ftd import ftd_file_upload
|
||||
from units.modules.utils import set_module_args, exit_json, fail_json, AnsibleFailJson, AnsibleExitJson
|
||||
|
||||
|
||||
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.modules.network.ftd.ftd_file_upload.Connection')
|
||||
return connection_class_mock.return_value
|
||||
|
||||
@pytest.mark.parametrize("missing_arg", ['operation', 'fileToUpload'])
|
||||
def test_module_should_fail_without_required_args(self, missing_arg):
|
||||
module_args = {'operation': 'uploadFile', 'fileToUpload': '/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)
|
||||
|
||||
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', 'fileToUpload': '/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', 'fileToUpload': '/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',
|
||||
'fileToUpload': '/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')
|
||||
0
test/units/plugins/httpapi/__init__.py
Normal file
0
test/units/plugins/httpapi/__init__.py
Normal file
255
test/units/plugins/httpapi/test_ftd.py
Normal file
255
test/units/plugins/httpapi/test_ftd.py
Normal file
@@ -0,0 +1,255 @@
|
||||
# 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/>.
|
||||
#
|
||||
|
||||
import json
|
||||
import os
|
||||
|
||||
from ansible.module_utils.six.moves.urllib.error import HTTPError
|
||||
|
||||
from ansible.compat.tests import mock
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import mock_open, patch
|
||||
from ansible.errors import AnsibleConnectionFailure
|
||||
from ansible.module_utils.connection import ConnectionError
|
||||
from ansible.module_utils.network.ftd.common import HTTPMethod, ResponseParams
|
||||
from ansible.module_utils.network.ftd.fdm_swagger_client import SpecProp, FdmSwaggerParser
|
||||
from ansible.module_utils.six import BytesIO, PY3, StringIO
|
||||
from ansible.plugins.httpapi.ftd import HttpApi, API_TOKEN_PATH_ENV_VAR
|
||||
|
||||
if PY3:
|
||||
BUILTINS_NAME = 'builtins'
|
||||
else:
|
||||
BUILTINS_NAME = '__builtin__'
|
||||
|
||||
|
||||
class TestFtdHttpApi(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.connection_mock = mock.Mock()
|
||||
self.ftd_plugin = HttpApi(self.connection_mock)
|
||||
self.ftd_plugin.access_token = 'ACCESS_TOKEN'
|
||||
|
||||
def test_login_should_request_tokens_when_no_refresh_token(self):
|
||||
self.connection_mock.send.return_value = self._connection_response(
|
||||
{'access_token': 'ACCESS_TOKEN', 'refresh_token': 'REFRESH_TOKEN'}
|
||||
)
|
||||
|
||||
self.ftd_plugin.login('foo', 'bar')
|
||||
|
||||
assert 'ACCESS_TOKEN' == self.ftd_plugin.access_token
|
||||
assert 'REFRESH_TOKEN' == self.ftd_plugin.refresh_token
|
||||
expected_body = json.dumps({'grant_type': 'password', 'username': 'foo', 'password': 'bar'})
|
||||
self.connection_mock.send.assert_called_once_with(mock.ANY, expected_body, headers=mock.ANY, method=mock.ANY)
|
||||
|
||||
def test_login_should_update_tokens_when_refresh_token_exists(self):
|
||||
self.ftd_plugin.refresh_token = 'REFRESH_TOKEN'
|
||||
self.connection_mock.send.return_value = self._connection_response(
|
||||
{'access_token': 'NEW_ACCESS_TOKEN', 'refresh_token': 'NEW_REFRESH_TOKEN'}
|
||||
)
|
||||
|
||||
self.ftd_plugin.login('foo', 'bar')
|
||||
|
||||
assert 'NEW_ACCESS_TOKEN' == self.ftd_plugin.access_token
|
||||
assert 'NEW_REFRESH_TOKEN' == self.ftd_plugin.refresh_token
|
||||
expected_body = json.dumps({'grant_type': 'refresh_token', 'refresh_token': 'REFRESH_TOKEN'})
|
||||
self.connection_mock.send.assert_called_once_with(mock.ANY, expected_body, headers=mock.ANY, method=mock.ANY)
|
||||
|
||||
@patch.dict(os.environ, {API_TOKEN_PATH_ENV_VAR: '/testLoginUrl'})
|
||||
def test_login_should_use_env_variable_when_set(self):
|
||||
self.connection_mock.send.return_value = self._connection_response(
|
||||
{'access_token': 'ACCESS_TOKEN', 'refresh_token': 'REFRESH_TOKEN'}
|
||||
)
|
||||
|
||||
self.ftd_plugin.login('foo', 'bar')
|
||||
|
||||
self.connection_mock.send.assert_called_once_with('/testLoginUrl', mock.ANY, headers=mock.ANY, method=mock.ANY)
|
||||
|
||||
def test_login_raises_exception_when_no_refresh_token_and_no_credentials(self):
|
||||
with self.assertRaises(AnsibleConnectionFailure) as res:
|
||||
self.ftd_plugin.login(None, None)
|
||||
assert 'Username and password are required' in str(res.exception)
|
||||
|
||||
def test_login_raises_exception_when_invalid_response(self):
|
||||
self.connection_mock.send.return_value = self._connection_response(
|
||||
{'no_access_token': 'ACCESS_TOKEN'}
|
||||
)
|
||||
|
||||
with self.assertRaises(ConnectionError) as res:
|
||||
self.ftd_plugin.login('foo', 'bar')
|
||||
|
||||
assert 'Server returned response without token info during connection authentication' in str(res.exception)
|
||||
|
||||
def test_logout_should_revoke_tokens(self):
|
||||
self.ftd_plugin.access_token = 'ACCESS_TOKEN_TO_REVOKE'
|
||||
self.ftd_plugin.refresh_token = 'REFRESH_TOKEN_TO_REVOKE'
|
||||
self.connection_mock.send.return_value = self._connection_response(None)
|
||||
|
||||
self.ftd_plugin.logout()
|
||||
|
||||
assert self.ftd_plugin.access_token is None
|
||||
assert self.ftd_plugin.refresh_token is None
|
||||
expected_body = json.dumps({'grant_type': 'revoke_token', 'access_token': 'ACCESS_TOKEN_TO_REVOKE',
|
||||
'token_to_revoke': 'REFRESH_TOKEN_TO_REVOKE'})
|
||||
self.connection_mock.send.assert_called_once_with(mock.ANY, expected_body, headers=mock.ANY, method=mock.ANY)
|
||||
|
||||
def test_send_request_should_send_correct_request(self):
|
||||
exp_resp = {'id': '123', 'name': 'foo'}
|
||||
self.connection_mock.send.return_value = self._connection_response(exp_resp)
|
||||
|
||||
resp = self.ftd_plugin.send_request('/test/{objId}', HTTPMethod.PUT,
|
||||
body_params={'name': 'foo'},
|
||||
path_params={'objId': '123'},
|
||||
query_params={'at': 0})
|
||||
|
||||
assert {ResponseParams.SUCCESS: True, ResponseParams.STATUS_CODE: 200,
|
||||
ResponseParams.RESPONSE: exp_resp} == resp
|
||||
self.connection_mock.send.assert_called_once_with('/test/123?at=0', '{"name": "foo"}', method=HTTPMethod.PUT,
|
||||
headers=self._expected_headers())
|
||||
|
||||
def test_send_request_should_return_empty_dict_when_no_response_data(self):
|
||||
self.connection_mock.send.return_value = self._connection_response(None)
|
||||
|
||||
resp = self.ftd_plugin.send_request('/test', HTTPMethod.GET)
|
||||
|
||||
assert {ResponseParams.SUCCESS: True, ResponseParams.STATUS_CODE: 200, ResponseParams.RESPONSE: {}} == resp
|
||||
self.connection_mock.send.assert_called_once_with('/test', None, method=HTTPMethod.GET,
|
||||
headers=self._expected_headers())
|
||||
|
||||
def test_send_request_should_return_error_info_when_http_error_raises(self):
|
||||
self.connection_mock.send.side_effect = HTTPError('http://testhost.com', 500, '', {},
|
||||
StringIO('{"errorMessage": "ERROR"}'))
|
||||
|
||||
resp = self.ftd_plugin.send_request('/test', HTTPMethod.GET)
|
||||
|
||||
assert {ResponseParams.SUCCESS: False, ResponseParams.STATUS_CODE: 500,
|
||||
ResponseParams.RESPONSE: {'errorMessage': 'ERROR'}} == resp
|
||||
|
||||
def test_send_request_raises_exception_when_invalid_response(self):
|
||||
self.connection_mock.send.return_value = self._connection_response('nonValidJson')
|
||||
|
||||
with self.assertRaises(ConnectionError) as res:
|
||||
self.ftd_plugin.send_request('/test', HTTPMethod.GET)
|
||||
|
||||
assert 'Invalid JSON response' in str(res.exception)
|
||||
|
||||
def test_handle_httperror_should_update_tokens_and_retry_on_auth_errors(self):
|
||||
self.ftd_plugin.refresh_token = 'REFRESH_TOKEN'
|
||||
self.connection_mock.send.return_value = self._connection_response(
|
||||
{'access_token': 'NEW_ACCESS_TOKEN', 'refresh_token': 'NEW_REFRESH_TOKEN'}
|
||||
)
|
||||
|
||||
retry = self.ftd_plugin.handle_httperror(HTTPError('http://testhost.com', 401, '', {}, None))
|
||||
|
||||
assert retry
|
||||
assert 'NEW_ACCESS_TOKEN' == self.ftd_plugin.access_token
|
||||
assert 'NEW_REFRESH_TOKEN' == self.ftd_plugin.refresh_token
|
||||
|
||||
def test_handle_httperror_should_not_retry_on_non_auth_errors(self):
|
||||
assert not self.ftd_plugin.handle_httperror(HTTPError('http://testhost.com', 500, '', {}, None))
|
||||
|
||||
@patch('os.path.isdir', mock.Mock(return_value=False))
|
||||
def test_download_file(self):
|
||||
self.connection_mock.send.return_value = self._connection_response('File content')
|
||||
|
||||
open_mock = mock_open()
|
||||
with patch('%s.open' % BUILTINS_NAME, open_mock):
|
||||
self.ftd_plugin.download_file('/files/1', '/tmp/test.txt')
|
||||
|
||||
open_mock.assert_called_once_with('/tmp/test.txt', 'wb')
|
||||
open_mock().write.assert_called_once_with(b'File content')
|
||||
|
||||
@patch('os.path.isdir', mock.Mock(return_value=True))
|
||||
def test_download_file_should_extract_filename_from_headers(self):
|
||||
filename = 'test_file.txt'
|
||||
response = mock.Mock()
|
||||
response.info.return_value = {'Content-Disposition': 'attachment; filename="%s"' % filename}
|
||||
dummy, response_data = self._connection_response('File content')
|
||||
self.connection_mock.send.return_value = response, response_data
|
||||
|
||||
open_mock = mock_open()
|
||||
with patch('%s.open' % BUILTINS_NAME, open_mock):
|
||||
self.ftd_plugin.download_file('/files/1', '/tmp/')
|
||||
|
||||
open_mock.assert_called_once_with('/tmp/%s' % filename, 'wb')
|
||||
open_mock().write.assert_called_once_with(b'File content')
|
||||
|
||||
@patch('os.path.basename', mock.Mock(return_value='test.txt'))
|
||||
@patch('ansible.plugins.httpapi.ftd.encode_multipart_formdata',
|
||||
mock.Mock(return_value=('--Encoded data--', 'multipart/form-data')))
|
||||
def test_upload_file(self):
|
||||
self.connection_mock.send.return_value = self._connection_response({'id': '123'})
|
||||
|
||||
open_mock = mock_open()
|
||||
with patch('%s.open' % BUILTINS_NAME, open_mock):
|
||||
resp = self.ftd_plugin.upload_file('/tmp/test.txt', '/files')
|
||||
|
||||
assert {'id': '123'} == resp
|
||||
exp_headers = self._expected_headers()
|
||||
exp_headers['Content-Length'] = len('--Encoded data--')
|
||||
exp_headers['Content-Type'] = 'multipart/form-data'
|
||||
self.connection_mock.send.assert_called_once_with('/files', data='--Encoded data--',
|
||||
headers=exp_headers, method=HTTPMethod.POST)
|
||||
open_mock.assert_called_once_with('/tmp/test.txt', 'rb')
|
||||
|
||||
@patch('os.path.basename', mock.Mock(return_value='test.txt'))
|
||||
@patch('ansible.plugins.httpapi.ftd.encode_multipart_formdata',
|
||||
mock.Mock(return_value=('--Encoded data--', 'multipart/form-data')))
|
||||
def test_upload_file_raises_exception_when_invalid_response(self):
|
||||
self.connection_mock.send.return_value = self._connection_response('invalidJsonResponse')
|
||||
|
||||
open_mock = mock_open()
|
||||
with patch('%s.open' % BUILTINS_NAME, open_mock):
|
||||
with self.assertRaises(ConnectionError) as res:
|
||||
self.ftd_plugin.upload_file('/tmp/test.txt', '/files')
|
||||
|
||||
assert 'Invalid JSON response' in str(res.exception)
|
||||
|
||||
@patch.object(FdmSwaggerParser, 'parse_spec')
|
||||
def test_get_operation_spec(self, parse_spec_mock):
|
||||
self.connection_mock.send.return_value = self._connection_response(None)
|
||||
parse_spec_mock.return_value = {
|
||||
SpecProp.OPERATIONS: {'testOp': 'Specification for testOp'}
|
||||
}
|
||||
|
||||
assert 'Specification for testOp' == self.ftd_plugin.get_operation_spec('testOp')
|
||||
assert self.ftd_plugin.get_operation_spec('nonExistingTestOp') is None
|
||||
|
||||
@patch.object(FdmSwaggerParser, 'parse_spec')
|
||||
def test_get_model_spec(self, parse_spec_mock):
|
||||
self.connection_mock.send.return_value = self._connection_response(None)
|
||||
parse_spec_mock.return_value = {
|
||||
SpecProp.MODELS: {'TestModel': 'Specification for TestModel'}
|
||||
}
|
||||
|
||||
assert 'Specification for TestModel' == self.ftd_plugin.get_model_spec('TestModel')
|
||||
assert self.ftd_plugin.get_model_spec('NonExistingTestModel') is None
|
||||
|
||||
@staticmethod
|
||||
def _connection_response(response, status=200):
|
||||
response_mock = mock.Mock()
|
||||
response_mock.getcode.return_value = status
|
||||
response_text = json.dumps(response) if type(response) is dict else response
|
||||
response_data = BytesIO(response_text.encode() if response_text else ''.encode())
|
||||
return response_mock, response_data
|
||||
|
||||
def _expected_headers(self):
|
||||
return {
|
||||
'Accept': 'application/json',
|
||||
'Authorization': 'Bearer %s' % self.ftd_plugin.access_token,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
Reference in New Issue
Block a user