Convert conductor manager unit tests to hardware types

As a side effect, correctly group tests in test cases.
The (partly broken with hardware types) mock_the_extension_manager
call is removed and replaced with only mocking what is really needed.

Change-Id: I1af9fa960920451916c20cdf7167a36fbcf613a7
This commit is contained in:
Dmitry Tantsur 2018-05-17 18:43:30 +02:00
parent a684883d22
commit a8e6fae750
4 changed files with 688 additions and 787 deletions

View File

@ -17,13 +17,10 @@
"""Test utils for Ironic Managers."""
import fixtures
from futurist import periodics
import mock
from oslo_utils import strutils
from oslo_utils import uuidutils
import pkg_resources
from stevedore import dispatch
from ironic.common import driver_factory
from ironic.common import exception
@ -32,47 +29,6 @@ from ironic.conductor import manager
from ironic import objects
class DriverFactoryFixture(fixtures.Fixture):
"""A fixture to mock a factor of drivers/hardware types/interfaces."""
def __init__(self, factory_class, ):
self.factory_class = factory_class
def mock_the_extension_manager(driver="fake", namespace="ironic.drivers"):
"""Get a fake stevedore NameDispatchExtensionManager instance.
:param namespace: A string representing the namespace over which to
search for entrypoints.
:returns mock_ext_mgr: A DriverFactory instance that has been faked.
:returns mock_ext: A real plugin loaded by mock_ext_mgr in the specified
namespace.
"""
entry_point = None
for ep in list(pkg_resources.iter_entry_points(namespace)):
s = "%s" % ep
if driver == s[:s.index(' =')]:
entry_point = ep
break
# NOTE(lucasagomes): Initialize the _extension_manager before
# instantiating a DriverFactory class to avoid
# a real NameDispatchExtensionManager to be created
# with the real namespace.
driver_factory.DriverFactory._extension_manager = (
dispatch.NameDispatchExtensionManager('ironic.no-such-namespace',
lambda x: True))
mock_ext_mgr = driver_factory.DriverFactory()
mock_ext = mock_ext_mgr._extension_manager._load_one_plugin(
entry_point, True, [], {}, False)
mock_ext_mgr._extension_manager.extensions = [mock_ext]
mock_ext_mgr._extension_manager.by_name = dict((e.name, e)
for e in [mock_ext])
return (mock_ext_mgr, mock_ext)
class CommonMixIn(object):
@staticmethod
def _create_node(**kwargs):

View File

@ -1,50 +0,0 @@
# coding=utf-8
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# 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.
"""Tests for Ironic Manager test utils."""
from ironic.tests import base
from ironic.tests.unit.conductor import mgr_utils
class UtilsTestCase(base.TestCase):
def test_fails_to_load_extension(self):
self.assertRaises(AttributeError,
mgr_utils.mock_the_extension_manager,
'fake',
'bad.namespace')
self.assertRaises(AttributeError,
mgr_utils.mock_the_extension_manager,
'no-such-driver',
'ironic.drivers')
def test_get_mockable_ext_mgr(self):
(mgr, ext) = mgr_utils.mock_the_extension_manager('fake',
'ironic.drivers')
# confirm that stevedore did not scan the actual entrypoints
self.assertNotEqual(mgr._extension_manager.namespace, 'ironic.drivers')
# confirm mgr has only one extension
self.assertEqual(1, len(mgr._extension_manager.extensions))
# confirm that we got a reference to the extension in this manager
self.assertEqual(ext, mgr._extension_manager.extensions[0])
# confirm that it is the "fake" driver we asked for
self.assertEqual("fake = ironic.drivers.fake:FakeDriver",
"%s" % ext.entry_point)
# Confirm driver is loaded
self.assertIn('fake', mgr.names)

View File

