Add missing 'autospec' to unit tests - /unit/common/

Proposing adding [H210] hacking check in tox.ini to require ‘autospec’,
‘spec’, or ‘spec_set’ in mock.patch/mock.patch.object calls.

This patch adds missing 'autospec' to unit tests in /tests/unit/dhcp/.

By using autospec=True, assert errors can be caught. [H210] requires that some
value for autospec, spec, or spec_set is defined. It could be autospec=False.

More patches to follow to cover all the unit tests to enable [H210] in
tox.ini.

Change-Id: I579cc09fcaa60b25697f739ddcd96770d55abd20
This commit is contained in:
Ramamani Yeleswarapu 2017-10-17 10:30:21 -07:00
parent 62d5325c94
commit 2e24088f92
11 changed files with 52 additions and 49 deletions

View File

@ -159,7 +159,7 @@ class TestCinderUtils(db_base.DbTestCase):
self.assertIsNone(cinder._get_attachment_id(self.node, unattached))
self.assertIsNone(cinder._get_attachment_id(self.node, no_attachment))
@mock.patch.object(datetime, 'datetime')
@mock.patch.object(datetime, 'datetime', autospec=True)
def test__create_metadata_dictionary(self, mock_datetime):
fake_time = '2017-06-05T00:33:26.574676'
mock_utcnow = mock.Mock()

View File

@ -38,10 +38,10 @@ class RequestContextTestCase(tests_base.TestCase):
"overwrite": True
}
@mock.patch.object(oslo_context.RequestContext, "__init__")
@mock.patch.object(oslo_context.RequestContext, "__init__", autospec=True)
def test_create_context(self, context_mock):
test_context = context.RequestContext()
context_mock.assert_called_once_with()
context_mock.assert_called_once_with(mock.ANY)
self.assertFalse(test_context.is_public_api)
def test_from_dict(self):
@ -62,14 +62,14 @@ class RequestContextTestCase(tests_base.TestCase):
admin_context = context.get_admin_context()
self.assertTrue(admin_context.is_admin)
@mock.patch.object(oslo_context, 'get_current')
@mock.patch.object(oslo_context, 'get_current', autospec=True)
def test_thread_without_context(self, context_get_mock):
self.context.update_store = mock.Mock()
context_get_mock.return_value = None
self.context.ensure_thread_contain_context()
self.context.update_store.assert_called_once_with()
@mock.patch.object(oslo_context, 'get_current')
@mock.patch.object(oslo_context, 'get_current', autospec=True)
def test_thread_with_context(self, context_get_mock):
self.context.update_store = mock.Mock()
context_get_mock.return_value = self.context

View File

@ -84,7 +84,7 @@ class DriverLoadTestCase(db_base.DbTestCase):
['fake'], driver_factory.DriverFactory._extension_manager.names())
self.assertTrue(mock_log.called)
@mock.patch.object(driver_factory, '_warn_if_unsupported')
@mock.patch.object(driver_factory, '_warn_if_unsupported', autospec=True)
def test_driver_init_checks_unsupported(self, mock_warn):
self.config(enabled_drivers=['fake'])
driver_factory.DriverFactory._init_extension_manager()
@ -150,7 +150,7 @@ class GetDriverTestCase(base.TestCase):
class NetworkInterfaceFactoryTestCase(db_base.DbTestCase):
@mock.patch.object(driver_factory, '_warn_if_unsupported')
@mock.patch.object(driver_factory, '_warn_if_unsupported', autospec=True)
def test_build_driver_for_task(self, mock_warn):
# flat, neutron, and noop network interfaces are enabled in base test
# case

View File

@ -156,7 +156,8 @@ class TestGlanceImageService(base.TestCase):
'properties': {},
'owner': None,
}
with mock.patch.object(self.service, 'call', return_value=image):
with mock.patch.object(self.service, 'call', return_value=image,
autospec=True):
image_meta = self.service.show(image_id)
self.service.call.assert_called_once_with('get', image_id)
self.assertEqual(expected, image_meta)
@ -164,7 +165,8 @@ class TestGlanceImageService(base.TestCase):
def test_show_makes_datetimes(self):
image_id = uuidutils.generate_uuid()
image = self._make_datetime_fixture()
with mock.patch.object(self.service, 'call', return_value=image):
with mock.patch.object(self.service, 'call', return_value=image,
autospec=True):
image_meta = self.service.show(image_id)
self.service.call.assert_called_once_with('get', image_id)
self.assertEqual(self.NOW_DATETIME, image_meta['created_at'])

