def _destroy_in_db(context, uuid): session = db_api.get_api_session() with session.begin(): result = session.query( api_models.CellMapping).filter_by(uuid=uuid).delete() if not result: raise exception.CellMappingNotFound(uuid=uuid)
def _get_by_uuid_from_db(context, uuid): db_mapping = context.session.query(api_models.CellMapping).filter_by( uuid=uuid).first() if not db_mapping: raise exception.CellMappingNotFound(uuid=uuid) return db_mapping
def _save_in_db(context, uuid, updates): db_mapping = context.session.query( api_models.CellMapping).filter_by(uuid=uuid).first() if not db_mapping: raise exception.CellMappingNotFound(uuid=uuid) db_mapping.update(updates) context.session.add(db_mapping) return db_mapping
def _get_by_uuid_from_db(context, uuid): session = db_api.get_api_session() with session.begin(): db_mapping = session.query( api_models.CellMapping).filter_by(uuid=uuid).first() if not db_mapping: raise exception.CellMappingNotFound(uuid=uuid) return db_mapping
def _save_in_db(context, uuid, updates): session = db_api.get_api_session() with session.begin(): db_mapping = session.query( api_models.CellMapping).filter_by(uuid=uuid).first() if not db_mapping: raise exception.CellMappingNotFound(uuid=uuid) db_mapping.update(updates) session.add(db_mapping) return db_mapping
def _destroy_in_db(context, uuid): result = context.session.query(api_models.CellMapping).filter_by( uuid=uuid).delete() if not result: raise exception.CellMappingNotFound(uuid=uuid)
class _TestCellMappingObject(object): @mock.patch.object(cell_mapping.CellMapping, '_get_by_uuid_from_db') def test_get_by_uuid(self, uuid_from_db): db_mapping = get_db_mapping() uuid_from_db.return_value = db_mapping mapping_obj = objects.CellMapping().get_by_uuid(self.context, db_mapping['uuid']) uuid_from_db.assert_called_once_with(self.context, db_mapping['uuid']) self.compare_obj(mapping_obj, db_mapping) @mock.patch.object(cell_mapping.CellMapping, '_get_by_uuid_from_db', side_effect=exception.CellMappingNotFound(uuid='fake')) def test_get_by_uuid_invalid(self, uuid_from_db): db_mapping = get_db_mapping() self.assertRaises(exception.CellMappingNotFound, objects.CellMapping().get_by_uuid, self.context, db_mapping['uuid']) uuid_from_db.assert_called_once_with(self.context, db_mapping['uuid']) @mock.patch.object(cell_mapping.CellMapping, '_create_in_db') def test_create(self, create_in_db): uuid = uuidutils.generate_uuid() db_mapping = get_db_mapping(uuid=uuid, name='test', database_connection='mysql+pymysql:///') create_in_db.return_value = db_mapping mapping_obj = objects.CellMapping(self.context) mapping_obj.uuid = uuid mapping_obj.name = 'test' mapping_obj.database_connection = 'mysql+pymysql:///' mapping_obj.create() create_in_db.assert_called_once_with(self.context, {'uuid': uuid, 'name': 'test', 'database_connection': 'mysql+pymysql:///'}) self.compare_obj(mapping_obj, db_mapping) @mock.patch.object(cell_mapping.CellMapping, '_save_in_db') def test_save(self, save_in_db): uuid = uuidutils.generate_uuid() db_mapping = get_db_mapping(database_connection='mysql+pymysql:///') save_in_db.return_value = db_mapping mapping_obj = objects.CellMapping(self.context) mapping_obj.uuid = uuid mapping_obj.database_connection = 'mysql+pymysql:///' mapping_obj.save() save_in_db.assert_called_once_with(self.context, uuid, {'uuid': uuid, 'database_connection': 'mysql+pymysql:///'}) self.compare_obj(mapping_obj, db_mapping) @mock.patch.object(cell_mapping.CellMapping, '_destroy_in_db') def test_destroy(self, destroy_in_db): uuid = uuidutils.generate_uuid() mapping_obj = objects.CellMapping(self.context) mapping_obj.uuid = uuid mapping_obj.destroy() destroy_in_db.assert_called_once_with(self.context, uuid) def test_is_cell0(self): self.assertFalse(objects.CellMapping().is_cell0()) self.assertFalse(objects.CellMapping( uuid=uuidutils.generate_uuid()).is_cell0()) self.assertTrue(objects.CellMapping( uuid=objects.CellMapping.CELL0_UUID).is_cell0()) def test_identity_no_name_set(self): cm = objects.CellMapping(uuid=uuids.cell1) self.assertEqual(uuids.cell1, cm.identity) def test_identity_name_is_none(self): cm = objects.CellMapping(uuid=uuids.cell1) self.assertEqual(uuids.cell1, cm.identity) def test_identity_with_name(self): cm = objects.CellMapping(uuid=uuids.cell1, name='foo') self.assertEqual('%s(foo)' % uuids.cell1, cm.identity) def test_obj_make_compatible(self): cell_mapping_obj = cell_mapping.CellMapping(context=self.context) fake_cell_mapping_obj = cell_mapping.CellMapping(context=self.context, uuid=uuids.cell, disabled=False) obj_primitive = fake_cell_mapping_obj.obj_to_primitive('1.0') obj = cell_mapping_obj.obj_from_primitive(obj_primitive) self.assertIn('uuid', obj) self.assertEqual(uuids.cell, obj.uuid) self.assertNotIn('disabled', obj)
class _TestCellMappingObject(object): @mock.patch.object(cell_mapping.CellMapping, '_get_by_uuid_from_db') def test_get_by_uuid(self, uuid_from_db): db_mapping = get_db_mapping() uuid_from_db.return_value = db_mapping mapping_obj = objects.CellMapping().get_by_uuid( self.context, db_mapping['uuid']) uuid_from_db.assert_called_once_with(self.context, db_mapping['uuid']) self.compare_obj(mapping_obj, db_mapping) @mock.patch.object(cell_mapping.CellMapping, '_get_by_uuid_from_db', side_effect=exception.CellMappingNotFound(uuid='fake')) def test_get_by_uuid_invalid(self, uuid_from_db): db_mapping = get_db_mapping() self.assertRaises(exception.CellMappingNotFound, objects.CellMapping().get_by_uuid, self.context, db_mapping['uuid']) uuid_from_db.assert_called_once_with(self.context, db_mapping['uuid']) @mock.patch.object(cell_mapping.CellMapping, '_create_in_db') def test_create(self, create_in_db): uuid = uuidutils.generate_uuid() db_mapping = get_db_mapping(uuid=uuid, name='test', database_connection='mysql+pymysql:///') create_in_db.return_value = db_mapping mapping_obj = objects.CellMapping(self.context) mapping_obj.uuid = uuid mapping_obj.name = 'test' mapping_obj.database_connection = 'mysql+pymysql:///' mapping_obj.create() create_in_db.assert_called_once_with( self.context, { 'uuid': uuid, 'name': 'test', 'database_connection': 'mysql+pymysql:///' }) self.compare_obj(mapping_obj, db_mapping) @mock.patch.object(cell_mapping.CellMapping, '_save_in_db') def test_save(self, save_in_db): uuid = uuidutils.generate_uuid() db_mapping = get_db_mapping(database_connection='mysql+pymysql:///') save_in_db.return_value = db_mapping mapping_obj = objects.CellMapping(self.context) mapping_obj.uuid = uuid mapping_obj.database_connection = 'mysql+pymysql:///' mapping_obj.save() save_in_db.assert_called_once_with( self.context, uuid, { 'uuid': uuid, 'database_connection': 'mysql+pymysql:///' }) self.compare_obj(mapping_obj, db_mapping) @mock.patch.object(cell_mapping.CellMapping, '_destroy_in_db') def test_destroy(self, destroy_in_db): uuid = uuidutils.generate_uuid() mapping_obj = objects.CellMapping(self.context) mapping_obj.uuid = uuid mapping_obj.destroy() destroy_in_db.assert_called_once_with(self.context, uuid) def test_is_cell0(self): self.assertFalse(objects.CellMapping().is_cell0()) self.assertFalse( objects.CellMapping(uuid=uuidutils.generate_uuid()).is_cell0()) self.assertTrue( objects.CellMapping( uuid=objects.CellMapping.CELL0_UUID).is_cell0()) def test_identity_no_name_set(self): cm = objects.CellMapping(uuid=uuids.cell1) self.assertEqual(uuids.cell1, cm.identity) def test_identity_name_is_none(self): cm = objects.CellMapping(uuid=uuids.cell1) self.assertEqual(uuids.cell1, cm.identity) def test_identity_with_name(self): cm = objects.CellMapping(uuid=uuids.cell1, name='foo') self.assertEqual('%s(foo)' % uuids.cell1, cm.identity) def test_obj_make_compatible(self): cell_mapping_obj = cell_mapping.CellMapping(context=self.context) fake_cell_mapping_obj = cell_mapping.CellMapping(context=self.context, uuid=uuids.cell, disabled=False) obj_primitive = fake_cell_mapping_obj.obj_to_primitive('1.0') obj = cell_mapping_obj.obj_from_primitive(obj_primitive) self.assertIn('uuid', obj) self.assertEqual(uuids.cell, obj.uuid) self.assertNotIn('disabled', obj) @mock.patch.object(cell_mapping.CellMapping, '_get_by_uuid_from_db') def test_formatted_db_url(self, mock_get): url = 'sqlite://*****:*****@localhost:123/nova?munchies=doritos#baz' varurl = ('{scheme}://not{username}:{password}@' '{hostname}:1{port}/{path}?{query}&flavor=coolranch' '#{fragment}') self.flags(connection=url, group='database') db_mapping = get_db_mapping(database_connection=varurl) mock_get.return_value = db_mapping mapping_obj = objects.CellMapping().get_by_uuid( self.context, db_mapping['uuid']) self.assertEqual(('sqlite://*****:*****@localhost:1123/nova?' 'munchies=doritos&flavor=coolranch#baz'), mapping_obj.database_connection) @mock.patch.object(cell_mapping.CellMapping, '_get_by_uuid_from_db') def test_formatted_mq_url(self, mock_get): url = 'rabbit://*****:*****@localhost:123/nova?munchies=doritos#baz' varurl = ('{scheme}://not{username}:{password}@' '{hostname}:1{port}/{path}?{query}&flavor=coolranch' '#{fragment}') self.flags(transport_url=url) db_mapping = get_db_mapping(transport_url=varurl) mock_get.return_value = db_mapping mapping_obj = objects.CellMapping().get_by_uuid( self.context, db_mapping['uuid']) self.assertEqual(('rabbit://*****:*****@localhost:1123/nova?' 'munchies=doritos&flavor=coolranch#baz'), mapping_obj.transport_url) @mock.patch.object(cell_mapping.CellMapping, '_get_by_uuid_from_db') def test_formatted_mq_url_multi_netloc1(self, mock_get): # Multiple netlocs, each with all parameters url = ('rabbit://*****:*****@otherhost:456,' 'bob:s3kret@localhost:123' '/nova?munchies=doritos#baz') varurl = ('{scheme}://not{username2}:{password1}@' '{hostname2}:1{port1}/{path}?{query}&flavor=coolranch' '#{fragment}') self.flags(transport_url=url) db_mapping = get_db_mapping(transport_url=varurl) mock_get.return_value = db_mapping mapping_obj = objects.CellMapping().get_by_uuid( self.context, db_mapping['uuid']) self.assertEqual(('rabbit://*****:*****@localhost:1456/nova?' 'munchies=doritos&flavor=coolranch#baz'), mapping_obj.transport_url) @mock.patch.object(cell_mapping.CellMapping, '_get_by_uuid_from_db') def test_formatted_mq_url_multi_netloc1_but_ipv6(self, mock_get): # Multiple netlocs, each with all parameters url = ('rabbit://*****:*****@otherhost:456,' 'bob:s3kret@[1:2::7]:123' '/nova?munchies=doritos#baz') varurl = ('{scheme}://not{username2}:{password1}@' '[{hostname2}]:1{port1}/{path}?{query}&flavor=coolranch' '#{fragment}') self.flags(transport_url=url) db_mapping = get_db_mapping(transport_url=varurl) mock_get.return_value = db_mapping mapping_obj = objects.CellMapping().get_by_uuid( self.context, db_mapping['uuid']) self.assertEqual(('rabbit://*****:*****@[1:2::7]:1456/nova?' 'munchies=doritos&flavor=coolranch#baz'), mapping_obj.transport_url) @mock.patch.object(cell_mapping.CellMapping, '_get_by_uuid_from_db') def test_formatted_mq_url_multi_netloc2(self, mock_get): # Multiple netlocs, without optional password and port url = ('rabbit://alice@otherhost,' 'bob:s3kret@localhost:123' '/nova?munchies=doritos#baz') varurl = ('{scheme}://not{username1}:{password2}@' '{hostname2}:1{port2}/{path}?{query}&flavor=coolranch' '#{fragment}') self.flags(transport_url=url) db_mapping = get_db_mapping(transport_url=varurl) mock_get.return_value = db_mapping mapping_obj = objects.CellMapping().get_by_uuid( self.context, db_mapping['uuid']) self.assertEqual(('rabbit://*****:*****@localhost:1123/nova?' 'munchies=doritos&flavor=coolranch#baz'), mapping_obj.transport_url) @mock.patch.object(cell_mapping.CellMapping, '_get_by_uuid_from_db') def test_formatted_mq_url_multi_netloc3(self, mock_get): # Multiple netlocs, without optional args url = ('rabbit://otherhost,' 'bob:s3kret@localhost:123' '/nova?munchies=doritos#baz') varurl = ('{scheme}://not{username2}:{password2}@' '{hostname1}:1{port2}/{path}?{query}&flavor=coolranch' '#{fragment}') self.flags(transport_url=url) db_mapping = get_db_mapping(transport_url=varurl) mock_get.return_value = db_mapping mapping_obj = objects.CellMapping().get_by_uuid( self.context, db_mapping['uuid']) self.assertEqual(('rabbit://*****:*****@otherhost:1123/nova?' 'munchies=doritos&flavor=coolranch#baz'), mapping_obj.transport_url) @mock.patch.object(cell_mapping.CellMapping, '_get_by_uuid_from_db') def test_formatted_url_without_base_set(self, mock_get): # Make sure we just pass through the template URL if the base # URLs are not set varurl = ('{scheme}://not{username2}:{password2}@' '{hostname1}:1{port2}/{path}?{query}&flavor=coolranch' '#{fragment}') self.flags(transport_url=None) self.flags(connection=None, group='database') db_mapping = get_db_mapping(transport_url=varurl, database_connection=varurl) mock_get.return_value = db_mapping mapping_obj = objects.CellMapping().get_by_uuid( self.context, db_mapping['uuid']) self.assertEqual(varurl, mapping_obj.database_connection) self.assertEqual(varurl, mapping_obj.transport_url)
class _TestCellMappingObject(object): @mock.patch.object(cell_mapping.CellMapping, '_get_by_uuid_from_db') def test_get_by_uuid(self, uuid_from_db): db_mapping = get_db_mapping() uuid_from_db.return_value = db_mapping mapping_obj = objects.CellMapping().get_by_uuid(self.context, db_mapping['uuid']) uuid_from_db.assert_called_once_with(self.context, db_mapping['uuid']) self.compare_obj(mapping_obj, db_mapping) @mock.patch.object(cell_mapping.CellMapping, '_get_by_uuid_from_db', side_effect=exception.CellMappingNotFound(uuid='fake')) def test_get_by_uuid_invalid(self, uuid_from_db): db_mapping = get_db_mapping() self.assertRaises(exception.CellMappingNotFound, objects.CellMapping().get_by_uuid, self.context, db_mapping['uuid']) uuid_from_db.assert_called_once_with(self.context, db_mapping['uuid']) @mock.patch.object(cell_mapping.CellMapping, '_create_in_db') def test_create(self, create_in_db): uuid = uuidutils.generate_uuid() db_mapping = get_db_mapping(uuid=uuid, name='test', database_connection='mysql:///') create_in_db.return_value = db_mapping mapping_obj = objects.CellMapping(self.context) mapping_obj.uuid = uuid mapping_obj.name = 'test' mapping_obj.database_connection = 'mysql:///' mapping_obj.create() create_in_db.assert_called_once_with(self.context, {'uuid': uuid, 'name': 'test', 'database_connection': 'mysql:///'}) self.compare_obj(mapping_obj, db_mapping) @mock.patch.object(cell_mapping.CellMapping, '_save_in_db') def test_save(self, save_in_db): uuid = uuidutils.generate_uuid() db_mapping = get_db_mapping(database_connection='mysql:///') save_in_db.return_value = db_mapping mapping_obj = objects.CellMapping(self.context) mapping_obj.uuid = uuid mapping_obj.database_connection = 'mysql:///' mapping_obj.save() save_in_db.assert_called_once_with(self.context, uuid, {'uuid': uuid, 'database_connection': 'mysql:///'}) self.compare_obj(mapping_obj, db_mapping) @mock.patch.object(cell_mapping.CellMapping, '_destroy_in_db') def test_destroy(self, destroy_in_db): uuid = uuidutils.generate_uuid() mapping_obj = objects.CellMapping(self.context) mapping_obj.uuid = uuid mapping_obj.destroy() destroy_in_db.assert_called_once_with(self.context, uuid)