From 2e6a2872e88a5026acf426d1f89e6b68954079c5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Micha=C5=82=20Dulko?= Date: Fri, 6 Nov 2015 12:37:23 +0100 Subject: [PATCH] Use Service object instead of DB API directly In cmd.manage and volume.api we had code accessing DB API directly. This should be done through Service versioned object instead and this commit fixes that. Change-Id: I7c65323279b86c37fce7ffeb2b2626508842edfb Closes-Bug: 1513806 --- cinder/cmd/manage.py | 4 ++-- cinder/tests/unit/api/contrib/test_snapshot_manage.py | 4 ++++ cinder/tests/unit/test_cmd.py | 2 +- cinder/volume/api.py | 4 ++-- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/cinder/cmd/manage.py b/cinder/cmd/manage.py index cd5396c24..f787db589 100644 --- a/cinder/cmd/manage.py +++ b/cinder/cmd/manage.py @@ -466,8 +466,8 @@ class ServiceCommands(object): """Completely removes a service.""" ctxt = context.get_admin_context() try: - svc = db.service_get_by_args(ctxt, host_name, binary) - db.service_destroy(ctxt, svc['id']) + svc = objects.Service.get_by_args(ctxt, host_name, binary) + svc.destroy() except exception.HostBinaryNotFound as e: print(_("Host not found. Failed to remove %(service)s" " on %(host)s.") % diff --git a/cinder/tests/unit/api/contrib/test_snapshot_manage.py b/cinder/tests/unit/api/contrib/test_snapshot_manage.py index 500c01a5a..9e36c6451 100644 --- a/cinder/tests/unit/api/contrib/test_snapshot_manage.py +++ b/cinder/tests/unit/api/contrib/test_snapshot_manage.py @@ -20,6 +20,7 @@ from cinder import context from cinder import exception from cinder import test from cinder.tests.unit.api import fakes +from cinder.tests.unit import fake_service def app(): @@ -77,6 +78,9 @@ class SnapshotManageTest(test.TestCase): called with the correct arguments, and that we return the correct HTTP code to the caller. """ + ctxt = context.RequestContext('admin', 'fake', True) + mock_db.return_value = fake_service.fake_service_obj(ctxt) + body = {'snapshot': {'volume_id': 'fake_volume_id', 'ref': 'fake_ref'}} res = self._get_resp(body) self.assertEqual(202, res.status_int, res) diff --git a/cinder/tests/unit/test_cmd.py b/cinder/tests/unit/test_cmd.py index da2a1179d..7499e3fc7 100644 --- a/cinder/tests/unit/test_cmd.py +++ b/cinder/tests/unit/test_cmd.py @@ -802,7 +802,7 @@ class TestCinderManageCmd(test.TestCase): @mock.patch('cinder.db.service_destroy') @mock.patch('cinder.db.service_get_by_args', - return_value = {'id': 'volID'}) + return_value = {'id': '12'}) def test_remove_service_success(self, mock_get_by_args, mock_service_destroy): service_commands = cinder_manage.ServiceCommands() diff --git a/cinder/volume/api.py b/cinder/volume/api.py index aa71bb483..5da39e1a0 100644 --- a/cinder/volume/api.py +++ b/cinder/volume/api.py @@ -1598,8 +1598,8 @@ class API(base.Base): metadata=None): host = volume_utils.extract_host(volume['host']) try: - self.db.service_get_by_host_and_topic( - context.elevated(), host, CONF.volume_topic) + objects.Service.get_by_host_and_topic(context.elevated(), host, + CONF.volume_topic) except exception.ServiceNotFound: with excutils.save_and_reraise_exception(): LOG.error(_LE('Unable to find service: %(service)s for ' -- 2.45.2