View File

@ -265,7 +265,7 @@ class FileImageServiceTestCase(base.TestCase):
class ServiceGetterTestCase(base.TestCase):
@mock.patch.object(image_service, '_get_glance_session')
@mock.patch.object(image_service, '_get_glance_session', autospec=True)
@mock.patch.object(glance_v2_service.GlanceImageService, '__init__',
return_value=None, autospec=True)
def test_get_glance_image_service(self, glance_service_mock,
@ -277,7 +277,7 @@ class ServiceGetterTestCase(base.TestCase):
self.context)
self.assertFalse(session_mock.called)
@mock.patch.object(image_service, '_get_glance_session')
@mock.patch.object(image_service, '_get_glance_session', autospec=True)
@mock.patch.object(glance_v1_service.GlanceImageService, '__init__',
return_value=None, autospec=True)
def test_get_glance_image_service_default_v1(self, glance_service_mock,
@ -290,7 +290,7 @@ class ServiceGetterTestCase(base.TestCase):
self.context)
self.assertFalse(session_mock.called)
@mock.patch.object(image_service, '_get_glance_session')
@mock.patch.object(image_service, '_get_glance_session', autospec=True)
@mock.patch.object(glance_v2_service.GlanceImageService, '__init__',
return_value=None, autospec=True)
def test_get_glance_image_service_url(self, glance_service_mock,
@ -302,7 +302,7 @@ class ServiceGetterTestCase(base.TestCase):
self.context)
self.assertFalse(session_mock.called)
@mock.patch.object(image_service, '_get_glance_session')
@mock.patch.object(image_service, '_get_glance_session', autospec=True)
@mock.patch.object(glance_v2_service.GlanceImageService, '__init__',
return_value=None, autospec=True)
def test_get_glance_image_service_no_token(self, glance_service_mock,
@ -318,7 +318,7 @@ class ServiceGetterTestCase(base.TestCase):
sess.get_token.assert_called_once_with()
self.assertEqual('admin-token', self.context.auth_token)
@mock.patch.object(image_service, '_get_glance_session')
@mock.patch.object(image_service, '_get_glance_session', autospec=True)
@mock.patch.object(glance_v2_service.GlanceImageService, '__init__',
return_value=None, autospec=True)
def test_get_glance_image_service_token_not_needed(self,

View File

@ -589,7 +589,7 @@ class FsImageTestCase(base.TestCase):
'4', '-boot-info-table', '-b', 'isolinux/isolinux.bin',
'-o', 'tgt_file', 'tmpdir')
@mock.patch.object(os.path, 'isfile', autspec=True)
@mock.patch.object(os.path, 'isfile', autospec=True)
def test_create_isolinux_image_for_bios(self, mock_isfile):
mock_isfile.return_value = False
self._test_create_isolinux_image_for_bios()
@ -599,7 +599,7 @@ class FsImageTestCase(base.TestCase):
self._test_create_isolinux_image_for_bios(
ldlinux_path='path/to/ldlinux.c32')
@mock.patch.object(os.path, 'isfile', autspec=True)
@mock.patch.object(os.path, 'isfile', autospec=True)
def test_create_isolinux_image_for_bios_default_ldlinux(self, mock_isfile):
mock_isfile.side_effect = [False, True]
self._test_create_isolinux_image_for_bios(

View File

@ -25,8 +25,8 @@ from ironic.tests.unit.db import base as db_base
from ironic.tests.unit.objects import utils as object_utils
@mock.patch.object(neutron, '_get_neutron_session')
@mock.patch.object(client.Client, "__init__")
@mock.patch.object(neutron, '_get_neutron_session', autospec=True)
@mock.patch.object(client.Client, "__init__", autospec=True)
class TestNeutronClient(base.TestCase):
def setUp(self):
@ -59,7 +59,7 @@ class TestNeutronClient(base.TestCase):
mock_client_init.return_value = None
neutron.get_client(token=token)
mock_client_init.assert_called_once_with(**expected)
mock_client_init.assert_called_once_with(mock.ANY, **expected)
def test_get_neutron_client_without_token(self, mock_client_init,
mock_session):
@ -72,7 +72,7 @@ class TestNeutronClient(base.TestCase):
'session': sess}
mock_client_init.return_value = None
neutron.get_client(token=None)
mock_client_init.assert_called_once_with(**expected)
mock_client_init.assert_called_once_with(mock.ANY, **expected)
def test_get_neutron_client_with_region(self, mock_client_init,
mock_session):
@ -86,7 +86,7 @@ class TestNeutronClient(base.TestCase):
mock_client_init.return_value = None
neutron.get_client(token=None)
mock_client_init.assert_called_once_with(**expected)
mock_client_init.assert_called_once_with(mock.ANY, **expected)
def test_get_neutron_client_noauth(self, mock_client_init, mock_session):
self.config(auth_strategy='noauth',
@ -101,7 +101,7 @@ class TestNeutronClient(base.TestCase):
mock_client_init.return_value = None
neutron.get_client(token=None)
mock_client_init.assert_called_once_with(**expected)
mock_client_init.assert_called_once_with(mock.ANY, **expected)
def test_out_range_auth_strategy(self, mock_client_init, mock_session):
self.assertRaises(ValueError, cfg.CONF.set_override,
@ -130,7 +130,7 @@ class TestNeutronNetworkActions(db_base.DbTestCase):
self.network_uuid = uuidutils.generate_uuid()
self.client_mock = mock.Mock()
patcher = mock.patch('ironic.common.neutron.get_client',
return_value=self.client_mock)
return_value=self.client_mock, autospec=True)
patcher.start()
self.addCleanup(patcher.stop)
@ -186,7 +186,7 @@ class TestNeutronNetworkActions(db_base.DbTestCase):
self._test_add_ports_to_network(is_client_id=False,
security_groups=None)
@mock.patch.object(neutron, '_verify_security_groups')
@mock.patch.object(neutron, '_verify_security_groups', autospec=True)
def test_add_ports_to_network_with_sg(self, verify_mock):
sg_ids = []
for i in range(2):
@ -301,7 +301,7 @@ class TestNeutronNetworkActions(db_base.DbTestCase):
self.client_mock.create_port.assert_called_once_with(expected_body)
self.assertTrue(vpi_mock.called)
@mock.patch.object(neutron, 'rollback_ports')
@mock.patch.object(neutron, 'rollback_ports', autospec=True)
def test_add_network_all_ports_fail(self, rollback_mock):
# Check that if creating a port fails, the ports are cleaned up
self.client_mock.create_port.side_effect = \
@ -313,7 +313,7 @@ class TestNeutronNetworkActions(db_base.DbTestCase):
self.network_uuid)
rollback_mock.assert_called_once_with(task, self.network_uuid)
@mock.patch.object(neutron, 'LOG')
@mock.patch.object(neutron, 'LOG', autospec=True)
def test_add_network_create_some_ports_fail(self, log_mock):
object_utils.create_test_port(
self.context, node_id=self.node.id,
@ -330,7 +330,7 @@ class TestNeutronNetworkActions(db_base.DbTestCase):
self.assertIn("Some errors were encountered when updating",
log_mock.warning.call_args_list[1][0][0])
@mock.patch.object(neutron, 'remove_neutron_ports')
@mock.patch.object(neutron, 'remove_neutron_ports', autospec=True)
def test_remove_ports_from_network(self, remove_mock):
with task_manager.acquire(self.context, self.node.uuid) as task:
neutron.remove_ports_from_network(task, self.network_uuid)
@ -340,7 +340,7 @@ class TestNeutronNetworkActions(db_base.DbTestCase):
'mac_address': [self.ports[0].address]}
)
@mock.patch.object(neutron, 'remove_neutron_ports')
@mock.patch.object(neutron, 'remove_neutron_ports', autospec=True)
def test_remove_ports_from_network_not_all_pxe_enabled(self, remove_mock):
object_utils.create_test_port(
self.context, node_id=self.node.id,
@ -417,21 +417,21 @@ class TestNeutronNetworkActions(db_base.DbTestCase):
res = neutron.get_local_group_information(task, pg)
self.assertEqual(expected, res)
@mock.patch.object(neutron, 'remove_ports_from_network')
@mock.patch.object(neutron, 'remove_ports_from_network', autospec=True)
def test_rollback_ports(self, remove_mock):
with task_manager.acquire(self.context, self.node.uuid) as task:
neutron.rollback_ports(task, self.network_uuid)
remove_mock.assert_called_once_with(task, self.network_uuid)
@mock.patch.object(neutron, 'LOG')
@mock.patch.object(neutron, 'remove_ports_from_network')
@mock.patch.object(neutron, 'LOG', autospec=True)
@mock.patch.object(neutron, 'remove_ports_from_network', autospec=True)
def test_rollback_ports_exception(self, remove_mock, log_mock):
remove_mock.side_effect = exception.NetworkError('boom')
with task_manager.acquire(self.context, self.node.uuid) as task:
neutron.rollback_ports(task, self.network_uuid)
self.assertTrue(log_mock.exception.called)
@mock.patch.object(neutron, 'LOG')
@mock.patch.object(neutron, 'LOG', autospec=True)
def test_validate_port_info_neutron_interface(self, log_mock):
self.node.network_interface = 'neutron'
self.node.save()
@ -442,7 +442,7 @@ class TestNeutronNetworkActions(db_base.DbTestCase):
self.assertTrue(res)
self.assertFalse(log_mock.warning.called)
@mock.patch.object(neutron, 'LOG')
@mock.patch.object(neutron, 'LOG', autospec=True)
def test_validate_port_info_neutron_interface_missed_info(self, log_mock):
self.node.network_interface = 'neutron'
self.node.save()
@ -454,7 +454,7 @@ class TestNeutronNetworkActions(db_base.DbTestCase):
self.assertFalse(res)
self.assertTrue(log_mock.warning.called)
@mock.patch.object(neutron, 'LOG')
@mock.patch.object(neutron, 'LOG', autospec=True)
def test_validate_port_info_flat_interface(self, log_mock):
self.node.network_interface = 'flat'
self.node.save()
@ -629,7 +629,7 @@ class TestUnbindPort(base.TestCase):
mock_client.return_value.update_port.assert_called_once_with(port_id,
body)
@mock.patch.object(neutron, 'LOG')
@mock.patch.object(neutron, 'LOG', autospec=True)
def test_unbind_neutron_port_failure(self, mock_log, mock_client):
mock_client.return_value.update_port.side_effect = (
neutron_client_exc.NeutronClientException())
@ -660,7 +660,7 @@ class TestUnbindPort(base.TestCase):
mock_client.return_value.update_port.assert_called_once_with(port_id,
body)
@mock.patch.object(neutron, 'LOG')
@mock.patch.object(neutron, 'LOG', autospec=True)
def test_unbind_neutron_port_not_found(self, mock_log, mock_client):
port_id = 'fake-port-id'
mock_client.return_value.update_port.side_effect = (

View File

@ -485,8 +485,7 @@ class TestPXEUtils(db_base.DbTestCase):
render_mock.return_value)
@mock.patch.object(os, 'chmod', autospec=True)
@mock.patch('ironic.common.pxe_utils._link_mac_pxe_configs',
autospec=True)
@mock.patch('ironic.common.pxe_utils._link_mac_pxe_configs', autospec=True)
@mock.patch('ironic.common.utils.write_to_file', autospec=True)
@mock.patch('ironic.common.utils.render_template', autospec=True)
@mock.patch('oslo_utils.fileutils.ensure_tree', autospec=True)
@ -747,7 +746,8 @@ class TestPXEUtils(db_base.DbTestCase):
@mock.patch('ironic.common.utils.rmtree_without_raise', autospec=True)
@mock.patch('ironic_lib.utils.unlink_without_raise', autospec=True)
@mock.patch('ironic.common.dhcp_factory.DHCPFactory.provider')
@mock.patch('ironic.common.dhcp_factory.DHCPFactory.provider',
autospec=True)
def test_clean_up_pxe_config_uefi(self, provider_mock, unlink_mock,
rmtree_mock):
ip_address = '10.10.0.1'
@ -770,9 +770,10 @@ class TestPXEUtils(db_base.DbTestCase):
rmtree_mock.assert_called_once_with(
os.path.join(CONF.pxe.tftp_root, self.node.uuid))
@mock.patch('ironic.common.utils.rmtree_without_raise')
@mock.patch('ironic.common.utils.rmtree_without_raise', autospec=True)
@mock.patch('ironic_lib.utils.unlink_without_raise', autospec=True)
@mock.patch('ironic.common.dhcp_factory.DHCPFactory.provider')
@mock.patch('ironic.common.dhcp_factory.DHCPFactory.provider',
autospec=True)
def test_clean_up_pxe_config_uefi_instance_info(self,
provider_mock, unlink_mock,
rmtree_mock):

View File

@ -32,7 +32,7 @@ if six.PY3:
file = io.BytesIO
@mock.patch.object(swift, '_get_swift_session')
@mock.patch.object(swift, '_get_swift_session', autospec=True)
@mock.patch.object(swift_client, 'Connection', autospec=True)
class SwiftTestCase(base.TestCase):
@ -68,7 +68,7 @@ class SwiftTestCase(base.TestCase):
'user': username,
'key': password}
connection_mock.assert_called_once_with(**params)
swift_session_mock.assert_not_called()
self.assertFalse(swift_session_mock.called)
@mock.patch.object(__builtin__, 'open', autospec=True)
def test_create_object(self, open_mock, connection_mock, keystone_mock):

