From 7ab2947720ce777a152dc8d2886ddc831f1756b9 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Fri, 30 Oct 2020 14:43:30 +0000 Subject: [PATCH] db: Remove 'nova.db.base' module This made sense back in the day where the ORM was configurable and we were making lots of direct calls to the database. Now, in a world where most things happen via o.vo, it's just noise. Remove it. Change-Id: I216cabcde5311abd46fdad9c95bb72c31b414010 Signed-off-by: Stephen Finucane --- nova/compute/api.py | 34 ++--- nova/conductor/manager.py | 4 +- nova/db/base.py | 27 ---- nova/manager.py | 4 +- nova/network/neutron.py | 4 +- nova/tests/unit/compute/test_host_api.py | 186 +++++++++++------------ nova/tests/unit/compute/test_virtapi.py | 1 - 7 files changed, 108 insertions(+), 152 deletions(-) delete mode 100644 nova/db/base.py diff --git a/nova/compute/api.py b/nova/compute/api.py index 87afce10d551..207772f5e048 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -53,7 +53,7 @@ from nova import conductor import nova.conf from nova import context as nova_context from nova import crypto -from nova.db import base +from nova.db import api as db from nova.db.sqlalchemy import api as db_api from nova import exception from nova import exception_wrapper @@ -334,11 +334,10 @@ def block_accelerators(until_service=None): @profiler.trace_cls("compute_api") -class API(base.Base): +class API: """API for interacting with the compute manager.""" - def __init__(self, image_api=None, network_api=None, volume_api=None, - **kwargs): + def __init__(self, image_api=None, network_api=None, volume_api=None): self.image_api = image_api or glance.API() self.network_api = network_api or neutron.API() self.volume_api = volume_api or cinder.API() @@ -352,7 +351,6 @@ class API(base.Base): self.key_manager = key_manager.API() # Help us to record host in EventReporter self.host = CONF.host - super(API, self).__init__(**kwargs) def _record_action_start(self, context, instance, action): objects.InstanceAction.action_start(context, instance.uuid, @@ -5087,7 +5085,7 @@ class API(base.Base): def get_instance_metadata(self, context, instance): """Get all metadata associated with an instance.""" - return self.db.instance_metadata_get(context, instance.uuid) + return db.instance_metadata_get(context, instance.uuid) @check_instance_lock @check_instance_state(vm_state=[vm_states.ACTIVE, vm_states.PAUSED, @@ -5718,13 +5716,12 @@ def _find_service_in_cell(context, service_id=None, service_host=None): raise exception.NotFound() -class HostAPI(base.Base): +class HostAPI: """Sub-set of the Compute Manager API for managing host operations.""" def __init__(self, rpcapi=None, servicegroup_api=None): self.rpcapi = rpcapi or compute_rpcapi.ComputeAPI() self.servicegroup_api = servicegroup_api or servicegroup.API() - super(HostAPI, self).__init__() def _assert_host_exists(self, context, host_name, must_be_up=False): """Raise HostNotFound if compute host doesn't exist.""" @@ -5968,11 +5965,9 @@ class HostAPI(base.Base): """Return the task logs within a given range, optionally filtering by host and/or state. """ - return self.db.task_log_get_all(context, task_name, - period_beginning, - period_ending, - host=host, - state=state) + return db.task_log_get_all( + context, task_name, period_beginning, period_ending, host=host, + state=state) def compute_node_get(self, context, compute_id): """Return compute node entry for particular integer ID or UUID.""" @@ -6063,7 +6058,7 @@ class HostAPI(base.Base): if cell.uuid == objects.CellMapping.CELL0_UUID: continue with nova_context.target_cell(context, cell) as cctxt: - cell_stats.append(self.db.compute_node_statistics(cctxt)) + cell_stats.append(db.compute_node_statistics(cctxt)) if cell_stats: keys = cell_stats[0].keys() @@ -6073,7 +6068,7 @@ class HostAPI(base.Base): return {} -class InstanceActionAPI(base.Base): +class InstanceActionAPI: """Sub-set of the Compute Manager API for managing instance actions.""" def actions_get(self, context, instance, limit=None, marker=None, @@ -6090,13 +6085,13 @@ class InstanceActionAPI(base.Base): context, action_id) -class AggregateAPI(base.Base): +class AggregateAPI: """Sub-set of the Compute Manager API for managing host aggregates.""" - def __init__(self, **kwargs): + + def __init__(self): self.compute_rpcapi = compute_rpcapi.ComputeAPI() self.query_client = query.SchedulerQueryClient() self._placement_client = None # Lazy-load on first access. - super(AggregateAPI, self).__init__(**kwargs) @property def placement_client(self): @@ -6380,14 +6375,13 @@ class AggregateAPI(base.Base): return aggregate -class KeypairAPI(base.Base): +class KeypairAPI: """Subset of the Compute Manager API for managing key pairs.""" wrap_exception = functools.partial( exception_wrapper.wrap_exception, service='api', binary='nova-api') def __init__(self): - super().__init__() self.notifier = rpc.get_notifier('api') def _notify(self, context, event_suffix, keypair_name): diff --git a/nova/conductor/manager.py b/nova/conductor/manager.py index 65b67238e3a0..b1be71d5f5b8 100644 --- a/nova/conductor/manager.py +++ b/nova/conductor/manager.py @@ -42,7 +42,6 @@ from nova.conductor.tasks import cross_cell_migrate from nova.conductor.tasks import live_migrate from nova.conductor.tasks import migrate from nova import context as nova_context -from nova.db import base from nova import exception from nova.i18n import _ from nova.image import glance @@ -224,7 +223,7 @@ def obj_target_cell(obj, cell): @profiler.trace_cls("rpc") -class ComputeTaskManager(base.Base): +class ComputeTaskManager: """Namespace for compute methods. This class presents an rpc API for nova-conductor under the 'compute_task' @@ -236,7 +235,6 @@ class ComputeTaskManager(base.Base): target = messaging.Target(namespace='compute_task', version='1.23') def __init__(self): - super(ComputeTaskManager, self).__init__() self.compute_rpcapi = compute_rpcapi.ComputeAPI() self.volume_api = cinder.API() self.image_api = glance.API() diff --git a/nova/db/base.py b/nova/db/base.py deleted file mode 100644 index 7285b616435c..000000000000 --- a/nova/db/base.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 2010 United States Government as represented by the -# Administrator of the National Aeronautics and Space Administration. -# 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. - -"""Base class for classes that need database access.""" - -import nova.db.api - - -class Base(object): - """DB driver is injected in the init method.""" - - def __init__(self): - super(Base, self).__init__() - self.db = nova.db.api diff --git a/nova/manager.py b/nova/manager.py index 322a35778035..a53db41efcdd 100644 --- a/nova/manager.py +++ b/nova/manager.py @@ -54,7 +54,7 @@ This module provides Manager, a base class for managers. from oslo_service import periodic_task import nova.conf -from nova.db import base +import nova.db.api from nova import profiler from nova import rpc @@ -86,7 +86,7 @@ class ManagerMeta(profiler.get_traced_meta(), type(PeriodicTasks)): """ -class Manager(base.Base, PeriodicTasks, metaclass=ManagerMeta): +class Manager(PeriodicTasks, metaclass=ManagerMeta): __trace_args__ = {"name": "rpc"} def __init__(self, host=None, service_name='undefined'): diff --git a/nova/network/neutron.py b/nova/network/neutron.py index ed456f54d919..9c00b017a68b 100644 --- a/nova/network/neutron.py +++ b/nova/network/neutron.py @@ -36,7 +36,6 @@ from oslo_utils import uuidutils from nova.compute import utils as compute_utils import nova.conf from nova import context as nova_context -from nova.db import base from nova import exception from nova.i18n import _ from nova.network import constants @@ -299,11 +298,10 @@ def _ensure_no_port_binding_failure(port): raise exception.PortBindingFailed(port_id=port['id']) -class API(base.Base): +class API: """API for interacting with the neutron 2.x API.""" def __init__(self): - super(API, self).__init__() self.last_neutron_extension_sync = None self.extensions = {} self.pci_whitelist = pci_whitelist.Whitelist( diff --git a/nova/tests/unit/compute/test_host_api.py b/nova/tests/unit/compute/test_host_api.py index 9850a23f7741..96b41dab1cd1 100644 --- a/nova/tests/unit/compute/test_host_api.py +++ b/nova/tests/unit/compute/test_host_api.py @@ -102,18 +102,21 @@ class ComputeHostAPITestCase(test.TestCase): _do_test() - def test_get_host_uptime_service_down(self): - @mock.patch.object(self.host_api.db, 'service_get_by_compute_host', - return_value=dict(test_service.fake_service, id=1)) - @mock.patch.object(self.host_api.servicegroup_api, 'service_is_up', - return_value=False) - def _do_test(mock_service_is_up, mock_service_get_by_compute_host): + @mock.patch('nova.db.api.service_get_by_compute_host') + def test_get_host_uptime_service_down( + self, mock_get_service_get_by_compute_host, + ): + mock_get_service_get_by_compute_host.return_value = dict( + test_service.fake_service, id=1) + + with mock.patch.object( + self.host_api.servicegroup_api, 'service_is_up', + return_value=False, + ): self.assertRaises(exception.ComputeServiceUnavailable, self.host_api.get_host_uptime, self.ctxt, 'fake_host') - _do_test() - def test_host_power_action(self): @mock.patch.object(self.host_api.rpcapi, 'host_power_action', @@ -226,39 +229,37 @@ class ComputeHostAPITestCase(test.TestCase): None, set_zones=False) mock_get_hm.assert_called_once_with(self.ctxt, cells[1].id) - def test_service_get_all_no_zones(self): + @mock.patch('nova.db.api.service_get_all') + def test_service_get_all_no_zones(self, mock_service_get_all): services = [dict(test_service.fake_service, id=1, topic='compute', host='host1'), dict(test_service.fake_service, topic='compute', host='host2')] - @mock.patch.object(self.host_api.db, 'service_get_all') - def _do_test(mock_service_get_all): - mock_service_get_all.return_value = services - # Test no filters - result = self.host_api.service_get_all(self.ctxt) - mock_service_get_all.assert_called_once_with(self.ctxt, - disabled=None) - self._compare_objs(result, services) + mock_service_get_all.return_value = services + # Test no filters + result = self.host_api.service_get_all(self.ctxt) + mock_service_get_all.assert_called_once_with(self.ctxt, + disabled=None) + self._compare_objs(result, services) - # Test no filters #2 - mock_service_get_all.reset_mock() - result = self.host_api.service_get_all(self.ctxt, filters={}) - mock_service_get_all.assert_called_once_with(self.ctxt, - disabled=None) - self._compare_objs(result, services) + # Test no filters #2 + mock_service_get_all.reset_mock() + result = self.host_api.service_get_all(self.ctxt, filters={}) + mock_service_get_all.assert_called_once_with(self.ctxt, + disabled=None) + self._compare_objs(result, services) - # Test w/ filter - mock_service_get_all.reset_mock() - result = self.host_api.service_get_all(self.ctxt, - filters=dict(host='host2')) - mock_service_get_all.assert_called_once_with(self.ctxt, - disabled=None) - self._compare_objs(result, [services[1]]) + # Test w/ filter + mock_service_get_all.reset_mock() + result = self.host_api.service_get_all(self.ctxt, + filters=dict(host='host2')) + mock_service_get_all.assert_called_once_with(self.ctxt, + disabled=None) + self._compare_objs(result, [services[1]]) - _do_test() - - def test_service_get_all(self): + @mock.patch('nova.db.api.service_get_all') + def test_service_get_all(self, mock_service_get_all): services = [dict(test_service.fake_service, topic='compute', host='host1'), dict(test_service.fake_service, @@ -269,77 +270,74 @@ class ComputeHostAPITestCase(test.TestCase): exp_service.update(availability_zone='nova', **service) exp_services.append(exp_service) - @mock.patch.object(self.host_api.db, 'service_get_all') - def _do_test(mock_service_get_all): - mock_service_get_all.return_value = services + mock_service_get_all.return_value = services - # Test no filters - result = self.host_api.service_get_all(self.ctxt, set_zones=True) - mock_service_get_all.assert_called_once_with(self.ctxt, - disabled=None) - self._compare_objs(result, exp_services) + # Test no filters + result = self.host_api.service_get_all(self.ctxt, set_zones=True) + mock_service_get_all.assert_called_once_with(self.ctxt, + disabled=None) + self._compare_objs(result, exp_services) - # Test no filters #2 - mock_service_get_all.reset_mock() - result = self.host_api.service_get_all(self.ctxt, filters={}, - set_zones=True) - mock_service_get_all.assert_called_once_with(self.ctxt, - disabled=None) - self._compare_objs(result, exp_services) + # Test no filters #2 + mock_service_get_all.reset_mock() + result = self.host_api.service_get_all(self.ctxt, filters={}, + set_zones=True) + mock_service_get_all.assert_called_once_with(self.ctxt, + disabled=None) + self._compare_objs(result, exp_services) - # Test w/ filter - mock_service_get_all.reset_mock() - result = self.host_api.service_get_all(self.ctxt, - filters=dict(host='host2'), - set_zones=True) - mock_service_get_all.assert_called_once_with(self.ctxt, - disabled=None) - self._compare_objs(result, [exp_services[1]]) + # Test w/ filter + mock_service_get_all.reset_mock() + result = self.host_api.service_get_all(self.ctxt, + filters=dict(host='host2'), + set_zones=True) + mock_service_get_all.assert_called_once_with(self.ctxt, + disabled=None) + self._compare_objs(result, [exp_services[1]]) - # Test w/ zone filter but no set_zones arg. - mock_service_get_all.reset_mock() - filters = {'availability_zone': 'nova'} - result = self.host_api.service_get_all(self.ctxt, - filters=filters) - mock_service_get_all.assert_called_once_with(self.ctxt, - disabled=None) - self._compare_objs(result, exp_services) + # Test w/ zone filter but no set_zones arg. + mock_service_get_all.reset_mock() + filters = {'availability_zone': 'nova'} + result = self.host_api.service_get_all(self.ctxt, + filters=filters) + mock_service_get_all.assert_called_once_with(self.ctxt, + disabled=None) + self._compare_objs(result, exp_services) - _do_test() + @mock.patch( + 'nova.db.api.service_get_by_compute_host', + return_value=test_service.fake_service) + def test_service_get_by_compute_host( + self, mock_service_get_by_compute_host, + ): + result = self.host_api.service_get_by_compute_host( + self.ctxt, 'fake-host') + self.assertEqual(test_service.fake_service['id'], result.id) - def test_service_get_by_compute_host(self): - @mock.patch.object(self.host_api.db, 'service_get_by_compute_host', - return_value=test_service.fake_service) - def _do_test(mock_service_get_by_compute_host): - result = self.host_api.service_get_by_compute_host(self.ctxt, - 'fake-host') - self.assertEqual(test_service.fake_service['id'], result.id) - - _do_test() - - def test_service_update_by_host_and_binary(self): + @mock.patch('nova.db.api.service_get_by_host_and_binary') + @mock.patch('nova.db.api.service_update') + def test_service_update_by_host_and_binary( + self, mock_service_update, mock_service_get_by_host_and_binary, + ): host_name = 'fake-host' binary = 'nova-compute' params_to_update = dict(disabled=True) service_id = 42 expected_result = dict(test_service.fake_service, id=service_id) - @mock.patch.object(self.host_api, '_update_compute_provider_status') - @mock.patch.object(self.host_api.db, 'service_get_by_host_and_binary') - @mock.patch.object(self.host_api.db, 'service_update') - def _do_test(mock_service_update, mock_service_get_by_host_and_binary, - mock_update_compute_provider_status): - mock_service_get_by_host_and_binary.return_value = expected_result - mock_service_update.return_value = expected_result + mock_service_get_by_host_and_binary.return_value = expected_result + mock_service_update.return_value = expected_result + with mock.patch.object( + self.host_api, '_update_compute_provider_status', + ) as mock_update_compute_provider_status: result = self.host_api.service_update_by_host_and_binary( self.ctxt, host_name, binary, params_to_update) + self._compare_obj(result, expected_result) mock_update_compute_provider_status.assert_called_once_with( self.ctxt, test.MatchType(objects.Service)) - _do_test() - @mock.patch('nova.compute.api.HostAPI._update_compute_provider_status', new_callable=mock.NonCallableMock) def test_service_update_no_update_provider_status(self, mock_ucps): @@ -399,17 +397,13 @@ class ComputeHostAPITestCase(test.TestCase): 'fake-host') self.assertEqual(['fake-responses'], result) - def test_task_log_get_all(self): - @mock.patch.object(self.host_api.db, 'task_log_get_all', - return_value='fake-response') - def _do_test(mock_task_log_get_all): - result = self.host_api.task_log_get_all(self.ctxt, 'fake-name', - 'fake-begin', 'fake-end', - host='fake-host', - state='fake-state') - self.assertEqual('fake-response', result) - - _do_test() + @mock.patch('nova.db.api.task_log_get_all', return_value='fake-response') + def test_task_log_get_all(self, mock_task_log_get_all): + result = self.host_api.task_log_get_all(self.ctxt, 'fake-name', + 'fake-begin', 'fake-end', + host='fake-host', + state='fake-state') + self.assertEqual('fake-response', result) @mock.patch.object(objects.CellMappingList, 'get_all', return_value=objects.CellMappingList(objects=[ diff --git a/nova/tests/unit/compute/test_virtapi.py b/nova/tests/unit/compute/test_virtapi.py index 7455efdf8e4d..e477bf911cc4 100644 --- a/nova/tests/unit/compute/test_virtapi.py +++ b/nova/tests/unit/compute/test_virtapi.py @@ -81,7 +81,6 @@ class FakeVirtAPITest(VirtAPIBaseTest): class FakeCompute(object): def __init__(self): self.conductor_api = mock.MagicMock() - self.db = mock.MagicMock() self._events = [] self.instance_events = mock.MagicMock() self.instance_events.prepare_for_instance_event.side_effect = \