diff options
author | Zuul <zuul@review.openstack.org> | 2018-01-22 23:30:27 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2018-01-22 23:30:28 +0000 |
commit | 83e3afbdee95ad5e01d790f54da25653decf2b25 (patch) | |
tree | 9da4bfb516ecafff708a550f7c9945d2da54040f /ironic/tests/unit/drivers | |
parent | c3ed7dfb9e7e878f30fbff68f066a409879b7a3b (diff) | |
parent | 346a9a3bfc5312deb78bda8a82ae238e031413bd (diff) | |
download | ironic-83e3afbdee95ad5e01d790f54da25653decf2b25.tar.gz |
Merge "Add XClarity Driver"
Diffstat (limited to 'ironic/tests/unit/drivers')
7 files changed, 388 insertions, 0 deletions
diff --git a/ironic/tests/unit/drivers/modules/xclarity/__init__.py b/ironic/tests/unit/drivers/modules/xclarity/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/ironic/tests/unit/drivers/modules/xclarity/__init__.py diff --git a/ironic/tests/unit/drivers/modules/xclarity/test_common.py b/ironic/tests/unit/drivers/modules/xclarity/test_common.py new file mode 100644 index 000000000..563e48f06 --- /dev/null +++ b/ironic/tests/unit/drivers/modules/xclarity/test_common.py @@ -0,0 +1,65 @@ +# Copyright 2017 Lenovo, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock + +from oslo_utils import importutils + +from ironic.drivers.modules.xclarity import common +from ironic.tests.unit.db import base as db_base +from ironic.tests.unit.db import utils as db_utils +from ironic.tests.unit.objects import utils as obj_utils + +xclarity_exceptions = importutils.try_import('xclarity_client.exceptions') +xclarity_constants = importutils.try_import('xclarity_client.constants') + + +class XClarityCommonTestCase(db_base.DbTestCase): + + def setUp(self): + super(XClarityCommonTestCase, self).setUp() + + self.config(manager_ip='1.2.3.4', group='xclarity') + self.config(username='user', group='xclarity') + self.config(password='password', group='xclarity') + + self.node = obj_utils.create_test_node( + self.context, driver='fake-xclarity', + properties=db_utils.get_test_xclarity_properties(), + driver_info=db_utils.get_test_xclarity_driver_info(), + ) + + def test_get_server_hardware_id(self): + driver_info = self.node.driver_info + driver_info['xclarity_hardware_id'] = 'test' + self.node.driver_info = driver_info + result = common.get_server_hardware_id(self.node) + self.assertEqual(result, 'test') + + @mock.patch.object(common, 'get_server_hardware_id', + spec_set=True, autospec=True) + @mock.patch.object(common, 'get_xclarity_client', + spec_set=True, autospec=True) + def test_check_node_managed_by_xclarity(self, mock_xc_client, + mock_validate_driver_info): + driver_info = self.node.driver_info + driver_info['xclarity_hardware_id'] = 'abcd' + self.node.driver_info = driver_info + + xclarity_client = mock_xc_client() + mock_validate_driver_info.return_value = '12345' + common.is_node_managed_by_xclarity(xclarity_client, + self.node) + xclarity_client.is_node_managed.assert_called_once_with('12345') diff --git a/ironic/tests/unit/drivers/modules/xclarity/test_management.py b/ironic/tests/unit/drivers/modules/xclarity/test_management.py new file mode 100644 index 000000000..1d4fc9209 --- /dev/null +++ b/ironic/tests/unit/drivers/modules/xclarity/test_management.py @@ -0,0 +1,125 @@ +# Copyright 2017 Lenovo, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import sys + +import six + +import mock + +from oslo_utils import importutils + +from ironic.common import boot_devices +from ironic.conductor import task_manager +from ironic.drivers.modules.xclarity import common +from ironic.drivers.modules.xclarity import management +from ironic.tests.unit.conductor import mgr_utils +from ironic.tests.unit.db import base as db_base +from ironic.tests.unit.db import utils as db_utils +from ironic.tests.unit.objects import utils as obj_utils + + +xclarity_client_exceptions = importutils.try_import( + 'xclarity_client.exceptions') + + +@mock.patch.object(common, 'get_xclarity_client', spect_set=True, + autospec=True) +class XClarityManagementDriverTestCase(db_base.DbTestCase): + + def setUp(self): + super(XClarityManagementDriverTestCase, self).setUp() + self.config(enabled_hardware_types=['xclarity'], + enabled_power_interfaces=['xclarity'], + enabled_management_interfaces=['xclarity']) + mgr_utils.mock_the_extension_manager( + driver='xclarity', namespace='ironic.hardware.types') + self.node = obj_utils.create_test_node( + self.context, + driver='xclarity', + driver_info=db_utils.get_test_xclarity_driver_info()) + + @mock.patch.object(common, 'get_server_hardware_id', + spect_set=True, autospec=True) + def test_validate(self, mock_validate, mock_get_xc_client): + with task_manager.acquire(self.context, self.node.uuid) as task: + task.driver.management.validate(task) + common.get_server_hardware_id(task.node) + mock_validate.assert_called_with(task.node) + + def test_get_properties(self, mock_get_xc_client): + + expected = common.REQUIRED_ON_DRIVER_INFO + self.assertItemsEqual(expected, + self.node.driver_info) + + @mock.patch.object(management.XClarityManagement, 'get_boot_device', + return_value='pxe') + def test_set_boot_device(self, mock_get_boot_device, + mock_get_xc_client): + with task_manager.acquire(self.context, self.node.uuid) as task: + task.driver.management.set_boot_device(task, 'pxe') + result = task.driver.management.get_boot_device(task) + self.assertEqual(result, 'pxe') + + def test_set_boot_device_fail(self, mock_get_xc_client): + with task_manager.acquire(self.context, self.node.uuid) as task: + xclarity_client_exceptions.XClarityError = Exception + sys.modules['xclarity_client.exceptions'] = ( + xclarity_client_exceptions) + if 'ironic.drivers.modules.xclarity' in sys.modules: + six.moves.reload_module( + sys.modules['ironic.drivers.modules.xclarity']) + ex = common.XClarityError('E') + mock_get_xc_client.return_value.set_node_boot_info.side_effect = ex + self.assertRaises(common.XClarityError, + task.driver.management.set_boot_device, + task, + "pxe") + + def test_get_supported_boot_devices(self, mock_get_xc_client): + with task_manager.acquire(self.context, self.node.uuid) as task: + expected = [boot_devices.PXE, boot_devices.BIOS, + boot_devices.DISK, boot_devices.CDROM] + self.assertItemsEqual( + expected, + task.driver.management.get_supported_boot_devices(task)) + + @mock.patch.object( + management.XClarityManagement, + 'get_boot_device', + return_value={'boot_device': 'pxe', 'persistent': False}) + def test_get_boot_device(self, mock_get_boot_device, mock_get_xc_client): + reference = {'boot_device': 'pxe', 'persistent': False} + with task_manager.acquire(self.context, self.node.uuid) as task: + expected_boot_device = task.driver.management.get_boot_device( + task=task) + + self.assertEqual(reference, expected_boot_device) + + def test_get_boot_device_fail(self, mock_xc_client): + with task_manager.acquire(self.context, self.node.uuid) as task: + xclarity_client_exceptions.XClarityError = Exception + sys.modules['xclarity_client.exceptions'] = ( + xclarity_client_exceptions) + if 'ironic.drivers.modules.xclarity' in sys.modules: + six.moves.reload_module( + sys.modules['ironic.drivers.modules.xclarity']) + ex = common.XClarityError('E') + mock_xc_client.return_value.get_node_all_boot_info.side_effect = ex + self.assertRaises( + common.XClarityError, + task.driver.management.get_boot_device, + task) diff --git a/ironic/tests/unit/drivers/modules/xclarity/test_power.py b/ironic/tests/unit/drivers/modules/xclarity/test_power.py new file mode 100644 index 000000000..f695c4c29 --- /dev/null +++ b/ironic/tests/unit/drivers/modules/xclarity/test_power.py @@ -0,0 +1,113 @@ +# Copyright 2017 Lenovo, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +STATE_POWER_ON = "power on" +STATE_POWER_OFF = "power off" +STATE_POWERING_ON = "power on" +STATE_POWERING_OFF = "power on" + +import sys + +import six + +import mock + +from oslo_utils import importutils + +from ironic.common import states +from ironic.conductor import task_manager +from ironic.drivers.modules.xclarity import common +from ironic.drivers.modules.xclarity import power +from ironic.tests.unit.conductor import mgr_utils +from ironic.tests.unit.db import base as db_base +from ironic.tests.unit.db import utils as db_utils +from ironic.tests.unit.objects import utils as obj_utils + +xclarity_constants = importutils.try_import('xclarity_client.constants') +xclarity_client_exceptions = importutils.try_import( + 'xclarity_client.exceptions') + + +@mock.patch.object(common, 'get_xclarity_client', + spect_set=True, autospec=True) +class XClarityPowerDriverTestCase(db_base.DbTestCase): + + def setUp(self): + super(XClarityPowerDriverTestCase, self).setUp() + self.config(enabled_hardware_types=['xclarity'], + enabled_power_interfaces=['xclarity'], + enabled_management_interfaces=['xclarity']) + mgr_utils.mock_the_extension_manager( + driver='xclarity', namespace='ironic.hardware.types') + self.node = obj_utils.create_test_node( + self.context, + driver='xclarity', + driver_info=db_utils.get_test_xclarity_driver_info()) + + def test_get_properties(self, mock_get_xc_client): + expected = common.REQUIRED_ON_DRIVER_INFO + self.assertItemsEqual(expected, + self.node.driver_info) + + @mock.patch.object(common, 'get_server_hardware_id', + spect_set=True, autospec=True) + def test_validate(self, mock_validate_driver_info, mock_get_xc_client): + with task_manager.acquire(self.context, self.node.uuid) as task: + task.driver.power.validate(task) + common.get_server_hardware_id(task.node) + mock_validate_driver_info.assert_called_with(task.node) + + @mock.patch.object(power.XClarityPower, 'get_power_state', + return_value=STATE_POWER_ON) + def test_get_power_state(self, mock_get_power_state, mock_get_xc_client): + with task_manager.acquire(self.context, self.node.uuid) as task: + result = power.XClarityPower.get_power_state(task) + self.assertEqual(STATE_POWER_ON, result) + + def test_get_power_state_fail(self, mock_xc_client): + with task_manager.acquire(self.context, self.node.uuid) as task: + xclarity_client_exceptions.XClarityError = Exception + sys.modules['xclarity_client.exceptions'] = ( + xclarity_client_exceptions) + if 'ironic.drivers.modules.xclarity' in sys.modules: + six.moves.reload_module( + sys.modules['ironic.drivers.modules.xclarity']) + ex = common.XClarityError('E') + mock_xc_client.return_value.get_node_power_status.side_effect = ex + self.assertRaises(common.XClarityError, + task.driver.power.get_power_state, + task) + + @mock.patch.object(power.XClarityPower, 'get_power_state', + return_value=states.POWER_ON) + def test_set_power(self, mock_set_power_state, mock_get_xc_client): + with task_manager.acquire(self.context, self.node.uuid) as task: + task.driver.power.set_power_state(task, states.POWER_ON) + expected = task.driver.power.get_power_state(task) + self.assertEqual(expected, states.POWER_ON) + + def test_set_power_fail(self, mock_xc_client): + with task_manager.acquire(self.context, self.node.uuid) as task: + xclarity_client_exceptions.XClarityError = Exception + sys.modules['xclarity_client.exceptions'] = ( + xclarity_client_exceptions) + if 'ironic.drivers.modules.xclarity' in sys.modules: + six.moves.reload_module( + sys.modules['ironic.drivers.modules.xclarity']) + ex = common.XClarityError('E') + mock_xc_client.return_value.set_node_power_status.side_effect = ex + self.assertRaises(common.XClarityError, + task.driver.power.set_power_state, + task, states.POWER_OFF) diff --git a/ironic/tests/unit/drivers/test_xclarity.py b/ironic/tests/unit/drivers/test_xclarity.py new file mode 100644 index 000000000..fdd94852f --- /dev/null +++ b/ironic/tests/unit/drivers/test_xclarity.py @@ -0,0 +1,49 @@ +# Copyright 2017 Lenovo, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +Test class for XClarity Driver +""" + +from ironic.conductor import task_manager +from ironic.drivers.modules import agent +from ironic.drivers.modules import iscsi_deploy +from ironic.drivers.modules import pxe +from ironic.drivers.xclarity import management as xc_management +from ironic.drivers.xclarity import power as xc_power + +from ironic.tests.unit.db import base as db_base +from ironic.tests.unit.objects import utils as obj_utils + + +class XClarityHardwareTestCase(db_base.DbTestCase): + + def setUp(self): + super(XClarityHardwareTestCase, self).setUp() + self.config(enabled_hardware_types=['xclarity'], + enabled_power_interfaces=['xclarity'], + enabled_management_interfaces=['xclarity']) + + def test_default_interfaces(self): + node = obj_utils.create_test_node(self.context, driver='xclarity') + with task_manager.acquire(self.context, node.id) as task: + self.assertIsInstance(task.driver.boot, + pxe.PXEBoot) + self.assertIsInstance(task.driver.deploy, + iscsi_deploy.ISCSIDeploy, + agent.AgentDeploy) + self.assertIsInstance(task.driver.management, + xc_management.XClarityManagement) + self.assertIsInstance(task.driver.power, + xc_power.XClarityPower) diff --git a/ironic/tests/unit/drivers/third_party_driver_mock_specs.py b/ironic/tests/unit/drivers/third_party_driver_mock_specs.py index d3c006b14..2574743c9 100644 --- a/ironic/tests/unit/drivers/third_party_driver_mock_specs.py +++ b/ironic/tests/unit/drivers/third_party_driver_mock_specs.py @@ -180,3 +180,21 @@ SUSHY_CONSTANTS_SPEC = ( 'BOOT_SOURCE_ENABLED_CONTINUOUS', 'BOOT_SOURCE_ENABLED_ONCE', ) + +XCLARITY_SPEC = ( + 'client', + 'states', + 'exceptions', + 'models', + 'utils', +) + +XCLARITY_CLIENT_CLS_SPEC = ( +) + +XCLARITY_STATES_SPEC = ( + 'STATE_POWERING_OFF', + 'STATE_POWERING_ON', + 'STATE_POWER_OFF', + 'STATE_POWER_ON', +) diff --git a/ironic/tests/unit/drivers/third_party_driver_mocks.py b/ironic/tests/unit/drivers/third_party_driver_mocks.py index 430293bb6..28e374198 100644 --- a/ironic/tests/unit/drivers/third_party_driver_mocks.py +++ b/ironic/tests/unit/drivers/third_party_driver_mocks.py @@ -270,3 +270,21 @@ if not sushy: if 'ironic.drivers.modules.redfish' in sys.modules: six.moves.reload_module( sys.modules['ironic.drivers.modules.redfish']) + +xclarity_client = importutils.try_import('xclarity_client') +if not xclarity_client: + xclarity_client = mock.MagicMock(spec_set=mock_specs.XCLARITY_SPEC) + sys.modules['xclarity_client'] = xclarity_client + sys.modules['xclarity_client.client'] = xclarity_client.client + states = mock.MagicMock( + spec_set=mock_specs.XCLARITY_STATES_SPEC, + STATE_POWER_ON="power on", + STATE_POWER_OFF="power off", + STATE_POWERING_ON="powering_on", + STATE_POWERING_OFF="powering_off") + sys.modules['xclarity_client.states'] = states + sys.modules['xclarity_client.exceptions'] = xclarity_client.exceptions + sys.modules['xclarity_client.utils'] = xclarity_client.utils + xclarity_client.exceptions.XClarityException = type('XClarityException', + (Exception,), {}) + sys.modules['xclarity_client.models'] = xclarity_client.models |