View File

@ -580,7 +580,7 @@ class JinjaTemplatingTestCase(base.TestCase):
self.params,
is_file=False))
@mock.patch('ironic.common.utils.jinja2.FileSystemLoader')
@mock.patch('ironic.common.utils.jinja2.FileSystemLoader', autospec=True)
def test_render_file(self, jinja_fsl_mock):
path = '/path/to/template.j2'
jinja_fsl_mock.return_value = jinja2.DictLoader(

View File

@ -22,7 +22,7 @@ CONF = cfg.CONF
class TestWSGIService(base.TestCase):
@mock.patch.object(wsgi_service.wsgi, 'Server')
@mock.patch.object(wsgi_service.wsgi, 'Server', autospec=True)
def test_workers_set_default(self, mock_server):
service_name = "ironic_api"
test_service = wsgi_service.WSGIService(service_name)
@ -34,19 +34,19 @@ class TestWSGIService(base.TestCase):
port=6385,
use_ssl=False)
@mock.patch.object(wsgi_service.wsgi, 'Server')
@mock.patch.object(wsgi_service.wsgi, 'Server', autospec=True)
def test_workers_set_correct_setting(self, mock_server):
self.config(api_workers=8, group='api')
test_service = wsgi_service.WSGIService("ironic_api")
self.assertEqual(8, test_service.workers)
@mock.patch.object(wsgi_service.wsgi, 'Server')
@mock.patch.object(wsgi_service.wsgi, 'Server', autospec=True)
def test_workers_set_zero_setting(self, mock_server):
self.config(api_workers=0, group='api')
test_service = wsgi_service.WSGIService("ironic_api")
self.assertEqual(processutils.get_worker_count(), test_service.workers)
@mock.patch.object(wsgi_service.wsgi, 'Server')
@mock.patch.object(wsgi_service.wsgi, 'Server', autospec=True)
def test_workers_set_negative_setting(self, mock_server):
self.config(api_workers=-2, group='api')
self.assertRaises(exception.ConfigInvalid,
@ -54,7 +54,7 @@ class TestWSGIService(base.TestCase):
'ironic_api')
self.assertFalse(mock_server.called)
@mock.patch.object(wsgi_service.wsgi, 'Server')
@mock.patch.object(wsgi_service.wsgi, 'Server', autospec=True)
def test_wsgi_service_with_ssl_enabled(self, mock_server):
self.config(enable_ssl_api=True, group='api')
service_name = 'ironic_api'