Merge "Fix 'container' field in VolumeMapping object"
This commit is contained in:
commit
3b5841a11c
|
@ -60,6 +60,7 @@ SUPPORTED_STORAGE_DRIVERS = \
|
|||
|
||||
DEFAULT_QUOTA_CLASS_NAME = 'default'
|
||||
|
||||
TYPE_ANY = -1
|
||||
TYPE_CONTAINER = 0
|
||||
TYPE_CAPSULE = 1
|
||||
TYPE_CAPSULE_CONTAINER = 2
|
||||
|
|
|
@ -159,7 +159,8 @@ class Connection(object):
|
|||
filter_names=filter_names)
|
||||
|
||||
def _add_container_type_filter(self, container_type, query):
|
||||
query = query.filter_by(container_type=container_type)
|
||||
if container_type != consts.TYPE_ANY:
|
||||
query = query.filter_by(container_type=container_type)
|
||||
return query
|
||||
|
||||
def list_containers(self, context, container_type, filters=None,
|
||||
|
|
|
@ -158,6 +158,32 @@ class ContainerBase(base.ZunPersistentObject, base.ZunObject):
|
|||
container = cls._from_db_object(cls(context), db_container)
|
||||
return container
|
||||
|
||||
@staticmethod
|
||||
def get_container_any_type(context, uuid):
|
||||
"""Find a container of any type based on uuid.
|
||||
|
||||
:param uuid: the uuid of a container.
|
||||
:param context: Security context
|
||||
:returns: a :class:`ContainerBase` object.
|
||||
"""
|
||||
db_container = dbapi.get_container_by_uuid(context, consts.TYPE_ANY,
|
||||
uuid)
|
||||
type = db_container['container_type']
|
||||
if type == consts.TYPE_CONTAINER:
|
||||
container_cls = Container
|
||||
elif type == consts.TYPE_CAPSULE:
|
||||
container_cls = Capsule
|
||||
elif type == consts.TYPE_CAPSULE_CONTAINER:
|
||||
container_cls = CapsuleContainer
|
||||
elif type == consts.TYPE_CAPSULE_INIT_CONTAINER:
|
||||
container_cls = CapsuleInitContainer
|
||||
else:
|
||||
raise exception.ZunException(_('Unknown container type: %s'), type)
|
||||
|
||||
obj = container_cls(context)
|
||||
container = container_cls._from_db_object(obj, db_container)
|
||||
return container
|
||||
|
||||
@base.remotable_classmethod
|
||||
def list(cls, context, limit=None, marker=None,
|
||||
sort_key=None, sort_dir=None, filters=None):
|
||||
|
|
|
@ -63,7 +63,7 @@ class VolumeMapping(base.ZunPersistentObject, base.ZunObject):
|
|||
'volume_provider': fields.StringField(nullable=False),
|
||||
'container_path': fields.StringField(nullable=True),
|
||||
'container_uuid': fields.UUIDField(nullable=True),
|
||||
'container': fields.ObjectField('Container', nullable=True),
|
||||
'container': fields.ObjectField('ContainerBase', nullable=True),
|
||||
'connection_info': fields.SensitiveStringField(nullable=True),
|
||||
'auto_remove': fields.BooleanField(nullable=True),
|
||||
'host': fields.StringField(nullable=True),
|
||||
|
@ -274,7 +274,7 @@ class VolumeMapping(base.ZunPersistentObject, base.ZunObject):
|
|||
setattr(self, attrname, value)
|
||||
self.obj_reset_changes(fields=[attrname])
|
||||
if attrname == 'container':
|
||||
self.container = container.Container.get_by_uuid(
|
||||
self.container = container.ContainerBase.get_container_any_type(
|
||||
self._context, self.container_uuid)
|
||||
self.obj_reset_changes(fields=['container'])
|
||||
if attrname == 'volume':
|
||||
|
|
|
@ -101,6 +101,7 @@ def get_test_container(**kwargs):
|
|||
'cpuset': kwargs.get('cpuset', None),
|
||||
'registry_id': kwargs.get('registry_id', None),
|
||||
'container_type': kwargs.get('container_type', consts.TYPE_CONTAINER),
|
||||
'capsule_id': kwargs.get('capsule_id', 33),
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@ class TestCapsuleObject(base.DbTestCase):
|
|||
super(TestCapsuleObject, self).setUp()
|
||||
self.fake_capsule = utils.get_test_container(
|
||||
container_type=consts.TYPE_CAPSULE)
|
||||
self.fake_capsule.pop('capsule_id')
|
||||
|
||||
def test_get_by_uuid(self):
|
||||
uuid = self.fake_capsule['uuid']
|
||||
|
|
|
@ -31,6 +31,7 @@ class TestContainerObject(base.DbTestCase):
|
|||
self.fake_cpuset = utils.get_cpuset_dict()
|
||||
self.fake_container = utils.get_test_container(
|
||||
cpuset=self.fake_cpuset, cpu_policy='dedicated')
|
||||
self.fake_container.pop('capsule_id')
|
||||
|
||||
def test_get_by_uuid(self):
|
||||
uuid = self.fake_container['uuid']
|
||||
|
@ -54,6 +55,30 @@ class TestContainerObject(base.DbTestCase):
|
|||
self.context, container_type, name)
|
||||
self.assertEqual(self.context, container._context)
|
||||
|
||||
def test_get_container_any_type(self):
|
||||
mapping = {
|
||||
consts.TYPE_CONTAINER: objects.Container,
|
||||
consts.TYPE_CAPSULE: objects.Capsule,
|
||||
consts.TYPE_CAPSULE_CONTAINER: objects.CapsuleContainer,
|
||||
consts.TYPE_CAPSULE_INIT_CONTAINER: objects.CapsuleInitContainer,
|
||||
}
|
||||
for type, container_cls in mapping.items():
|
||||
self._test_get_container_any_type(type, container_cls)
|
||||
|
||||
def _test_get_container_any_type(self, container_type, container_cls):
|
||||
fake_container = utils.get_test_container(
|
||||
container_type=container_type)
|
||||
uuid = fake_container['uuid']
|
||||
with mock.patch.object(self.dbapi, 'get_container_by_uuid',
|
||||
autospec=True) as mock_get_container:
|
||||
mock_get_container.return_value = fake_container
|
||||
container = objects.Container.get_container_any_type(
|
||||
self.context, uuid)
|
||||
mock_get_container.assert_called_once_with(
|
||||
self.context, consts.TYPE_ANY, uuid)
|
||||
self.assertEqual(self.context, container._context)
|
||||
self.assertIsInstance(container, container_cls)
|
||||
|
||||
def test_list(self):
|
||||
with mock.patch.object(self.dbapi, 'list_containers',
|
||||
autospec=True) as mock_get_list:
|
||||
|
|
Loading…
Reference in New Issue