mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-17 19:41:43 -07: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:
parent
ab3c2120fb
commit
be191cce6c
1170 changed files with 732 additions and 751 deletions
0
tests/unit/plugins/modules/remote_management/__init__.py
Normal file
0
tests/unit/plugins/modules/remote_management/__init__.py
Normal file
|
@ -0,0 +1,195 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
#
|
||||
# Dell EMC OpenManage Ansible Modules
|
||||
# Version 2.0
|
||||
# Copyright (C) 2019 Dell Inc.
|
||||
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# All rights reserved. Dell, EMC, and other trademarks are trademarks of Dell Inc. or its subsidiaries.
|
||||
# Other trademarks may be trademarks of their respective owners.
|
||||
#
|
||||
|
||||
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.modules.remote_management.dellemc import ome_device_info
|
||||
from ansible.module_utils.six.moves.urllib.error import HTTPError
|
||||
|
||||
default_args = {'hostname': '192.168.0.1', 'username': 'username', 'password': 'password'}
|
||||
resource_basic_inventory = {"basic_inventory": "DeviceService/Devices"}
|
||||
resource_detailed_inventory = {"detailed_inventory:": {"device_id": {1234: None},
|
||||
"device_service_tag": {1345: "MXL1234"}}}
|
||||
|
||||
|
||||
class TestOmeDeviceInfo(object):
|
||||
module = ome_device_info
|
||||
|
||||
@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.remote_management.dellemc.ome_device_info.RestOME')
|
||||
return connection_class_mock.return_value
|
||||
|
||||
@pytest.fixture
|
||||
def response_mock(self, mocker):
|
||||
response_class_mock = mocker.patch('ansible_collections.community.general.plugins.module_utils.remote_management.dellemc.ome.OpenURLResponse')
|
||||
return response_class_mock
|
||||
|
||||
@pytest.fixture
|
||||
def validate_inputs_mock(self, mocker):
|
||||
response_class_mock = mocker.patch('ansible_collections.community.general.plugins.modules.remote_management.dellemc.ome_device_info._validate_inputs')
|
||||
response_class_mock.return_value = None
|
||||
|
||||
@pytest.fixture
|
||||
def get_device_identifier_map_mock(self, mocker):
|
||||
response_class_mock = mocker.patch(
|
||||
'ansible_collections.community.general.plugins.modules.remote_management.dellemc.ome_device_info._get_device_identifier_map'
|
||||
)
|
||||
response_class_mock.return_value = resource_detailed_inventory
|
||||
return response_class_mock.return_value
|
||||
|
||||
@pytest.fixture
|
||||
def get_resource_parameters_mock(self, mocker):
|
||||
response_class_mock = mocker.patch(
|
||||
'ansible_collections.community.general.plugins.modules.remote_management.dellemc.ome_device_info._get_resource_parameters'
|
||||
)
|
||||
return response_class_mock
|
||||
|
||||
def test_main_basic_inventory_success_case(self, module_mock, validate_inputs_mock, connection_mock, get_resource_parameters_mock, response_mock):
|
||||
get_resource_parameters_mock.return_value = resource_basic_inventory
|
||||
connection_mock.__enter__.return_value = connection_mock
|
||||
connection_mock.invoke_request.return_value = response_mock
|
||||
response_mock.json_data = {"value": [{"device_id1": "details", "device_id2": "details"}]}
|
||||
response_mock.status_code = 200
|
||||
result = self._run_module(default_args)
|
||||
assert result['changed'] is False
|
||||
assert 'device_info' in result
|
||||
|
||||
def test_main_basic_inventory_failure_case(self, module_mock, validate_inputs_mock, connection_mock, get_resource_parameters_mock, response_mock):
|
||||
get_resource_parameters_mock.return_value = resource_basic_inventory
|
||||
connection_mock.__enter__.return_value = connection_mock
|
||||
connection_mock.invoke_request.return_value = response_mock
|
||||
response_mock.status_code = 500
|
||||
result = self._run_module_with_fail_json(default_args)
|
||||
assert result['msg'] == 'Failed to fetch the device information'
|
||||
|
||||
def test_main_detailed_inventory_success_case(self, module_mock, validate_inputs_mock, connection_mock, get_resource_parameters_mock, response_mock):
|
||||
default_args.update({"fact_subset": "detailed_inventory", "system_query_options": {"device_id": [1234], "device_service_tag": ["MXL1234"]}})
|
||||
detailed_inventory = {"detailed_inventory:": {"device_id": {1234: "DeviceService/Devices(1234)/InventoryDetails"},
|
||||
"device_service_tag": {"MXL1234": "DeviceService/Devices(4321)/InventoryDetails"}}}
|
||||
get_resource_parameters_mock.return_value = detailed_inventory
|
||||
connection_mock.__enter__.return_value = connection_mock
|
||||
connection_mock.invoke_request.return_value = response_mock
|
||||
response_mock.json_data = {"value": [{"device_id": {"1234": "details"}}, {"device_service_tag": {"MXL1234": "details"}}]}
|
||||
response_mock.status_code = 200
|
||||
result = self._run_module(default_args)
|
||||
assert result['changed'] is False
|
||||
assert 'device_info' in result
|
||||
|
||||
def test_main_HTTPError_error_case(self, module_mock, validate_inputs_mock, connection_mock, get_resource_parameters_mock, response_mock):
|
||||
get_resource_parameters_mock.return_value = resource_basic_inventory
|
||||
connection_mock.__enter__.return_value = connection_mock
|
||||
connection_mock.invoke_request.side_effect = HTTPError('http://testhost.com', 400, '', {}, None)
|
||||
response_mock.json_data = {"value": [{"device_id1": "details", "device_id2": "details"}]}
|
||||
response_mock.status_code = 400
|
||||
result = self._run_module_with_fail_json(default_args)
|
||||
assert 'device_info' not in result
|
||||
assert result['failed'] is True
|
||||
|
||||
@pytest.mark.parametrize("fact_subset, mutually_exclusive_call", [("basic_inventory", False), ("detailed_inventory", True)])
|
||||
def test_validate_inputs(self, fact_subset, mutually_exclusive_call, mocker):
|
||||
module_params = {"fact_subset": fact_subset}
|
||||
check_mutually_inclusive_arguments_mock = mocker.patch(
|
||||
'ansible_collections.community.general.plugins.modules.remote_management.dellemc.ome_device_info._check_mutually_inclusive_arguments')
|
||||
check_mutually_inclusive_arguments_mock.return_value = None
|
||||
self.module._validate_inputs(module_params)
|
||||
if mutually_exclusive_call:
|
||||
check_mutually_inclusive_arguments_mock.assert_called()
|
||||
else:
|
||||
check_mutually_inclusive_arguments_mock.assert_not_called()
|
||||
check_mutually_inclusive_arguments_mock.reset_mock()
|
||||
|
||||
system_query_options_params = [{"system_query_options": None}, {"system_query_options": {"device_id": None}},
|
||||
{"system_query_options": {"device_service_tag": None}}]
|
||||
|
||||
@pytest.mark.parametrize("system_query_options_params", system_query_options_params)
|
||||
def test_check_mutually_inclusive_arguments(self, system_query_options_params):
|
||||
module_params = {"fact_subset": "subsystem_health"}
|
||||
required_args = ["device_id", "device_service_tag"]
|
||||
module_params.update(system_query_options_params)
|
||||
with pytest.raises(ValueError) as ex:
|
||||
self.module._check_mutually_inclusive_arguments(module_params["fact_subset"], module_params, ["device_id", "device_service_tag"])
|
||||
assert "One of the following {0} is required for {1}".format(required_args, module_params["fact_subset"]) == str(ex.value)
|
||||
|
||||
params = [{"fact_subset": "basic_inventory", "system_query_options": {"device_id": [1234]}},
|
||||
{"fact_subset": "subsystem_health", "system_query_options": {"device_service_tag": ["MXL1234"]}},
|
||||
{"fact_subset": "detailed_inventory", "system_query_options": {"device_id": [1234], "inventory_type": "serverDeviceCards"}}]
|
||||
|
||||
@pytest.mark.parametrize("module_params", params)
|
||||
def test_get_resource_parameters(self, module_params, connection_mock):
|
||||
self.module._get_resource_parameters(module_params, connection_mock)
|
||||
|
||||
@pytest.mark.parametrize("module_params,data", [({"system_query_options": None}, None), ({"system_query_options": {"fileter": None}}, None),
|
||||
({"system_query_options": {"filter": "abc"}}, "$filter")])
|
||||
def test_get_query_parameters(self, module_params, data):
|
||||
res = self.module._get_query_parameters(module_params)
|
||||
if data is not None:
|
||||
assert data in res
|
||||
else:
|
||||
assert res is None
|
||||
|
||||
@pytest.mark.parametrize("module_params", params)
|
||||
def test_get_device_identifier_map(self, module_params, connection_mock, mocker):
|
||||
get_device_id_from_service_tags_mock = mocker.patch(
|
||||
'ansible_collections.community.general.plugins.modules.remote_management.dellemc.ome_device_info._get_device_id_from_service_tags'
|
||||
)
|
||||
get_device_id_from_service_tags_mock.return_value = None
|
||||
res = self.module._get_device_identifier_map(module_params, connection_mock)
|
||||
assert isinstance(res, dict)
|
||||
|
||||
def test_check_duplicate_device_id(self):
|
||||
self.module._check_duplicate_device_id([1234], {1234: "MX1234"})
|
||||
assert self.module.device_fact_error_report["MX1234"] == "Duplicate report of device_id: 1234"
|
||||
|
||||
@pytest.mark.parametrize("val,expected_res", [(123, True), ("abc", False)])
|
||||
def test_is_int(self, val, expected_res):
|
||||
actual_res = self.module.is_int(val)
|
||||
assert actual_res == expected_res
|
||||
|
||||
def test_get_device_id_from_service_tags(self, connection_mock, response_mock):
|
||||
connection_mock.__enter__.return_value = connection_mock
|
||||
connection_mock.invoke_request.return_value = response_mock
|
||||
response_mock.json_data = {"value": [{"DeviceServiceTag": "MX1234", "Id": 1234}]}
|
||||
response_mock.status_code = 200
|
||||
response_mock.success = True
|
||||
self.module._get_device_id_from_service_tags(["MX1234", "INVALID"], connection_mock)
|
||||
|
||||
def test_get_device_id_from_service_tags_error_case(self, connection_mock, response_mock):
|
||||
connection_mock.__enter__.return_value = connection_mock
|
||||
connection_mock.invoke_request.side_effect = HTTPError('http://testhost.com',
|
||||
400, '', {}, None)
|
||||
response_mock.json_data = {"value": [{"DeviceServiceTag": "MX1234", "Id": 1234}]}
|
||||
response_mock.status_code = 200
|
||||
response_mock.success = True
|
||||
with pytest.raises(HTTPError) as ex:
|
||||
self.module._get_device_id_from_service_tags(["INVALID"], connection_mock)
|
||||
|
||||
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
|
|
@ -0,0 +1,94 @@
|
|||
import json
|
||||
|
||||
import pytest
|
||||
from ansible_collections.community.general.tests.unit.compat import mock
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.lxca import lxca_cmms
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
@mock.patch('ansible_collections.community.general.plugins.module_utils.remote_management.lxca.common.close_conn', autospec=True)
|
||||
def setup_module(close_conn):
|
||||
close_conn.return_value = True
|
||||
|
||||
|
||||
class TestMyModule():
|
||||
@pytest.mark.parametrize('patch_ansible_module',
|
||||
[
|
||||
{},
|
||||
{
|
||||
"auth_url": "https://10.240.14.195",
|
||||
"login_user": "USERID",
|
||||
},
|
||||
{
|
||||
"auth_url": "https://10.240.14.195",
|
||||
"login_password": "Password",
|
||||
},
|
||||
{
|
||||
"login_user": "USERID",
|
||||
"login_password": "Password",
|
||||
},
|
||||
],
|
||||
indirect=['patch_ansible_module'])
|
||||
@pytest.mark.usefixtures('patch_ansible_module')
|
||||
@mock.patch('ansible_collections.community.general.plugins.module_utils.remote_management.lxca.common.setup_conn', autospec=True)
|
||||
@mock.patch('ansible_collections.community.general.plugins.modules.remote_management.lxca.lxca_cmms.execute_module', autospec=True)
|
||||
def test_without_required_parameters(self, _setup_conn, _execute_module,
|
||||
mocker, capfd, setup_module):
|
||||
"""Failure must occurs when all parameters are missing"""
|
||||
with pytest.raises(SystemExit):
|
||||
_setup_conn.return_value = "Fake connection"
|
||||
_execute_module.return_value = "Fake execution"
|
||||
lxca_cmms.main()
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
assert results['failed']
|
||||
assert 'missing required arguments' in results['msg']
|
||||
|
||||
@mock.patch('ansible_collections.community.general.plugins.module_utils.remote_management.lxca.common.setup_conn', autospec=True)
|
||||
@mock.patch('ansible_collections.community.general.plugins.modules.remote_management.lxca.lxca_cmms.execute_module', autospec=True)
|
||||
@mock.patch('ansible_collections.community.general.plugins.modules.remote_management.lxca.lxca_cmms.AnsibleModule', autospec=True)
|
||||
def test__argument_spec(self, ansible_mod_cls, _execute_module, _setup_conn, setup_module):
|
||||
expected_arguments_spec = dict(
|
||||
login_user=dict(required=True),
|
||||
login_password=dict(required=True, no_log=True),
|
||||
command_options=dict(default='cmms', choices=['cmms', 'cmms_by_uuid',
|
||||
'cmms_by_chassis_uuid']),
|
||||
auth_url=dict(required=True),
|
||||
uuid=dict(default=None),
|
||||
chassis=dict(default=None),
|
||||
)
|
||||
_setup_conn.return_value = "Fake connection"
|
||||
_execute_module.return_value = []
|
||||
mod_obj = ansible_mod_cls.return_value
|
||||
args = {
|
||||
"auth_url": "https://10.243.30.195",
|
||||
"login_user": "USERID",
|
||||
"login_password": "password",
|
||||
"command_options": "cmms",
|
||||
}
|
||||
mod_obj.params = args
|
||||
lxca_cmms.main()
|
||||
assert(mock.call(argument_spec=expected_arguments_spec,
|
||||
supports_check_mode=False) == ansible_mod_cls.call_args)
|
||||
|
||||
@mock.patch('ansible_collections.community.general.plugins.module_utils.remote_management.lxca.common.setup_conn', autospec=True)
|
||||
@mock.patch('ansible_collections.community.general.plugins.modules.remote_management.lxca.lxca_cmms._cmms_by_uuid',
|
||||
autospec=True)
|
||||
@mock.patch('ansible_collections.community.general.plugins.modules.remote_management.lxca.lxca_cmms.AnsibleModule',
|
||||
autospec=True)
|
||||
def test__cmms_empty_list(self, ansible_mod_cls, _get_cmms, _setup_conn, setup_module):
|
||||
mod_obj = ansible_mod_cls.return_value
|
||||
args = {
|
||||
"auth_url": "https://10.243.30.195",
|
||||
"login_user": "USERID",
|
||||
"login_password": "password",
|
||||
"uuid": "3C737AA5E31640CE949B10C129A8B01F",
|
||||
"command_options": "cmms_by_uuid",
|
||||
}
|
||||
mod_obj.params = args
|
||||
_setup_conn.return_value = "Fake connection"
|
||||
empty_nodes_list = []
|
||||
_get_cmms.return_value = empty_nodes_list
|
||||
ret_cmms = _get_cmms(mod_obj, args)
|
||||
assert mock.call(mod_obj, mod_obj.params) == _get_cmms.call_args
|
||||
assert _get_cmms.return_value == ret_cmms
|
|
@ -0,0 +1,98 @@
|
|||
import json
|
||||
|
||||
import pytest
|
||||
from ansible_collections.community.general.tests.unit.compat import mock
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.lxca import lxca_nodes
|
||||
from ansible_collections.community.general.plugins.module_utils.remote_management.lxca.common import setup_conn
|
||||
from ansible_collections.community.general.plugins.module_utils.remote_management.lxca.common import close_conn
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
@mock.patch('ansible_collections.community.general.plugins.module_utils.remote_management.lxca.common.close_conn', autospec=True)
|
||||
def setup_module(close_conn):
|
||||
close_conn.return_value = True
|
||||
|
||||
|
||||
class TestMyModule():
|
||||
@pytest.mark.parametrize('patch_ansible_module',
|
||||
[
|
||||
{},
|
||||
{
|
||||
"auth_url": "https://10.240.14.195",
|
||||
"login_user": "USERID",
|
||||
},
|
||||
{
|
||||
"auth_url": "https://10.240.14.195",
|
||||
"login_password": "Password",
|
||||
},
|
||||
{
|
||||
"login_user": "USERID",
|
||||
"login_password": "Password",
|
||||
},
|
||||
],
|
||||
indirect=['patch_ansible_module'])
|
||||
@pytest.mark.usefixtures('patch_ansible_module')
|
||||
@mock.patch('ansible_collections.community.general.plugins.module_utils.remote_management.lxca.common.setup_conn', autospec=True)
|
||||
@mock.patch('ansible_collections.community.general.plugins.modules.remote_management.lxca.lxca_nodes.execute_module', autospec=True)
|
||||
def test_without_required_parameters(self, _setup_conn, _execute_module,
|
||||
mocker, capfd, setup_module):
|
||||
"""Failure must occurs when all parameters are missing"""
|
||||
with pytest.raises(SystemExit):
|
||||
_setup_conn.return_value = "Fake connection"
|
||||
_execute_module.return_value = "Fake execution"
|
||||
lxca_nodes.main()
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
assert results['failed']
|
||||
assert 'missing required arguments' in results['msg']
|
||||
|
||||
@mock.patch('ansible_collections.community.general.plugins.module_utils.remote_management.lxca.common.setup_conn', autospec=True)
|
||||
@mock.patch('ansible_collections.community.general.plugins.modules.remote_management.lxca.lxca_nodes.execute_module', autospec=True)
|
||||
@mock.patch('ansible_collections.community.general.plugins.modules.remote_management.lxca.lxca_nodes.AnsibleModule', autospec=True)
|
||||
def test__argument_spec(self, ansible_mod_cls, _execute_module, _setup_conn, setup_module):
|
||||
expected_arguments_spec = dict(
|
||||
login_user=dict(required=True),
|
||||
login_password=dict(required=True, no_log=True),
|
||||
command_options=dict(default='nodes', choices=['nodes', 'nodes_by_uuid',
|
||||
'nodes_by_chassis_uuid',
|
||||
'nodes_status_managed',
|
||||
'nodes_status_unmanaged']),
|
||||
auth_url=dict(required=True),
|
||||
uuid=dict(default=None),
|
||||
chassis=dict(default=None),
|
||||
)
|
||||
_setup_conn.return_value = "Fake connection"
|
||||
_execute_module.return_value = []
|
||||
mod_obj = ansible_mod_cls.return_value
|
||||
args = {
|
||||
"auth_url": "https://10.243.30.195",
|
||||
"login_user": "USERID",
|
||||
"login_password": "password",
|
||||
"command_options": "nodes",
|
||||
}
|
||||
mod_obj.params = args
|
||||
lxca_nodes.main()
|
||||
assert(mock.call(argument_spec=expected_arguments_spec,
|
||||
supports_check_mode=False) == ansible_mod_cls.call_args)
|
||||
|
||||
@mock.patch('ansible_collections.community.general.plugins.module_utils.remote_management.lxca.common.setup_conn', autospec=True)
|
||||
@mock.patch('ansible_collections.community.general.plugins.modules.remote_management.lxca.lxca_nodes._nodes_by_uuid',
|
||||
autospec=True)
|
||||
@mock.patch('ansible_collections.community.general.plugins.modules.remote_management.lxca.lxca_nodes.AnsibleModule',
|
||||
autospec=True)
|
||||
def test__nodes_empty_list(self, ansible_mod_cls, _get_nodes, _setup_conn, setup_module):
|
||||
mod_obj = ansible_mod_cls.return_value
|
||||
args = {
|
||||
"auth_url": "https://10.243.30.195",
|
||||
"login_user": "USERID",
|
||||
"login_password": "password",
|
||||
"uuid": "3C737AA5E31640CE949B10C129A8B01F",
|
||||
"command_options": "nodes_by_uuid",
|
||||
}
|
||||
mod_obj.params = args
|
||||
_setup_conn.return_value = "Fake connection"
|
||||
empty_nodes_list = []
|
||||
_get_nodes.return_value = empty_nodes_list
|
||||
ret_nodes = _get_nodes(mod_obj, args)
|
||||
assert mock.call(mod_obj, mod_obj.params) == _get_nodes.call_args
|
||||
assert _get_nodes.return_value == ret_nodes
|
|
@ -0,0 +1,23 @@
|
|||
# Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
import pytest
|
||||
|
||||
from mock import Mock, patch
|
||||
from .oneview_module_loader import ONEVIEW_MODULE_UTILS_PATH
|
||||
from hpOneView.oneview_client import OneViewClient
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_ov_client():
|
||||
patcher_json_file = patch.object(OneViewClient, 'from_json_file')
|
||||
client = patcher_json_file.start()
|
||||
return client.return_value
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_ansible_module():
|
||||
patcher_ansible = patch(ONEVIEW_MODULE_UTILS_PATH + '.AnsibleModule')
|
||||
patcher_ansible = patcher_ansible.start()
|
||||
ansible_module = Mock()
|
||||
patcher_ansible.return_value = ansible_module
|
||||
return ansible_module
|
|
@ -0,0 +1,214 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (2016-2017) Hewlett Packard Enterprise Development LP
|
||||
#
|
||||
# This program 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.
|
||||
#
|
||||
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import pytest
|
||||
import re
|
||||
import yaml
|
||||
|
||||
from mock import Mock, patch
|
||||
from .oneview_module_loader import ONEVIEW_MODULE_UTILS_PATH
|
||||
from hpOneView.oneview_client import OneViewClient
|
||||
|
||||
|
||||
class OneViewBaseTest(object):
|
||||
@pytest.fixture(autouse=True)
|
||||
def setUp(self, mock_ansible_module, mock_ov_client, request):
|
||||
marker = request.node.get_marker('resource')
|
||||
self.resource = getattr(mock_ov_client, "%s" % (marker.args))
|
||||
self.mock_ov_client = mock_ov_client
|
||||
self.mock_ansible_module = mock_ansible_module
|
||||
|
||||
@pytest.fixture
|
||||
def testing_module(self):
|
||||
resource_name = type(self).__name__.replace('Test', '')
|
||||
resource_module_path_name = resource_name.replace('Module', '')
|
||||
resource_module_path_name = re.findall('[A-Z][^A-Z]*', resource_module_path_name)
|
||||
resource_module_path_name = 'oneview_' + str.join('_', resource_module_path_name).lower()
|
||||
|
||||
ansible_collections = __import__('ansible_collections')
|
||||
oneview_module = ansible_collections.community.general.plugins.modules.remote_management.oneview
|
||||
resource_module = getattr(oneview_module, resource_module_path_name)
|
||||
self.testing_class = getattr(resource_module, resource_name)
|
||||
testing_module = self.testing_class.__module__.split('.')[-1]
|
||||
testing_module = getattr(oneview_module, testing_module)
|
||||
try:
|
||||
# Load scenarios from module examples (Also checks if it is a valid yaml)
|
||||
EXAMPLES = yaml.load(testing_module.EXAMPLES, yaml.SafeLoader)
|
||||
|
||||
except yaml.scanner.ScannerError:
|
||||
message = "Something went wrong while parsing yaml from {0}.EXAMPLES".format(self.testing_class.__module__)
|
||||
raise Exception(message)
|
||||
return testing_module
|
||||
|
||||
def test_main_function_should_call_run_method(self, testing_module, mock_ansible_module):
|
||||
mock_ansible_module.params = {'config': 'config.json'}
|
||||
|
||||
main_func = getattr(testing_module, 'main')
|
||||
|
||||
with patch.object(self.testing_class, "run") as mock_run:
|
||||
main_func()
|
||||
mock_run.assert_called_once()
|
||||
|
||||
|
||||
class FactsParamsTest(OneViewBaseTest):
|
||||
def test_should_get_all_using_filters(self, testing_module):
|
||||
self.resource.get_all.return_value = []
|
||||
|
||||
params_get_all_with_filters = dict(
|
||||
config='config.json',
|
||||
name=None,
|
||||
params={
|
||||
'start': 1,
|
||||
'count': 3,
|
||||
'sort': 'name:descending',
|
||||
'filter': 'purpose=General',
|
||||
'query': 'imported eq true'
|
||||
})
|
||||
self.mock_ansible_module.params = params_get_all_with_filters
|
||||
|
||||
self.testing_class().run()
|
||||
|
||||
self.resource.get_all.assert_called_once_with(start=1, count=3, sort='name:descending', filter='purpose=General', query='imported eq true')
|
||||
|
||||
def test_should_get_all_without_params(self, testing_module):
|
||||
self.resource.get_all.return_value = []
|
||||
|
||||
params_get_all_with_filters = dict(
|
||||
config='config.json',
|
||||
name=None
|
||||
)
|
||||
self.mock_ansible_module.params = params_get_all_with_filters
|
||||
|
||||
self.testing_class().run()
|
||||
|
||||
self.resource.get_all.assert_called_once_with()
|
||||
|
||||
|
||||
class OneViewBaseTestCase(object):
|
||||
mock_ov_client_from_json_file = None
|
||||
testing_class = None
|
||||
mock_ansible_module = None
|
||||
mock_ov_client = None
|
||||
testing_module = None
|
||||
EXAMPLES = None
|
||||
|
||||
def configure_mocks(self, test_case, testing_class):
|
||||
"""
|
||||
Preload mocked OneViewClient instance and AnsibleModule
|
||||
Args:
|
||||
test_case (object): class instance (self) that are inheriting from OneViewBaseTestCase
|
||||
testing_class (object): class being tested
|
||||
"""
|
||||
self.testing_class = testing_class
|
||||
|
||||
# Define OneView Client Mock (FILE)
|
||||
patcher_json_file = patch.object(OneViewClient, 'from_json_file')
|
||||
test_case.addCleanup(patcher_json_file.stop)
|
||||
self.mock_ov_client_from_json_file = patcher_json_file.start()
|
||||
|
||||
# Define OneView Client Mock
|
||||
self.mock_ov_client = self.mock_ov_client_from_json_file.return_value
|
||||
|
||||
# Define Ansible Module Mock
|
||||
patcher_ansible = patch(ONEVIEW_MODULE_UTILS_PATH + '.AnsibleModule')
|
||||
test_case.addCleanup(patcher_ansible.stop)
|
||||
mock_ansible_module = patcher_ansible.start()
|
||||
self.mock_ansible_module = Mock()
|
||||
mock_ansible_module.return_value = self.mock_ansible_module
|
||||
|
||||
self.__set_module_examples()
|
||||
|
||||
def test_main_function_should_call_run_method(self):
|
||||
self.mock_ansible_module.params = {'config': 'config.json'}
|
||||
|
||||
main_func = getattr(self.testing_module, 'main')
|
||||
|
||||
with patch.object(self.testing_class, "run") as mock_run:
|
||||
main_func()
|
||||
mock_run.assert_called_once()
|
||||
|
||||
def __set_module_examples(self):
|
||||
# Load scenarios from module examples (Also checks if it is a valid yaml)
|
||||
ansible_collections = __import__('ansible_collections')
|
||||
testing_module = self.testing_class.__module__.split('.')[-1]
|
||||
self.testing_module = getattr(ansible_collections.community.general.plugins.modules.remote_management.oneview, testing_module)
|
||||
|
||||
try:
|
||||
# Load scenarios from module examples (Also checks if it is a valid yaml)
|
||||
self.EXAMPLES = yaml.load(self.testing_module.EXAMPLES, yaml.SafeLoader)
|
||||
|
||||
except yaml.scanner.ScannerError:
|
||||
message = "Something went wrong while parsing yaml from {0}.EXAMPLES".format(self.testing_class.__module__)
|
||||
raise Exception(message)
|
||||
|
||||
|
||||
class FactsParamsTestCase(OneViewBaseTestCase):
|
||||
"""
|
||||
FactsParamsTestCase has common test for classes that support pass additional
|
||||
parameters when retrieving all resources.
|
||||
"""
|
||||
|
||||
def configure_client_mock(self, resorce_client):
|
||||
"""
|
||||
Args:
|
||||
resorce_client: Resource client that is being called
|
||||
"""
|
||||
self.resource_client = resorce_client
|
||||
|
||||
def __validations(self):
|
||||
if not self.testing_class:
|
||||
raise Exception("Mocks are not configured, you must call 'configure_mocks' before running this test.")
|
||||
|
||||
if not self.resource_client:
|
||||
raise Exception(
|
||||
"Mock for the client not configured, you must call 'configure_client_mock' before running this test.")
|
||||
|
||||
def test_should_get_all_using_filters(self):
|
||||
self.__validations()
|
||||
self.resource_client.get_all.return_value = []
|
||||
|
||||
params_get_all_with_filters = dict(
|
||||
config='config.json',
|
||||
name=None,
|
||||
params={
|
||||
'start': 1,
|
||||
'count': 3,
|
||||
'sort': 'name:descending',
|
||||
'filter': 'purpose=General',
|
||||
'query': 'imported eq true'
|
||||
})
|
||||
self.mock_ansible_module.params = params_get_all_with_filters
|
||||
|
||||
self.testing_class().run()
|
||||
|
||||
self.resource_client.get_all.assert_called_once_with(start=1, count=3, sort='name:descending',
|
||||
filter='purpose=General',
|
||||
query='imported eq true')
|
||||
|
||||
def test_should_get_all_without_params(self):
|
||||
self.__validations()
|
||||
self.resource_client.get_all.return_value = []
|
||||
|
||||
params_get_all_with_filters = dict(
|
||||
config='config.json',
|
||||
name=None
|
||||
)
|
||||
self.mock_ansible_module.params = params_get_all_with_filters
|
||||
|
||||
self.testing_class().run()
|
||||
|
||||
self.resource_client.get_all.assert_called_once_with()
|
|
@ -0,0 +1,27 @@
|
|||
# Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
import sys
|
||||
from ansible_collections.community.general.tests.unit.compat.mock import Mock
|
||||
|
||||
# FIXME: These should be done inside of a fixture so that they're only mocked during
|
||||
# these unittests
|
||||
sys.modules['hpOneView'] = Mock()
|
||||
sys.modules['hpOneView.oneview_client'] = Mock()
|
||||
|
||||
ONEVIEW_MODULE_UTILS_PATH = 'ansible_collections.community.general.plugins.module_utils.oneview'
|
||||
from ansible_collections.community.general.plugins.module_utils.oneview import (OneViewModuleException,
|
||||
OneViewModuleTaskError,
|
||||
OneViewModuleResourceNotFound,
|
||||
OneViewModuleBase)
|
||||
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.oneview.oneview_ethernet_network import EthernetNetworkModule
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.oneview.oneview_ethernet_network_info import EthernetNetworkInfoModule
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.oneview.oneview_fc_network import FcNetworkModule
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.oneview.oneview_fc_network_info import FcNetworkInfoModule
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.oneview.oneview_fcoe_network import FcoeNetworkModule
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.oneview.oneview_fcoe_network_info import FcoeNetworkInfoModule
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.oneview.oneview_network_set import NetworkSetModule
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.oneview.oneview_network_set_info import NetworkSetInfoModule
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.oneview.oneview_san_manager import SanManagerModule
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.oneview.oneview_san_manager_info import SanManagerInfoModule
|
|
@ -0,0 +1,74 @@
|
|||
# Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.oneview.oneview_datacenter_info import DatacenterInfoModule
|
||||
from .hpe_test_utils import FactsParamsTest
|
||||
|
||||
PARAMS_GET_CONNECTED = dict(
|
||||
config='config.json',
|
||||
name="MyDatacenter",
|
||||
options=['visualContent']
|
||||
)
|
||||
|
||||
|
||||
class TestDatacenterInfoModule(FactsParamsTest):
|
||||
@pytest.fixture(autouse=True)
|
||||
def setUp(self, mock_ansible_module, mock_ov_client):
|
||||
self.resource = mock_ov_client.datacenters
|
||||
self.mock_ansible_module = mock_ansible_module
|
||||
self.mock_ov_client = mock_ov_client
|
||||
|
||||
def test_should_get_all_datacenters(self):
|
||||
self.resource.get_all.return_value = {"name": "Data Center Name"}
|
||||
|
||||
self.mock_ansible_module.params = dict(config='config.json',)
|
||||
|
||||
DatacenterInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
datacenters=({"name": "Data Center Name"})
|
||||
)
|
||||
|
||||
def test_should_get_datacenter_by_name(self):
|
||||
self.resource.get_by.return_value = [{"name": "Data Center Name"}]
|
||||
|
||||
self.mock_ansible_module.params = dict(config='config.json', name="MyDatacenter")
|
||||
|
||||
DatacenterInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
datacenters=([{"name": "Data Center Name"}])
|
||||
)
|
||||
|
||||
def test_should_get_datacenter_visual_content(self):
|
||||
self.resource.get_by.return_value = [{"name": "Data Center Name", "uri": "/rest/datacenter/id"}]
|
||||
|
||||
self.resource.get_visual_content.return_value = {
|
||||
"name": "Visual Content"}
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_GET_CONNECTED
|
||||
|
||||
DatacenterInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
datacenter_visual_content={'name': 'Visual Content'},
|
||||
datacenters=[{'name': 'Data Center Name', 'uri': '/rest/datacenter/id'}]
|
||||
)
|
||||
|
||||
def test_should_get_none_datacenter_visual_content(self):
|
||||
self.resource.get_by.return_value = []
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_GET_CONNECTED
|
||||
|
||||
DatacenterInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
datacenter_visual_content=None,
|
||||
datacenters=[]
|
||||
)
|
|
@ -0,0 +1,132 @@
|
|||
# Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.oneview.oneview_enclosure_info import EnclosureInfoModule
|
||||
from .hpe_test_utils import FactsParamsTestCase
|
||||
|
||||
|
||||
ERROR_MSG = 'Fake message error'
|
||||
|
||||
PARAMS_GET_ALL = dict(
|
||||
config='config.json',
|
||||
name=None
|
||||
)
|
||||
|
||||
PARAMS_GET_BY_NAME = dict(
|
||||
config='config.json',
|
||||
name="Test-Enclosure",
|
||||
options=[]
|
||||
)
|
||||
|
||||
PARAMS_GET_BY_NAME_WITH_OPTIONS = dict(
|
||||
config='config.json',
|
||||
name="Test-Enclosure",
|
||||
options=['utilization', 'environmentalConfiguration', 'script']
|
||||
)
|
||||
|
||||
PARAMS_GET_UTILIZATION_WITH_PARAMS = dict(
|
||||
config='config.json',
|
||||
name="Test-Enclosure",
|
||||
options=[dict(utilization=dict(fields='AveragePower',
|
||||
filter=['startDate=2016-06-30T03:29:42.000Z',
|
||||
'endDate=2016-07-01T03:29:42.000Z'],
|
||||
view='day',
|
||||
refresh=True))]
|
||||
)
|
||||
|
||||
PRESENT_ENCLOSURES = [{
|
||||
"name": "Test-Enclosure",
|
||||
"uri": "/rest/enclosures/c6bf9af9-48e7-4236-b08a-77684dc258a5"
|
||||
}]
|
||||
|
||||
ENCLOSURE_SCRIPT = '# script content'
|
||||
|
||||
ENCLOSURE_UTILIZATION = {
|
||||
"isFresh": "True"
|
||||
}
|
||||
|
||||
ENCLOSURE_ENVIRONMENTAL_CONFIG = {
|
||||
"calibratedMaxPower": "2500"
|
||||
}
|
||||
|
||||
|
||||
class EnclosureInfoSpec(unittest.TestCase,
|
||||
FactsParamsTestCase):
|
||||
def setUp(self):
|
||||
self.configure_mocks(self, EnclosureInfoModule)
|
||||
self.enclosures = self.mock_ov_client.enclosures
|
||||
FactsParamsTestCase.configure_client_mock(self, self.enclosures)
|
||||
|
||||
def test_should_get_all_enclosures(self):
|
||||
self.enclosures.get_all.return_value = PRESENT_ENCLOSURES
|
||||
self.mock_ansible_module.params = PARAMS_GET_ALL
|
||||
|
||||
EnclosureInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
enclosures=(PRESENT_ENCLOSURES)
|
||||
)
|
||||
|
||||
def test_should_get_enclosure_by_name(self):
|
||||
self.enclosures.get_by.return_value = PRESENT_ENCLOSURES
|
||||
self.mock_ansible_module.params = PARAMS_GET_BY_NAME
|
||||
|
||||
EnclosureInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
enclosures=(PRESENT_ENCLOSURES)
|
||||
|
||||
)
|
||||
|
||||
def test_should_get_enclosure_by_name_with_options(self):
|
||||
self.enclosures.get_by.return_value = PRESENT_ENCLOSURES
|
||||
self.enclosures.get_script.return_value = ENCLOSURE_SCRIPT
|
||||
self.enclosures.get_utilization.return_value = ENCLOSURE_UTILIZATION
|
||||
self.enclosures.get_environmental_configuration.return_value = ENCLOSURE_ENVIRONMENTAL_CONFIG
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_GET_BY_NAME_WITH_OPTIONS
|
||||
|
||||
EnclosureInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
enclosures=PRESENT_ENCLOSURES,
|
||||
enclosure_script=ENCLOSURE_SCRIPT,
|
||||
enclosure_environmental_configuration=ENCLOSURE_ENVIRONMENTAL_CONFIG,
|
||||
enclosure_utilization=ENCLOSURE_UTILIZATION
|
||||
)
|
||||
|
||||
def test_should_get_all_utilization_data(self):
|
||||
self.enclosures.get_by.return_value = PRESENT_ENCLOSURES
|
||||
self.enclosures.get_script.return_value = ENCLOSURE_SCRIPT
|
||||
self.enclosures.get_utilization.return_value = ENCLOSURE_UTILIZATION
|
||||
self.enclosures.get_environmental_configuration.return_value = ENCLOSURE_ENVIRONMENTAL_CONFIG
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_GET_BY_NAME_WITH_OPTIONS
|
||||
|
||||
EnclosureInfoModule().run()
|
||||
|
||||
self.enclosures.get_utilization.assert_called_once_with(PRESENT_ENCLOSURES[0]['uri'], fields='', filter='',
|
||||
view='', refresh='')
|
||||
|
||||
def test_should_get_utilization_with_parameters(self):
|
||||
self.enclosures.get_by.return_value = PRESENT_ENCLOSURES
|
||||
self.enclosures.get_script.return_value = ENCLOSURE_SCRIPT
|
||||
self.enclosures.get_utilization.return_value = ENCLOSURE_UTILIZATION
|
||||
self.enclosures.get_environmental_configuration.return_value = ENCLOSURE_ENVIRONMENTAL_CONFIG
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_GET_UTILIZATION_WITH_PARAMS
|
||||
|
||||
EnclosureInfoModule().run()
|
||||
|
||||
date_filter = ["startDate=2016-06-30T03:29:42.000Z", "endDate=2016-07-01T03:29:42.000Z"]
|
||||
|
||||
self.enclosures.get_utilization.assert_called_once_with(
|
||||
PRESENT_ENCLOSURES[0]['uri'], fields='AveragePower', filter=date_filter, view='day', refresh=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -0,0 +1,400 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (2016-2017) Hewlett Packard Enterprise Development LP
|
||||
#
|
||||
# This program 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.
|
||||
#
|
||||
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import yaml
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest, mock
|
||||
from .oneview_module_loader import EthernetNetworkModule
|
||||
from .hpe_test_utils import OneViewBaseTestCase
|
||||
|
||||
FAKE_MSG_ERROR = 'Fake message error'
|
||||
DEFAULT_ETHERNET_NAME = 'Test Ethernet Network'
|
||||
RENAMED_ETHERNET = 'Renamed Ethernet Network'
|
||||
|
||||
DEFAULT_ENET_TEMPLATE = dict(
|
||||
name=DEFAULT_ETHERNET_NAME,
|
||||
vlanId=200,
|
||||
ethernetNetworkType="Tagged",
|
||||
purpose="General",
|
||||
smartLink=False,
|
||||
privateNetwork=False,
|
||||
connectionTemplateUri=None
|
||||
)
|
||||
|
||||
PARAMS_FOR_PRESENT = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(name=DEFAULT_ETHERNET_NAME)
|
||||
)
|
||||
|
||||
PARAMS_TO_RENAME = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(name=DEFAULT_ETHERNET_NAME,
|
||||
newName=RENAMED_ETHERNET)
|
||||
)
|
||||
|
||||
YAML_PARAMS_WITH_CHANGES = """
|
||||
config: "config.json"
|
||||
state: present
|
||||
data:
|
||||
name: 'Test Ethernet Network'
|
||||
purpose: Management
|
||||
connectionTemplateUri: ~
|
||||
bandwidth:
|
||||
maximumBandwidth: 3000
|
||||
typicalBandwidth: 2000
|
||||
"""
|
||||
|
||||
YAML_RESET_CONNECTION_TEMPLATE = """
|
||||
config: "{{ config }}"
|
||||
state: default_bandwidth_reset
|
||||
data:
|
||||
name: 'network name'
|
||||
"""
|
||||
|
||||
PARAMS_FOR_SCOPES_SET = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(name=DEFAULT_ETHERNET_NAME)
|
||||
)
|
||||
|
||||
PARAMS_FOR_ABSENT = dict(
|
||||
config='config.json',
|
||||
state='absent',
|
||||
data=dict(name=DEFAULT_ETHERNET_NAME)
|
||||
)
|
||||
|
||||
PARAMS_FOR_BULK_CREATED = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(namePrefix="TestNetwork", vlanIdRange="1-2,5,9-10")
|
||||
)
|
||||
|
||||
DEFAULT_BULK_ENET_TEMPLATE = [
|
||||
{'name': 'TestNetwork_1', 'vlanId': 1},
|
||||
{'name': 'TestNetwork_2', 'vlanId': 2},
|
||||
{'name': 'TestNetwork_5', 'vlanId': 5},
|
||||
{'name': 'TestNetwork_9', 'vlanId': 9},
|
||||
{'name': 'TestNetwork_10', 'vlanId': 10},
|
||||
]
|
||||
|
||||
DICT_PARAMS_WITH_CHANGES = yaml.safe_load(YAML_PARAMS_WITH_CHANGES)["data"]
|
||||
|
||||
|
||||
class EthernetNetworkModuleSpec(unittest.TestCase,
|
||||
OneViewBaseTestCase):
|
||||
"""
|
||||
OneViewBaseTestCase provides the mocks used in this test case
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.configure_mocks(self, EthernetNetworkModule)
|
||||
self.resource = self.mock_ov_client.ethernet_networks
|
||||
|
||||
def test_should_create_new_ethernet_network(self):
|
||||
self.resource.get_by.return_value = []
|
||||
self.resource.create.return_value = DEFAULT_ENET_TEMPLATE
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_PRESENT
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=EthernetNetworkModule.MSG_CREATED,
|
||||
ansible_facts=dict(ethernet_network=DEFAULT_ENET_TEMPLATE)
|
||||
)
|
||||
|
||||
def test_should_not_update_when_data_is_equals(self):
|
||||
self.resource.get_by.return_value = [DEFAULT_ENET_TEMPLATE]
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_PRESENT
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
msg=EthernetNetworkModule.MSG_ALREADY_PRESENT,
|
||||
ansible_facts=dict(ethernet_network=DEFAULT_ENET_TEMPLATE)
|
||||
)
|
||||
|
||||
def test_update_when_data_has_modified_attributes(self):
|
||||
data_merged = DEFAULT_ENET_TEMPLATE.copy()
|
||||
data_merged['purpose'] = 'Management'
|
||||
|
||||
self.resource.get_by.return_value = [DEFAULT_ENET_TEMPLATE]
|
||||
self.resource.update.return_value = data_merged
|
||||
self.mock_ov_client.connection_templates.get.return_value = {"uri": "uri"}
|
||||
|
||||
self.mock_ansible_module.params = yaml.load(YAML_PARAMS_WITH_CHANGES)
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=EthernetNetworkModule.MSG_UPDATED,
|
||||
ansible_facts=dict(ethernet_network=data_merged)
|
||||
)
|
||||
|
||||
def test_update_when_only_bandwidth_has_modified_attributes(self):
|
||||
self.resource.get_by.return_value = [DICT_PARAMS_WITH_CHANGES]
|
||||
self.mock_ov_client.connection_templates.get.return_value = {"uri": "uri"}
|
||||
|
||||
self.mock_ansible_module.params = yaml.load(YAML_PARAMS_WITH_CHANGES)
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=EthernetNetworkModule.MSG_UPDATED,
|
||||
ansible_facts=dict(ethernet_network=DICT_PARAMS_WITH_CHANGES)
|
||||
)
|
||||
|
||||
def test_update_when_data_has_modified_attributes_but_bandwidth_is_equal(self):
|
||||
data_merged = DEFAULT_ENET_TEMPLATE.copy()
|
||||
data_merged['purpose'] = 'Management'
|
||||
|
||||
self.resource.get_by.return_value = [DEFAULT_ENET_TEMPLATE]
|
||||
self.resource.update.return_value = data_merged
|
||||
self.mock_ov_client.connection_templates.get.return_value = {
|
||||
"bandwidth": DICT_PARAMS_WITH_CHANGES['bandwidth']}
|
||||
|
||||
self.mock_ansible_module.params = yaml.load(YAML_PARAMS_WITH_CHANGES)
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=EthernetNetworkModule.MSG_UPDATED,
|
||||
ansible_facts=dict(ethernet_network=data_merged)
|
||||
)
|
||||
|
||||
def test_update_successfully_even_when_connection_template_uri_not_exists(self):
|
||||
data_merged = DEFAULT_ENET_TEMPLATE.copy()
|
||||
del data_merged['connectionTemplateUri']
|
||||
|
||||
self.resource.get_by.return_value = [DEFAULT_ENET_TEMPLATE]
|
||||
self.resource.update.return_value = data_merged
|
||||
|
||||
self.mock_ansible_module.params = yaml.load(YAML_PARAMS_WITH_CHANGES)
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=EthernetNetworkModule.MSG_UPDATED,
|
||||
ansible_facts=dict(ethernet_network=data_merged)
|
||||
)
|
||||
|
||||
def test_rename_when_resource_exists(self):
|
||||
data_merged = DEFAULT_ENET_TEMPLATE.copy()
|
||||
data_merged['name'] = RENAMED_ETHERNET
|
||||
params_to_rename = PARAMS_TO_RENAME.copy()
|
||||
|
||||
self.resource.get_by.return_value = [DEFAULT_ENET_TEMPLATE]
|
||||
self.resource.update.return_value = data_merged
|
||||
|
||||
self.mock_ansible_module.params = params_to_rename
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.resource.update.assert_called_once_with(data_merged)
|
||||
|
||||
def test_create_with_new_name_when_resource_not_exists(self):
|
||||
data_merged = DEFAULT_ENET_TEMPLATE.copy()
|
||||
data_merged['name'] = RENAMED_ETHERNET
|
||||
params_to_rename = PARAMS_TO_RENAME.copy()
|
||||
|
||||
self.resource.get_by.return_value = []
|
||||
self.resource.create.return_value = DEFAULT_ENET_TEMPLATE
|
||||
|
||||
self.mock_ansible_module.params = params_to_rename
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.resource.create.assert_called_once_with(PARAMS_TO_RENAME['data'])
|
||||
|
||||
def test_should_remove_ethernet_network(self):
|
||||
self.resource.get_by.return_value = [DEFAULT_ENET_TEMPLATE]
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_ABSENT
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=EthernetNetworkModule.MSG_DELETED
|
||||
)
|
||||
|
||||
def test_should_do_nothing_when_ethernet_network_not_exist(self):
|
||||
self.resource.get_by.return_value = []
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_ABSENT
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
msg=EthernetNetworkModule.MSG_ALREADY_ABSENT
|
||||
)
|
||||
|
||||
def test_should_create_all_ethernet_networks(self):
|
||||
self.resource.get_range.side_effect = [[], DEFAULT_BULK_ENET_TEMPLATE]
|
||||
self.resource.create_bulk.return_value = DEFAULT_BULK_ENET_TEMPLATE
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_BULK_CREATED
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.resource.create_bulk.assert_called_once_with(
|
||||
dict(namePrefix="TestNetwork", vlanIdRange="1-2,5,9-10"))
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=EthernetNetworkModule.MSG_BULK_CREATED,
|
||||
ansible_facts=dict(ethernet_network_bulk=DEFAULT_BULK_ENET_TEMPLATE))
|
||||
|
||||
def test_should_create_missing_ethernet_networks(self):
|
||||
enet_get_range_return = [
|
||||
{'name': 'TestNetwork_1', 'vlanId': 1},
|
||||
{'name': 'TestNetwork_2', 'vlanId': 2},
|
||||
]
|
||||
|
||||
self.resource.get_range.side_effect = [enet_get_range_return, DEFAULT_BULK_ENET_TEMPLATE]
|
||||
self.resource.dissociate_values_or_ranges.return_value = [1, 2, 5, 9, 10]
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_BULK_CREATED
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.resource.create_bulk.assert_called_once_with(
|
||||
dict(namePrefix="TestNetwork", vlanIdRange="5,9,10"))
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True, msg=EthernetNetworkModule.MSG_MISSING_BULK_CREATED,
|
||||
ansible_facts=dict(ethernet_network_bulk=DEFAULT_BULK_ENET_TEMPLATE))
|
||||
|
||||
def test_should_create_missing_ethernet_networks_with_just_one_difference(self):
|
||||
enet_get_range_return = [
|
||||
{'name': 'TestNetwork_1', 'vlanId': 1},
|
||||
{'name': 'TestNetwork_2', 'vlanId': 2},
|
||||
]
|
||||
|
||||
self.resource.get_range.side_effect = [enet_get_range_return, DEFAULT_BULK_ENET_TEMPLATE]
|
||||
self.resource.dissociate_values_or_ranges.return_value = [1, 2, 5]
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_BULK_CREATED
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.resource.create_bulk.assert_called_once_with({'vlanIdRange': '5-5', 'namePrefix': 'TestNetwork'})
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=EthernetNetworkModule.MSG_MISSING_BULK_CREATED,
|
||||
ansible_facts=dict(ethernet_network_bulk=DEFAULT_BULK_ENET_TEMPLATE))
|
||||
|
||||
def test_should_do_nothing_when_ethernet_networks_already_exist(self):
|
||||
self.resource.get_range.return_value = DEFAULT_BULK_ENET_TEMPLATE
|
||||
self.resource.dissociate_values_or_ranges.return_value = [1, 2, 5, 9, 10]
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_BULK_CREATED
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False, msg=EthernetNetworkModule.MSG_BULK_ALREADY_EXIST,
|
||||
ansible_facts=dict(ethernet_network_bulk=DEFAULT_BULK_ENET_TEMPLATE))
|
||||
|
||||
def test_reset_successfully(self):
|
||||
self.resource.get_by.return_value = [DICT_PARAMS_WITH_CHANGES]
|
||||
self.mock_ov_client.connection_templates.update.return_value = {'result': 'success'}
|
||||
self.mock_ov_client.connection_templates.get.return_value = {
|
||||
"bandwidth": DICT_PARAMS_WITH_CHANGES['bandwidth']}
|
||||
|
||||
self.mock_ov_client.connection_templates.get_default.return_value = {"bandwidth": {
|
||||
"max": 1
|
||||
}}
|
||||
|
||||
self.mock_ansible_module.params = yaml.load(YAML_RESET_CONNECTION_TEMPLATE)
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True, msg=EthernetNetworkModule.MSG_CONNECTION_TEMPLATE_RESET,
|
||||
ansible_facts=dict(ethernet_network_connection_template={'result': 'success'}))
|
||||
|
||||
def test_should_fail_when_reset_not_existing_ethernet_network(self):
|
||||
self.resource.get_by.return_value = [None]
|
||||
|
||||
self.mock_ansible_module.params = yaml.load(YAML_RESET_CONNECTION_TEMPLATE)
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.fail_json.assert_called_once_with(
|
||||
exception=mock.ANY,
|
||||
msg=EthernetNetworkModule.MSG_ETHERNET_NETWORK_NOT_FOUND
|
||||
)
|
||||
|
||||
def test_update_scopes_when_different(self):
|
||||
params_to_scope = PARAMS_FOR_PRESENT.copy()
|
||||
params_to_scope['data']['scopeUris'] = ['test']
|
||||
self.mock_ansible_module.params = params_to_scope
|
||||
|
||||
resource_data = DEFAULT_ENET_TEMPLATE.copy()
|
||||
resource_data['scopeUris'] = ['fake']
|
||||
resource_data['uri'] = 'rest/ethernet/fake'
|
||||
self.resource.get_by.return_value = [resource_data]
|
||||
|
||||
patch_return = resource_data.copy()
|
||||
patch_return['scopeUris'] = ['test']
|
||||
self.resource.patch.return_value = patch_return
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.resource.patch.assert_called_once_with('rest/ethernet/fake',
|
||||
operation='replace',
|
||||
path='/scopeUris',
|
||||
value=['test'])
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
ansible_facts=dict(ethernet_network=patch_return),
|
||||
msg=EthernetNetworkModule.MSG_UPDATED
|
||||
)
|
||||
|
||||
def test_should_do_nothing_when_scopes_are_the_same(self):
|
||||
params_to_scope = PARAMS_FOR_PRESENT.copy()
|
||||
params_to_scope['data']['scopeUris'] = ['test']
|
||||
self.mock_ansible_module.params = params_to_scope
|
||||
|
||||
resource_data = DEFAULT_ENET_TEMPLATE.copy()
|
||||
resource_data['scopeUris'] = ['test']
|
||||
self.resource.get_by.return_value = [resource_data]
|
||||
|
||||
EthernetNetworkModule().run()
|
||||
|
||||
self.resource.patch.not_been_called()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
ansible_facts=dict(ethernet_network=resource_data),
|
||||
msg=EthernetNetworkModule.MSG_ALREADY_PRESENT
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -0,0 +1,100 @@
|
|||
# Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||
|
||||
from .oneview_module_loader import EthernetNetworkInfoModule
|
||||
from .hpe_test_utils import FactsParamsTestCase
|
||||
|
||||
ERROR_MSG = 'Fake message error'
|
||||
|
||||
PARAMS_GET_ALL = dict(
|
||||
config='config.json',
|
||||
name=None
|
||||
)
|
||||
|
||||
PARAMS_GET_BY_NAME = dict(
|
||||
config='config.json',
|
||||
name="Test Ethernet Network",
|
||||
options=[]
|
||||
)
|
||||
|
||||
PARAMS_GET_BY_NAME_WITH_OPTIONS = dict(
|
||||
config='config.json',
|
||||
name="Test Ethernet Network",
|
||||
options=['associatedProfiles', 'associatedUplinkGroups']
|
||||
)
|
||||
|
||||
PRESENT_ENETS = [{
|
||||
"name": "Test Ethernet Network",
|
||||
"uri": "/rest/ethernet-networks/d34dcf5e-0d8e-441c-b00d-e1dd6a067188"
|
||||
}]
|
||||
|
||||
ENET_ASSOCIATED_UPLINK_GROUP_URIS = [
|
||||
"/rest/uplink-sets/c6bf9af9-48e7-4236-b08a-77684dc258a5",
|
||||
"/rest/uplink-sets/e2f0031b-52bd-4223-9ac1-d91cb519d548"
|
||||
]
|
||||
|
||||
ENET_ASSOCIATED_PROFILE_URIS = [
|
||||
"/rest/server-profiles/83e2e117-59dc-4e33-9f24-462af951cbbe",
|
||||
"/rest/server-profiles/57d3af2a-b6d2-4446-8645-f38dd808ea4d"
|
||||
]
|
||||
|
||||
ENET_ASSOCIATED_UPLINK_GROUPS = [dict(uri=ENET_ASSOCIATED_UPLINK_GROUP_URIS[0], name='Uplink Set 1'),
|
||||
dict(uri=ENET_ASSOCIATED_UPLINK_GROUP_URIS[1], name='Uplink Set 2')]
|
||||
|
||||
ENET_ASSOCIATED_PROFILES = [dict(uri=ENET_ASSOCIATED_PROFILE_URIS[0], name='Server Profile 1'),
|
||||
dict(uri=ENET_ASSOCIATED_PROFILE_URIS[1], name='Server Profile 2')]
|
||||
|
||||
|
||||
class EthernetNetworkInfoSpec(unittest.TestCase,
|
||||
FactsParamsTestCase
|
||||
):
|
||||
def setUp(self):
|
||||
self.configure_mocks(self, EthernetNetworkInfoModule)
|
||||
self.ethernet_networks = self.mock_ov_client.ethernet_networks
|
||||
FactsParamsTestCase.configure_client_mock(self, self.ethernet_networks)
|
||||
|
||||
def test_should_get_all_enets(self):
|
||||
self.ethernet_networks.get_all.return_value = PRESENT_ENETS
|
||||
self.mock_ansible_module.params = PARAMS_GET_ALL
|
||||
|
||||
EthernetNetworkInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
ethernet_networks=(PRESENT_ENETS)
|
||||
)
|
||||
|
||||
def test_should_get_enet_by_name(self):
|
||||
self.ethernet_networks.get_by.return_value = PRESENT_ENETS
|
||||
self.mock_ansible_module.params = PARAMS_GET_BY_NAME
|
||||
|
||||
EthernetNetworkInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
ethernet_networks=(PRESENT_ENETS)
|
||||
)
|
||||
|
||||
def test_should_get_enet_by_name_with_options(self):
|
||||
self.ethernet_networks.get_by.return_value = PRESENT_ENETS
|
||||
self.ethernet_networks.get_associated_profiles.return_value = ENET_ASSOCIATED_PROFILE_URIS
|
||||
self.ethernet_networks.get_associated_uplink_groups.return_value = ENET_ASSOCIATED_UPLINK_GROUP_URIS
|
||||
self.mock_ov_client.server_profiles.get.side_effect = ENET_ASSOCIATED_PROFILES
|
||||
self.mock_ov_client.uplink_sets.get.side_effect = ENET_ASSOCIATED_UPLINK_GROUPS
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_GET_BY_NAME_WITH_OPTIONS
|
||||
|
||||
EthernetNetworkInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
ethernet_networks=PRESENT_ENETS,
|
||||
enet_associated_profiles=ENET_ASSOCIATED_PROFILES,
|
||||
enet_associated_uplink_groups=ENET_ASSOCIATED_UPLINK_GROUPS
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -0,0 +1,178 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (2016-2017) Hewlett Packard Enterprise Development LP
|
||||
#
|
||||
# This program 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.
|
||||
#
|
||||
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||
from .oneview_module_loader import FcNetworkModule
|
||||
from .hpe_test_utils import OneViewBaseTestCase
|
||||
|
||||
FAKE_MSG_ERROR = 'Fake message error'
|
||||
|
||||
DEFAULT_FC_NETWORK_TEMPLATE = dict(
|
||||
name='New FC Network 2',
|
||||
autoLoginRedistribution=True,
|
||||
fabricType='FabricAttach'
|
||||
)
|
||||
|
||||
PARAMS_FOR_PRESENT = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(name=DEFAULT_FC_NETWORK_TEMPLATE['name'])
|
||||
)
|
||||
|
||||
PARAMS_WITH_CHANGES = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(name=DEFAULT_FC_NETWORK_TEMPLATE['name'],
|
||||
newName="New Name",
|
||||
fabricType='DirectAttach')
|
||||
)
|
||||
|
||||
PARAMS_FOR_ABSENT = dict(
|
||||
config='config.json',
|
||||
state='absent',
|
||||
data=dict(name=DEFAULT_FC_NETWORK_TEMPLATE['name'])
|
||||
)
|
||||
|
||||
|
||||
class FcNetworkModuleSpec(unittest.TestCase,
|
||||
OneViewBaseTestCase):
|
||||
"""
|
||||
OneViewBaseTestCase provides the mocks used in this test case
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.configure_mocks(self, FcNetworkModule)
|
||||
self.resource = self.mock_ov_client.fc_networks
|
||||
|
||||
def test_should_create_new_fc_network(self):
|
||||
self.resource.get_by.return_value = []
|
||||
self.resource.create.return_value = DEFAULT_FC_NETWORK_TEMPLATE
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_PRESENT
|
||||
|
||||
FcNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=FcNetworkModule.MSG_CREATED,
|
||||
ansible_facts=dict(fc_network=DEFAULT_FC_NETWORK_TEMPLATE)
|
||||
)
|
||||
|
||||
def test_should_not_update_when_data_is_equals(self):
|
||||
self.resource.get_by.return_value = [DEFAULT_FC_NETWORK_TEMPLATE]
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_PRESENT
|
||||
|
||||
FcNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
msg=FcNetworkModule.MSG_ALREADY_PRESENT,
|
||||
ansible_facts=dict(fc_network=DEFAULT_FC_NETWORK_TEMPLATE)
|
||||
)
|
||||
|
||||
def test_update_when_data_has_modified_attributes(self):
|
||||
data_merged = DEFAULT_FC_NETWORK_TEMPLATE.copy()
|
||||
|
||||
data_merged['fabricType'] = 'DirectAttach'
|
||||
|
||||
self.resource.get_by.return_value = [DEFAULT_FC_NETWORK_TEMPLATE]
|
||||
self.resource.update.return_value = data_merged
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_WITH_CHANGES
|
||||
|
||||
FcNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=FcNetworkModule.MSG_UPDATED,
|
||||
ansible_facts=dict(fc_network=data_merged)
|
||||
)
|
||||
|
||||
def test_should_remove_fc_network(self):
|
||||
self.resource.get_by.return_value = [DEFAULT_FC_NETWORK_TEMPLATE]
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_ABSENT
|
||||
|
||||
FcNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=FcNetworkModule.MSG_DELETED
|
||||
)
|
||||
|
||||
def test_should_do_nothing_when_fc_network_not_exist(self):
|
||||
self.resource.get_by.return_value = []
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_ABSENT
|
||||
|
||||
FcNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
msg=FcNetworkModule.MSG_ALREADY_ABSENT
|
||||
)
|
||||
|
||||
def test_update_scopes_when_different(self):
|
||||
params_to_scope = PARAMS_FOR_PRESENT.copy()
|
||||
params_to_scope['data']['scopeUris'] = ['test']
|
||||
self.mock_ansible_module.params = params_to_scope
|
||||
|
||||
resource_data = DEFAULT_FC_NETWORK_TEMPLATE.copy()
|
||||
resource_data['scopeUris'] = ['fake']
|
||||
resource_data['uri'] = 'rest/fc/fake'
|
||||
self.resource.get_by.return_value = [resource_data]
|
||||
|
||||
patch_return = resource_data.copy()
|
||||
patch_return['scopeUris'] = ['test']
|
||||
self.resource.patch.return_value = patch_return
|
||||
|
||||
FcNetworkModule().run()
|
||||
|
||||
self.resource.patch.assert_called_once_with('rest/fc/fake',
|
||||
operation='replace',
|
||||
path='/scopeUris',
|
||||
value=['test'])
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
ansible_facts=dict(fc_network=patch_return),
|
||||
msg=FcNetworkModule.MSG_UPDATED
|
||||
)
|
||||
|
||||
def test_should_do_nothing_when_scopes_are_the_same(self):
|
||||
params_to_scope = PARAMS_FOR_PRESENT.copy()
|
||||
params_to_scope['data']['scopeUris'] = ['test']
|
||||
self.mock_ansible_module.params = params_to_scope
|
||||
|
||||
resource_data = DEFAULT_FC_NETWORK_TEMPLATE.copy()
|
||||
resource_data['scopeUris'] = ['test']
|
||||
self.resource.get_by.return_value = [resource_data]
|
||||
|
||||
FcNetworkModule().run()
|
||||
|
||||
self.resource.patch.not_been_called()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
ansible_facts=dict(fc_network=resource_data),
|
||||
msg=FcNetworkModule.MSG_ALREADY_PRESENT
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -0,0 +1,57 @@
|
|||
# Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||
from .oneview_module_loader import FcNetworkInfoModule
|
||||
from .hpe_test_utils import FactsParamsTestCase
|
||||
|
||||
ERROR_MSG = 'Fake message error'
|
||||
|
||||
PARAMS_GET_ALL = dict(
|
||||
config='config.json',
|
||||
name=None
|
||||
)
|
||||
|
||||
PARAMS_GET_BY_NAME = dict(
|
||||
config='config.json',
|
||||
name="Test FC Network"
|
||||
)
|
||||
|
||||
PRESENT_NETWORKS = [{
|
||||
"name": "Test FC Network",
|
||||
"uri": "/rest/fc-networks/c6bf9af9-48e7-4236-b08a-77684dc258a5"
|
||||
}]
|
||||
|
||||
|
||||
class FcNetworkInfoSpec(unittest.TestCase,
|
||||
FactsParamsTestCase):
|
||||
def setUp(self):
|
||||
self.configure_mocks(self, FcNetworkInfoModule)
|
||||
self.fc_networks = self.mock_ov_client.fc_networks
|
||||
FactsParamsTestCase.configure_client_mock(self, self.fc_networks)
|
||||
|
||||
def test_should_get_all_fc_networks(self):
|
||||
self.fc_networks.get_all.return_value = PRESENT_NETWORKS
|
||||
self.mock_ansible_module.params = PARAMS_GET_ALL
|
||||
|
||||
FcNetworkInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
fc_networks=PRESENT_NETWORKS
|
||||
)
|
||||
|
||||
def test_should_get_fc_network_by_name(self):
|
||||
self.fc_networks.get_by.return_value = PRESENT_NETWORKS
|
||||
self.mock_ansible_module.params = PARAMS_GET_BY_NAME
|
||||
|
||||
FcNetworkInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
fc_networks=PRESENT_NETWORKS
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -0,0 +1,176 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (2016-2017) Hewlett Packard Enterprise Development LP
|
||||
#
|
||||
# This program 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.
|
||||
#
|
||||
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||
from .oneview_module_loader import FcoeNetworkModule
|
||||
from .hpe_test_utils import OneViewBaseTestCase
|
||||
|
||||
FAKE_MSG_ERROR = 'Fake message error'
|
||||
|
||||
DEFAULT_FCOE_NETWORK_TEMPLATE = dict(
|
||||
name='New FCoE Network 2',
|
||||
vlanId="201",
|
||||
connectionTemplateUri=None
|
||||
)
|
||||
|
||||
PARAMS_FOR_PRESENT = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(name=DEFAULT_FCOE_NETWORK_TEMPLATE['name'])
|
||||
)
|
||||
|
||||
PARAMS_WITH_CHANGES = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(name=DEFAULT_FCOE_NETWORK_TEMPLATE['name'],
|
||||
fabricType='DirectAttach',
|
||||
newName='New Name')
|
||||
)
|
||||
|
||||
PARAMS_FOR_ABSENT = dict(
|
||||
config='config.json',
|
||||
state='absent',
|
||||
data=dict(name=DEFAULT_FCOE_NETWORK_TEMPLATE['name'])
|
||||
)
|
||||
|
||||
|
||||
class FcoeNetworkSpec(unittest.TestCase,
|
||||
OneViewBaseTestCase):
|
||||
"""
|
||||
OneViewBaseTestCase provides the mocks used in this test case
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.configure_mocks(self, FcoeNetworkModule)
|
||||
self.resource = self.mock_ov_client.fcoe_networks
|
||||
|
||||
def test_should_create_new_fcoe_network(self):
|
||||
self.resource.get_by.return_value = []
|
||||
self.resource.create.return_value = DEFAULT_FCOE_NETWORK_TEMPLATE
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_PRESENT
|
||||
|
||||
FcoeNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=FcoeNetworkModule.MSG_CREATED,
|
||||
ansible_facts=dict(fcoe_network=DEFAULT_FCOE_NETWORK_TEMPLATE)
|
||||
)
|
||||
|
||||
def test_should_not_update_when_data_is_equals(self):
|
||||
self.resource.get_by.return_value = [DEFAULT_FCOE_NETWORK_TEMPLATE]
|
||||
self.mock_ansible_module.params = PARAMS_FOR_PRESENT.copy()
|
||||
|
||||
FcoeNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
msg=FcoeNetworkModule.MSG_ALREADY_PRESENT,
|
||||
ansible_facts=dict(fcoe_network=DEFAULT_FCOE_NETWORK_TEMPLATE)
|
||||
)
|
||||
|
||||
def test_update_when_data_has_modified_attributes(self):
|
||||
data_merged = DEFAULT_FCOE_NETWORK_TEMPLATE.copy()
|
||||
data_merged['fabricType'] = 'DirectAttach'
|
||||
|
||||
self.resource.get_by.return_value = [DEFAULT_FCOE_NETWORK_TEMPLATE]
|
||||
self.resource.update.return_value = data_merged
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_WITH_CHANGES
|
||||
|
||||
FcoeNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=FcoeNetworkModule.MSG_UPDATED,
|
||||
ansible_facts=dict(fcoe_network=data_merged)
|
||||
)
|
||||
|
||||
def test_should_remove_fcoe_network(self):
|
||||
self.resource.get_by.return_value = [DEFAULT_FCOE_NETWORK_TEMPLATE]
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_ABSENT
|
||||
|
||||
FcoeNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=FcoeNetworkModule.MSG_DELETED
|
||||
)
|
||||
|
||||
def test_should_do_nothing_when_fcoe_network_not_exist(self):
|
||||
self.resource.get_by.return_value = []
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_ABSENT
|
||||
|
||||
FcoeNetworkModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
msg=FcoeNetworkModule.MSG_ALREADY_ABSENT
|
||||
)
|
||||
|
||||
def test_update_scopes_when_different(self):
|
||||
params_to_scope = PARAMS_FOR_PRESENT.copy()
|
||||
params_to_scope['data']['scopeUris'] = ['test']
|
||||
self.mock_ansible_module.params = params_to_scope
|
||||
|
||||
resource_data = DEFAULT_FCOE_NETWORK_TEMPLATE.copy()
|
||||
resource_data['scopeUris'] = ['fake']
|
||||
resource_data['uri'] = 'rest/fcoe/fake'
|
||||
self.resource.get_by.return_value = [resource_data]
|
||||
|
||||
patch_return = resource_data.copy()
|
||||
patch_return['scopeUris'] = ['test']
|
||||
self.resource.patch.return_value = patch_return
|
||||
|
||||
FcoeNetworkModule().run()
|
||||
|
||||
self.resource.patch.assert_called_once_with('rest/fcoe/fake',
|
||||
operation='replace',
|
||||
path='/scopeUris',
|
||||
value=['test'])
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
ansible_facts=dict(fcoe_network=patch_return),
|
||||
msg=FcoeNetworkModule.MSG_UPDATED
|
||||
)
|
||||
|
||||
def test_should_do_nothing_when_scopes_are_the_same(self):
|
||||
params_to_scope = PARAMS_FOR_PRESENT.copy()
|
||||
params_to_scope['data']['scopeUris'] = ['test']
|
||||
self.mock_ansible_module.params = params_to_scope
|
||||
|
||||
resource_data = DEFAULT_FCOE_NETWORK_TEMPLATE.copy()
|
||||
resource_data['scopeUris'] = ['test']
|
||||
self.resource.get_by.return_value = [resource_data]
|
||||
|
||||
FcoeNetworkModule().run()
|
||||
|
||||
self.resource.patch.not_been_called()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
ansible_facts=dict(fcoe_network=resource_data),
|
||||
msg=FcoeNetworkModule.MSG_ALREADY_PRESENT
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -0,0 +1,60 @@
|
|||
# Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||
|
||||
from .oneview_module_loader import FcoeNetworkInfoModule
|
||||
|
||||
from .hpe_test_utils import FactsParamsTestCase
|
||||
|
||||
ERROR_MSG = 'Fake message error'
|
||||
|
||||
PARAMS_GET_ALL = dict(
|
||||
config='config.json',
|
||||
name=None
|
||||
)
|
||||
|
||||
PARAMS_GET_BY_NAME = dict(
|
||||
config='config.json',
|
||||
name="Test FCoE Networks"
|
||||
)
|
||||
|
||||
PRESENT_NETWORKS = [{
|
||||
"name": "Test FCoE Networks",
|
||||
"uri": "/rest/fcoe-networks/c6bf9af9-48e7-4236-b08a-77684dc258a5"
|
||||
}]
|
||||
|
||||
|
||||
class FcoeNetworkInfoSpec(unittest.TestCase,
|
||||
FactsParamsTestCase
|
||||
):
|
||||
def setUp(self):
|
||||
self.configure_mocks(self, FcoeNetworkInfoModule)
|
||||
self.fcoe_networks = self.mock_ov_client.fcoe_networks
|
||||
FactsParamsTestCase.configure_client_mock(self, self.fcoe_networks)
|
||||
|
||||
def test_should_get_all_fcoe_network(self):
|
||||
self.fcoe_networks.get_all.return_value = PRESENT_NETWORKS
|
||||
self.mock_ansible_module.params = PARAMS_GET_ALL
|
||||
|
||||
FcoeNetworkInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
fcoe_networks=PRESENT_NETWORKS
|
||||
)
|
||||
|
||||
def test_should_get_fcoe_network_by_name(self):
|
||||
self.fcoe_networks.get_by.return_value = PRESENT_NETWORKS
|
||||
self.mock_ansible_module.params = PARAMS_GET_BY_NAME
|
||||
|
||||
FcoeNetworkInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
fcoe_networks=PRESENT_NETWORKS
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -0,0 +1,257 @@
|
|||
# Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from copy import deepcopy
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest, mock
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.oneview.oneview_logical_interconnect_group import LogicalInterconnectGroupModule
|
||||
from .hpe_test_utils import OneViewBaseTestCase
|
||||
|
||||
|
||||
FAKE_MSG_ERROR = 'Fake message error'
|
||||
|
||||
DEFAULT_LIG_NAME = 'Test Logical Interconnect Group'
|
||||
RENAMED_LIG = 'Renamed Logical Interconnect Group'
|
||||
|
||||
DEFAULT_LIG_TEMPLATE = dict(
|
||||
name=DEFAULT_LIG_NAME,
|
||||
uplinkSets=[],
|
||||
enclosureType='C7000',
|
||||
interconnectMapTemplate=dict(
|
||||
interconnectMapEntryTemplates=[]
|
||||
)
|
||||
)
|
||||
|
||||
PARAMS_LIG_TEMPLATE_WITH_MAP = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(
|
||||
name=DEFAULT_LIG_NAME,
|
||||
uplinkSets=[],
|
||||
enclosureType='C7000',
|
||||
interconnectMapTemplate=dict(
|
||||
interconnectMapEntryTemplates=[
|
||||
{
|
||||
"logicalDownlinkUri": None,
|
||||
"logicalLocation": {
|
||||
"locationEntries": [
|
||||
{
|
||||
"relativeValue": "1",
|
||||
"type": "Bay"
|
||||
},
|
||||
{
|
||||
"relativeValue": 1,
|
||||
"type": "Enclosure"
|
||||
}
|
||||
]
|
||||
},
|
||||
"permittedInterconnectTypeName": "HP VC Flex-10/10D Module"
|
||||
}]
|
||||
)
|
||||
))
|
||||
|
||||
PARAMS_FOR_PRESENT = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(name=DEFAULT_LIG_NAME)
|
||||
)
|
||||
|
||||
PARAMS_TO_RENAME = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(name=DEFAULT_LIG_NAME,
|
||||
newName=RENAMED_LIG)
|
||||
)
|
||||
|
||||
PARAMS_WITH_CHANGES = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(name=DEFAULT_LIG_NAME,
|
||||
description='It is an example')
|
||||
)
|
||||
|
||||
PARAMS_FOR_ABSENT = dict(
|
||||
config='config.json',
|
||||
state='absent',
|
||||
data=dict(name=DEFAULT_LIG_NAME)
|
||||
)
|
||||
|
||||
|
||||
class LogicalInterconnectGroupGeneralSpec(unittest.TestCase,
|
||||
OneViewBaseTestCase):
|
||||
def setUp(self):
|
||||
self.configure_mocks(self, LogicalInterconnectGroupModule)
|
||||
self.resource = self.mock_ov_client.logical_interconnect_groups
|
||||
|
||||
def test_should_create_new_lig(self):
|
||||
self.resource.get_by.return_value = []
|
||||
self.resource.create.return_value = DEFAULT_LIG_TEMPLATE
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_PRESENT
|
||||
|
||||
LogicalInterconnectGroupModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=LogicalInterconnectGroupModule.MSG_CREATED,
|
||||
ansible_facts=dict(logical_interconnect_group=DEFAULT_LIG_TEMPLATE)
|
||||
)
|
||||
|
||||
def test_should_create_new_with_named_permitted_interconnect_type(self):
|
||||
self.resource.get_by.return_value = []
|
||||
self.resource.create.return_value = PARAMS_FOR_PRESENT
|
||||
|
||||
self.mock_ansible_module.params = deepcopy(PARAMS_LIG_TEMPLATE_WITH_MAP)
|
||||
|
||||
LogicalInterconnectGroupModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=LogicalInterconnectGroupModule.MSG_CREATED,
|
||||
ansible_facts=dict(logical_interconnect_group=PARAMS_FOR_PRESENT.copy())
|
||||
)
|
||||
|
||||
def test_should_fail_when_permitted_interconnect_type_name_not_exists(self):
|
||||
self.resource.get_by.return_value = []
|
||||
self.resource.create.return_value = PARAMS_FOR_PRESENT
|
||||
self.mock_ov_client.interconnect_types.get_by.return_value = []
|
||||
|
||||
self.mock_ansible_module.params = deepcopy(PARAMS_LIG_TEMPLATE_WITH_MAP)
|
||||
|
||||
LogicalInterconnectGroupModule().run()
|
||||
|
||||
self.mock_ansible_module.fail_json.assert_called_once_with(
|
||||
exception=mock.ANY,
|
||||
msg=LogicalInterconnectGroupModule.MSG_INTERCONNECT_TYPE_NOT_FOUND)
|
||||
|
||||
def test_should_not_update_when_data_is_equals(self):
|
||||
self.resource.get_by.return_value = [DEFAULT_LIG_TEMPLATE]
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_PRESENT
|
||||
|
||||
LogicalInterconnectGroupModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
msg=LogicalInterconnectGroupModule.MSG_ALREADY_PRESENT,
|
||||
ansible_facts=dict(logical_interconnect_group=DEFAULT_LIG_TEMPLATE)
|
||||
)
|
||||
|
||||
def test_update_when_data_has_modified_attributes(self):
|
||||
data_merged = DEFAULT_LIG_TEMPLATE.copy()
|
||||
data_merged['description'] = 'New description'
|
||||
|
||||
self.resource.get_by.return_value = [DEFAULT_LIG_TEMPLATE]
|
||||
self.resource.update.return_value = data_merged
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_WITH_CHANGES
|
||||
|
||||
LogicalInterconnectGroupModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=LogicalInterconnectGroupModule.MSG_UPDATED,
|
||||
ansible_facts=dict(logical_interconnect_group=data_merged)
|
||||
)
|
||||
|
||||
def test_rename_when_resource_exists(self):
|
||||
data_merged = DEFAULT_LIG_TEMPLATE.copy()
|
||||
data_merged['name'] = RENAMED_LIG
|
||||
params_to_rename = PARAMS_TO_RENAME.copy()
|
||||
|
||||
self.resource.get_by.return_value = [DEFAULT_LIG_TEMPLATE]
|
||||
self.resource.update.return_value = data_merged
|
||||
|
||||
self.mock_ansible_module.params = params_to_rename
|
||||
|
||||
LogicalInterconnectGroupModule().run()
|
||||
|
||||
self.resource.update.assert_called_once_with(data_merged)
|
||||
|
||||
def test_create_with_newName_when_resource_not_exists(self):
|
||||
data_merged = DEFAULT_LIG_TEMPLATE.copy()
|
||||
data_merged['name'] = RENAMED_LIG
|
||||
params_to_rename = PARAMS_TO_RENAME.copy()
|
||||
|
||||
self.resource.get_by.return_value = []
|
||||
self.resource.create.return_value = DEFAULT_LIG_TEMPLATE
|
||||
|
||||
self.mock_ansible_module.params = params_to_rename
|
||||
|
||||
LogicalInterconnectGroupModule().run()
|
||||
|
||||
self.resource.create.assert_called_once_with(PARAMS_TO_RENAME['data'])
|
||||
|
||||
def test_should_remove_lig(self):
|
||||
self.resource.get_by.return_value = [DEFAULT_LIG_TEMPLATE]
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_ABSENT
|
||||
|
||||
LogicalInterconnectGroupModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=LogicalInterconnectGroupModule.MSG_DELETED
|
||||
)
|
||||
|
||||
def test_should_do_nothing_when_lig_not_exist(self):
|
||||
self.resource.get_by.return_value = []
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_ABSENT
|
||||
|
||||
LogicalInterconnectGroupModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
msg=LogicalInterconnectGroupModule.MSG_ALREADY_ABSENT
|
||||
)
|
||||
|
||||
def test_update_scopes_when_different(self):
|
||||
params_to_scope = PARAMS_FOR_PRESENT.copy()
|
||||
params_to_scope['data']['scopeUris'] = ['test']
|
||||
self.mock_ansible_module.params = params_to_scope
|
||||
|
||||
resource_data = DEFAULT_LIG_TEMPLATE.copy()
|
||||
resource_data['scopeUris'] = ['fake']
|
||||
resource_data['uri'] = 'rest/lig/fake'
|
||||
self.resource.get_by.return_value = [resource_data]
|
||||
|
||||
patch_return = resource_data.copy()
|
||||
patch_return['scopeUris'] = ['test']
|
||||
self.resource.patch.return_value = patch_return
|
||||
|
||||
LogicalInterconnectGroupModule().run()
|
||||
|
||||
self.resource.patch.assert_called_once_with('rest/lig/fake',
|
||||
operation='replace',
|
||||
path='/scopeUris',
|
||||
value=['test'])
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
ansible_facts=dict(logical_interconnect_group=patch_return),
|
||||
msg=LogicalInterconnectGroupModule.MSG_UPDATED
|
||||
)
|
||||
|
||||
def test_should_do_nothing_when_scopes_are_the_same(self):
|
||||
params_to_scope = PARAMS_FOR_PRESENT.copy()
|
||||
params_to_scope['data']['scopeUris'] = ['test']
|
||||
self.mock_ansible_module.params = params_to_scope
|
||||
|
||||
resource_data = DEFAULT_LIG_TEMPLATE.copy()
|
||||
resource_data['scopeUris'] = ['test']
|
||||
self.resource.get_by.return_value = [resource_data]
|
||||
|
||||
LogicalInterconnectGroupModule().run()
|
||||
|
||||
self.resource.patch.not_been_called()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
ansible_facts=dict(logical_interconnect_group=resource_data),
|
||||
msg=LogicalInterconnectGroupModule.MSG_ALREADY_PRESENT
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -0,0 +1,59 @@
|
|||
# Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||
from ansible_collections.community.general.plugins.modules.remote_management.oneview.oneview_logical_interconnect_group_info import (
|
||||
LogicalInterconnectGroupInfoModule
|
||||
)
|
||||
from .hpe_test_utils import FactsParamsTestCase
|
||||
|
||||
|
||||
ERROR_MSG = 'Fake message error'
|
||||
|
||||
PARAMS_GET_ALL = dict(
|
||||
config='config.json',
|
||||
name=None
|
||||
)
|
||||
|
||||
PARAMS_GET_BY_NAME = dict(
|
||||
config='config.json',
|
||||
name="Test Logical Interconnect Group"
|
||||
)
|
||||
|
||||
PRESENT_LIGS = [{
|
||||
"name": "Test Logical Interconnect Group",
|
||||
"uri": "/rest/logical-interconnect-groups/ebb4ada8-08df-400e-8fac-9ff987ac5140"
|
||||
}]
|
||||
|
||||
|
||||
class LogicalInterconnectGroupInfoSpec(unittest.TestCase, FactsParamsTestCase):
|
||||
def setUp(self):
|
||||
self.configure_mocks(self, LogicalInterconnectGroupInfoModule)
|
||||
self.logical_interconnect_groups = self.mock_ov_client.logical_interconnect_groups
|
||||
FactsParamsTestCase.configure_client_mock(self, self.logical_interconnect_groups)
|
||||
|
||||
def test_should_get_all_ligs(self):
|
||||
self.logical_interconnect_groups.get_all.return_value = PRESENT_LIGS
|
||||
self.mock_ansible_module.params = PARAMS_GET_ALL
|
||||
|
||||
LogicalInterconnectGroupInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
logical_interconnect_groups=(PRESENT_LIGS)
|
||||
)
|
||||
|
||||
def test_should_get_lig_by_name(self):
|
||||
self.logical_interconnect_groups.get_by.return_value = PRESENT_LIGS
|
||||
self.mock_ansible_module.params = PARAMS_GET_BY_NAME
|
||||
|
||||
LogicalInterconnectGroupInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
logical_interconnect_groups=(PRESENT_LIGS)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -0,0 +1,183 @@
|
|||
# Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest, mock
|
||||
from .hpe_test_utils import OneViewBaseTestCase
|
||||
from .oneview_module_loader import NetworkSetModule
|
||||
|
||||
FAKE_MSG_ERROR = 'Fake message error'
|
||||
|
||||
NETWORK_SET = dict(
|
||||
name='OneViewSDK Test Network Set',
|
||||
networkUris=['/rest/ethernet-networks/aaa-bbb-ccc']
|
||||
)
|
||||
|
||||
NETWORK_SET_WITH_NEW_NAME = dict(name='OneViewSDK Test Network Set - Renamed')
|
||||
|
||||
PARAMS_FOR_PRESENT = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(name=NETWORK_SET['name'],
|
||||
networkUris=['/rest/ethernet-networks/aaa-bbb-ccc'])
|
||||
)
|
||||
|
||||
PARAMS_WITH_CHANGES = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(name=NETWORK_SET['name'],
|
||||
newName=NETWORK_SET['name'] + " - Renamed",
|
||||
networkUris=['/rest/ethernet-networks/aaa-bbb-ccc', 'Name of a Network'])
|
||||
)
|
||||
|
||||
PARAMS_FOR_ABSENT = dict(
|
||||
config='config.json',
|
||||
state='absent',
|
||||
data=dict(name=NETWORK_SET['name'])
|
||||
)
|
||||
|
||||
|
||||
class NetworkSetModuleSpec(unittest.TestCase,
|
||||
OneViewBaseTestCase):
|
||||
"""
|
||||
OneViewBaseTestCase has common tests for class constructor and main function,
|
||||
also provides the mocks used in this test case.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.configure_mocks(self, NetworkSetModule)
|
||||
self.resource = self.mock_ov_client.network_sets
|
||||
self.ethernet_network_client = self.mock_ov_client.ethernet_networks
|
||||
|
||||
def test_should_create_new_network_set(self):
|
||||
self.resource.get_by.return_value = []
|
||||
self.resource.create.return_value = NETWORK_SET
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_PRESENT
|
||||
|
||||
NetworkSetModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=NetworkSetModule.MSG_CREATED,
|
||||
ansible_facts=dict(network_set=NETWORK_SET)
|
||||
)
|
||||
|
||||
def test_should_not_update_when_data_is_equals(self):
|
||||
self.resource.get_by.return_value = [NETWORK_SET]
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_PRESENT
|
||||
|
||||
NetworkSetModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
msg=NetworkSetModule.MSG_ALREADY_PRESENT,
|
||||
ansible_facts=dict(network_set=NETWORK_SET)
|
||||
)
|
||||
|
||||
def test_update_when_data_has_modified_attributes(self):
|
||||
data_merged = dict(name=NETWORK_SET['name'] + " - Renamed",
|
||||
networkUris=['/rest/ethernet-networks/aaa-bbb-ccc',
|
||||
'/rest/ethernet-networks/ddd-eee-fff']
|
||||
)
|
||||
|
||||
self.resource.get_by.side_effect = [NETWORK_SET], []
|
||||
self.resource.update.return_value = data_merged
|
||||
self.ethernet_network_client.get_by.return_value = [{'uri': '/rest/ethernet-networks/ddd-eee-fff'}]
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_WITH_CHANGES
|
||||
|
||||
NetworkSetModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=NetworkSetModule.MSG_UPDATED,
|
||||
ansible_facts=dict(network_set=data_merged)
|
||||
)
|
||||
|
||||
def test_should_raise_exception_when_ethernet_network_not_found(self):
|
||||
self.resource.get_by.side_effect = [NETWORK_SET], []
|
||||
self.ethernet_network_client.get_by.return_value = []
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_WITH_CHANGES
|
||||
|
||||
NetworkSetModule().run()
|
||||
|
||||
self.mock_ansible_module.fail_json.assert_called_once_with(
|
||||
exception=mock.ANY,
|
||||
msg=NetworkSetModule.MSG_ETHERNET_NETWORK_NOT_FOUND + "Name of a Network"
|
||||
)
|
||||
|
||||
def test_should_remove_network(self):
|
||||
self.resource.get_by.return_value = [NETWORK_SET]
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_ABSENT
|
||||
|
||||
NetworkSetModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=NetworkSetModule.MSG_DELETED
|
||||
)
|
||||
|
||||
def test_should_do_nothing_when_network_set_not_exist(self):
|
||||
self.resource.get_by.return_value = []
|
||||
|
||||
self.mock_ansible_module.params = PARAMS_FOR_ABSENT
|
||||
|
||||
NetworkSetModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
msg=NetworkSetModule.MSG_ALREADY_ABSENT
|
||||
)
|
||||
|
||||
def test_update_scopes_when_different(self):
|
||||
params_to_scope = PARAMS_FOR_PRESENT.copy()
|
||||
params_to_scope['data']['scopeUris'] = ['test']
|
||||
self.mock_ansible_module.params = params_to_scope
|
||||
|
||||
resource_data = NETWORK_SET.copy()
|
||||
resource_data['scopeUris'] = ['fake']
|
||||
resource_data['uri'] = 'rest/network-sets/fake'
|
||||
self.resource.get_by.return_value = [resource_data]
|
||||
|
||||
patch_return = resource_data.copy()
|
||||
patch_return['scopeUris'] = ['test']
|
||||
self.resource.patch.return_value = patch_return
|
||||
|
||||
NetworkSetModule().run()
|
||||
|
||||
self.resource.patch.assert_called_once_with('rest/network-sets/fake',
|
||||
operation='replace',
|
||||
path='/scopeUris',
|
||||
value=['test'])
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
ansible_facts=dict(network_set=patch_return),
|
||||
msg=NetworkSetModule.MSG_UPDATED
|
||||
)
|
||||
|
||||
def test_should_do_nothing_when_scopes_are_the_same(self):
|
||||
params_to_scope = PARAMS_FOR_PRESENT.copy()
|
||||
params_to_scope['data']['scopeUris'] = ['test']
|
||||
self.mock_ansible_module.params = params_to_scope
|
||||
|
||||
resource_data = NETWORK_SET.copy()
|
||||
resource_data['scopeUris'] = ['test']
|
||||
self.resource.get_by.return_value = [resource_data]
|
||||
|
||||
NetworkSetModule().run()
|
||||
|
||||
self.resource.patch.not_been_called()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
ansible_facts=dict(network_set=resource_data),
|
||||
msg=NetworkSetModule.MSG_ALREADY_PRESENT
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -0,0 +1,117 @@
|
|||
# Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||
from .oneview_module_loader import NetworkSetInfoModule
|
||||
from .hpe_test_utils import FactsParamsTestCase
|
||||
|
||||
ERROR_MSG = 'Fake message error'
|
||||
|
||||
PARAMS_GET_ALL = dict(
|
||||
config='config.json',
|
||||
name=None
|
||||
)
|
||||
|
||||
PARAMS_GET_ALL_WITHOUT_ETHERNET = dict(
|
||||
config='config.json',
|
||||
name=None,
|
||||
options=['withoutEthernet']
|
||||
)
|
||||
|
||||
PARAMS_GET_BY_NAME = dict(
|
||||
config='config.json',
|
||||
name='Network Set 1'
|
||||
)
|
||||
|
||||
PARAMS_GET_BY_NAME_WITHOUT_ETHERNET = dict(
|
||||
config='config.json',
|
||||
name='Network Set 1',
|
||||
options=['withoutEthernet']
|
||||
)
|
||||
|
||||
|
||||
class NetworkSetInfoSpec(unittest.TestCase,
|
||||
FactsParamsTestCase):
|
||||
def setUp(self):
|
||||
self.configure_mocks(self, NetworkSetInfoModule)
|
||||
self.network_sets = self.mock_ov_client.network_sets
|
||||
FactsParamsTestCase.configure_client_mock(self, self.network_sets)
|
||||
|
||||
def test_should_get_all_network_sets(self):
|
||||
network_sets = [{
|
||||
"name": "Network Set 1",
|
||||
"networkUris": ['/rest/ethernet-networks/aaa-bbb-ccc']
|
||||
}, {
|
||||
"name": "Network Set 2",
|
||||
"networkUris": ['/rest/ethernet-networks/ddd-eee-fff', '/rest/ethernet-networks/ggg-hhh-fff']
|
||||
}]
|
||||
|
||||
self.network_sets.get_all.return_value = network_sets
|
||||
self.mock_ansible_module.params = PARAMS_GET_ALL
|
||||
|
||||
NetworkSetInfoModule().run()
|
||||
|
||||
self.network_sets.get_all.assert_called_once_with()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
network_sets=network_sets)
|
||||
|
||||
def test_should_get_all_network_sets_without_ethernet(self):
|
||||
network_sets = [{
|
||||
"name": "Network Set 1",
|
||||
"networkUris": []
|
||||
}, {
|
||||
"name": "Network Set 2",
|
||||
"networkUris": []
|
||||
}]
|
||||
|
||||
self.network_sets.get_all.return_value = network_sets
|
||||
self.mock_ansible_module.params = PARAMS_GET_ALL
|
||||
|
||||
NetworkSetInfoModule().run()
|
||||
|
||||
self.network_sets.get_all.assert_called_once_with()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
network_sets=network_sets)
|
||||
|
||||
def test_should_get_network_set_by_name(self):
|
||||
network_sets = [{
|
||||
"name": "Network Set 1",
|
||||
"networkUris": ['/rest/ethernet-networks/aaa-bbb-ccc']
|
||||
}]
|
||||
|
||||
self.network_sets.get_by.return_value = network_sets
|
||||
self.mock_ansible_module.params = PARAMS_GET_BY_NAME
|
||||
|
||||
NetworkSetInfoModule().run()
|
||||
|
||||
self.network_sets.get_by.assert_called_once_with('name', 'Network Set 1')
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
network_sets=network_sets)
|
||||
|
||||
def test_should_get_network_set_by_name_without_ethernet(self):
|
||||
network_sets = [{
|
||||
"name": "Network Set 1",
|
||||
"networkUris": []
|
||||
}]
|
||||
|
||||
self.network_sets.get_all_without_ethernet.return_value = network_sets
|
||||
self.mock_ansible_module.params = PARAMS_GET_BY_NAME_WITHOUT_ETHERNET
|
||||
|
||||
NetworkSetInfoModule().run()
|
||||
|
||||
expected_filter = "\"'name'='Network Set 1'\""
|
||||
self.network_sets.get_all_without_ethernet.assert_called_once_with(filter=expected_filter)
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
network_sets=network_sets)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -0,0 +1,239 @@
|
|||
# Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest, mock
|
||||
from .oneview_module_loader import SanManagerModule
|
||||
from .hpe_test_utils import OneViewBaseTestCase
|
||||
from copy import deepcopy
|
||||
|
||||
FAKE_MSG_ERROR = 'Fake message error'
|
||||
|
||||
DEFAULT_SAN_MANAGER_TEMPLATE = dict(
|
||||
name='172.18.15.1',
|
||||
providerDisplayName='Brocade Network Advisor',
|
||||
uri='/rest/fc-sans/device-managers/UUU-AAA-BBB',
|
||||
refreshState='OK',
|
||||
connectionInfo=[
|
||||
{
|
||||
'valueFormat': 'IPAddressOrHostname',
|
||||
'displayName': 'Host',
|
||||
'name': 'Host',
|
||||
'valueType': 'String',
|
||||
'required': False,
|
||||
'value': '172.18.15.1'
|
||||
}]
|
||||
)
|
||||
|
||||
|
||||
class SanManagerModuleSpec(unittest.TestCase,
|
||||
OneViewBaseTestCase):
|
||||
PARAMS_FOR_PRESENT = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=DEFAULT_SAN_MANAGER_TEMPLATE
|
||||
)
|
||||
|
||||
PARAMS_FOR_CONNECTION_INFORMATION_SET = dict(
|
||||
config='config.json',
|
||||
state='connection_information_set',
|
||||
data=DEFAULT_SAN_MANAGER_TEMPLATE.copy()
|
||||
)
|
||||
|
||||
PARAMS_WITH_CHANGES = dict(
|
||||
config='config.json',
|
||||
state='present',
|
||||
data=dict(name=DEFAULT_SAN_MANAGER_TEMPLATE['name'],
|
||||
refreshState='RefreshPending')
|
||||
)
|
||||
|
||||
PARAMS_FOR_ABSENT = dict(
|
||||
config='config.json',
|
||||
state='absent',
|
||||
data=dict(name=DEFAULT_SAN_MANAGER_TEMPLATE['name'])
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
self.configure_mocks(self, SanManagerModule)
|
||||
self.resource = self.mock_ov_client.san_managers
|
||||
|
||||
def test_should_add_new_san_manager(self):
|
||||
self.resource.get_by_name.return_value = []
|
||||
self.resource.get_provider_uri.return_value = '/rest/fc-sans/providers/123/device-managers'
|
||||
self.resource.add.return_value = DEFAULT_SAN_MANAGER_TEMPLATE
|
||||
|
||||
self.mock_ansible_module.params = self.PARAMS_FOR_PRESENT
|
||||
|
||||
SanManagerModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=SanManagerModule.MSG_CREATED,
|
||||
ansible_facts=dict(san_manager=DEFAULT_SAN_MANAGER_TEMPLATE)
|
||||
)
|
||||
|
||||
def test_should_find_provider_uri_to_add(self):
|
||||
self.resource.get_by_name.return_value = []
|
||||
self.resource.get_provider_uri.return_value = '/rest/fc-sans/providers/123/device-managers'
|
||||
self.resource.add.return_value = DEFAULT_SAN_MANAGER_TEMPLATE
|
||||
|
||||
self.mock_ansible_module.params = self.PARAMS_FOR_PRESENT
|
||||
|
||||
SanManagerModule().run()
|
||||
|
||||
provider_display_name = DEFAULT_SAN_MANAGER_TEMPLATE['providerDisplayName']
|
||||
self.resource.get_provider_uri.assert_called_once_with(provider_display_name)
|
||||
|
||||
def test_should_not_update_when_data_is_equals(self):
|
||||
output_data = deepcopy(DEFAULT_SAN_MANAGER_TEMPLATE)
|
||||
output_data.pop('connectionInfo')
|
||||
self.resource.get_by_name.return_value = deepcopy(DEFAULT_SAN_MANAGER_TEMPLATE)
|
||||
self.resource.get_provider_uri.return_value = '/rest/fc-sans/providers/123/device-managers'
|
||||
|
||||
self.mock_ansible_module.params = self.PARAMS_FOR_PRESENT
|
||||
|
||||
SanManagerModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
msg=SanManagerModule.MSG_ALREADY_PRESENT,
|
||||
ansible_facts=dict(san_manager=output_data)
|
||||
)
|
||||
|
||||
def test_update_when_data_has_modified_attributes(self):
|
||||
data_merged = deepcopy(DEFAULT_SAN_MANAGER_TEMPLATE)
|
||||
data_merged['fabricType'] = 'DirectAttach'
|
||||
|
||||
self.resource.get_by_name.return_value = DEFAULT_SAN_MANAGER_TEMPLATE
|
||||
self.resource.get_provider_uri.return_value = '/rest/fc-sans/providers/123/device-managers'
|
||||
|
||||
self.resource.update.return_value = data_merged
|
||||
self.mock_ansible_module.params = self.PARAMS_WITH_CHANGES
|
||||
|
||||
SanManagerModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=SanManagerModule.MSG_UPDATED,
|
||||
ansible_facts=dict(san_manager=data_merged)
|
||||
)
|
||||
|
||||
def test_update_should_not_send_connection_info_when_not_informed_on_data(self):
|
||||
merged_data = deepcopy(DEFAULT_SAN_MANAGER_TEMPLATE)
|
||||
merged_data['refreshState'] = 'RefreshPending'
|
||||
output_data = deepcopy(merged_data)
|
||||
output_data.pop('connectionInfo')
|
||||
|
||||
self.resource.get_by_name.return_value = DEFAULT_SAN_MANAGER_TEMPLATE
|
||||
self.resource.get_provider_uri.return_value = '/rest/fc-sans/providers/123/device-managers'
|
||||
|
||||
self.resource.update.return_value = merged_data
|
||||
self.mock_ansible_module.params = self.PARAMS_WITH_CHANGES
|
||||
|
||||
SanManagerModule().run()
|
||||
|
||||
self.resource.update.assert_called_once_with(resource=output_data, id_or_uri=output_data['uri'])
|
||||
|
||||
def test_should_remove_san_manager(self):
|
||||
self.resource.get_by_name.return_value = deepcopy(DEFAULT_SAN_MANAGER_TEMPLATE)
|
||||
self.resource.get_provider_uri.return_value = '/rest/fc-sans/providers/123/device-managers'
|
||||
|
||||
self.mock_ansible_module.params = self.PARAMS_FOR_ABSENT.copy()
|
||||
|
||||
SanManagerModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=SanManagerModule.MSG_DELETED
|
||||
)
|
||||
|
||||
def test_should_do_nothing_when_san_manager_not_exist(self):
|
||||
self.resource.get_by_name.return_value = []
|
||||
|
||||
self.mock_ansible_module.params = self.PARAMS_FOR_ABSENT.copy()
|
||||
|
||||
SanManagerModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
msg=SanManagerModule.MSG_ALREADY_ABSENT
|
||||
)
|
||||
|
||||
def test_should_fail_when_name_not_found(self):
|
||||
self.resource.get_by_name.return_value = []
|
||||
self.resource.get_provider_uri.return_value = None
|
||||
|
||||
self.mock_ansible_module.params = self.PARAMS_FOR_PRESENT
|
||||
|
||||
SanManagerModule().run()
|
||||
|
||||
self.mock_ansible_module.fail_json.assert_called_once_with(
|
||||
exception=mock.ANY,
|
||||
msg="The provider 'Brocade Network Advisor' was not found."
|
||||
)
|
||||
|
||||
def test_should_fail_when_name_and_hosts_in_connectionInfo_missing(self):
|
||||
bad_params = deepcopy(self.PARAMS_FOR_PRESENT)
|
||||
bad_params['data'].pop('name')
|
||||
bad_params['data'].pop('connectionInfo')
|
||||
|
||||
self.mock_ansible_module.params = bad_params
|
||||
|
||||
SanManagerModule().run()
|
||||
|
||||
msg = 'A "name" or "connectionInfo" must be provided inside the "data" field for this operation. '
|
||||
msg += 'If a "connectionInfo" is provided, the "Host" name is considered as the "name" for the resource.'
|
||||
|
||||
self.mock_ansible_module.fail_json.assert_called_once_with(exception=mock.ANY, msg=msg)
|
||||
|
||||
def test_connection_information_set_should_set_the_connection_information(self):
|
||||
data_merged = deepcopy(DEFAULT_SAN_MANAGER_TEMPLATE)
|
||||
data_merged['fabricType'] = 'DirectAttach'
|
||||
|
||||
self.resource.get_by_name.return_value = DEFAULT_SAN_MANAGER_TEMPLATE
|
||||
self.resource.get_provider_uri.return_value = '/rest/fc-sans/providers/123/device-managers'
|
||||
|
||||
self.resource.update.return_value = data_merged
|
||||
self.mock_ansible_module.params = self.PARAMS_FOR_CONNECTION_INFORMATION_SET
|
||||
|
||||
SanManagerModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=SanManagerModule.MSG_UPDATED,
|
||||
ansible_facts=dict(san_manager=data_merged)
|
||||
)
|
||||
|
||||
def test_should_add_new_san_manager_when_connection_information_set_called_without_resource(self):
|
||||
self.resource.get_by_name.return_value = []
|
||||
self.resource.get_provider_uri.return_value = '/rest/fc-sans/providers/123/device-managers'
|
||||
self.resource.add.return_value = DEFAULT_SAN_MANAGER_TEMPLATE
|
||||
|
||||
self.mock_ansible_module.params = self.PARAMS_FOR_CONNECTION_INFORMATION_SET
|
||||
|
||||
SanManagerModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
msg=SanManagerModule.MSG_CREATED,
|
||||
ansible_facts=dict(san_manager=DEFAULT_SAN_MANAGER_TEMPLATE)
|
||||
)
|
||||
|
||||
def test_should_fail_when_required_attribute_missing(self):
|
||||
bad_params = deepcopy(self.PARAMS_FOR_CONNECTION_INFORMATION_SET)
|
||||
bad_params['data'] = self.PARAMS_FOR_CONNECTION_INFORMATION_SET['data'].copy()
|
||||
bad_params['data'].pop('connectionInfo')
|
||||
|
||||
self.resource.get_by_name.return_value = DEFAULT_SAN_MANAGER_TEMPLATE
|
||||
self.resource.get_provider_uri.return_value = '/rest/fc-sans/providers/123/device-managers'
|
||||
|
||||
self.mock_ansible_module.params = bad_params
|
||||
|
||||
SanManagerModule().run()
|
||||
|
||||
msg = 'A connectionInfo field is required for this operation.'
|
||||
|
||||
self.mock_ansible_module.fail_json.assert_called_once_with(exception=mock.ANY, msg=msg)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -0,0 +1,68 @@
|
|||
# Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||
from .oneview_module_loader import SanManagerInfoModule
|
||||
from .hpe_test_utils import FactsParamsTestCase
|
||||
|
||||
|
||||
class SanManagerInfoSpec(unittest.TestCase, FactsParamsTestCase):
|
||||
ERROR_MSG = 'Fake message error'
|
||||
|
||||
PARAMS_GET_ALL = dict(
|
||||
config='config.json',
|
||||
provider_display_name=None
|
||||
)
|
||||
|
||||
PARAMS_GET_BY_PROVIDER_DISPLAY_NAME = dict(
|
||||
config='config.json',
|
||||
provider_display_name="Brocade Network Advisor"
|
||||
)
|
||||
|
||||
PRESENT_SAN_MANAGERS = [{
|
||||
"providerDisplayName": "Brocade Network Advisor",
|
||||
"uri": "/rest/fc-sans/device-managers//d60efc8a-15b8-470c-8470-738d16d6b319"
|
||||
}]
|
||||
|
||||
def setUp(self):
|
||||
self.configure_mocks(self, SanManagerInfoModule)
|
||||
self.san_managers = self.mock_ov_client.san_managers
|
||||
|
||||
FactsParamsTestCase.configure_client_mock(self, self.san_managers)
|
||||
|
||||
def test_should_get_all(self):
|
||||
self.san_managers.get_all.return_value = self.PRESENT_SAN_MANAGERS
|
||||
self.mock_ansible_module.params = self.PARAMS_GET_ALL
|
||||
|
||||
SanManagerInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
san_managers=self.PRESENT_SAN_MANAGERS
|
||||
)
|
||||
|
||||
def test_should_get_by_display_name(self):
|
||||
self.san_managers.get_by_provider_display_name.return_value = self.PRESENT_SAN_MANAGERS[0]
|
||||
self.mock_ansible_module.params = self.PARAMS_GET_BY_PROVIDER_DISPLAY_NAME
|
||||
|
||||
SanManagerInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
san_managers=self.PRESENT_SAN_MANAGERS
|
||||
)
|
||||
|
||||
def test_should_return_empty_list_when_get_by_display_name_is_null(self):
|
||||
self.san_managers.get_by_provider_display_name.return_value = None
|
||||
self.mock_ansible_module.params = self.PARAMS_GET_BY_PROVIDER_DISPLAY_NAME
|
||||
|
||||
SanManagerInfoModule().run()
|
||||
|
||||
self.mock_ansible_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
san_managers=[]
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Add table
Add a link
Reference in a new issue