def test_describe_new_instance_then_its_alien_image_attribute(self): os_instance_id = fakes.random_os_id() os_image_id = fakes.random_os_id() alien_project_id = fakes.random_os_id() os_instance = { 'id': os_instance_id, 'flavor': {'id': 'fake'}, 'image': {'id': os_image_id}, } os_image = { 'id': os_image_id, 'owner': alien_project_id, 'is_public': True, } self.nova_admin.servers.list.return_value = [ fakes.OSInstance_full(os_instance)] self.glance.images.get.return_value = fakes.OSImage(os_image) reservations = instance_api.describe_instances(self.context) instance = reservations['reservationSet'][0]['instancesSet'][0] image_id = instance['imageId'] # NOTE(ft): ensure that InvalidAMIID.NotFound is not raised self.assertRaises(exception.AuthFailure, image_api.describe_image_attribute, self.context, image_id, 'description')
def _test_describe_new_instance_then_its_image(self, image_project_id): os_instance_id = fakes.random_os_id() os_image_id = fakes.random_os_id() os_instance = { 'id': os_instance_id, 'flavor': {'id': 'fake'}, 'image': {'id': os_image_id}, } os_image = { 'id': os_image_id, 'owner': image_project_id, 'is_public': True, } self.nova_admin.servers.list.return_value = [ fakes.OSInstance_full(os_instance)] self.glance.images.list.return_value = [fakes.OSImage(os_image)] reservations = instance_api.describe_instances(self.context) instance = reservations['reservationSet'][0]['instancesSet'][0] image_id = instance['imageId'] image = (image_api.describe_images(self.context, image_id=[image_id]) ['imagesSet'][0]) self.assertEqual(image_id, image['imageId']) self.assertEqual(image_project_id, image['imageOwnerId']) expected_project_id = (fakes.ID_OS_PROJECT if image_project_id == fakes.ID_OS_PROJECT else None) self.assert_image_project(expected_project_id, image['imageId'])
def test_describe_new_instance_then_its_volume(self): os_instance_id = fakes.random_os_id() os_volume_id = fakes.random_os_id() os_instance = { 'id': os_instance_id, 'flavor': { 'id': 'fake' }, 'volumes_attached': [{ 'id': os_volume_id }], } os_volume = { 'id': os_volume_id, 'status': 'in-use', 'attachments': [{ 'device': '/dev/vdb', 'server_id': os_instance_id }], } self.nova_admin.servers.list.return_value = [ fakes.OSInstance_full(os_instance) ] self.cinder.volumes.list.return_value = [fakes.OSVolume(os_volume)] reservations = instance_api.describe_instances(self.context) instance = reservations['reservationSet'][0]['instancesSet'][0] volume_id = instance['blockDeviceMapping'][0]['ebs']['volumeId'] volume_api.describe_volumes(self.context, [volume_id])
def _setup_items(self): db_api.add_item(self.context, 'fake', {}) db_api.add_item(self.context, 'fake', {'is_public': True}) db_api.add_item(self.context, 'fake1', {'os_id': fakes.random_os_id()}) db_api.add_item(self.other_context, 'fake', {}) db_api.add_item(self.other_context, 'fake', {'is_public': False}) db_api.add_item(self.other_context, 'fake', {'is_public': True}) db_api.add_item(self.other_context, 'fake1', {'is_public': False, 'os_id': fakes.random_os_id()})
def setUp(self): super(DbApiTestCase, self).setUp() engine = session.get_engine() engine.dispose() conn = engine.connect() conn.connection.executescript(self.DB_SCHEMA) self.context = ec2_context.RequestContext(fakes.ID_OS_USER, fakes.ID_OS_PROJECT) self.other_context = ec2_context.RequestContext( fakes.random_os_id(), fakes.random_os_id())
def _setup_items(self): db_api.add_item(self.context, 'fake', {}) db_api.add_item(self.context, 'fake', {'is_public': True}) db_api.add_item(self.context, 'fake1', {'os_id': fakes.random_os_id()}) db_api.add_item(self.other_context, 'fake', {}) db_api.add_item(self.other_context, 'fake', {'is_public': False}) db_api.add_item(self.other_context, 'fake', {'is_public': True}) db_api.add_item(self.other_context, 'fake1', { 'is_public': False, 'os_id': fakes.random_os_id() })
def _get_new_ebs_image(self, image_project_id=None, bdm_image_project_id=None): os_image_id = fakes.random_os_id() os_snapshot_id = fakes.random_os_id() os_bdm_image_id = fakes.random_os_id() os_image = { 'id': os_image_id, 'owner': image_project_id, 'is_public': True, 'container_format': 'ami', 'bdm_v2': True, 'block_device_mapping': [{ 'device_name': '/dev/vds', 'source_type': 'snapshot', 'destination_type': 'volume', 'snapshot_id': os_snapshot_id }], } if os_bdm_image_id: os_image['block_device_mapping'].append({ 'device_name': '/dev/vdi', 'source_type': 'image', 'destination_type': 'volume', 'image_id': os_bdm_image_id, 'size': 100 }) os_snapshot = { 'id': os_snapshot_id, } os_bdm_image = { 'id': os_bdm_image_id, 'owner': bdm_image_project_id, 'is_public': True, } os_images = [fakes.OSImage(os_image)] if bdm_image_project_id: os_images.append(fakes.OSImage(os_bdm_image)) self.glance.images.list.return_value = os_images self.cinder.volume_snapshots.list.return_value = ([ fakes.OSSnapshot(os_snapshot) ] if image_project_id == fakes.ID_OS_PROJECT else []) images = image_api.describe_images(self.context) return next(i for i in images['imagesSet'] if i['blockDeviceMapping'])
def do_test(s3_conn, s3_download_file, s3_decrypt_image, s3_untarzip_image): (s3_conn.return_value. get_object.return_value) = {'Body': FILE_MANIFEST_XML} s3_download_file.return_value = tempf s3_untarzip_image.return_value = tempf os_image_id = fakes.random_os_id() (glance.images.create.return_value) = ( fakes.OSImage({'id': os_image_id, 'status': 'queued'})) data = [ ({'image_location': 'testbucket_1/test.img.manifest.xml'}, 'testbucket_1', 'test.img.manifest.xml'), ({'image_location': '/testbucket_2/test.img.manifest.xml'}, 'testbucket_2', 'test.img.manifest.xml')] for mdata, bucket, manifest in data: image = image_api._s3_create(fake_context, mdata) eventlet.sleep() self.glance.images.update.assert_called_with( os_image_id, image_state='available') self.glance.images.upload.assert_any_call( os_image_id, mock.ANY) s3_conn.return_value.get_object.assert_called_with( Bucket=bucket, Key=manifest) s3_download_file.assert_called_with( mock.ANY, bucket, 'foo', mock.ANY) s3_decrypt_image.assert_called_with( fake_context, mock.ANY, 'foo', 'foo', mock.ANY) s3_untarzip_image.assert_called_with(mock.ANY, mock.ANY) do_test()
def test_describe_new_alien_bdm_image_from_new_alien_image(self): alien_project_id = fakes.random_os_id() image = self._get_new_ebs_image(image_project_id=alien_project_id, bdm_image_project_id=alien_project_id) image_id = self._find_snapshot_id_in_bdm(image, '/dev/vdi') image_api.describe_images(self.context, image_id=[image_id]) self.assert_image_project(None, image_id)
def test_add_item(self): new_item = { 'os_id': fakes.random_os_id(), 'vpc_id': fakes.random_ec2_id('fake_vpc'), 'str_attr': 'fake_str', 'int_attr': 1234, 'bool_attr': True, 'dict_attr': { 'key1': 'val1', 'key2': 'val2' }, 'list_attr': ['fake_str', 1234, True, { 'key': 'val' }, []] } item = db_api.add_item(self.context, 'fake', new_item) self.assertIn('id', item) self.assertIsNotNone(item['id']) item_id = item.pop('id') self.assertTrue(validator.validate_ec2_id(item_id, '', ['fake'])) self.assertThat(item, matchers.DictMatches(new_item, orderless_lists=True)) item = db_api.get_item_by_id(self.context, item_id) new_item['id'] = item_id self.assertThat(item, matchers.DictMatches(new_item, orderless_lists=True))
def test_add_item_isolation(self): os_id = fakes.random_os_id() db_api.add_item(self.context, 'fake', {'os_id': os_id}) self.assertRaises(orm_exception.NoResultFound, db_api.add_item, self.context, 'fake1', {'os_id': os_id}) self.assertRaises(orm_exception.NoResultFound, db_api.add_item, self.other_context, 'fake', {'os_id': os_id})
def test_restore_item(self): os_id = fakes.random_os_id() item = {'os_id': os_id, 'key': 'val1'} new_item = db_api.add_item(self.context, 'fake', item) item['id'] = new_item['id'] self.assertRaises(exception.EC2DBDuplicateEntry, db_api.restore_item, self.context, 'fake', item)
def test_mismatch_project_id(self): self.fake_context.project_id = fakes.random_os_id() self.assertRaises( exception.EC2MetadataNotFound, api.get_metadata_item, self.fake_context, ['2009-04-04'], fakes.ID_OS_INSTANCE_1, fakes.IP_NETWORK_INTERFACE_2, self.cache_region)
def test_restore_item(self): os_id = fakes.random_os_id() item = {'os_id': os_id, 'key': 'val1'} new_item = db_api.add_item(self.context, 'fake', item) item['id'] = new_item['id'] self.assertRaises( exception.EC2DBDuplicateEntry, db_api.restore_item, self.context, 'fake', item)
def test_describe_new_alien_bdm_image_from_new_alien_image(self): alien_project_id = fakes.random_os_id() image = self._get_new_ebs_image( image_project_id=alien_project_id, bdm_image_project_id=alien_project_id) image_id = self._find_snapshot_id_in_bdm(image, '/dev/vdi') image_api.describe_images(self.context, image_id=[image_id]) self.assert_image_project(None, image_id)
def _get_new_ebs_image(self, image_project_id=None, bdm_image_project_id=None): os_image_id = fakes.random_os_id() os_snapshot_id = fakes.random_os_id() os_bdm_image_id = fakes.random_os_id() os_image = { 'id': os_image_id, 'owner': image_project_id, 'is_public': True, 'container_format': 'ami', 'properties': { 'bdm_v2': True, 'block_device_mapping': [{'device_name': '/dev/vds', 'source_type': 'snapshot', 'destination_type': 'volume', 'snapshot_id': os_snapshot_id}], }, } if os_bdm_image_id: os_image['properties']['block_device_mapping'].append({ 'device_name': '/dev/vdi', 'source_type': 'image', 'destination_type': 'volume', 'image_id': os_bdm_image_id, 'size': 100}) os_snapshot = { 'id': os_snapshot_id, } os_bdm_image = { 'id': os_bdm_image_id, 'owner': bdm_image_project_id, 'is_public': True, } os_images = [fakes.OSImage(os_image)] if bdm_image_project_id: os_images.append(fakes.OSImage(os_bdm_image)) self.glance.images.list.return_value = os_images self.cinder.volume_snapshots.list.return_value = ( [fakes.OSSnapshot(os_snapshot)] if image_project_id == fakes.ID_OS_PROJECT else []) images = image_api.describe_images(self.context) return next(i for i in images['imagesSet'] if i['blockDeviceMapping'])
def test_add_item_isolation(self): os_id = fakes.random_os_id() db_api.add_item(self.context, 'fake', {'os_id': os_id}) self.assertRaises( orm_exception.NoResultFound, db_api.add_item, self.context, 'fake1', {'os_id': os_id}) self.assertRaises( orm_exception.NoResultFound, db_api.add_item, self.other_context, 'fake', {'os_id': os_id})
def _test_create_image(self, instance_status, no_reboot, is_ebs_instance): self.set_mock_db_items(fakes.DB_INSTANCE_2) os_instance = mock.MagicMock() os_instance.configure_mock(id=fakes.ID_OS_INSTANCE_2, status=instance_status) stop_called = iter([False, True]) os_instance.stop.side_effect = lambda: next(stop_called) os_instance.get.side_effect = lambda: (setattr(os_instance, 'status', 'SHUTOFF') if next(stop_called) else None) image_id = fakes.random_ec2_id('ami') os_image_id = fakes.random_os_id() os_instance.create_image.return_value = os_image_id self.glance.images.get.return_value = fakes.OSImage( {'id': os_image_id}, from_get=True) self.nova.servers.get.return_value = os_instance is_ebs_instance.return_value = True self.db_api.add_item.side_effect = tools.get_db_api_add_item(image_id) resp = self.execute('CreateImage', {'InstanceId': fakes.ID_EC2_INSTANCE_2, 'Name': 'fake_name', 'Description': 'fake desc', 'NoReboot': str(no_reboot)}) self.assertEqual({'imageId': image_id}, resp) self.db_api.get_item_by_id.assert_called_once_with( mock.ANY, fakes.ID_EC2_INSTANCE_2) self.nova.servers.get.assert_called_once_with(fakes.ID_OS_INSTANCE_2) is_ebs_instance.assert_called_once_with(mock.ANY, os_instance.id) expected_image = {'is_public': False, 'description': 'fake desc'} if no_reboot: expected_image['os_id'] = os_image_id self.db_api.add_item.assert_called_once_with( mock.ANY, 'ami', expected_image) if not no_reboot: eventlet.sleep() if not no_reboot: os_instance.stop.assert_called_once_with() os_instance.get.assert_called_once_with() os_instance.start.assert_called_once_with() if no_reboot: os_instance.create_image.assert_called_once_with('fake_name') else: os_instance.create_image.assert_called_once_with( 'fake_name', metadata={'ec2_id': image_id}) self.db_api.update_item.assert_called_once_with( mock.ANY, {'id': image_id, 'is_public': False, 'description': 'fake desc', 'os_id': os_image_id, 'vpc_id': None}) self.db_api.reset_mock() self.nova.servers.reset_mock()
def _test_create_image(self, instance_status, no_reboot, is_ebs_instance): self.set_mock_db_items(fakes.DB_INSTANCE_2) os_instance = mock.MagicMock() os_instance.configure_mock(id=fakes.ID_OS_INSTANCE_2, status=instance_status) stop_called = iter([False, True]) os_instance.stop.side_effect = lambda: next(stop_called) os_instance.get.side_effect = lambda: (setattr( os_instance, 'status', 'SHUTOFF') if next(stop_called) else None) image_id = fakes.random_ec2_id('ami') os_image_id = fakes.random_os_id() os_instance.create_image.return_value = os_image_id self.glance.images.get.return_value = fakes.OSImage( {'id': os_image_id}, from_get=True) self.nova.servers.get.return_value = os_instance is_ebs_instance.return_value = True self.db_api.add_item.side_effect = tools.get_db_api_add_item(image_id) resp = self.execute( 'CreateImage', { 'InstanceId': fakes.ID_EC2_INSTANCE_2, 'Name': 'fake_name', 'Description': 'fake desc', 'NoReboot': str(no_reboot) }) self.assertEqual({'imageId': image_id}, resp) self.db_api.get_item_by_id.assert_called_once_with( mock.ANY, fakes.ID_EC2_INSTANCE_2) self.nova.servers.get.assert_called_once_with(fakes.ID_OS_INSTANCE_2) is_ebs_instance.assert_called_once_with(mock.ANY, os_instance.id) expected_image = {'is_public': False, 'description': 'fake desc'} if no_reboot: expected_image['os_id'] = os_image_id self.db_api.add_item.assert_called_once_with(mock.ANY, 'ami', expected_image) if not no_reboot: eventlet.sleep() if not no_reboot: os_instance.stop.assert_called_once_with() os_instance.get.assert_called_once_with() os_instance.start.assert_called_once_with() if no_reboot: os_instance.create_image.assert_called_once_with('fake_name') else: os_instance.create_image.assert_called_once_with( 'fake_name', metadata={'ec2_id': image_id}) self.db_api.update_item.assert_called_once_with( mock.ANY, { 'id': image_id, 'is_public': False, 'description': 'fake desc', 'os_id': os_image_id, 'vpc_id': None }) self.db_api.reset_mock() self.nova.servers.reset_mock()
def test_add_item_id(self): os_id = fakes.random_os_id() item_id = db_api.add_item_id(self.context, 'fake', os_id) self.assertTrue(validator.validate_ec2_id(item_id, '', ['fake'])) item = db_api.get_item_by_id(self.context, item_id) self.assertIsNone(item) item = db_api.add_item(self.context, 'fake', {'os_id': os_id}) self.assertThat(item, matchers.DictMatches({'id': item_id, 'os_id': os_id, 'vpc_id': None})) # NOTE(ft): add os_id when item exists item_id = db_api.add_item_id(self.context, 'fake', os_id) self.assertEqual(item_id, item['id']) # NOTE(ft): add os_id when id exists os_id = fakes.random_os_id() item_id1 = db_api.add_item_id(self.context, 'fake', os_id) item_id2 = db_api.add_item_id(self.context, 'fake', os_id) self.assertEqual(item_id1, item_id2)
def test_replace_route_table_association_invalid_parameters(self): def do_check(params, error_code): self.assert_execution_error(error_code, 'ReplaceRouteTableAssociation', params) self.set_mock_db_items() do_check( { 'AssociationId': fakes.ID_EC2_ROUTE_TABLE_ASSOCIATION_1, 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1 }, 'InvalidRouteTableID.NotFound') # NOTE(ft): association with vpc is obsolete self.set_mock_db_items(fakes.DB_ROUTE_TABLE_1) do_check( { 'AssociationId': fakes.ID_EC2_ROUTE_TABLE_ASSOCIATION_1, 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1 }, 'InvalidAssociationID.NotFound') # NOTE(ft): association with subnet is obsolete (no subnet) self.set_mock_db_items(fakes.DB_ROUTE_TABLE_3) do_check( { 'AssociationId': fakes.ID_EC2_ROUTE_TABLE_ASSOCIATION_3, 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_3 }, 'InvalidAssociationID.NotFound') # NOTE(ft): association with subnet is obsolete (subnet is # disassociated) self.set_mock_db_items( fakes.DB_ROUTE_TABLE_3, tools.purge_dict(fakes.DB_SUBNET_2, ['route_table_id'])) do_check( { 'AssociationId': fakes.ID_EC2_ROUTE_TABLE_ASSOCIATION_3, 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_3 }, 'InvalidAssociationID.NotFound') # NOTE(ft): association belongs to different vpc id_ec2_subnet_vpc_2 = fakes.random_ec2_id('subnet') db_subnet_vpc_2 = { 'id': id_ec2_subnet_vpc_2, 'os_id': fakes.random_os_id(), 'vpc_id': fakes.ID_EC2_VPC_2, 'route_table_id': fakes.random_ec2_id('rtb') } self.set_mock_db_items(fakes.DB_ROUTE_TABLE_2, db_subnet_vpc_2) do_check( { 'AssociationId': ec2utils.change_ec2_id_kind(id_ec2_subnet_vpc_2, 'rtbassoc'), 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2 }, 'InvalidParameterValue')
def _test_describe_new_images(self, image_project_id=None, aki_image_project_id=None, with_id_mapping=False): os_image_id = fakes.random_os_id() os_aki_image_id = fakes.random_os_id() os_image = { 'id': os_image_id, 'owner': image_project_id, 'is_public': True, 'container_format': 'ami', 'properties': { 'kernel_id': os_aki_image_id, }, } os_aki_image = { 'id': os_aki_image_id, 'owner': aki_image_project_id, 'is_public': True, 'container_format': 'aki', } self.glance.images.list.return_value = ( [fakes.OSImage(os_image), fakes.OSImage(os_aki_image)] if with_id_mapping else [fakes.OSImage(os_aki_image), fakes.OSImage(os_image)]) images = image_api.describe_images(self.context) image = next(i for i in images['imagesSet'] if i['imageType'] == 'machine') aki_image = next(i for i in images['imagesSet'] if i['imageType'] == 'kernel') self.assertEqual(image_project_id, image['imageOwnerId']) self.assert_image_project( (image_project_id if image_project_id == fakes.ID_OS_PROJECT else None), image['imageId']) self.assertEqual(aki_image_project_id, aki_image['imageOwnerId']) self.assert_image_project( (aki_image_project_id if aki_image_project_id == fakes.ID_OS_PROJECT else None), aki_image['imageId'])
def test_register_image_by_url(self, is_ebs_instance): self.set_mock_db_items(fakes.DB_INSTANCE_2) is_ebs_instance.return_value = True # Setup the mock parameters image_id = fakes.random_ec2_id('ami') os_image_id = fakes.random_os_id() self.glance.images.create.return_value = fakes.OSImage( {'id': os_image_id}, from_get=True) self.db_api.add_item.side_effect = tools.get_db_api_add_item(image_id) # Setup Import Command import_command = 'RegisterImage' # Setup the import arguments args = { 'Name': 'TestImage123', 'ImageLocation': fakes.LOCATION_IMAGE_2, 'Architecture': 'x86_64' } # Execute the import image process resp = self.execute(import_command, args) # Assert that the image returned is equal to what was expected self.assertEqual({'imageId': image_id}, resp) # Assert that Glance Image Create was called self.glance.images.create.assert_called_once_with( name='TestImage123', disk_format='raw', container_format='bare', visibility='private', architecture='x86_64', image_location=fakes.LOCATION_IMAGE_2) # Assert that Glance Image Import was called self.glance.images.image_import.assert_called_once_with( os_image_id, method='web-download', uri=fakes.LOCATION_IMAGE_2) # Assert that the image was created expected_image = {'is_public': False, 'os_id': mock.ANY, 'description': None} self.db_api.add_item.assert_called_once_with( mock.ANY, 'ami', expected_image) # Reset all test settings/state self.db_api.reset_mock() self.glance.reset_mock()
def test_delete_subnet_vpn(self): context = base.create_context() cleaner = common.OnCrashCleaner() # subnet is not connected to the vpn vpn_connection_api._delete_subnet_vpn(context, self.neutron, cleaner, fakes.DB_SUBNET_1, fakes.DB_VPN_CONNECTION_1) self.assertFalse(self.db_api.update_item.called) self.assertFalse(self.neutron.delete_ipsec_site_connection.called) # delete subnet vpn connection vpn_connection_api._delete_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_2, copy.deepcopy(fakes.DB_VPN_CONNECTION_1)) self.db_api.update_item.assert_called_once_with( mock.ANY, tools.update_dict(fakes.DB_VPN_CONNECTION_1, {'os_ipsec_site_connections': {}})) self.neutron.delete_ipsec_site_connection.assert_called_once_with( fakes.ID_OS_IPSEC_SITE_CONNECTION_2) # delete subnet vpn connection, leave connections of other subnets self.db_api.reset_mock() self.neutron.reset_mock() id_os_connection = fakes.random_os_id() vpn_connection_1 = copy.deepcopy(fakes.DB_VPN_CONNECTION_1) (vpn_connection_1['os_ipsec_site_connections'][fakes.ID_EC2_SUBNET_1] ) = id_os_connection vpn_connection_api._delete_subnet_vpn(context, self.neutron, cleaner, fakes.DB_SUBNET_1, vpn_connection_1) self.db_api.update_item.assert_called_once_with( mock.ANY, fakes.DB_VPN_CONNECTION_1) self.neutron.delete_ipsec_site_connection.assert_called_once_with( id_os_connection) # rollback of deleting subnet vpn connection self.db_api.reset_mock() self.neutron.reset_mock() try: with common.OnCrashCleaner() as cleaner: vpn_connection_api._delete_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_2, copy.deepcopy(fakes.DB_VPN_CONNECTION_1)) raise Exception('fake-exception') except Exception as ex: if ex.message != 'fake-exception': raise self.db_api.update_item.assert_called_with(mock.ANY, fakes.DB_VPN_CONNECTION_1) self.assertFalse(self.neutron.create_ipsec_site_connection.called)
def test_s3_create_image_locations(self, osimage_update): self.configure(image_decryption_dir=None) _handle, tempf = tempfile.mkstemp() fake_context = base.create_context() with contextlib.nested( mock.patch('ec2api.api.image._s3_conn'), mock.patch('ec2api.api.image._s3_download_file'), mock.patch('ec2api.api.image._s3_decrypt_image'), mock.patch('ec2api.api.image._s3_untarzip_image')) as ( s3_conn, s3_download_file, s3_decrypt_image, s3_untarzip_image): (s3_conn.return_value.get_bucket.return_value.get_key.return_value. get_contents_as_string.return_value) = FILE_MANIFEST_XML s3_download_file.return_value = tempf s3_untarzip_image.return_value = tempf (self.glance.images.create.return_value) = (fakes.OSImage({ 'id': fakes.random_os_id(), 'status': 'queued' })) data = [({ 'properties': { 'image_location': 'testbucket_1/test.img.manifest.xml' } }, 'testbucket_1', 'test.img.manifest.xml'), ({ 'properties': { 'image_location': '/testbucket_2/test.img.manifest.xml' } }, 'testbucket_2', 'test.img.manifest.xml')] for mdata, bucket, manifest in data: image = image_api._s3_create(fake_context, mdata) eventlet.sleep() osimage_update.assert_called_with( image, properties={'image_state': 'available'}) osimage_update.assert_any_call(image, data=mock.ANY) s3_conn.return_value.get_bucket.assert_called_with(bucket) (s3_conn.return_value.get_bucket.return_value.get_key. assert_called_with(manifest)) (s3_conn.return_value.get_bucket.return_value.get_key. return_value.get_contents_as_string.assert_called_with()) s3_download_file.assert_called_with( s3_conn.return_value.get_bucket.return_value, 'foo', mock.ANY) s3_decrypt_image.assert_called_with(fake_context, mock.ANY, 'foo', 'foo', mock.ANY) s3_untarzip_image.assert_called_with(mock.ANY, mock.ANY)
def test_delete_subnet_vpn(self): context = base.create_context() cleaner = common.OnCrashCleaner() # subnet is not connected to the vpn vpn_connection_api._delete_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_1, fakes.DB_VPN_CONNECTION_1) self.assertFalse(self.db_api.update_item.called) self.assertFalse(self.neutron.delete_ipsec_site_connection.called) # delete subnet vpn connection vpn_connection_api._delete_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_2, copy.deepcopy(fakes.DB_VPN_CONNECTION_1)) self.db_api.update_item.assert_called_once_with( mock.ANY, tools.update_dict(fakes.DB_VPN_CONNECTION_1, {'os_ipsec_site_connections': {}})) self.neutron.delete_ipsec_site_connection.assert_called_once_with( fakes.ID_OS_IPSEC_SITE_CONNECTION_2) # delete subnet vpn connection, leave connections of other subnets self.db_api.reset_mock() self.neutron.reset_mock() id_os_connection = fakes.random_os_id() vpn_connection_1 = copy.deepcopy(fakes.DB_VPN_CONNECTION_1) (vpn_connection_1['os_ipsec_site_connections'] [fakes.ID_EC2_SUBNET_1]) = id_os_connection vpn_connection_api._delete_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_1, vpn_connection_1) self.db_api.update_item.assert_called_once_with( mock.ANY, fakes.DB_VPN_CONNECTION_1) self.neutron.delete_ipsec_site_connection.assert_called_once_with( id_os_connection) # rollback of deleting subnet vpn connection self.db_api.reset_mock() self.neutron.reset_mock() try: with common.OnCrashCleaner() as cleaner: vpn_connection_api._delete_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_2, copy.deepcopy(fakes.DB_VPN_CONNECTION_1)) raise Exception('fake-exception') except Exception as ex: if str(ex) != 'fake-exception': raise self.db_api.update_item.assert_called_with( mock.ANY, fakes.DB_VPN_CONNECTION_1) self.assertFalse(self.neutron.create_ipsec_site_connection.called)
def test_stop_vpn_connection(self): # delete several connections os_conn_ids = [fakes.random_os_id() for _x in range(3)] fake_conn = {"os_ipsec_site_connections": {fakes.random_ec2_id("subnet"): conn_id for conn_id in os_conn_ids}} vpn_connection_api._stop_vpn_connection(self.neutron, fake_conn) self.assertEqual(3, self.neutron.delete_ipsec_site_connection.call_count) for conn_id in os_conn_ids: self.neutron.delete_ipsec_site_connection.assert_any_call(conn_id) # delete several connections with exception suppressing self.neutron.reset_mock() self.neutron.delete_ipsec_site_connection.side_effect = [None, neutron_exception.NotFound(), None] vpn_connection_api._stop_vpn_connection(self.neutron, fake_conn) self.assertEqual(3, self.neutron.delete_ipsec_site_connection.call_count)
def test_describe_new_instance_then_its_volume(self): os_instance_id = fakes.random_os_id() os_volume_id = fakes.random_os_id() os_instance = { 'id': os_instance_id, 'flavor': {'id': 'fake'}, 'volumes_attached': [{'id': os_volume_id}], } os_volume = { 'id': os_volume_id, 'status': 'in-use', 'attachments': [{'device': '/dev/vdb', 'server_id': os_instance_id}], } self.nova_admin.servers.list.return_value = [ fakes.OSInstance_full(os_instance)] self.cinder.volumes.list.return_value = [ fakes.OSVolume(os_volume)] reservations = instance_api.describe_instances(self.context) instance = reservations['reservationSet'][0]['instancesSet'][0] volume_id = instance['blockDeviceMapping'][0]['ebs']['volumeId'] volume_api.describe_volumes(self.context, [volume_id])
def test_add_item_id(self): os_id = fakes.random_os_id() item_id = db_api.add_item_id(self.context, 'fake', os_id) self.assertTrue(validator.validate_ec2_id(item_id, '', ['fake'])) item = db_api.get_item_by_id(self.context, item_id) self.assertIsNone(item) item = db_api.add_item(self.context, 'fake', {'os_id': os_id}) self.assertThat( item, matchers.DictMatches({ 'id': item_id, 'os_id': os_id, 'vpc_id': None })) # NOTE(ft): add os_id when item exists item_id = db_api.add_item_id(self.context, 'fake', os_id) self.assertEqual(item_id, item['id']) # NOTE(ft): add os_id when id exists os_id = fakes.random_os_id() item_id1 = db_api.add_item_id(self.context, 'fake', os_id) item_id2 = db_api.add_item_id(self.context, 'fake', os_id) self.assertEqual(item_id1, item_id2)
def test_add_item_defaults(self): def do_check(new_item): item = db_api.add_item(self.context, 'fake', new_item) item_id = item.pop('id') if 'id' in new_item: new_item_id = new_item.pop('id') self.assertNotEqual(new_item_id, item_id) new_item.setdefault('os_id', None) new_item.setdefault('vpc_id', None) self.assertThat(item, matchers.DictMatches(new_item, orderless_lists=True)) do_check({}) do_check({'os_id': fakes.random_os_id()}) do_check({'vpc_id': fakes.random_ec2_id('fake_vpc')}) do_check({'id': fakes.random_ec2_id('fake')})
def test_add_item_defaults(self): def do_check(new_item): item = db_api.add_item(self.context, 'fake', new_item) item_id = item.pop('id') if 'id' in new_item: new_item_id = new_item.pop('id') self.assertNotEqual(new_item_id, item_id) new_item.setdefault('os_id', None) new_item.setdefault('vpc_id', None) self.assertThat( item, matchers.DictMatches(new_item, orderless_lists=True)) do_check({}) do_check({'os_id': fakes.random_os_id()}) do_check({'vpc_id': fakes.random_ec2_id('fake_vpc')}) do_check({'id': fakes.random_ec2_id('fake')})
def test_s3_create_image_locations(self, osimage_update): self.configure(image_decryption_dir=None) _handle, tempf = tempfile.mkstemp() fake_context = base.create_context() with contextlib.nested( mock.patch('ec2api.api.image._s3_conn'), mock.patch('ec2api.api.image._s3_download_file'), mock.patch('ec2api.api.image._s3_decrypt_image'), mock.patch('ec2api.api.image._s3_untarzip_image') ) as (s3_conn, s3_download_file, s3_decrypt_image, s3_untarzip_image): (s3_conn.return_value. get_bucket.return_value. get_key.return_value. get_contents_as_string.return_value) = FILE_MANIFEST_XML s3_download_file.return_value = tempf s3_untarzip_image.return_value = tempf (self.glance.images.create.return_value) = ( fakes.OSImage({'id': fakes.random_os_id(), 'status': 'queued'})) data = [ ({'properties': { 'image_location': 'testbucket_1/test.img.manifest.xml'}}, 'testbucket_1', 'test.img.manifest.xml'), ({'properties': { 'image_location': '/testbucket_2/test.img.manifest.xml'}}, 'testbucket_2', 'test.img.manifest.xml')] for mdata, bucket, manifest in data: image = image_api._s3_create(fake_context, mdata) eventlet.sleep() osimage_update.assert_called_with( image, properties={'image_state': 'available'}) osimage_update.assert_any_call( image, data=mock.ANY) s3_conn.return_value.get_bucket.assert_called_with(bucket) (s3_conn.return_value.get_bucket.return_value. get_key.assert_called_with(manifest)) (s3_conn.return_value.get_bucket.return_value. get_key.return_value. get_contents_as_string.assert_called_with()) s3_download_file.assert_called_with( s3_conn.return_value.get_bucket.return_value, 'foo', mock.ANY) s3_decrypt_image.assert_called_with( fake_context, mock.ANY, 'foo', 'foo', mock.ANY) s3_untarzip_image.assert_called_with(mock.ANY, mock.ANY)
def do_test(s3_conn, s3_download_file, s3_decrypt_image, s3_untarzip_image): (s3_conn.return_value.get_bucket.return_value.get_key.return_value. get_contents_as_string.return_value) = FILE_MANIFEST_XML s3_download_file.return_value = tempf s3_untarzip_image.return_value = tempf (glance.images.create.return_value) = (fakes.OSImage({ 'id': fakes.random_os_id(), 'status': 'queued' })) data = [({ 'properties': { 'image_location': 'testbucket_1/test.img.manifest.xml' } }, 'testbucket_1', 'test.img.manifest.xml'), ({ 'properties': { 'image_location': '/testbucket_2/test.img.manifest.xml' } }, 'testbucket_2', 'test.img.manifest.xml')] for mdata, bucket, manifest in data: image = image_api._s3_create(fake_context, mdata) eventlet.sleep() osimage_update.assert_called_with( image, properties={'image_state': 'available'}) osimage_update.assert_any_call(image, data=mock.ANY) s3_conn.return_value.get_bucket.assert_called_with(bucket) (s3_conn.return_value.get_bucket.return_value.get_key. assert_called_with(manifest)) (s3_conn.return_value.get_bucket.return_value.get_key. return_value.get_contents_as_string.assert_called_with()) s3_download_file.assert_called_with( s3_conn.return_value.get_bucket.return_value, 'foo', mock.ANY) s3_decrypt_image.assert_called_with(fake_context, mock.ANY, 'foo', 'foo', mock.ANY) s3_untarzip_image.assert_called_with(mock.ANY, mock.ANY) do_test()
def test_add_item_with_same_os_id(self): # NOTE(ft): check normal update item on add os_id = fakes.random_os_id() item1 = db_api.add_item(self.context, 'fake', {'os_id': os_id, 'key': 'val1', 'key1': 'val'}) item_id = item1['id'] item2 = db_api.add_item(self.context, 'fake', {'os_id': os_id, 'key': 'val2', 'key2': 'val'}) expected_item = {'id': item_id, 'os_id': os_id, 'vpc_id': None, 'key': 'val2', 'key1': 'val', 'key2': 'val'} self.assertThat(item2, matchers.DictMatches(expected_item))
def test_add_item(self): new_item = {'os_id': fakes.random_os_id(), 'vpc_id': fakes.random_ec2_id('fake_vpc'), 'str_attr': 'fake_str', 'int_attr': 1234, 'bool_attr': True, 'dict_attr': {'key1': 'val1', 'key2': 'val2'}, 'list_attr': ['fake_str', 1234, True, {'key': 'val'}, []]} item = db_api.add_item(self.context, 'fake', new_item) self.assertIn('id', item) self.assertIsNotNone(item['id']) item_id = item.pop('id') self.assertTrue(validator.validate_ec2_id(item_id, '', ['fake'])) self.assertThat(item, matchers.DictMatches(new_item, orderless_lists=True)) item = db_api.get_item_by_id(self.context, item_id) new_item['id'] = item_id self.assertThat(item, matchers.DictMatches(new_item, orderless_lists=True))
def do_test(s3_conn, s3_download_file, s3_decrypt_image, s3_untarzip_image): (s3_conn.return_value. get_bucket.return_value. get_key.return_value. get_contents_as_string.return_value) = FILE_MANIFEST_XML s3_download_file.return_value = tempf s3_untarzip_image.return_value = tempf (glance.images.create.return_value) = ( fakes.OSImage({'id': fakes.random_os_id(), 'status': 'queued'})) data = [ ({'properties': { 'image_location': 'testbucket_1/test.img.manifest.xml'}}, 'testbucket_1', 'test.img.manifest.xml'), ({'properties': { 'image_location': '/testbucket_2/test.img.manifest.xml'}}, 'testbucket_2', 'test.img.manifest.xml')] for mdata, bucket, manifest in data: image = image_api._s3_create(fake_context, mdata) eventlet.sleep() osimage_update.assert_called_with( image, properties={'image_state': 'available'}) osimage_update.assert_any_call( image, data=mock.ANY) s3_conn.return_value.get_bucket.assert_called_with(bucket) (s3_conn.return_value.get_bucket.return_value. get_key.assert_called_with(manifest)) (s3_conn.return_value.get_bucket.return_value. get_key.return_value. get_contents_as_string.assert_called_with()) s3_download_file.assert_called_with( s3_conn.return_value.get_bucket.return_value, 'foo', mock.ANY) s3_decrypt_image.assert_called_with( fake_context, mock.ANY, 'foo', 'foo', mock.ANY) s3_untarzip_image.assert_called_with(mock.ANY, mock.ANY) do_test()
def test_associate_route_table_invalid_parameters(self): def do_check(params, error_code): self.assert_execution_error(error_code, 'AssociateRouteTable', params) self.set_mock_db_items() do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1, 'SubnetId': fakes.ID_EC2_SUBNET_1 }, 'InvalidRouteTableID.NotFound') self.set_mock_db_items(fakes.DB_ROUTE_TABLE_1) do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1, 'SubnetId': fakes.ID_EC2_SUBNET_1 }, 'InvalidSubnetID.NotFound') id_ec2_subnet_vpc_2 = fakes.random_ec2_id('subnet') db_subnet_vpc_2 = { 'id': id_ec2_subnet_vpc_2, 'os_id': fakes.random_os_id(), 'vpc_id': fakes.ID_EC2_VPC_2 } self.set_mock_db_items(fakes.DB_ROUTE_TABLE_1, db_subnet_vpc_2) do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1, 'SubnetId': id_ec2_subnet_vpc_2 }, 'InvalidParameterValue') subnet_2 = tools.update_dict( fakes.DB_SUBNET_2, {'route_table_id': fakes.ID_EC2_ROUTE_TABLE_2}) self.set_mock_db_items(fakes.DB_ROUTE_TABLE_1, subnet_2) do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1, 'SubnetId': fakes.ID_EC2_SUBNET_2 }, 'Resource.AlreadyAssociated')
def test_stop_vpn_connection(self): # delete several connections os_conn_ids = [fakes.random_os_id() for _x in range(3)] fake_conn = { 'os_ipsec_site_connections': { fakes.random_ec2_id('subnet'): conn_id for conn_id in os_conn_ids } } vpn_connection_api._stop_vpn_connection(self.neutron, fake_conn) self.assertEqual(3, self.neutron.delete_ipsec_site_connection.call_count) for conn_id in os_conn_ids: self.neutron.delete_ipsec_site_connection.assert_any_call(conn_id) # delete several connections with exception suppressing self.neutron.reset_mock() self.neutron.delete_ipsec_site_connection.side_effect = [ None, neutron_exception.NotFound(), None ] vpn_connection_api._stop_vpn_connection(self.neutron, fake_conn) self.assertEqual(3, self.neutron.delete_ipsec_site_connection.call_count)
def test_add_item_with_same_os_id(self): # NOTE(ft): check normal update item on add os_id = fakes.random_os_id() item1 = db_api.add_item(self.context, 'fake', { 'os_id': os_id, 'key': 'val1', 'key1': 'val' }) item_id = item1['id'] item2 = db_api.add_item(self.context, 'fake', { 'os_id': os_id, 'key': 'val2', 'key2': 'val' }) expected_item = { 'id': item_id, 'os_id': os_id, 'vpc_id': None, 'key': 'val2', 'key1': 'val', 'key2': 'val' } self.assertThat(item2, matchers.DictMatches(expected_item))
def test_os_id_to_ec2_id(self, db_api): fake_context = mock.Mock(service_catalog=[{'type': 'fake'}]) fake_id = fakes.random_ec2_id('fake') fake_os_id = fakes.random_os_id() # no cache, item is found db_api.get_items_ids.return_value = [(fake_id, fake_os_id)] item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id) self.assertEqual(fake_id, item_id) db_api.get_items_ids.assert_called_once_with( fake_context, 'fake', item_ids=None, item_os_ids=(fake_os_id,)) self.assertFalse(db_api.add_item_id.called) # no cache, item isn't found db_api.get_items_ids.return_value = [] db_api.add_item_id.return_value = fake_id item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id) self.assertEqual(fake_id, item_id) db_api.add_item_id.assert_called_once_with( fake_context, 'fake', fake_os_id, project_id=None) # no item in cache, item isn't found db_api.reset_mock() ids_cache = {fakes.random_os_id(): fakes.random_ec2_id('fake')} item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id, ids_by_os_id=ids_cache) self.assertEqual(fake_id, item_id) self.assertIn(fake_os_id, ids_cache) self.assertEqual(fake_id, ids_cache[fake_os_id]) db_api.add_item_id.assert_called_once_with( fake_context, 'fake', fake_os_id, project_id=None) # no item in cache, item is found db_api.reset_mock() db_api.get_items_ids.return_value = [(fake_id, fake_os_id)] ids_cache = {} item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id, ids_by_os_id=ids_cache) self.assertEqual(fake_id, item_id) self.assertEqual({fake_os_id: fake_id}, ids_cache) self.assertFalse(db_api.add_item_id.called) # item in cache db_api.reset_mock() ids_cache = {fake_os_id: fake_id} item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id, ids_by_os_id=ids_cache) self.assertEqual(fake_id, item_id) self.assertEqual({fake_os_id: fake_id}, ids_cache) self.assertFalse(db_api.get_items_ids.called) self.assertFalse(db_api.add_item_id.called) # item in items dict items_dict = {fake_os_id: {'id': fake_id, 'os_id': fake_os_id}} ids_cache = {} item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id, items_by_os_id=items_dict, ids_by_os_id=ids_cache) self.assertEqual(fake_id, item_id) self.assertFalse(db_api.get_items_ids.called) self.assertFalse(db_api.add_item_id.called) self.assertEqual({}, ids_cache) # item not in items dict, item is found items_dict = {fake_os_id: {'id': fake_id, 'os_id': fake_os_id}} db_api.get_items_ids.return_value = [(fake_id, fake_os_id)] item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id, items_by_os_id=items_dict) self.assertEqual(fake_id, item_id) self.assertFalse(db_api.add_item_id.called)
def setUp(self): super(DbApiTestCase, self).setUp() self.context = mock.NonCallableMock( project_id=fakes.random_os_id()) self.other_context = mock.NonCallableMock( project_id=fakes.random_os_id())
def test_describe_images_being_created(self): db_api = self.mock_db() glance = self.mock_glance() context = base.create_context() image_id = fakes.random_ec2_id('ami') image = { 'id': image_id, 'os_id': None, 'is_public': False, 'description': 'fake desc' } db_api.set_mock_items(image) db_api.get_public_items.return_value = [] # describe cases when no glance image exists glance.images.list.return_value = [] expected = { 'imagesSet': [{ 'imageId': image_id, 'description': 'fake desc', 'imageOwnerId': fakes.ID_OS_PROJECT, 'imageState': 'pending', 'imageType': 'machine', 'isPublic': False }] } # describe all images result = image_api.describe_images(context) self.assertEqual(expected, result) # describe the image result = image_api.describe_images(context, image_id=[image_id]) self.assertEqual(expected, result) # describe failed image image['state'] = 'failed' expected['imagesSet'][0]['imageState'] = 'failed' result = image_api.describe_images(base.create_context()) self.assertEqual(expected, result) # describe cases when glance image exists, db item is yet not updated del image['state'] os_image_id = fakes.random_os_id() os_image = { 'id': os_image_id, 'owner': fakes.ID_OS_PROJECT, 'status': 'active', 'is_public': False, 'properties': { 'ec2_id': image_id } } glance.images.list.return_value = [fakes.OSImage(os_image)] expected['imagesSet'] = [{ 'architecture': None, 'creationDate': None, 'description': 'fake desc', 'imageId': image_id, 'imageLocation': 'None (None)', 'imageOwnerId': fakes.ID_OS_PROJECT, 'imageState': 'available', 'imageType': 'machine', 'isPublic': False, 'name': None, 'rootDeviceType': 'instance-store' }] # describe all images result = image_api.describe_images(context) self.assertEqual(expected, result) db_api.update_item.assert_called_once_with( context, tools.update_dict(image, {'os_id': os_image_id})) # describe the image db_api.reset_mock() result = image_api.describe_images(context, image_id=[image_id]) self.assertEqual(expected, result) db_api.update_item.assert_called_once_with( context, tools.update_dict(image, {'os_id': os_image_id}))
def test_describe_images_being_created(self): db_api = self.mock_db() glance = self.mock_glance() context = base.create_context() image_id = fakes.random_ec2_id('ami') image = {'id': image_id, 'os_id': None, 'is_public': False, 'description': 'fake desc'} db_api.set_mock_items(image) db_api.get_public_items.return_value = [] # describe cases when no glance image exists glance.images.list.return_value = [] expected = {'imagesSet': [{'imageId': image_id, 'description': 'fake desc', 'imageOwnerId': fakes.ID_OS_PROJECT, 'imageState': 'pending', 'imageType': 'machine', 'isPublic': False}]} # describe all images result = image_api.describe_images(context) self.assertEqual(expected, result) # describe the image result = image_api.describe_images(context, image_id=[image_id]) self.assertEqual(expected, result) # describe with filter result = image_api.describe_images( context, filter=[{'name': 'name', 'value': 'noname'}]) self.assertEqual({'imagesSet': []}, result) # describe failed image image['state'] = 'failed' expected['imagesSet'][0]['imageState'] = 'failed' result = image_api.describe_images(base.create_context()) self.assertEqual(expected, result) # describe cases when glance image exists, db item is yet not updated del image['state'] os_image_id = fakes.random_os_id() os_image = {'id': os_image_id, 'owner': fakes.ID_OS_PROJECT, 'status': 'active', 'visibility': 'private', 'ec2_id': image_id} glance.images.list.return_value = [fakes.OSImage(os_image)] expected['imagesSet'] = [{ 'architecture': None, 'creationDate': None, 'description': 'fake desc', 'imageId': image_id, 'imageLocation': 'None (None)', 'imageOwnerId': fakes.ID_OS_PROJECT, 'imageState': 'available', 'imageType': 'machine', 'isPublic': False, 'name': None, 'rootDeviceType': 'instance-store'}] # describe all images result = image_api.describe_images(context) self.assertEqual(expected, result) db_api.update_item.assert_called_once_with( context, tools.update_dict(image, {'os_id': os_image_id})) # describe the image db_api.reset_mock() result = image_api.describe_images(context, image_id=[image_id]) self.assertEqual(expected, result) db_api.update_item.assert_called_once_with( context, tools.update_dict(image, {'os_id': os_image_id}))
def test_os_id_to_ec2_id(self, db_api): fake_context = base.create_context() fake_id = fakes.random_ec2_id('fake') fake_os_id = fakes.random_os_id() # no cache, item is found db_api.get_items_ids.return_value = [(fake_id, fake_os_id)] item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id) self.assertEqual(fake_id, item_id) db_api.get_items_ids.assert_called_once_with( fake_context, 'fake', item_ids=None, item_os_ids=(fake_os_id,)) self.assertFalse(db_api.add_item_id.called) # no cache, item isn't found db_api.get_items_ids.return_value = [] db_api.add_item_id.return_value = fake_id item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id) self.assertEqual(fake_id, item_id) db_api.add_item_id.assert_called_once_with( fake_context, 'fake', fake_os_id, None) # no item in cache, item isn't found db_api.reset_mock() ids_cache = {fakes.random_os_id(): fakes.random_ec2_id('fake')} item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id, ids_by_os_id=ids_cache) self.assertEqual(fake_id, item_id) self.assertIn(fake_os_id, ids_cache) self.assertEqual(fake_id, ids_cache[fake_os_id]) db_api.add_item_id.assert_called_once_with( fake_context, 'fake', fake_os_id, None) # no item in cache, item is found db_api.reset_mock() db_api.get_items_ids.return_value = [(fake_id, fake_os_id)] ids_cache = {} item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id, ids_by_os_id=ids_cache) self.assertEqual(fake_id, item_id) self.assertEqual({fake_os_id: fake_id}, ids_cache) self.assertFalse(db_api.add_item_id.called) # item in cache db_api.reset_mock() ids_cache = {fake_os_id: fake_id} item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id, ids_by_os_id=ids_cache) self.assertEqual(fake_id, item_id) self.assertEqual({fake_os_id: fake_id}, ids_cache) self.assertFalse(db_api.get_items_ids.called) self.assertFalse(db_api.add_item_id.called) # item in items dict items_dict = {fake_os_id: {'id': fake_id, 'os_id': fake_os_id}} ids_cache = {} item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id, items_by_os_id=items_dict, ids_by_os_id=ids_cache) self.assertEqual(fake_id, item_id) self.assertFalse(db_api.get_items_ids.called) self.assertFalse(db_api.add_item_id.called) self.assertEqual({}, ids_cache) # item not in items dict, item is found items_dict = {fake_os_id: {'id': fake_id, 'os_id': fake_os_id}} db_api.get_items_ids.return_value = [(fake_id, fake_os_id)] item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id, items_by_os_id=items_dict) self.assertEqual(fake_id, item_id) self.assertFalse(db_api.add_item_id.called)
def test_set_subnet_vpn(self): context = base.create_context() cleaner = common.OnCrashCleaner() cidrs = [fakes.CIDR_VPN_1_STATIC, fakes.CIDR_VPN_1_PROPAGATED_1] # create ipsec site connection case id_os_connection = fakes.random_os_id() os_connection = { "vpnservice_id": fakes.ID_OS_VPNSERVICE_1, "ikepolicy_id": fakes.ID_OS_IKEPOLICY_1, "ipsecpolicy_id": fakes.ID_OS_IPSECPOLICY_1, "peer_address": fakes.IP_CUSTOMER_GATEWAY_ADDRESS_1, "peer_cidrs": cidrs, "psk": fakes.PRE_SHARED_KEY_1, "name": (fakes.ID_EC2_VPN_CONNECTION_1 + "/" + fakes.ID_EC2_SUBNET_1), "peer_id": fakes.IP_CUSTOMER_GATEWAY_ADDRESS_1, "mtu": 1427, "initiator": "response-only", } self.neutron.create_ipsec_site_connection.side_effect = tools.get_neutron_create( "ipsec_site_connection", id_os_connection ) vpn_connection_api._set_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_1, copy.deepcopy(fakes.DB_VPN_CONNECTION_1), fakes.DB_CUSTOMER_GATEWAY_1, cidrs, ) self.neutron.create_ipsec_site_connection.assert_called_once_with({"ipsec_site_connection": os_connection}) vpn_connection_1 = copy.deepcopy(fakes.DB_VPN_CONNECTION_1) (vpn_connection_1["os_ipsec_site_connections"][fakes.ID_EC2_SUBNET_1]) = id_os_connection self.db_api.update_item.assert_called_once_with(context, vpn_connection_1) # update ipsec site connection case self.db_api.reset_mock() self.neutron.reset_mock() vpn_connection_api._set_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_2, fakes.DB_VPN_CONNECTION_1, fakes.DB_CUSTOMER_GATEWAY_1, cidrs, ) self.neutron.update_ipsec_site_connection.assert_called_once_with( fakes.ID_OS_IPSEC_SITE_CONNECTION_2, {"ipsec_site_connection": {"peer_cidrs": cidrs}} ) self.assertFalse(self.neutron.create_ipsec_site_connection.called) self.assertFalse(self.db_api.update_item.called) # rollback creating of ipsec site connection case self.db_api.reset_mock() self.neutron.reset_mock() try: with common.OnCrashCleaner() as cleaner: vpn_connection_api._set_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_1, copy.deepcopy(fakes.DB_VPN_CONNECTION_1), fakes.DB_CUSTOMER_GATEWAY_1, cidrs, ) raise Exception("fake-exception") except Exception as ex: if ex.message != "fake-exception": raise self.neutron.delete_ipsec_site_connection.assert_called_once_with(id_os_connection) self.db_api.update_item.assert_called_with(mock.ANY, fakes.DB_VPN_CONNECTION_1) # rollback updating of ipsec site connection case self.db_api.reset_mock() self.neutron.reset_mock() try: with common.OnCrashCleaner() as cleaner: vpn_connection_api._set_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_2, fakes.DB_VPN_CONNECTION_1, fakes.DB_CUSTOMER_GATEWAY_1, cidrs, ) raise Exception("fake-exception") except Exception as ex: if ex.message != "fake-exception": raise self.assertFalse(self.neutron.delete_ipsec_site_connection.called) self.assertFalse(self.db_api.update_item.called)
def test_create_route_invalid_parameters(self): id_ec2_eni_vpc_2 = fakes.random_ec2_id('eni') eni_vpc_2 = fakes.gen_db_network_interface( id_ec2_eni_vpc_2, fakes.random_os_id(), fakes.ID_EC2_VPC_2, fakes.random_ec2_id('subnet'), '10.20.0.10', instance_id=fakes.ID_EC2_INSTANCE_2) eni_2_in_instance_1 = fakes.gen_db_network_interface( fakes.random_ec2_id('eni'), fakes.random_os_id(), fakes.ID_EC2_VPC_1, fakes.random_ec2_id('subnet'), '10.10.3.15', instance_id=fakes.ID_EC2_INSTANCE_1) self.set_mock_db_items(fakes.DB_ROUTE_TABLE_1, fakes.DB_ROUTE_TABLE_2, fakes.DB_VPC_1, eni_vpc_2, fakes.DB_IGW_1, fakes.DB_IGW_2, fakes.DB_NETWORK_INTERFACE_1, fakes.DB_NETWORK_INTERFACE_2) def do_check(params, error_code): self.assert_execution_error(error_code, 'CreateRoute', params) do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1, 'DestinationCidrBlock': 'not_a_cidr', 'GatewayId': fakes.ID_EC2_IGW_1 }, 'InvalidParameterValue') do_check( { 'RouteTableId': fakes.random_ec2_id('rtb'), 'DestinationCidrBlock': fakes.CIDR_VPC_1, 'GatewayId': fakes.ID_EC2_IGW_1 }, 'InvalidRouteTableID.NotFound') # NOTE(ft): redefine vpc local route do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1, 'DestinationCidrBlock': fakes.CIDR_VPC_1, 'GatewayId': fakes.ID_EC2_IGW_1 }, 'InvalidParameterValue') # NOTE(ft): create route for cidr lesser than vpc cidr do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1, 'DestinationCidrBlock': fakes.IP_NETWORK_INTERFACE_1 + '/24', 'GatewayId': fakes.ID_EC2_IGW_1 }, 'InvalidParameterValue') # NOTE(ft): redefine existed route by route with another attributes do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2, 'DestinationCidrBlock': '0.0.0.0/0', 'NetworkInterfaceId': fakes.ID_EC2_NETWORK_INTERFACE_1 }, 'RouteAlreadyExists') # NOTE(ft): missed traffic receiver do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2, 'DestinationCidrBlock': '0.0.0.0/0' }, 'MissingParameter') # NOTE(ft): more than one traffic receiver do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2, 'DestinationCidrBlock': '0.0.0.0/0', 'NetworkInterfaceId': fakes.ID_EC2_NETWORK_INTERFACE_1, 'GatewayId': fakes.ID_EC2_IGW_1 }, 'InvalidParameterCombination') # NOTE(ft): gateway from different vpc do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2, 'DestinationCidrBlock': '192.168.100.0/0', 'GatewayId': fakes.ID_EC2_IGW_2 }, 'InvalidParameterValue') # NOTE(ft): network interface from different vpc do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2, 'DestinationCidrBlock': '192.168.100.0/0', 'NetworkInterfaceId': id_ec2_eni_vpc_2 }, 'InvalidParameterValue') # NOTE(ft): not vpc instance do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2, 'DestinationCidrBlock': '192.168.100.0/0', 'InstanceId': fakes.ID_EC2_INSTANCE_2 }, 'InvalidParameterValue') # NOTE(ft): multiple network interfaces in instance self.add_mock_db_items(eni_2_in_instance_1) do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2, 'DestinationCidrBlock': '192.168.100.0/0', 'InstanceId': fakes.ID_EC2_INSTANCE_1 }, 'InvalidInstanceID') # NOTE(ft): different vpc instance do_check( { 'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2, 'DestinationCidrBlock': '192.168.100.0/0', 'InstanceId': fakes.ID_EC2_INSTANCE_2 }, 'InvalidParameterValue')
def setUp(self): super(DbApiTestCase, self).setUp() self.context = mock.NonCallableMock(project_id=fakes.random_os_id()) self.other_context = mock.NonCallableMock( project_id=fakes.random_os_id())
def test_describe_new_instance_then_its_alien_image(self): self._test_describe_new_instance_then_its_image(fakes.random_os_id())
def test_set_subnet_vpn(self): context = base.create_context() cleaner = common.OnCrashCleaner() cidrs = [fakes.CIDR_VPN_1_STATIC, fakes.CIDR_VPN_1_PROPAGATED_1] # create ipsec site connection case id_os_connection = fakes.random_os_id() os_connection = { 'vpnservice_id': fakes.ID_OS_VPNSERVICE_1, 'ikepolicy_id': fakes.ID_OS_IKEPOLICY_1, 'ipsecpolicy_id': fakes.ID_OS_IPSECPOLICY_1, 'peer_address': fakes.IP_CUSTOMER_GATEWAY_ADDRESS_1, 'peer_cidrs': cidrs, 'psk': fakes.PRE_SHARED_KEY_1, 'name': (fakes.ID_EC2_VPN_CONNECTION_1 + '/' + fakes.ID_EC2_SUBNET_1), 'peer_id': fakes.IP_CUSTOMER_GATEWAY_ADDRESS_1, 'mtu': 1427, 'initiator': 'response-only', } self.neutron.create_ipsec_site_connection.side_effect = ( tools.get_neutron_create('ipsec_site_connection', id_os_connection)) vpn_connection_api._set_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_1, copy.deepcopy(fakes.DB_VPN_CONNECTION_1), fakes.DB_CUSTOMER_GATEWAY_1, cidrs) self.neutron.create_ipsec_site_connection.assert_called_once_with( {'ipsec_site_connection': os_connection}) vpn_connection_1 = copy.deepcopy(fakes.DB_VPN_CONNECTION_1) (vpn_connection_1['os_ipsec_site_connections'] [fakes.ID_EC2_SUBNET_1]) = id_os_connection self.db_api.update_item.assert_called_once_with( context, vpn_connection_1) # update ipsec site connection case self.db_api.reset_mock() self.neutron.reset_mock() vpn_connection_api._set_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_2, fakes.DB_VPN_CONNECTION_1, fakes.DB_CUSTOMER_GATEWAY_1, cidrs) self.neutron.update_ipsec_site_connection.assert_called_once_with( fakes.ID_OS_IPSEC_SITE_CONNECTION_2, {'ipsec_site_connection': {'peer_cidrs': cidrs}}) self.assertFalse(self.neutron.create_ipsec_site_connection.called) self.assertFalse(self.db_api.update_item.called) # rollback creating of ipsec site connection case self.db_api.reset_mock() self.neutron.reset_mock() try: with common.OnCrashCleaner() as cleaner: vpn_connection_api._set_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_1, copy.deepcopy(fakes.DB_VPN_CONNECTION_1), fakes.DB_CUSTOMER_GATEWAY_1, cidrs) raise Exception('fake-exception') except Exception as ex: if str(ex) != 'fake-exception': raise self.neutron.delete_ipsec_site_connection.assert_called_once_with( id_os_connection) self.db_api.update_item.assert_called_with( mock.ANY, fakes.DB_VPN_CONNECTION_1) # rollback updating of ipsec site connection case self.db_api.reset_mock() self.neutron.reset_mock() try: with common.OnCrashCleaner() as cleaner: vpn_connection_api._set_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_2, fakes.DB_VPN_CONNECTION_1, fakes.DB_CUSTOMER_GATEWAY_1, cidrs) raise Exception('fake-exception') except Exception as ex: if str(ex) != 'fake-exception': raise self.assertFalse(self.neutron.delete_ipsec_site_connection.called) self.assertFalse(self.db_api.update_item.called)
def test_describe_new_alien_ami_local_aki_images_with_mappings(self): alien_project_id = fakes.random_os_id() self._test_describe_new_images( image_project_id=alien_project_id, aki_image_project_id=fakes.ID_OS_PROJECT, with_id_mapping=True)
def test_describe_new_alien_snapshot_from_new_image(self): image = self._get_new_ebs_image(image_project_id=fakes.random_os_id()) snapshot_id = self._find_snapshot_id_in_bdm(image, '/dev/vds') self.assertRaises(exception.InvalidSnapshotNotFound, snapshot_api.describe_snapshots, self.context, [snapshot_id])
def test_mismatch_project_id(self): self.fake_context.project_id = fakes.random_os_id() self.assertRaises(exception.EC2MetadataNotFound, api.get_metadata_item, self.fake_context, ['2009-04-04'], fakes.ID_OS_INSTANCE_1, fakes.IP_NETWORK_INTERFACE_2)