@ -30,6 +30,7 @@ from ironic.conductor import notification_utils
from ironic.conductor import task_manager
from ironic.drivers import fake_hardware
from ironic.drivers import generic
from ironic.drivers.modules import fake
from ironic import objects
from ironic.objects import fields
from ironic.tests import base as tests_base
@ -447,76 +448,85 @@ class RegisterInterfacesTestCase(mgr_utils.ServiceSetUpMixin,
self.assertFalse(reg_mock.called)
@mock.patch.object(fake.FakeConsole, 'start_console', autospec=True)
@mock.patch.object(notification_utils, 'emit_console_notification')
class StartConsolesTestCase(mgr_utils.ServiceSetUpMixin, db_base.DbTestCase):
@mock.patch.object(notification_utils, 'emit_console_notification')
def test__start_consoles(self, mock_notify):
def test__start_consoles(self, mock_notify, mock_start_console):
obj_utils.create_test_node(self.context,
driver='fake',
driver='fake-hardware',
console_enabled=True)
obj_utils.create_test_node(
self.context,
uuid=uuidutils.generate_uuid(),
driver='fake',
driver='fake-hardware',
console_enabled=True
)
obj_utils.create_test_node(
self.context,
uuid=uuidutils.generate_uuid(),
driver='fake'
driver='fake-hardware'
)
self._start_service()
with mock.patch.object(self.driver.console,
'start_console') as mock_start_console:
self.service._start_consoles(self.context)
self.assertEqual(2, mock_start_console.call_count)
mock_notify.assert_has_calls(
[mock.call(mock.ANY, 'console_restore',
fields.NotificationStatus.START),
mock.call(mock.ANY, 'console_restore',
fields.NotificationStatus.END)])
self.service._start_consoles(self.context)
self.assertEqual(2, mock_start_console.call_count)
mock_notify.assert_has_calls(
[mock.call(mock.ANY, 'console_restore',
fields.NotificationStatus.START),
mock.call(mock.ANY, 'console_restore',
fields.NotificationStatus.END)])
@mock.patch.object(notification_utils, 'emit_console_notification')
def test__start_consoles_no_console_enabled(self, mock_notify):
def test__start_consoles_no_console_enabled(self, mock_notify,
mock_start_console):
obj_utils.create_test_node(self.context,
driver='fake',
driver='fake-hardware',
console_enabled=False)
self._start_service()
with mock.patch.object(self.driver.console,
'start_console') as mock_start_console:
self.service._start_consoles(self.context)
self.assertFalse(mock_start_console.called)
self.assertFalse(mock_notify.called)
self.service._start_consoles(self.context)
self.assertFalse(mock_start_console.called)
self.assertFalse(mock_notify.called)
@mock.patch.object(notification_utils, 'emit_console_notification')
def test__start_consoles_failed(self, mock_notify):
def test__start_consoles_failed(self, mock_notify, mock_start_console):
test_node = obj_utils.create_test_node(self.context,
driver='fake',
driver='fake-hardware',
console_enabled=True)
self._start_service()
with mock.patch.object(self.driver.console,
'start_console') as mock_start_console:
mock_start_console.side_effect = Exception()
self.service._start_consoles(self.context)
mock_start_console.assert_called_once_with(mock.ANY)
test_node.refresh()
self.assertFalse(test_node.console_enabled)
self.assertIsNotNone(test_node.last_error)
mock_notify.assert_has_calls(
[mock.call(mock.ANY, 'console_restore',
fields.NotificationStatus.START),
mock.call(mock.ANY, 'console_restore',
fields.NotificationStatus.ERROR)])
mock_start_console.side_effect = Exception()
self.service._start_consoles(self.context)
mock_start_console.assert_called_once_with(mock.ANY, mock.ANY)
test_node.refresh()
self.assertFalse(test_node.console_enabled)
self.assertIsNotNone(test_node.last_error)
mock_notify.assert_has_calls(
[mock.call(mock.ANY, 'console_restore',
fields.NotificationStatus.START),
mock.call(mock.ANY, 'console_restore',
fields.NotificationStatus.ERROR)])
@mock.patch.object(notification_utils, 'emit_console_notification')
@mock.patch.object(base_manager, 'LOG')
def test__start_consoles_node_locked(self, log_mock, mock_notify):
def test__start_consoles_node_locked(self, log_mock, mock_notify,
mock_start_console):
test_node = obj_utils.create_test_node(self.context,
driver='fake',
driver='fake-hardware',
console_enabled=True,
reservation='fake-host')
self._start_service()
with mock.patch.object(self.driver.console,
'start_console') as mock_start_console:
self.service._start_consoles(self.context)
self.assertFalse(mock_start_console.called)
test_node.refresh()
self.assertTrue(test_node.console_enabled)
self.assertIsNone(test_node.last_error)
self.assertTrue(log_mock.warning.called)
self.assertFalse(mock_notify.called)
@mock.patch.object(base_manager, 'LOG')
def test__start_consoles_node_not_found(self, log_mock, mock_notify,
mock_start_console):
test_node = obj_utils.create_test_node(self.context,
driver='fake-hardware',
console_enabled=True)
self._start_service()
with mock.patch.object(task_manager, 'acquire') as mock_acquire:
mock_acquire.side_effect = exception.NodeNotFound(node='not found')
self.service._start_consoles(self.context)
self.assertFalse(mock_start_console.called)
test_node.refresh()
@ -524,22 +534,3 @@ class StartConsolesTestCase(mgr_utils.ServiceSetUpMixin, db_base.DbTestCase):
self.assertIsNone(test_node.last_error)
self.assertTrue(log_mock.warning.called)
self.assertFalse(mock_notify.called)
@mock.patch.object(notification_utils, 'emit_console_notification')
@mock.patch.object(base_manager, 'LOG')
def test__start_consoles_node_not_found(self, log_mock, mock_notify):
test_node = obj_utils.create_test_node(self.context,
driver='fake',
console_enabled=True)
self._start_service()
with mock.patch.object(task_manager, 'acquire') as mock_acquire:
mock_acquire.side_effect = exception.NodeNotFound(node='not found')
with mock.patch.object(self.driver.console,
'start_console') as mock_start_console:
self.service._start_consoles(self.context)
self.assertFalse(mock_start_console.called)
test_node.refresh()
self.assertTrue(test_node.console_enabled)
self.assertIsNone(test_node.last_error)
self.assertTrue(log_mock.warning.called)
self.assertFalse(mock_notify.called)

File diff suppressed because it is too large Load Diff