Clean up baymodel query of usage from a bay

For baymodel reference of a bay, we can use bay.baymodel instead of using
bay.baymodel_id to get baymodel object, this patch does a cleanup.

Implemented: blueprint add-baymodel-object-to-bay
Change-Id: I568cc090d37d6666d8d02e87788aa5fff79e46e8
This commit is contained in:
Eli Qiao 2015-12-15 14:13:15 +08:00
parent 3e1bedd580
commit da89eae7e6
5 changed files with 62 additions and 89 deletions

View File

@ -71,13 +71,12 @@ def enforce_bay_types(*bay_types):
bay = objects.Bay.get_by_uuid(pecan.request.context, bay_ident)
else:
bay = objects.Bay.get_by_name(pecan.request.context, bay_ident)
baymodel = objects.BayModel.get_by_uuid(pecan.request.context,
bay.baymodel_id)
if baymodel.coe not in bay_types:
if bay.baymodel.coe not in bay_types:
raise exception.InvalidParameterValue(
'Cannot fulfill request with a %(bay_type)s bay, '
'expecting a %(supported_bay_types)s bay.' %
{'bay_type': baymodel.coe,
{'bay_type': bay.baymodel.coe,
'supported_bay_types': '/'.join(bay_types)})
return func(*args, **kwargs)

View File

@ -20,7 +20,6 @@ from oslo_log import log
from oslo_utils import importutils
import six
from magnum import objects
from magnum.objects.fields import BayType as bay_type
@ -69,10 +68,9 @@ class MonitorBase(object):
def create_monitor(context, bay):
baymodel = objects.BayModel.get_by_uuid(context, bay.baymodel_id)
if baymodel.coe in COE_CLASS_PATH:
coe_cls = importutils.import_class(COE_CLASS_PATH[baymodel.coe])
if bay.baymodel.coe in COE_CLASS_PATH:
coe_cls = importutils.import_class(COE_CLASS_PATH[bay.baymodel.coe])
return coe_cls(context, bay)
LOG.debug("Cannot create monitor with bay type '%s'" % baymodel.coe)
LOG.debug("Cannot create monitor with bay type '%s'" % bay.baymodel.coe)
return None

View File

@ -15,6 +15,7 @@ from magnum import objects
from magnum.objects import fields
from magnum.tests.unit.api import base as api_base
from magnum.tests.unit.db import utils
from magnum.tests.unit.objects import utils as obj_utils
from oslo_policy import policy
@ -29,15 +30,16 @@ class TestContainerController(api_base.FunctionalTest):
p = patch('magnum.objects.Bay.get_by_uuid')
self.mock_bay_get_by_uuid = p.start()
self.addCleanup(p.stop)
p = patch('magnum.objects.BayModel.get_by_uuid')
self.mock_baymodel_get_by_uuid = p.start()
self.addCleanup(p.stop)
def fake_get_by_uuid(context, uuid):
return objects.Bay(self.context, **utils.get_test_bay(uuid=uuid))
bay_dict = utils.get_test_bay(uuid=uuid)
baymodel = obj_utils.get_test_baymodel(
context, coe='swarm', uuid=bay_dict['baymodel_id'])
bay = objects.Bay(self.context, **bay_dict)
bay.baymodel = baymodel
return bay
self.mock_bay_get_by_uuid.side_effect = fake_get_by_uuid
self.mock_baymodel_get_by_uuid.return_value.coe = 'swarm'
@patch('magnum.conductor.api.API.container_create')
def test_create_container(self, mock_container_create):

View File

@ -19,6 +19,7 @@ from oslo_config import cfg
from magnum.api import validation as v
from magnum.common import exception
from magnum.tests import base
from magnum.tests.unit.objects import utils as obj_utils
class TestValidation(base.BaseTestCase):
@ -26,7 +27,6 @@ class TestValidation(base.BaseTestCase):
def _test_enforce_bay_types(
self,
mock_bay_get_by_uuid,
mock_baymodel_get_by_uuid,
mock_pecan_request,
bay_type,
allowed_bay_types,
@ -43,19 +43,18 @@ class TestValidation(base.BaseTestCase):
context = mock_pecan_request.context
bay = mock.MagicMock()
bay.baymodel_id = 'baymodel_id'
baymodel = mock.MagicMock()
baymodel.coe = bay_type
baymodel = obj_utils.get_test_baymodel(context,
uuid='baymodel_id',
coe=bay_type)
bay.baymodel = baymodel
mock_bay_get_by_uuid.return_value = bay
mock_baymodel_get_by_uuid.return_value = baymodel
if assert_raised:
self.assertRaises(
exception.InvalidParameterValue, test, self, *args)
else:
ret = test(self, *args)
mock_baymodel_get_by_uuid.assert_called_once_with(
context, 'baymodel_id')
if hasattr(args[0], 'bay_uuid'):
mock_bay_get_by_uuid.assert_called_once_with(context,
args[0].bay_uuid)
@ -65,120 +64,108 @@ class TestValidation(base.BaseTestCase):
self.assertEqual(args[1], ret)
@mock.patch('pecan.request')
@mock.patch('magnum.objects.BayModel.get_by_uuid')
@mock.patch('magnum.objects.Bay.get_by_uuid')
def test_enforce_bay_types_one_allowed(
self,
mock_bay_get_by_uuid,
mock_baymodel_get_by_uuid,
mock_pecan_request):
obj = mock.MagicMock()
obj.name = 'test_object'
obj.bay_uuid = 'bay_uuid'
bay_type = 'type1'
allowed_bay_types = ['type1']
bay_type = 'swarm'
allowed_bay_types = ['swarm']
self._test_enforce_bay_types(
mock_bay_get_by_uuid, mock_baymodel_get_by_uuid,
mock_pecan_request, bay_type, allowed_bay_types, False, obj)
mock_bay_get_by_uuid, mock_pecan_request,
bay_type, allowed_bay_types, False, obj)
@mock.patch('pecan.request')
@mock.patch('magnum.objects.BayModel.get_by_uuid')
@mock.patch('magnum.objects.Bay.get_by_uuid')
def test_enforce_bay_types_two_allowed(
self,
mock_bay_get_by_uuid,
mock_baymodel_get_by_uuid,
mock_pecan_request):
obj = mock.MagicMock()
obj.name = 'test_object'
obj.bay_uuid = 'bay_uuid'
bay_type = 'type1'
allowed_bay_types = ['type1', 'type2']
bay_type = 'swarm'
allowed_bay_types = ['swarm', 'mesos']
self._test_enforce_bay_types(
mock_bay_get_by_uuid, mock_baymodel_get_by_uuid,
mock_pecan_request, bay_type, allowed_bay_types, False, obj)
mock_bay_get_by_uuid, mock_pecan_request,
bay_type, allowed_bay_types, False, obj)
@mock.patch('pecan.request')
@mock.patch('magnum.objects.BayModel.get_by_uuid')
@mock.patch('magnum.objects.Bay.get_by_uuid')
def test_enforce_bay_types_not_allowed(
self,
mock_bay_get_by_uuid,
mock_baymodel_get_by_uuid,
mock_pecan_request):
obj = mock.MagicMock()
obj.name = 'test_object'
obj.bay_uuid = 'bay_uuid'
bay_type = 'type1'
allowed_bay_types = ['type2']
bay_type = 'swarm'
allowed_bay_types = ['mesos']
self._test_enforce_bay_types(
mock_bay_get_by_uuid, mock_baymodel_get_by_uuid,
mock_pecan_request, bay_type, allowed_bay_types,
mock_bay_get_by_uuid, mock_pecan_request,
bay_type, allowed_bay_types,
True, obj)
@mock.patch('pecan.request')
@mock.patch('magnum.objects.BayModel.get_by_uuid')
@mock.patch('magnum.objects.Bay.get_by_uuid')
def test_enforce_bay_types_with_bay_uuid(self, mock_bay_get_by_uuid,
mock_baymodel_get_by_uuid,
mock_pecan_request):
bay_ident = 'e74c40e0-d825-11e2-a28f-0800200c9a66'
bay_type = 'type1'
allowed_bay_types = ['type1']
bay_type = 'swarm'
allowed_bay_types = ['swarm']
self._test_enforce_bay_types(
mock_bay_get_by_uuid, mock_baymodel_get_by_uuid,
mock_pecan_request, bay_type, allowed_bay_types, False,
mock_bay_get_by_uuid, mock_pecan_request,
bay_type, allowed_bay_types, False,
None, bay_ident)
@mock.patch('pecan.request')
@mock.patch('magnum.objects.BayModel.get_by_uuid')
@mock.patch('magnum.objects.Bay.get_by_uuid')
def test_enforce_bay_types_with_bay_uuid_not_allowed(
self, mock_bay_get_by_uuid,
mock_baymodel_get_by_uuid, mock_pecan_request):
def test_enforce_bay_types_with_bay_uuid_not_allowed(self,
mock_bay_get_by_uuid,
mock_pecan_request):
bay_ident = 'e74c40e0-d825-11e2-a28f-0800200c9a66'
bay_type = 'type1'
allowed_bay_types = ['type2']
bay_type = 'swarm'
allowed_bay_types = ['mesos']
self._test_enforce_bay_types(
mock_bay_get_by_uuid, mock_baymodel_get_by_uuid,
mock_pecan_request, bay_type, allowed_bay_types, True,
mock_bay_get_by_uuid, mock_pecan_request,
bay_type, allowed_bay_types, True,
None, bay_ident)
@mock.patch('pecan.request')
@mock.patch('magnum.objects.BayModel.get_by_uuid')
@mock.patch('magnum.objects.Bay.get_by_name')
def test_enforce_bay_types_with_bay_name(self, mock_bay_get_by_uuid,
mock_baymodel_get_by_uuid,
mock_pecan_request):
bay_ident = 'bay_name'
bay_type = 'type1'
allowed_bay_types = ['type1']
bay_type = 'swarm'
allowed_bay_types = ['swarm']
self._test_enforce_bay_types(
mock_bay_get_by_uuid, mock_baymodel_get_by_uuid,
mock_pecan_request, bay_type, allowed_bay_types, False,
mock_bay_get_by_uuid, mock_pecan_request,
bay_type, allowed_bay_types, False,
None, bay_ident)
@mock.patch('pecan.request')
@mock.patch('magnum.objects.BayModel.get_by_uuid')
@mock.patch('magnum.objects.Bay.get_by_name')
def test_enforce_bay_types_with_bay_name_not_allowed(
self, mock_bay_get_by_uuid,
mock_baymodel_get_by_uuid, mock_pecan_request):
def test_enforce_bay_types_with_bay_name_not_allowed(self,
mock_bay_get_by_uuid,
mock_pecan_request):
bay_ident = 'bay_name'
bay_type = 'type1'
allowed_bay_types = ['type2']
bay_type = 'swarm'
allowed_bay_types = ['mesos']
self._test_enforce_bay_types(
mock_bay_get_by_uuid, mock_baymodel_get_by_uuid,
mock_pecan_request, bay_type, allowed_bay_types, True,
mock_bay_get_by_uuid, mock_pecan_request,
bay_type, allowed_bay_types, True,
None, bay_ident)
def _test_enforce_network_driver_types_create(

View File

@ -24,6 +24,7 @@ from magnum.conductor import swarm_monitor
from magnum import objects
from magnum.tests import base
from magnum.tests.unit.db import utils
from magnum.tests.unit.objects import utils as obj_utils
class MonitorsTestCase(base.TestCase):
@ -56,38 +57,24 @@ class MonitorsTestCase(base.TestCase):
self.mock_metrics_spec.return_value = self.test_metrics_spec
self.addCleanup(p.stop)
@mock.patch('magnum.objects.BayModel.get_by_uuid')
def test_create_monitor_success(self, mock_baymodel_get_by_uuid):
baymodel = mock.MagicMock()
baymodel.coe = 'swarm'
mock_baymodel_get_by_uuid.return_value = baymodel
def test_create_monitor_success(self):
self.bay.baymodel = obj_utils.get_test_baymodel(
self.context, uuid=self.bay.baymodel_id, coe='swarm')
monitor = monitors.create_monitor(self.context, self.bay)
self.assertIsInstance(monitor, swarm_monitor.SwarmMonitor)
@mock.patch('magnum.objects.BayModel.get_by_uuid')
def test_create_monitor_k8s_bay(self, mock_baymodel_get_by_uuid):
baymodel = mock.MagicMock()
baymodel.coe = 'kubernetes'
mock_baymodel_get_by_uuid.return_value = baymodel
def test_create_monitor_k8s_bay(self):
self.bay.baymodel = obj_utils.get_test_baymodel(
self.context, uuid=self.bay.baymodel_id, coe='kubernetes')
monitor = monitors.create_monitor(self.context, self.bay)
self.assertIsInstance(monitor, k8s_monitor.K8sMonitor)
@mock.patch('magnum.objects.BayModel.get_by_uuid')
def test_create_monitor_mesos_bay(self, mock_baymodel_get_by_uuid):
baymodel = mock.MagicMock()
baymodel.coe = 'mesos'
mock_baymodel_get_by_uuid.return_value = baymodel
def test_create_monitor_mesos_bay(self):
self.bay.baymodel = obj_utils.get_test_baymodel(
self.context, uuid=self.bay.baymodel_id, coe='mesos')
monitor = monitors.create_monitor(self.context, self.bay)
self.assertIsInstance(monitor, mesos_monitor.MesosMonitor)
@mock.patch('magnum.objects.BayModel.get_by_uuid')
def test_create_monitor_unsupported_coe(self, mock_baymodel_get_by_uuid):
baymodel = mock.MagicMock()
baymodel.coe = 'unsupported'
mock_baymodel_get_by_uuid.return_value = baymodel
monitor = monitors.create_monitor(self.context, self.bay)
self.assertIsNone(monitor)
@mock.patch('magnum.common.docker_utils.docker_for_bay')
def test_swarm_monitor_pull_data_success(self, mock_docker_for_bay):
mock_docker = mock.MagicMock()