def test_get_os_image(self, db_api, glance): glance = glance.return_value fake_context = base.create_context() os_image = fakes.OSImage(fakes.OS_IMAGE_1) glance.images.get.return_value = os_image # check normal flow db_api.get_items_ids.return_value = [ (fakes.ID_EC2_IMAGE_1, fakes.ID_OS_IMAGE_1)] self.assertEqual( os_image, ec2utils.get_os_image(fake_context, fakes.ID_EC2_IMAGE_1)) db_api.get_items_ids.assert_called_with( mock.ANY, 'ami', item_ids=(fakes.ID_EC2_IMAGE_1,), item_os_ids=None) glance.images.get.assert_called_with(fakes.ID_OS_IMAGE_1) # check case of absence of an image in OS glance.images.get.side_effect = glance_exception.HTTPNotFound() self.assertRaises( exception.InvalidAMIIDNotFound, ec2utils.get_os_image, fake_context, fakes.ID_EC2_IMAGE_1) # check case of an unknown image id db_api.get_items_ids.return_value = [] self.assertRaises( exception.InvalidAMIIDNotFound, ec2utils.get_os_image, fake_context, fakes.random_ec2_id('ami'))
def test_s3_create_bdm(self, spawn_n): glance = self.mock_glance() metadata = {'image_location': 'fake_bucket/fake_manifest', 'root_device_name': '/dev/sda1', 'block_device_mapping': [ {'device_name': '/dev/sda1', 'snapshot_id': fakes.ID_OS_SNAPSHOT_1, 'delete_on_termination': True}, {'device_name': '/dev/sda2', 'virtual_name': 'ephemeral0'}, {'device_name': '/dev/sdb0', 'no_device': True}]} fake_context = base.create_context() with mock.patch('ec2api.api.image._s3_conn') as s3_conn: (s3_conn.return_value. get_object.return_value) = {'Body': FILE_MANIFEST_XML} image_api._s3_create(fake_context, metadata) glance.images.create.assert_called_once_with( disk_format='ami', container_format='ami', visibility='private', architecture='x86_64', image_state='pending', root_device_name='/dev/sda1', block_device_mapping=[{'device_name': '/dev/sda1', 'snapshot_id': fakes.ID_OS_SNAPSHOT_1, 'delete_on_termination': True}, {'device_name': '/dev/sda2', 'virtual_name': 'ephemeral0'}, {'device_name': '/dev/sdb0', 'no_device': True}], image_location='fake_bucket/fake_manifest')
def test_s3_parse_manifest(self): self.set_mock_db_items(fakes.DB_IMAGE_AKI_1, fakes.DB_IMAGE_ARI_1) self.db_api.get_item_by_id.return_value = None self.glance.images.get.side_effect = ( tools.get_by_1st_arg_getter({ fakes.ID_OS_IMAGE_AKI_1: fakes.OSImage(fakes.OS_IMAGE_AKI_1), fakes.ID_OS_IMAGE_ARI_1: fakes.OSImage(fakes.OS_IMAGE_ARI_1)})) metadata, image_parts, key, iv = image_api._s3_parse_manifest( base.create_context(), AMI_MANIFEST_XML) expected_metadata = { 'disk_format': 'ami', 'container_format': 'ami', 'properties': {'architecture': 'x86_64', 'kernel_id': fakes.ID_OS_IMAGE_AKI_1, 'ramdisk_id': fakes.ID_OS_IMAGE_ARI_1, 'mappings': [ {"device": "sda1", "virtual": "ami"}, {"device": "/dev/sda1", "virtual": "root"}, {"device": "sda2", "virtual": "ephemeral0"}, {"device": "sda3", "virtual": "swap"}]}} self.assertThat(metadata, matchers.DictMatches(expected_metadata, orderless_lists=True)) self.assertThat(image_parts, matchers.ListMatches(['foo'])) self.assertEqual('foo', key) self.assertEqual('foo', iv) self.db_api.get_items_ids.assert_any_call( mock.ANY, 'aki', item_ids=(fakes.ID_EC2_IMAGE_AKI_1,), item_os_ids=None) self.db_api.get_items_ids.assert_any_call( mock.ANY, 'ari', item_ids=(fakes.ID_EC2_IMAGE_ARI_1,), item_os_ids=None)
def test_stop_gateway_vpn_connections(self, stop_vpn_connection): context = base.create_context() cleaner = common.OnCrashCleaner() vpn_connection_3 = tools.update_dict( fakes.DB_VPN_CONNECTION_1, {"id": fakes.random_ec2_id("vpn"), "os_ipsec_site_connections": {}} ) self.set_mock_db_items(fakes.DB_VPN_CONNECTION_1, vpn_connection_3, fakes.DB_VPN_CONNECTION_2) vpn_connection_api._stop_gateway_vpn_connections(context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1) self.assertEqual(2, stop_vpn_connection.call_count) stop_vpn_connection.assert_any_call(self.neutron, fakes.DB_VPN_CONNECTION_1) stop_vpn_connection.assert_any_call(self.neutron, vpn_connection_3) self.assertEqual(2, self.db_api.update_item.call_count) self.db_api.update_item.assert_any_call( mock.ANY, tools.update_dict(fakes.DB_VPN_CONNECTION_1, {"os_ipsec_site_connections": {}}) ) self.db_api.update_item.assert_any_call(mock.ANY, vpn_connection_3) self.db_api.reset_mock() self.neutron.reset_mock() stop_vpn_connection.reset_mock() self.set_mock_db_items(fakes.DB_VPN_CONNECTION_1) try: with common.OnCrashCleaner() as cleaner: vpn_connection_api._stop_gateway_vpn_connections(context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_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)
def test_create_subnet_vpnservice(self): self.neutron.create_vpnservice.side_effect = tools.get_neutron_create( 'vpnservice', fakes.ID_OS_VPNSERVICE_1) context = base.create_context() cleaner = common.OnCrashCleaner() vpn_gateway_api._create_subnet_vpnservice( context, self.neutron, cleaner, copy.deepcopy(self.DB_SUBNET_1_NO_VPN), fakes.DB_VPC_1) self.neutron.create_vpnservice.assert_called_once_with( {'vpnservice': tools.purge_dict(fakes.OS_VPNSERVICE_1, ('id',))}) self.db_api.update_item.assert_called_once_with( mock.ANY, fakes.DB_SUBNET_1) try: with common.OnCrashCleaner() as cleaner: vpn_gateway_api._create_subnet_vpnservice( context, self.neutron, cleaner, copy.deepcopy(self.DB_SUBNET_1_NO_VPN), fakes.DB_VPC_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, self.DB_SUBNET_1_NO_VPN) self.neutron.delete_vpnservice.assert_called_once_with( fakes.ID_OS_VPNSERVICE_1)
def test_stop_vpn_in_subnet(self, delete_vpnservice, delete_subnet_vpn): context = base.create_context() cleaner = common.OnCrashCleaner() mock_manager = mock.Mock() mock_manager.attach_mock(delete_vpnservice, 'delete_vpnservice') mock_manager.attach_mock(delete_subnet_vpn, 'delete_subnet_vpn') self.set_mock_db_items(fakes.DB_VPN_CONNECTION_1, fakes.DB_VPN_CONNECTION_2) vpn_gateway_api._stop_vpn_in_subnet( context, self.neutron, cleaner, copy.deepcopy(fakes.DB_SUBNET_1)) mock_manager.has_calls([ mock.call.delete_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_1, fakes.DB_VPN_CONNECTION_1), mock.call.delete_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_1, fakes.DB_VPN_CONNECTION_2), mock.call.delete_vpnservice( self.neutron, fakes.ID_OS_VPNSERVICE_1, fakes.ID_EC2_SUBNET_1)]) delete_subnet_vpn.reset_mock() delete_vpnservice.reset_mock() vpn_gateway_api._stop_vpn_in_subnet( context, self.neutron, cleaner, self.DB_SUBNET_1_NO_VPN) self.assertFalse(delete_subnet_vpn.called) self.assertFalse(delete_vpnservice.called)
def test_start_vpn_in_subnet(self, create_subnet_vpnservice, reset_vpn_connection): context = base.create_context() cleaner = common.OnCrashCleaner() mock_manager = mock.Mock() mock_manager.attach_mock(create_subnet_vpnservice, 'create_subnet_vpnservice') mock_manager.attach_mock(reset_vpn_connection, 'reset_vpn_connection') self.set_mock_db_items(fakes.DB_VPN_GATEWAY_1, fakes.DB_VPN_GATEWAY_2) vpn_gateway_api._start_vpn_in_subnet( context, self.neutron, cleaner, copy.deepcopy(fakes.DB_SUBNET_1), fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1) mock_manager.assert_has_calls([ mock.call.create_subnet_vpnservice( context, self.neutron, cleaner, fakes.DB_SUBNET_1, fakes.DB_VPC_1), mock.call.reset_vpn_connection( context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1, subnets=[fakes.DB_SUBNET_1], route_tables=[fakes.DB_ROUTE_TABLE_1])]) create_subnet_vpnservice.reset_mock() reset_vpn_connection.reset_mock() self.add_mock_db_items(self.DB_VPN_GATEWAY_1_DETACHED) vpn_gateway_api._start_vpn_in_subnet( context, self.neutron, cleaner, copy.deepcopy(fakes.DB_SUBNET_1), fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1) self.assertFalse(create_subnet_vpnservice.called) self.assertFalse(reset_vpn_connection.called)
def test_s3_create_bdm(self, spawn_n): glance = self.mock_glance() metadata = { 'properties': { 'image_location': 'fake_bucket/fake_manifest', 'root_device_name': '/dev/sda1', 'block_device_mapping': [{ 'device_name': '/dev/sda1', 'snapshot_id': fakes.ID_OS_SNAPSHOT_1, 'delete_on_termination': True }, { 'device_name': '/dev/sda2', 'virtual_name': 'ephemeral0' }, { 'device_name': '/dev/sdb0', 'no_device': True }] } } fake_context = base.create_context() with mock.patch('ec2api.api.image._s3_conn') as s3_conn: (s3_conn.return_value.get_bucket.return_value.get_key.return_value. get_contents_as_string.return_value) = FILE_MANIFEST_XML image_api._s3_create(fake_context, metadata) glance.images.create.assert_called_once_with( disk_format='ami', container_format='ami', is_public=False, properties={ 'architecture': 'x86_64', 'image_state': 'pending', 'root_device_name': '/dev/sda1', 'block_device_mapping': [{ 'device_name': '/dev/sda1', 'snapshot_id': fakes.ID_OS_SNAPSHOT_1, 'delete_on_termination': True }, { 'device_name': '/dev/sda2', 'virtual_name': 'ephemeral0' }, { 'device_name': '/dev/sdb0', 'no_device': True }], 'image_location': 'fake_bucket/fake_manifest' })
def test_s3_parse_manifest(self): self.set_mock_db_items(fakes.DB_IMAGE_AKI_1, fakes.DB_IMAGE_ARI_1) self.db_api.get_item_by_id.return_value = None self.glance.images.get.side_effect = (tools.get_by_1st_arg_getter({ fakes.ID_OS_IMAGE_AKI_1: fakes.OSImage(fakes.OS_IMAGE_AKI_1), fakes.ID_OS_IMAGE_ARI_1: fakes.OSImage(fakes.OS_IMAGE_ARI_1) })) metadata, image_parts, key, iv = image_api._s3_parse_manifest( base.create_context(), AMI_MANIFEST_XML) expected_metadata = { 'disk_format': 'ami', 'container_format': 'ami', 'properties': { 'architecture': 'x86_64', 'kernel_id': fakes.ID_OS_IMAGE_AKI_1, 'ramdisk_id': fakes.ID_OS_IMAGE_ARI_1, 'mappings': [{ "device": "sda1", "virtual": "ami" }, { "device": "/dev/sda1", "virtual": "root" }, { "device": "sda2", "virtual": "ephemeral0" }, { "device": "sda3", "virtual": "swap" }] } } self.assertThat( metadata, matchers.DictMatches(expected_metadata, orderless_lists=True)) self.assertThat(image_parts, matchers.ListMatches(['foo'])) self.assertEqual('foo', key) self.assertEqual('foo', iv) self.db_api.get_items_ids.assert_any_call( mock.ANY, 'aki', item_ids=(fakes.ID_EC2_IMAGE_AKI_1, ), item_os_ids=None) self.db_api.get_items_ids.assert_any_call( mock.ANY, 'ari', item_ids=(fakes.ID_EC2_IMAGE_ARI_1, ), item_os_ids=None)
def test_get_db_items(self): describer = image_api.ImageDescriber() describer.context = base.create_context() # NOTE(ft): the first requested image appears is user owend and public, # the second is absent db_api = self.mock_db() db_api.set_mock_items(fakes.DB_IMAGE_1) describer.ids = set([fakes.ID_EC2_IMAGE_1, fakes.ID_EC2_IMAGE_2]) self.assertRaises(exception.InvalidAMIIDNotFound, describer.get_db_items)
def do_test1(unpack_request, get_context, get_ids): get_context.return_value = base.create_context(is_os_admin=True) unpack_request.return_value = mock.sentinel.private_ip get_ids.return_value = (mock.sentinel.os_instance_id, mock.sentinel.project_id) retval = self.handler._get_requester(req) self.assertEqual(expected, retval) get_context.assert_called_with() unpack_request.assert_called_with(req) get_ids.assert_called_with(get_context.return_value, mock.sentinel.private_ip)
def setUp(self): super(MetadataApiTestCase, self).setUp() self.instance_api = self.mock("ec2api.metadata.api.instance_api") self.set_mock_db_items(fakes.DB_INSTANCE_1) self.instance_api.describe_instances.return_value = {"reservationSet": [fakes.EC2_RESERVATION_1]} self.instance_api.describe_instance_attribute.return_value = { "instanceId": fakes.ID_EC2_INSTANCE_1, "userData": {"value": base64.b64encode("fake_user_data")}, } self.fake_context = base.create_context()
def test_s3_create_image_locations(self, osimage_update): self.configure(image_decryption_dir=None) glance = self.mock_glance() _handle, tempf = tempfile.mkstemp() fake_context = base.create_context() @mock.patch('ec2api.api.image._s3_untarzip_image') @mock.patch('ec2api.api.image._s3_decrypt_image') @mock.patch('ec2api.api.image._s3_download_file') @mock.patch('ec2api.api.image._s3_conn') 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_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 setUp(self): super(MetadataApiTestCase, self).setUp() self.instance_api = self.mock('ec2api.metadata.api.instance_api') self.set_mock_db_items(fakes.DB_INSTANCE_1) self.instance_api.describe_instances.return_value = { 'reservationSet': [fakes.EC2_RESERVATION_1]} userDataValue = base64.b64encode(FAKE_USER_DATA.encode('utf-8')) self.instance_api.describe_instance_attribute.return_value = { 'instanceId': fakes.ID_EC2_INSTANCE_1, 'userData': {'value': userDataValue}} self.fake_context = base.create_context()
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_s3_create_image_locations(self, osimage_update): self.configure(image_decryption_dir=None) glance = self.mock_glance() _handle, tempf = tempfile.mkstemp() fake_context = base.create_context() @mock.patch('ec2api.api.image._s3_untarzip_image') @mock.patch('ec2api.api.image._s3_decrypt_image') @mock.patch('ec2api.api.image._s3_download_file') @mock.patch('ec2api.api.image._s3_conn') 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_get_metadata_integral(self, network_interface_api, security_group_api, create_region): fake_context = base.create_context(is_os_admin=True) self.set_mock_db_items(fakes.DB_INSTANCE_1, fakes.DB_INSTANCE_2, fakes.DB_NETWORK_INTERFACE_1, fakes.DB_NETWORK_INTERFACE_2, fakes.DB_IMAGE_1, fakes.DB_IMAGE_2, fakes.DB_IMAGE_ARI_1, fakes.DB_IMAGE_AKI_1, fakes.DB_VOLUME_1, fakes.DB_VOLUME_2, fakes.DB_VOLUME_3) self.nova_admin.servers.list.return_value = [ fakes.OSInstance_full(fakes.OS_INSTANCE_1), fakes.OSInstance_full(fakes.OS_INSTANCE_2) ] self.nova_admin.servers.get.side_effect = tools.get_by_1st_arg_getter({ fakes.ID_OS_INSTANCE_1: fakes.OSInstance_full(fakes.OS_INSTANCE_1), fakes.ID_OS_INSTANCE_2: fakes.OSInstance_full(fakes.OS_INSTANCE_2) }) self.nova_admin.keypairs._get.return_value = (fakes.NovaKeyPair( fakes.OS_KEY_PAIR)) self.cinder.volumes.list.return_value = [ fakes.OSVolume(fakes.OS_VOLUME_1), fakes.OSVolume(fakes.OS_VOLUME_2), fakes.OSVolume(fakes.OS_VOLUME_3) ] network_interface_api.describe_network_interfaces.side_effect = ( lambda *args, **kwargs: copy.deepcopy({ 'networkInterfaceSet': [fakes.EC2_NETWORK_INTERFACE_1, fakes.EC2_NETWORK_INTERFACE_2] })) security_group_api.describe_security_groups.return_value = { 'securityGroupInfo': [fakes.EC2_SECURITY_GROUP_1, fakes.EC2_SECURITY_GROUP_3] } create_region.get.return_value = cache_core.NO_VALUE retval = api.get_metadata_item(fake_context, ['latest', 'meta-data', 'instance-id'], fakes.ID_OS_INSTANCE_1, fakes.IP_NETWORK_INTERFACE_2, create_region) self.assertEqual(fakes.ID_EC2_INSTANCE_1, retval) retval = api.get_metadata_item(fake_context, ['latest', 'meta-data', 'instance-id'], fakes.ID_OS_INSTANCE_2, '10.200.1.15', create_region) self.assertEqual(fakes.ID_EC2_INSTANCE_2, retval)
def test_get_metadata_by_instance_id(self, get_context, unpack_request, get_metadata_item): get_context.return_value = base.create_context(is_os_admin=True) unpack_request.return_value = ("fake_instance_id", "fake_project_id", "fake_instance_ip") get_metadata_item.return_value = "fake_item" req = mock.Mock(headers={"X-Instance-ID": "fake_instance_id"}) retval = self.handler._get_metadata(req, ["fake_ver", "fake_attr"]) self.assertEqual("fake_item", retval) get_context.assert_called_with() unpack_request.assert_called_with(req) get_metadata_item.assert_called_with( get_context.return_value, ["fake_ver", "fake_attr"], "fake_instance_id", "fake_instance_ip" ) self.assertEqual("fake_project_id", get_context.return_value.project_id)
def test_get_db_items(self, db_api): describer = image_api.ImageDescriber() describer.context = base.create_context() # NOTE(ft): the first requested image appears is user owend and public, # the second is absent db_api.get_items.side_effect = (tools.get_db_api_get_items()) db_api.get_items_by_ids.side_effect = ( tools.get_db_api_get_items_by_ids(fakes.DB_IMAGE_1)) db_api.get_public_items.side_effect = [[fakes.DB_IMAGE_1], [], []] describer.ids = set([fakes.ID_EC2_IMAGE_1, fakes.ID_EC2_IMAGE_2]) self.assertRaises(exception.InvalidAMIIDNotFound, describer.get_db_items)
def test_get_metadata(self, get_context, get_metadata_item): get_context.return_value = base.create_context(is_os_admin=True) requester = {'os_instance_id': mock.sentinel.os_instance_id, 'project_id': mock.sentinel.project_id, 'private_ip': mock.sentinel.private_ip} get_metadata_item.return_value = 'fake_item' retval = self.handler._get_metadata(['fake_ver', 'fake_attr'], requester) self.assertEqual('fake_item', retval) get_context.assert_called_with() get_metadata_item.assert_called_with( get_context.return_value, ['fake_ver', 'fake_attr'], mock.sentinel.os_instance_id, mock.sentinel.private_ip) self.assertEqual(mock.sentinel.project_id, get_context.return_value.project_id)
def test_get_db_items(self, db_api): describer = image_api.ImageDescriber() describer.context = base.create_context() # NOTE(ft): the first requested image appears is user owend and public, # the second is absent db_api.get_items.side_effect = ( tools.get_db_api_get_items()) db_api.get_items_by_ids.side_effect = ( tools.get_db_api_get_items_by_ids(fakes.DB_IMAGE_1)) db_api.get_public_items.side_effect = [ [fakes.DB_IMAGE_1], [], []] describer.ids = set([fakes.ID_EC2_IMAGE_1, fakes.ID_EC2_IMAGE_2]) self.assertRaises(exception.InvalidAMIIDNotFound, describer.get_db_items)
def test_update_vpn_routes(self, reset_vpn_connections): context = base.create_context() cleaner = common.OnCrashCleaner() self.set_mock_db_items() vpn_connection_api._update_vpn_routes( context, self.neutron, cleaner, fakes.DB_ROUTE_TABLE_1, [fakes.DB_SUBNET_1]) self.assertFalse(reset_vpn_connections.called) self.set_mock_db_items(fakes.DB_VPN_GATEWAY_1) vpn_connection_api._update_vpn_routes( context, self.neutron, cleaner, fakes.DB_ROUTE_TABLE_1, [fakes.DB_SUBNET_1]) reset_vpn_connections.assert_called_once_with( context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1, route_tables=[fakes.DB_ROUTE_TABLE_1], subnets=[fakes.DB_SUBNET_1])
def test_get_metadata(self, get_context, get_metadata_item): get_context.return_value = base.create_context(is_os_admin=True) requester = {'os_instance_id': mock.sentinel.os_instance_id, 'project_id': mock.sentinel.project_id, 'private_ip': mock.sentinel.private_ip} get_metadata_item.return_value = 'fake_item' self.handler.cache_region = 'fake_region' retval = self.handler._get_metadata(['fake_ver', 'fake_attr'], requester) self.assertEqual('fake_item', retval) get_context.assert_called_with() get_metadata_item.assert_called_with( get_context.return_value, ['fake_ver', 'fake_attr'], mock.sentinel.os_instance_id, mock.sentinel.private_ip, 'fake_region') self.assertEqual(mock.sentinel.project_id, get_context.return_value.project_id)
def test_get_metadata_by_instance_id(self, get_context, unpack_request, get_metadata_item): get_context.return_value = base.create_context(is_os_admin=True) unpack_request.return_value = ('fake_instance_id', 'fake_project_id', 'fake_instance_ip') get_metadata_item.return_value = 'fake_item' req = mock.Mock(headers={'X-Instance-ID': 'fake_instance_id'}) retval = self.handler._get_metadata(req, ['fake_ver', 'fake_attr']) self.assertEqual('fake_item', retval) get_context.assert_called_with() unpack_request.assert_called_with(req) get_metadata_item.assert_called_with(get_context.return_value, ['fake_ver', 'fake_attr'], 'fake_instance_id', 'fake_instance_ip') self.assertEqual('fake_project_id', get_context.return_value.project_id)
def test_get_metadata_integral(self, network_interface_api, security_group_api, create_region): fake_context = base.create_context(is_os_admin=True) self.set_mock_db_items( fakes.DB_INSTANCE_1, fakes.DB_INSTANCE_2, fakes.DB_NETWORK_INTERFACE_1, fakes.DB_NETWORK_INTERFACE_2, fakes.DB_IMAGE_1, fakes.DB_IMAGE_2, fakes.DB_IMAGE_ARI_1, fakes.DB_IMAGE_AKI_1, fakes.DB_VOLUME_1, fakes.DB_VOLUME_2, fakes.DB_VOLUME_3) self.nova_admin.servers.list.return_value = [ fakes.OSInstance_full(fakes.OS_INSTANCE_1), fakes.OSInstance_full(fakes.OS_INSTANCE_2)] self.nova_admin.servers.get.side_effect = tools.get_by_1st_arg_getter({ fakes.ID_OS_INSTANCE_1: fakes.OSInstance_full(fakes.OS_INSTANCE_1), fakes.ID_OS_INSTANCE_2: fakes.OSInstance_full(fakes.OS_INSTANCE_2) }) self.nova_admin.keypairs._get.return_value = ( fakes.NovaKeyPair(fakes.OS_KEY_PAIR)) self.cinder.volumes.list.return_value = [ fakes.OSVolume(fakes.OS_VOLUME_1), fakes.OSVolume(fakes.OS_VOLUME_2), fakes.OSVolume(fakes.OS_VOLUME_3)] network_interface_api.describe_network_interfaces.side_effect = ( lambda *args, **kwargs: copy.deepcopy({ 'networkInterfaceSet': [fakes.EC2_NETWORK_INTERFACE_1, fakes.EC2_NETWORK_INTERFACE_2]})) security_group_api.describe_security_groups.return_value = { 'securityGroupInfo': [fakes.EC2_SECURITY_GROUP_1, fakes.EC2_SECURITY_GROUP_3]} create_region.get.return_value = cache_core.NO_VALUE retval = api.get_metadata_item( fake_context, ['latest', 'meta-data', 'instance-id'], fakes.ID_OS_INSTANCE_1, fakes.IP_NETWORK_INTERFACE_2, create_region) self.assertEqual(fakes.ID_EC2_INSTANCE_1, retval) retval = api.get_metadata_item( fake_context, ['latest', 'meta-data', 'instance-id'], fakes.ID_OS_INSTANCE_2, '10.200.1.15', create_region) self.assertEqual(fakes.ID_EC2_INSTANCE_2, retval)
def test_s3_create_image_locations(self): self.configure(image_decryption_dir=None) glance = self.mock_glance() _handle, tempf = tempfile.mkstemp() fake_context = base.create_context() @mock.patch('ec2api.api.image._s3_untarzip_image') @mock.patch('ec2api.api.image._s3_decrypt_image') @mock.patch('ec2api.api.image._s3_download_file') @mock.patch('ec2api.api.image._s3_conn') 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_get_os_public_network(self, neutron): neutron = neutron.return_value context = base.create_context() conf = self.useFixture(config_fixture.Config()) conf.config(external_network='fake_public_network') neutron.list_networks.return_value = {'networks': ['network_object']} net = ec2utils.get_os_public_network(context) self.assertEqual('network_object', net) neutron.list_networks.assert_called_once_with( **{ 'router:external': True, 'name': 'fake_public_network' }) neutron.list_networks.return_value = {'networks': []} with fixtures.FakeLogger() as log: self.assertRaises(exception.Unsupported, ec2utils.get_os_public_network, context) self.assertNotEqual(0, len(log.output)) self.assertIn('fake_public_network', log.output) neutron.list_networks.return_value = {'networks': ['obj1', 'obj2']} with fixtures.FakeLogger() as log: self.assertRaises(exception.Unsupported, ec2utils.get_os_public_network, context) self.assertNotEqual(0, len(log.output)) self.assertIn('fake_public_network', log.output) conf.config(external_network=None) with fixtures.FakeLogger() as log: self.assertRaises(exception.Unsupported, ec2utils.get_os_public_network, context) self.assertNotEqual(0, len(log.output)) self.assertNotIn('None', log.output) neutron.list_networks.return_value = {'networks': []} with fixtures.FakeLogger() as log: self.assertRaises(exception.Unsupported, ec2utils.get_os_public_network, context) self.assertNotEqual(0, len(log.output)) self.assertNotIn('None', log.output)
def test_delete_subnet_vpnservice(self, delete_vpnservice): context = base.create_context() cleaner = common.OnCrashCleaner() vpn_gateway_api._delete_subnet_vpnservice( context, self.neutron, cleaner, copy.deepcopy(fakes.DB_SUBNET_1)) self.db_api.update_item.assert_called_once_with( mock.ANY, self.DB_SUBNET_1_NO_VPN) try: with common.OnCrashCleaner() as cleaner: vpn_gateway_api._delete_subnet_vpnservice( context, self.neutron, cleaner, copy.deepcopy(fakes.DB_SUBNET_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_SUBNET_1) self.assertFalse(self.neutron.create_vpnservice.called)
def test_update_vpn_routes(self, reset_vpn_connections): context = base.create_context() cleaner = common.OnCrashCleaner() self.set_mock_db_items() vpn_connection_api._update_vpn_routes(context, self.neutron, cleaner, fakes.DB_ROUTE_TABLE_1, [fakes.DB_SUBNET_1]) self.assertFalse(reset_vpn_connections.called) self.set_mock_db_items(fakes.DB_VPN_GATEWAY_1) vpn_connection_api._update_vpn_routes(context, self.neutron, cleaner, fakes.DB_ROUTE_TABLE_1, [fakes.DB_SUBNET_1]) reset_vpn_connections.assert_called_once_with( context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1, route_tables=[fakes.DB_ROUTE_TABLE_1], subnets=[fakes.DB_SUBNET_1])
def test_get_os_public_network(self, neutron): neutron = neutron.return_value context = base.create_context() conf = self.useFixture(config_fixture.Config()) conf.config(external_network='fake_public_network') neutron.list_networks.return_value = {'networks': ['network_object']} net = ec2utils.get_os_public_network(context) self.assertEqual('network_object', net) neutron.list_networks.assert_called_once_with( **{'router:external': True, 'name': 'fake_public_network'}) neutron.list_networks.return_value = {'networks': []} with fixtures.FakeLogger() as log: self.assertRaises(exception.Unsupported, ec2utils.get_os_public_network, context) self.assertNotEqual(0, len(log.output)) self.assertIn('fake_public_network', log.output) neutron.list_networks.return_value = {'networks': ['obj1', 'obj2']} with fixtures.FakeLogger() as log: self.assertRaises(exception.Unsupported, ec2utils.get_os_public_network, context) self.assertNotEqual(0, len(log.output)) self.assertIn('fake_public_network', log.output) conf.config(external_network=None) with fixtures.FakeLogger() as log: self.assertRaises(exception.Unsupported, ec2utils.get_os_public_network, context) self.assertNotEqual(0, len(log.output)) self.assertNotIn('None', log.output) neutron.list_networks.return_value = {'networks': []} with fixtures.FakeLogger() as log: self.assertRaises(exception.Unsupported, ec2utils.get_os_public_network, context) self.assertNotEqual(0, len(log.output)) self.assertNotIn('None', log.output)
def test_delete_subnet_vpnservice(self, delete_vpnservice): context = base.create_context() cleaner = common.OnCrashCleaner() vpn_gateway_api._delete_subnet_vpnservice( context, self.neutron, cleaner, copy.deepcopy(fakes.DB_SUBNET_1)) self.db_api.update_item.assert_called_once_with( mock.ANY, self.DB_SUBNET_1_NO_VPN) try: with common.OnCrashCleaner() as cleaner: vpn_gateway_api._delete_subnet_vpnservice( context, self.neutron, cleaner, copy.deepcopy(fakes.DB_SUBNET_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_SUBNET_1) self.assertFalse(self.neutron.create_vpnservice.called)
def test_s3_create_bdm(self, spawn_n): glance = self.mock_glance() metadata = {'properties': { 'image_location': 'fake_bucket/fake_manifest', 'root_device_name': '/dev/sda1', 'block_device_mapping': [ {'device_name': '/dev/sda1', 'snapshot_id': fakes.ID_OS_SNAPSHOT_1, 'delete_on_termination': True}, {'device_name': '/dev/sda2', 'virtual_name': 'ephemeral0'}, {'device_name': '/dev/sdb0', 'no_device': True}]}} fake_context = base.create_context() with mock.patch('ec2api.api.image._s3_conn') as s3_conn: (s3_conn.return_value. get_bucket.return_value. get_key.return_value. get_contents_as_string.return_value) = FILE_MANIFEST_XML image_api._s3_create(fake_context, metadata) glance.images.create.assert_called_once_with( disk_format='ami', container_format='ami', is_public=False, properties={'architecture': 'x86_64', 'image_state': 'pending', 'root_device_name': '/dev/sda1', 'block_device_mapping': [ {'device_name': '/dev/sda1', 'snapshot_id': fakes.ID_OS_SNAPSHOT_1, 'delete_on_termination': True}, {'device_name': '/dev/sda2', 'virtual_name': 'ephemeral0'}, {'device_name': '/dev/sdb0', 'no_device': True}], 'image_location': 'fake_bucket/fake_manifest'})
def test_stop_gateway_vpn_connections(self, stop_vpn_connection): context = base.create_context() cleaner = common.OnCrashCleaner() vpn_connection_3 = tools.update_dict(fakes.DB_VPN_CONNECTION_1, { 'id': fakes.random_ec2_id('vpn'), 'os_ipsec_site_connections': {} }) self.set_mock_db_items(fakes.DB_VPN_CONNECTION_1, vpn_connection_3, fakes.DB_VPN_CONNECTION_2) vpn_connection_api._stop_gateway_vpn_connections( context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1) self.assertEqual(2, stop_vpn_connection.call_count) stop_vpn_connection.assert_any_call(self.neutron, fakes.DB_VPN_CONNECTION_1) stop_vpn_connection.assert_any_call(self.neutron, vpn_connection_3) self.assertEqual(2, self.db_api.update_item.call_count) self.db_api.update_item.assert_any_call( mock.ANY, tools.update_dict(fakes.DB_VPN_CONNECTION_1, {'os_ipsec_site_connections': {}})) self.db_api.update_item.assert_any_call(mock.ANY, vpn_connection_3) self.db_api.reset_mock() self.neutron.reset_mock() stop_vpn_connection.reset_mock() self.set_mock_db_items(fakes.DB_VPN_CONNECTION_1) try: with common.OnCrashCleaner() as cleaner: vpn_connection_api._stop_gateway_vpn_connections( context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_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)
def test_start_vpn_in_subnet(self, create_subnet_vpnservice, reset_vpn_connection): context = base.create_context() cleaner = common.OnCrashCleaner() mock_manager = mock.Mock() mock_manager.attach_mock(create_subnet_vpnservice, 'create_subnet_vpnservice') mock_manager.attach_mock(reset_vpn_connection, 'reset_vpn_connection') self.set_mock_db_items(fakes.DB_VPN_GATEWAY_1, fakes.DB_VPN_GATEWAY_2) vpn_gateway_api._start_vpn_in_subnet(context, self.neutron, cleaner, copy.deepcopy(fakes.DB_SUBNET_1), fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1) mock_manager.assert_has_calls([ mock.call.create_subnet_vpnservice(context, self.neutron, cleaner, fakes.DB_SUBNET_1, fakes.DB_VPC_1), mock.call.reset_vpn_connection( context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1, subnets=[fakes.DB_SUBNET_1], route_tables=[fakes.DB_ROUTE_TABLE_1]) ]) create_subnet_vpnservice.reset_mock() reset_vpn_connection.reset_mock() self.add_mock_db_items(self.DB_VPN_GATEWAY_1_DETACHED) vpn_gateway_api._start_vpn_in_subnet(context, self.neutron, cleaner, copy.deepcopy(fakes.DB_SUBNET_1), fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1) self.assertFalse(create_subnet_vpnservice.called) self.assertFalse(reset_vpn_connection.called)
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 setUp(self): super(DBItemsAutoCreationTestCase, self).setUp() self.mock_all_os() self.context = base.create_context()
def test_reset_vpn_connections(self, get_route_table_vpn_cidrs, set_subnet_vpn, delete_subnet_vpn): context = base.create_context() cleaner = common.OnCrashCleaner() vpn_gateway_3 = { 'id': fakes.random_ec2_id('vpn'), 'os_id': None, 'vpc_id': None } vpn_connection_api._reset_vpn_connections(context, self.neutron, cleaner, vpn_gateway_3) self.assertEqual(0, len(self.db_api.mock_calls)) self.assertFalse(get_route_table_vpn_cidrs.called) self.assertFalse(set_subnet_vpn.called) self.assertFalse(delete_subnet_vpn.called) customer_gateway_3 = {'id': fakes.random_ec2_id('cgw')} subnet_3 = { 'id': fakes.random_ec2_id('subnet'), 'vpc_id': fakes.ID_EC2_VPC_2 } vpn_connection_3 = { 'id': fakes.random_ec2_id('vpn'), 'vpn_gateway_id': fakes.ID_EC2_VPN_GATEWAY_1, 'customer_gateway_id': customer_gateway_3['id'], 'cidrs': [] } self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_VPC_2, fakes.DB_CUSTOMER_GATEWAY_1, fakes.DB_CUSTOMER_GATEWAY_2, customer_gateway_3, fakes.DB_SUBNET_1, fakes.DB_SUBNET_2, subnet_3, fakes.DB_ROUTE_TABLE_1, fakes.DB_ROUTE_TABLE_2, fakes.DB_ROUTE_TABLE_3, fakes.DB_VPN_CONNECTION_1, fakes.DB_VPN_CONNECTION_2, vpn_connection_3) # common case vpn_connection_api._reset_vpn_connections(context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1) self.assertEqual(2, set_subnet_vpn.call_count) set_subnet_vpn.assert_any_call(context, self.neutron, cleaner, fakes.DB_SUBNET_2, fakes.DB_VPN_CONNECTION_1, fakes.DB_CUSTOMER_GATEWAY_1, [fakes.CIDR_VPN_1_STATIC]) set_subnet_vpn.assert_any_call(context, self.neutron, cleaner, fakes.DB_SUBNET_2, vpn_connection_3, customer_gateway_3, [fakes.CIDR_VPN_1_STATIC]) self.assertEqual(2, delete_subnet_vpn.call_count) delete_subnet_vpn.assert_any_call(context, self.neutron, cleaner, fakes.DB_SUBNET_1, fakes.DB_VPN_CONNECTION_1) delete_subnet_vpn.assert_any_call(context, self.neutron, cleaner, fakes.DB_SUBNET_1, vpn_connection_3) self.assertEqual(2, get_route_table_vpn_cidrs.call_count) get_route_table_vpn_cidrs.assert_any_call( fakes.DB_ROUTE_TABLE_1, fakes.DB_VPN_GATEWAY_1, [fakes.DB_VPN_CONNECTION_1, vpn_connection_3]) get_route_table_vpn_cidrs.assert_any_call( fakes.DB_ROUTE_TABLE_3, fakes.DB_VPN_GATEWAY_1, [fakes.DB_VPN_CONNECTION_1, vpn_connection_3]) # reset for the vpn connection set_subnet_vpn.reset_mock() delete_subnet_vpn.reset_mock() self.db_api.reset_mock() get_route_table_vpn_cidrs.reset_mock() vpn_connection_api._reset_vpn_connections( context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1, vpn_connections=[fakes.DB_VPN_CONNECTION_1]) self.assertEqual(1, set_subnet_vpn.call_count) self.assertEqual(1, delete_subnet_vpn.call_count) self.assertNotIn(mock.call(mock.ANY, 'vpn'), self.db_api.get_items.mock_calls) # reset for the subnet list set_subnet_vpn.reset_mock() delete_subnet_vpn.reset_mock() self.db_api.reset_mock() get_route_table_vpn_cidrs.reset_mock() vpn_connection_api._reset_vpn_connections(context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1, subnets=[fakes.DB_SUBNET_1]) self.assertFalse(set_subnet_vpn.called) self.assertEqual(2, delete_subnet_vpn.call_count) self.assertNotIn(mock.call(mock.ANY, 'subnets'), self.db_api.get_items.mock_calls) # reset for the subnet list and the route table set_subnet_vpn.reset_mock() delete_subnet_vpn.reset_mock() self.db_api.reset_mock() get_route_table_vpn_cidrs.reset_mock() vpn_connection_api._reset_vpn_connections( context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1, subnets=[fakes.DB_SUBNET_2], route_tables=[fakes.DB_ROUTE_TABLE_3]) self.assertEqual(2, set_subnet_vpn.call_count) self.assertFalse(delete_subnet_vpn.called) self.assertNotIn(mock.call(mock.ANY, 'subnets'), self.db_api.get_items.mock_calls) self.assertNotIn(mock.call(mock.ANY, 'rtb'), self.db_api.get_items.mock_calls)
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 setUp(self): super(VpcPrivateTestCase, self).setUp() self.context = base.create_context() self.nova, self.nova_admin = self.mock_nova() self.neutron = self.mock_neutron() self.db_api = self.mock_db()
def test_reset_vpn_connections(self, get_route_table_vpn_cidrs, set_subnet_vpn, delete_subnet_vpn): context = base.create_context() cleaner = common.OnCrashCleaner() vpn_gateway_3 = {'id': fakes.random_ec2_id('vpn'), 'os_id': None, 'vpc_id': None} vpn_connection_api._reset_vpn_connections( context, self.neutron, cleaner, vpn_gateway_3) self.assertEqual(0, len(self.db_api.mock_calls)) self.assertFalse(get_route_table_vpn_cidrs.called) self.assertFalse(set_subnet_vpn.called) self.assertFalse(delete_subnet_vpn.called) customer_gateway_3 = {'id': fakes.random_ec2_id('cgw')} subnet_3 = {'id': fakes.random_ec2_id('subnet'), 'vpc_id': fakes.ID_EC2_VPC_2} vpn_connection_3 = {'id': fakes.random_ec2_id('vpn'), 'vpn_gateway_id': fakes.ID_EC2_VPN_GATEWAY_1, 'customer_gateway_id': customer_gateway_3['id'], 'cidrs': []} self.set_mock_db_items( fakes.DB_VPC_1, fakes.DB_VPC_2, fakes.DB_CUSTOMER_GATEWAY_1, fakes.DB_CUSTOMER_GATEWAY_2, customer_gateway_3, fakes.DB_SUBNET_1, fakes.DB_SUBNET_2, subnet_3, fakes.DB_ROUTE_TABLE_1, fakes.DB_ROUTE_TABLE_2, fakes.DB_ROUTE_TABLE_3, fakes.DB_VPN_CONNECTION_1, fakes.DB_VPN_CONNECTION_2, vpn_connection_3) # common case vpn_connection_api._reset_vpn_connections( context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1) self.assertEqual(2, set_subnet_vpn.call_count) set_subnet_vpn.assert_any_call( context, self.neutron, cleaner, fakes.DB_SUBNET_2, fakes.DB_VPN_CONNECTION_1, fakes.DB_CUSTOMER_GATEWAY_1, [fakes.CIDR_VPN_1_STATIC]) set_subnet_vpn.assert_any_call( context, self.neutron, cleaner, fakes.DB_SUBNET_2, vpn_connection_3, customer_gateway_3, [fakes.CIDR_VPN_1_STATIC]) self.assertEqual(2, delete_subnet_vpn.call_count) delete_subnet_vpn.assert_any_call( context, self.neutron, cleaner, fakes.DB_SUBNET_1, fakes.DB_VPN_CONNECTION_1) delete_subnet_vpn.assert_any_call( context, self.neutron, cleaner, fakes.DB_SUBNET_1, vpn_connection_3) self.assertEqual(2, get_route_table_vpn_cidrs.call_count) get_route_table_vpn_cidrs.assert_any_call( fakes.DB_ROUTE_TABLE_1, fakes.DB_VPN_GATEWAY_1, [fakes.DB_VPN_CONNECTION_1, vpn_connection_3]) get_route_table_vpn_cidrs.assert_any_call( fakes.DB_ROUTE_TABLE_3, fakes.DB_VPN_GATEWAY_1, [fakes.DB_VPN_CONNECTION_1, vpn_connection_3]) # reset for the vpn connection set_subnet_vpn.reset_mock() delete_subnet_vpn.reset_mock() self.db_api.reset_mock() get_route_table_vpn_cidrs.reset_mock() vpn_connection_api._reset_vpn_connections( context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1, vpn_connections=[fakes.DB_VPN_CONNECTION_1]) self.assertEqual(1, set_subnet_vpn.call_count) self.assertEqual(1, delete_subnet_vpn.call_count) self.assertNotIn(mock.call(mock.ANY, 'vpn'), self.db_api.get_items.mock_calls) # reset for the subnet list set_subnet_vpn.reset_mock() delete_subnet_vpn.reset_mock() self.db_api.reset_mock() get_route_table_vpn_cidrs.reset_mock() vpn_connection_api._reset_vpn_connections( context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1, subnets=[fakes.DB_SUBNET_1]) self.assertFalse(set_subnet_vpn.called) self.assertEqual(2, delete_subnet_vpn.call_count) self.assertNotIn(mock.call(mock.ANY, 'subnets'), self.db_api.get_items.mock_calls) # reset for the subnet list and the route table set_subnet_vpn.reset_mock() delete_subnet_vpn.reset_mock() self.db_api.reset_mock() get_route_table_vpn_cidrs.reset_mock() vpn_connection_api._reset_vpn_connections( context, self.neutron, cleaner, fakes.DB_VPN_GATEWAY_1, subnets=[fakes.DB_SUBNET_2], route_tables=[fakes.DB_ROUTE_TABLE_3]) self.assertEqual(2, set_subnet_vpn.call_count) self.assertFalse(delete_subnet_vpn.called) self.assertNotIn(mock.call(mock.ANY, 'subnets'), self.db_api.get_items.mock_calls) self.assertNotIn(mock.call(mock.ANY, 'rtb'), self.db_api.get_items.mock_calls)
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 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_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_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_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}))