def test_get_db_items(self, db_api): items = [{ 'id': fakes.random_ec2_id('fake'), 'fake_key': 'fake_value' }, { 'id': fakes.random_ec2_id('fake'), 'fake_key': 'fake_value' }] db_api.get_items.return_value = items db_api.get_items_by_ids.return_value = items def check_with_no_filter(empty_filter): res = ec2utils.get_db_items('fake_context', 'fake', empty_filter) self.assertThat(res, matchers.ListMatches(items)) db_api.get_items.assert_called_once_with('fake_context', 'fake') db_api.reset_mock() check_with_no_filter(None) check_with_no_filter([]) def check_with_filter(item_ids): res = ec2utils.get_db_items('fake_context', 'fake', item_ids) self.assertThat(res, matchers.ListMatches(items)) db_api.get_items_by_ids.assert_called_once_with( 'fake_context', set(item_ids)) db_api.reset_mock() item_ids = [i['id'] for i in items] check_with_filter(item_ids) check_with_filter(item_ids * 2) def check_not_found(kind, ex_class): items = [{ 'id': fakes.random_ec2_id(kind), 'fake_key': 'fake_value' } for _ in range(2)] item_ids = [i['id'] for i in items] item_ids.append(fakes.random_ec2_id(kind)) db_api.get_items_by_ids.return_value = items self.assertRaises(ex_class, ec2utils.get_db_items, 'fake_context', kind, item_ids) db_api.reset_mock() check_not_found('vpc', exception.InvalidVpcIDNotFound) check_not_found('igw', exception.InvalidInternetGatewayIDNotFound) check_not_found('subnet', exception.InvalidSubnetIDNotFound) check_not_found('eni', exception.InvalidNetworkInterfaceIDNotFound) check_not_found('dopt', exception.InvalidDhcpOptionsIDNotFound) check_not_found('eipalloc', exception.InvalidAllocationIDNotFound) check_not_found('sg', exception.InvalidGroupNotFound) check_not_found('rtb', exception.InvalidRouteTableIDNotFound) check_not_found('i', exception.InvalidInstanceIDNotFound) check_not_found('vol', exception.InvalidVolumeNotFound) check_not_found('snap', exception.InvalidSnapshotNotFound) check_not_found('ami', exception.InvalidAMIIDNotFound) check_not_found('aki', exception.InvalidAMIIDNotFound) check_not_found('ari', exception.InvalidAMIIDNotFound) check_not_found('vgw', exception.InvalidVpnGatewayIDNotFound) check_not_found('cgw', exception.InvalidCustomerGatewayIDNotFound) check_not_found('vpn', exception.InvalidVpnConnectionIDNotFound)
def test_get_items_by_ids(self): self._setup_items() fake_kind_items = db_api.get_items(self.context, 'fake') fake1_kind_items = db_api.get_items(self.context, 'fake1') item_id = fake_kind_items[0]['id'] other_item_id = db_api.get_items(self.other_context, 'fake')[0]['id'] items = db_api.get_items_by_ids(self.context, []) self.assertEqual(0, len(items)) items = db_api.get_items_by_ids(self.context, set([])) self.assertEqual(0, len(items)) items = db_api.get_items_by_ids(self.context, [i['id'] for i in fake_kind_items]) self.assertEqual(2, len(items)) items = db_api.get_items_by_ids( self.context, (fake_kind_items[0]['id'], fake1_kind_items[0]['id'])) self.assertEqual(2, len(items)) items = db_api.get_items_by_ids(self.context, (item_id, )) self.assertEqual(1, len(items)) self.assertEqual(item_id, items[0]['id']) items = db_api.get_items_by_ids(self.context, (other_item_id, )) self.assertEqual(0, len(items)) items = db_api.get_items_by_ids(self.context, (item_id, other_item_id)) self.assertEqual(1, len(items)) items = db_api.get_items_by_ids( self.context, (fakes.random_ec2_id('fake')), ) self.assertEqual(0, len(items)) items = db_api.get_items_by_ids(self.context, (item_id, fakes.random_ec2_id('fake'))) self.assertEqual(1, len(items))
def test_get_items_by_ids(self): self._setup_items() fake_kind_items = db_api.get_items(self.context, 'fake') fake1_kind_items = db_api.get_items(self.context, 'fake1') item_id = fake_kind_items[0]['id'] other_item_id = db_api.get_items(self.other_context, 'fake')[0]['id'] items = db_api.get_items_by_ids(self.context, []) self.assertEqual(0, len(items)) items = db_api.get_items_by_ids(self.context, set([])) self.assertEqual(0, len(items)) items = db_api.get_items_by_ids(self.context, [i['id'] for i in fake_kind_items]) self.assertEqual(2, len(items)) items = db_api.get_items_by_ids( self.context, (fake_kind_items[0]['id'], fake1_kind_items[0]['id'])) self.assertEqual(2, len(items)) items = db_api.get_items_by_ids(self.context, (item_id,)) self.assertEqual(1, len(items)) self.assertEqual(item_id, items[0]['id']) items = db_api.get_items_by_ids(self.context, (other_item_id,)) self.assertEqual(0, len(items)) items = db_api.get_items_by_ids(self.context, (item_id, other_item_id)) self.assertEqual(1, len(items)) items = db_api.get_items_by_ids(self.context, (fakes.random_ec2_id('fake')),) self.assertEqual(0, len(items)) items = db_api.get_items_by_ids(self.context, (item_id, fakes.random_ec2_id('fake'))) self.assertEqual(1, len(items))
def test_get_tags(self): item1_id = fakes.random_ec2_id('fake') item2_id = fakes.random_ec2_id('fake') item3_id = fakes.random_ec2_id('fake1') tag1 = {'item_id': item1_id, 'key': 'key1', 'value': 'val1'} tag2 = {'item_id': item2_id, 'key': 'key2', 'value': 'val2'} tag3 = {'item_id': item3_id, 'key': 'key3', 'value': 'val3'} db_api.add_tags(self.context, [tag1, tag2, tag3]) self.assertThat( db_api.get_tags(self.context), matchers.ListMatches([tag1, tag2, tag3], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', )), matchers.ListMatches([tag1, tag2], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', ), [item1_id, item2_id]), matchers.ListMatches([tag1, tag2], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', ), (item1_id, )), matchers.ListMatches([tag1], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', ), (item3_id, )), matchers.ListMatches([])) self.assertThat( db_api.get_tags(self.context, item_ids=(item1_id, item3_id)), matchers.ListMatches([tag1, tag3], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', 'fake1'), (item2_id, item3_id)), matchers.ListMatches([tag2, tag3], orderless_lists=True))
def check_not_found(kind, ex_class): items = [{'id': fakes.random_ec2_id(kind), 'fake_key': 'fake_value'} for _ in range(2)] item_ids = [i['id'] for i in items] item_ids.append(fakes.random_ec2_id(kind)) db_api.get_items_by_ids.return_value = items self.assertRaises(ex_class, ec2utils.get_db_items, 'fake_context', kind, item_ids) db_api.reset_mock()
def test_get_db_items(self, db_api): items = [{'id': fakes.random_ec2_id('fake'), 'fake_key': 'fake_value'}, {'id': fakes.random_ec2_id('fake'), 'fake_key': 'fake_value'}] db_api.get_items.return_value = items db_api.get_items_by_ids.return_value = items def check_with_no_filter(empty_filter): res = ec2utils.get_db_items('fake_context', 'fake', empty_filter) self.assertThat(res, matchers.ListMatches(items)) db_api.get_items.assert_called_once_with('fake_context', 'fake') db_api.reset_mock() check_with_no_filter(None) check_with_no_filter([]) def check_with_filter(item_ids): res = ec2utils.get_db_items('fake_context', 'fake', item_ids) self.assertThat(res, matchers.ListMatches(items)) db_api.get_items_by_ids.assert_called_once_with( 'fake_context', set(item_ids)) db_api.reset_mock() item_ids = [i['id'] for i in items] check_with_filter(item_ids) check_with_filter(item_ids * 2) def check_not_found(kind, ex_class): items = [{'id': fakes.random_ec2_id(kind), 'fake_key': 'fake_value'} for _ in range(2)] item_ids = [i['id'] for i in items] item_ids.append(fakes.random_ec2_id(kind)) db_api.get_items_by_ids.return_value = items self.assertRaises(ex_class, ec2utils.get_db_items, 'fake_context', kind, item_ids) db_api.reset_mock() check_not_found('vpc', exception.InvalidVpcIDNotFound) check_not_found('igw', exception.InvalidInternetGatewayIDNotFound) check_not_found('subnet', exception.InvalidSubnetIDNotFound) check_not_found('eni', exception.InvalidNetworkInterfaceIDNotFound) check_not_found('dopt', exception.InvalidDhcpOptionsIDNotFound) check_not_found('eipalloc', exception.InvalidAllocationIDNotFound) check_not_found('sg', exception.InvalidGroupNotFound) check_not_found('rtb', exception.InvalidRouteTableIDNotFound) check_not_found('i', exception.InvalidInstanceIDNotFound) check_not_found('vol', exception.InvalidVolumeNotFound) check_not_found('snap', exception.InvalidSnapshotNotFound) check_not_found('ami', exception.InvalidAMIIDNotFound) check_not_found('aki', exception.InvalidAMIIDNotFound) check_not_found('ari', exception.InvalidAMIIDNotFound) check_not_found('vgw', exception.InvalidVpnGatewayIDNotFound) check_not_found('cgw', exception.InvalidCustomerGatewayIDNotFound) check_not_found('vpn', exception.InvalidVpnConnectionIDNotFound)
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_deregister_image_invalid_parameters(self): self._setup_model() self.assert_execution_error('InvalidAMIID.NotFound', 'DeregisterImage', {'ImageId': fakes.random_ec2_id('ami')}) # deregister asynchronously creating image image_id = fakes.random_ec2_id('ami') self.add_mock_db_items({'id': image_id, 'os_id': None}) self.assert_execution_error('IncorrectState', 'DeregisterImage', {'ImageId': image_id})
def test_create_tags_invalid_parameters(self): # NOTE(ft): check tag validity checks self.assert_execution_error('InvalidParameterValue', 'CreateTags', { 'ResourceId.1': fakes.ID_EC2_VPC_1, 'Tag.1.Value': '' }) self.assert_execution_error('InvalidParameterValue', 'CreateTags', { 'ResourceId.1': fakes.ID_EC2_VPC_1, 'Tag.1.Key': '' }) self.assert_execution_error('InvalidParameterValue', 'CreateTags', { 'ResourceId.1': fakes.ID_EC2_VPC_1, 'Tag.1.Key': 'a' * 128 }) self.assert_execution_error( 'InvalidParameterValue', 'CreateTags', { 'ResourceId.1': fakes.ID_EC2_VPC_1, 'Tag.1.Key': 'fake-key', 'Tag.1.Value': 'a' * 256 }) # NOTE(ft): check resource type check self.assert_execution_error( 'InvalidID', 'CreateTags', { 'ResourceId.1': fakes.random_ec2_id('fake'), 'Tag.1.Key': 'fake-key', 'Tag.1.Value': 'fake-value' }) # NOTE(ft): check resource existence check self.db_api.get_item_by_id.return_value = None for r_id in tag_api.RESOURCE_TYPES: if r_id in ('ami', 'ari', 'aki'): continue exc_class = ec2utils.NOT_FOUND_EXCEPTION_MAP[r_id] try: error_code = exc_class.ec2_code except AttributeError: error_code = exc_class.__name__ self.assert_execution_error( error_code, 'CreateTags', { 'ResourceId.1': fakes.random_ec2_id(r_id), 'Tag.1.Key': 'fake-key', 'Tag.1.Value': 'fake-value' })
def check_not_found(kind, ex_class): ec2_id = fakes.random_ec2_id(kind) self.assertRaises(ex_class, ec2utils.get_db_item, 'fake_context', ec2_id) db_api.get_item_by_id.assert_called_once_with( 'fake_context', ec2_id) db_api.reset_mock()
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_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')) # check case of creating image db_api.get_items_ids.return_value = [(fakes.ID_EC2_IMAGE_1, None)] self.assertIsNone( ec2utils.get_os_image(fake_context, fakes.ID_EC2_IMAGE_1))
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_modify_image_attributes_invalid_parameters(self): image_id = fakes.random_ec2_id('ami') self.set_mock_db_items({'id': image_id, 'os_id': None}) self.assert_execution_error('IncorrectState', 'ModifyImageAttribute', { 'ImageId': image_id, 'Attribute': 'kernel' })
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_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_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_delete_tags(self): item1_id = fakes.random_ec2_id('fake') item2_id = fakes.random_ec2_id('fake') item3_id = fakes.random_ec2_id('fake1') tag1_1 = {'item_id': item1_id, 'key': 'key1', 'value': 'val_a'} tag1_2 = {'item_id': item1_id, 'key': 'key2', 'value': 'val_b'} tag2_1 = {'item_id': item2_id, 'key': 'key1', 'value': 'val_c'} tag2_2 = {'item_id': item2_id, 'key': 'key2', 'value': 'val_a'} tag3_1 = {'item_id': item3_id, 'key': 'key1', 'value': 'val_b'} tag3_2 = {'item_id': item3_id, 'key': 'key2', 'value': 'val_d'} db_api.add_tags(self.context, [tag1_1, tag2_1, tag3_1, tag1_2, tag2_2, tag3_2]) def do_check(*tag_list): self.assertThat(db_api.get_tags(self.context), matchers.ListMatches(tag_list, orderless_lists=True)) db_api.add_tags(self.context, [tag1_1, tag2_1, tag3_1, tag1_2, tag2_2, tag3_2]) db_api.delete_tags(self.context, []) do_check(tag1_1, tag1_2, tag2_1, tag2_2, tag3_1, tag3_2) db_api.delete_tags(self.context, [item1_id]) do_check(tag2_1, tag2_2, tag3_1, tag3_2) db_api.delete_tags(self.context, [item1_id, item3_id]) do_check(tag2_1, tag2_2) db_api.delete_tags(self.context, [item1_id, item2_id, item3_id], [{'key': 'key1'}, {'value': 'val_d'}, {'key': 'key2', 'value': 'val_b'}]) do_check(tag2_2)
def test_modify_image_attributes_invalid_parameters(self): image_id = fakes.random_ec2_id('ami') self.set_mock_db_items({'id': image_id, 'os_id': None}) self.assert_execution_error('IncorrectState', 'ModifyImageAttribute', {'ImageId': image_id, 'Attribute': 'kernel'})
def check_not_found(kind, ex_class): ec2_id = fakes.random_ec2_id(kind) self.assertRaises(ex_class, ec2utils.get_db_item, 'fake_context', ec2_id) db_api.get_item_by_id.assert_called_once_with('fake_context', ec2_id) db_api.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_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_delete_tags_isolation(self): item_id = fakes.random_ec2_id('fake') tag1 = {'item_id': item_id, 'key': 'key', 'value': 'val1'} db_api.add_tags(self.context, [tag1]) tag2 = {'item_id': item_id, 'key': 'key', 'value': 'val2'} db_api.add_tags(self.other_context, [tag2]) db_api.delete_tags(self.context, item_id) self.assertThat(db_api.get_tags(self.other_context), matchers.ListMatches([tag2]))
def test_describe_images_invalid_parameters(self): self._setup_model() self.assert_execution_error('InvalidAMIID.NotFound', 'DescribeImages', {'ImageId.1': fakes.random_ec2_id('ami')}) self.glance.images.list.side_effect = lambda: [] self.assert_execution_error('InvalidAMIID.NotFound', 'DescribeImages', {'ImageId.1': fakes.ID_EC2_IMAGE_1})
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_describe_volumes_invalid_parameters(self): self.cinder.volumes.list.return_value = [fakes.OSVolume(fakes.OS_VOLUME_1), fakes.OSVolume(fakes.OS_VOLUME_2)] self.nova.servers.list.return_value = [fakes.OSInstance_full(fakes.OS_INSTANCE_2)] self.assert_execution_error( "InvalidVolume.NotFound", "DescribeVolumes", {"VolumeId.1": fakes.random_ec2_id("vol")} ) self.cinder.volumes.list.side_effect = lambda: [] self.assert_execution_error("InvalidVolume.NotFound", "DescribeVolumes", {"VolumeId.1": fakes.ID_EC2_VOLUME_1})
def test_add_tags(self): item1_id = fakes.random_ec2_id('fake') item2_id = fakes.random_ec2_id('fake') item3_id = fakes.random_ec2_id('fake') tag1_01 = {'item_id': item1_id, 'key': 'key1', 'value': None} tag1_1 = {'item_id': item1_id, 'key': 'key1', 'value': 'val'} tag1_2 = {'item_id': item1_id, 'key': 'key2', 'value': 'val'} tag1_3 = {'item_id': item1_id, 'key': 'key3', 'value': 'val'} tag2_1 = {'item_id': item2_id, 'key': 'key1', 'value': None} tag2_2 = {'item_id': item2_id, 'key': 'key2', 'value': 'val'} tag3_1 = {'item_id': item3_id, 'key': 'key1', 'value': 'val'} tag3_3 = {'item_id': item3_id, 'key': 'key3', 'value': 'val'} db_api.add_tags(self.context, [tag1_01, tag2_1, tag1_2, tag2_2]) db_api.add_tags(self.context, [tag1_1, tag3_1, tag1_3, tag3_3]) tags = db_api.get_tags(self.context) self.assertThat( tags, matchers.ListMatches( [tag1_1, tag1_2, tag1_3, tag2_1, tag2_2, tag3_1, tag3_3], orderless_lists=True))
def test_create_tags_invalid_parameters(self): # NOTE(ft): check tag validity checks self.assert_execution_error('InvalidParameterValue', 'CreateTags', {'ResourceId.1': fakes.ID_EC2_VPC_1, 'Tag.1.Value': ''}) self.assert_execution_error('InvalidParameterValue', 'CreateTags', {'ResourceId.1': fakes.ID_EC2_VPC_1, 'Tag.1.Key': ''}) self.assert_execution_error('InvalidParameterValue', 'CreateTags', {'ResourceId.1': fakes.ID_EC2_VPC_1, 'Tag.1.Key': 'a' * 128}) self.assert_execution_error('InvalidParameterValue', 'CreateTags', {'ResourceId.1': fakes.ID_EC2_VPC_1, 'Tag.1.Key': 'fake-key', 'Tag.1.Value': 'a' * 256}) # NOTE(ft): check resource type check self.assert_execution_error( 'InvalidID', 'CreateTags', {'ResourceId.1': fakes.random_ec2_id('fake'), 'Tag.1.Key': 'fake-key', 'Tag.1.Value': 'fake-value'}) # NOTE(ft): check resource existence check self.db_api.get_item_by_id.return_value = None for r_id in tag_api.RESOURCE_TYPES: if r_id in ('ami', 'ari', 'aki'): continue exc_class = ec2utils.NOT_FOUND_EXCEPTION_MAP[r_id] try: error_code = exc_class.ec2_code except AttributeError: error_code = exc_class.__name__ self.assert_execution_error( error_code, 'CreateTags', {'ResourceId.1': fakes.random_ec2_id(r_id), 'Tag.1.Key': 'fake-key', 'Tag.1.Value': 'fake-value'})
def test_create_tags(self): self.db_api.get_item_by_id.return_value = {'id': 'fake'} # NOTE(ft): check create several tags for several resources resp = self.execute('CreateTags', {'ResourceId.1': fakes.ID_EC2_VPC_1, 'ResourceId.2': fakes.ID_EC2_SUBNET_1, 'Tag.1.Key': 'private', 'Tag.1.Value': '', 'Tag.2.Key': 'admin', 'Tag.2.Value': 'John Smith'}) self.assertEqual({'return': True}, resp) self.assertEqual(1, self.db_api.add_tags.call_count) self.assertEqual(2, len(self.db_api.add_tags.call_args)) self.assertThat(self.db_api.add_tags.call_args[0][1], matchers.ListMatches( [{'item_id': fakes.ID_EC2_VPC_1, 'key': 'private', 'value': ''}, {'item_id': fakes.ID_EC2_SUBNET_1, 'key': 'private', 'value': ''}, {'item_id': fakes.ID_EC2_VPC_1, 'key': 'admin', 'value': 'John Smith'}, {'item_id': fakes.ID_EC2_SUBNET_1, 'key': 'admin', 'value': 'John Smith'}], orderless_lists=True)) # NOTE(ft): check a tag can be created for all valid resource types resource_ids = [fakes.random_ec2_id(r_t) for r_t in ['dopt', 'ami', 'aki', 'ari', 'cgw', 'i', 'igw', 'eni', 'rtb', 'snap', 'subnet', 'sg', 'vgw', 'vol', 'vpc', 'vpn']] self.assertEqual(len(resource_ids), len(tag_api.RESOURCE_TYPES)) params = {'ResourceId.%s' % num: r_id for num, r_id in enumerate(resource_ids)} params.update({'Tag.1.Key': 'tag', 'Tag.1.Value': 'value'}) resp = self.execute('CreateTags', params) self.assertEqual({'return': True}, resp) # NOTE(ft): check create a tag for non-existing images self.db_api.get_item_by_id.return_value = None resp = self.execute('CreateTags', {'ResourceId.1': fakes.ID_EC2_IMAGE_1, 'ResourceId.2': fakes.ID_EC2_IMAGE_AKI_1, 'ResourceId.3': fakes.ID_EC2_IMAGE_ARI_1, 'Tag.1.Key': 'Oracle RAC node', 'Tag.1.Value': ''}) self.assertEqual({'return': True}, resp)
def test_delete_tags(self): item1_id = fakes.random_ec2_id('fake') item2_id = fakes.random_ec2_id('fake') item3_id = fakes.random_ec2_id('fake1') tag1_1 = {'item_id': item1_id, 'key': 'key1', 'value': 'val_a'} tag1_2 = {'item_id': item1_id, 'key': 'key2', 'value': 'val_b'} tag2_1 = {'item_id': item2_id, 'key': 'key1', 'value': 'val_c'} tag2_2 = {'item_id': item2_id, 'key': 'key2', 'value': 'val_a'} tag3_1 = {'item_id': item3_id, 'key': 'key1', 'value': 'val_b'} tag3_2 = {'item_id': item3_id, 'key': 'key2', 'value': 'val_d'} db_api.add_tags(self.context, [tag1_1, tag2_1, tag3_1, tag1_2, tag2_2, tag3_2]) def do_check(*tag_list): self.assertThat( db_api.get_tags(self.context), matchers.ListMatches(tag_list, orderless_lists=True)) db_api.add_tags(self.context, [tag1_1, tag2_1, tag3_1, tag1_2, tag2_2, tag3_2]) db_api.delete_tags(self.context, []) do_check(tag1_1, tag1_2, tag2_1, tag2_2, tag3_1, tag3_2) db_api.delete_tags(self.context, [item1_id]) do_check(tag2_1, tag2_2, tag3_1, tag3_2) db_api.delete_tags(self.context, [item1_id, item3_id]) do_check(tag2_1, tag2_2) db_api.delete_tags(self.context, [item1_id, item2_id, item3_id], [{ 'key': 'key1' }, { 'value': 'val_d' }, { 'key': 'key2', 'value': 'val_b' }]) do_check(tag2_2)
def test_delete_item(self): item = db_api.add_item(self.context, 'fake', {}) db_api.delete_item(self.context, item['id']) item = db_api.get_item_by_id(self.context, item['id']) self.assertIsNone(item) # NOTE(ft): delete not existing item should pass quitely db_api.delete_item(self.context, fakes.random_ec2_id('fake')) item = db_api.add_item(self.context, 'fake', {}) db_api.delete_item(self.other_context, item['id']) item = db_api.get_item_by_id(self.context, item['id']) self.assertIsNotNone(item)
def test_get_item_by_id(self): self._setup_items() item_id = db_api.get_items(self.context, 'fake')[0]['id'] other_item_id = db_api.get_items(self.other_context, 'fake')[0]['id'] item = db_api.get_item_by_id(self.context, item_id) self.assertThat(item, matchers.DictMatches({'id': item_id, 'os_id': None, 'vpc_id': None})) item = db_api.get_item_by_id(self.context, other_item_id) self.assertIsNone(item) item = db_api.get_item_by_id(self.context, fakes.random_ec2_id('fake')) self.assertIsNone(item)
def test_get_tags(self): item1_id = fakes.random_ec2_id('fake') item2_id = fakes.random_ec2_id('fake') item3_id = fakes.random_ec2_id('fake1') tag1 = {'item_id': item1_id, 'key': 'key1', 'value': 'val1'} tag2 = {'item_id': item2_id, 'key': 'key2', 'value': 'val2'} tag3 = {'item_id': item3_id, 'key': 'key3', 'value': 'val3'} db_api.add_tags(self.context, [tag1, tag2, tag3]) self.assertThat(db_api.get_tags(self.context), matchers.ListMatches([tag1, tag2, tag3], orderless_lists=True)) self.assertThat(db_api.get_tags(self.context, ('fake',)), matchers.ListMatches([tag1, tag2], orderless_lists=True)) self.assertThat(db_api.get_tags(self.context, ('fake',), [item1_id, item2_id]), matchers.ListMatches([tag1, tag2], orderless_lists=True)) self.assertThat(db_api.get_tags(self.context, ('fake',), (item1_id,)), matchers.ListMatches([tag1], orderless_lists=True)) self.assertThat(db_api.get_tags(self.context, ('fake',), (item3_id,)), matchers.ListMatches([])) self.assertThat(db_api.get_tags(self.context, item_ids=(item1_id, item3_id)), matchers.ListMatches([tag1, tag3], orderless_lists=True)) self.assertThat(db_api.get_tags(self.context, ('fake', 'fake1'), (item2_id, item3_id)), matchers.ListMatches([tag2, tag3], orderless_lists=True))
def test_add_tags(self): item1_id = fakes.random_ec2_id('fake') item2_id = fakes.random_ec2_id('fake') item3_id = fakes.random_ec2_id('fake') tag1_01 = {'item_id': item1_id, 'key': 'key1', 'value': None} tag1_1 = {'item_id': item1_id, 'key': 'key1', 'value': 'val'} tag1_2 = {'item_id': item1_id, 'key': 'key2', 'value': 'val'} tag1_3 = {'item_id': item1_id, 'key': 'key3', 'value': 'val'} tag2_1 = {'item_id': item2_id, 'key': 'key1', 'value': None} tag2_2 = {'item_id': item2_id, 'key': 'key2', 'value': 'val'} tag3_1 = {'item_id': item3_id, 'key': 'key1', 'value': 'val'} tag3_3 = {'item_id': item3_id, 'key': 'key3', 'value': 'val'} db_api.add_tags(self.context, [tag1_01, tag2_1, tag1_2, tag2_2]) db_api.add_tags(self.context, [tag1_1, tag3_1, tag1_3, tag3_3]) tags = db_api.get_tags(self.context) self.assertThat(tags, matchers.ListMatches([tag1_1, tag1_2, tag1_3, tag2_1, tag2_2, tag3_1, tag3_3], orderless_lists=True))
def test_describe_snapshots_invalid_parameters(self): self.cinder.volume_snapshots.list.return_value = [ fakes.OSSnapshot(fakes.OS_SNAPSHOT_1), fakes.OSSnapshot(fakes.OS_SNAPSHOT_2)] self.assert_execution_error( 'InvalidSnapshot.NotFound', 'DescribeSnapshots', {'SnapshotId.1': fakes.random_ec2_id('snap')}) self.cinder.volume_snapshots.list.side_effect = lambda: [] self.assert_execution_error( 'InvalidSnapshot.NotFound', 'DescribeSnapshots', {'SnapshotId.1': fakes.ID_EC2_SNAPSHOT_1})
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_volumes_invalid_parameters(self): self.cinder.volumes.list.return_value = [ fakes.OSVolume(fakes.OS_VOLUME_1), fakes.OSVolume(fakes.OS_VOLUME_2) ] self.assert_execution_error('InvalidVolume.NotFound', 'DescribeVolumes', {'VolumeId.1': fakes.random_ec2_id('vol')}) self.cinder.volumes.list.side_effect = lambda: [] self.assert_execution_error('InvalidVolume.NotFound', 'DescribeVolumes', {'VolumeId.1': fakes.ID_EC2_VOLUME_1})
def test_get_item_by_id(self): self._setup_items() item_id = db_api.get_items(self.context, 'fake')[0]['id'] other_item_id = db_api.get_items(self.other_context, 'fake')[0]['id'] item = db_api.get_item_by_id(self.context, item_id) self.assertThat( item, matchers.DictMatches({ 'id': item_id, 'os_id': None, 'vpc_id': None })) item = db_api.get_item_by_id(self.context, other_item_id) self.assertIsNone(item) item = db_api.get_item_by_id(self.context, fakes.random_ec2_id('fake')) self.assertIsNone(item)
def test_add_tags_isolation(self): item_id = fakes.random_ec2_id('fake') tag1 = {'item_id': item_id, 'key': 'key1', 'value': 'val1'} tag2 = {'item_id': item_id, 'key': 'key2', 'value': 'val2'} db_api.add_tags(self.context, [tag1, tag2]) db_api.add_tags(self.other_context, [{ 'item_id': item_id, 'key': 'key1', 'value': 'val1_1' }, { 'item_id': item_id, 'key': 'key3', 'value': 'val3' }]) tags = db_api.get_tags(self.context) self.assertThat( tags, matchers.ListMatches([tag1, tag2], orderless_lists=True))
def test_add_tags_isolation(self): item_id = fakes.random_ec2_id('fake') tag1 = {'item_id': item_id, 'key': 'key1', 'value': 'val1'} tag2 = {'item_id': item_id, 'key': 'key2', 'value': 'val2'} db_api.add_tags(self.context, [tag1, tag2]) db_api.add_tags(self.other_context, [{'item_id': item_id, 'key': 'key1', 'value': 'val1_1'}, {'item_id': item_id, 'key': 'key3', 'value': 'val3'}]) tags = db_api.get_tags(self.context) self.assertThat(tags, matchers.ListMatches([tag1, tag2], orderless_lists=True))
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_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_get_os_image(self, db_api, glance): glance = glance.return_value fake_context = mock.Mock(service_catalog=[{'type': 'fake'}]) os_image = fakes.OSImage(fakes.OS_IMAGE_1) glance.images.get.return_value = os_image # NOTE(ft): check normal flow for an user owned image db_api.get_public_items.return_value = [] db_api.get_item_by_id.return_value = fakes.DB_IMAGE_1 self.assertEqual( os_image, ec2utils.get_os_image(fake_context, fakes.ID_EC2_IMAGE_1)) db_api.get_item_by_id.assert_called_with( mock.ANY, fakes.ID_EC2_IMAGE_1) glance.images.get.assert_called_with(fakes.ID_OS_IMAGE_1) # NOTE(ft): check normal flow for a public image db_api.get_public_items.return_value = [fakes.DB_IMAGE_1] db_api.get_item_by_id.return_value = None self.assertEqual( os_image, ec2utils.get_os_image(fake_context, fakes.ID_EC2_IMAGE_1)) db_api.get_public_items.assert_called_with( mock.ANY, 'ami', (fakes.ID_EC2_IMAGE_1,)) glance.images.get.assert_called_with(fakes.ID_OS_IMAGE_1) # NOTE(ft): 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) # NOTE(ft): check case of an unknown image id db_api.get_public_items.return_value = [] db_api.get_item_by_id.return_value = None self.assertRaises( exception.InvalidAMIIDNotFound, ec2utils.get_os_image, fake_context, fakes.random_ec2_id('ami'))
def test_get_public_items(self): self._setup_items() items = db_api.get_public_items(self.context, 'fake') self.assertEqual(2, len(items)) public_item_ids = [i['id'] for i in items] items = db_api.get_public_items(self.context, 'fake', public_item_ids) self.assertEqual(2, len(items)) items = db_api.get_public_items(self.context, 'fake', [public_item_ids[0]]) self.assertEqual(1, len(items)) items = db_api.get_public_items(self.context, 'fake', (public_item_ids[1], )) self.assertEqual(1, len(items)) items = db_api.get_public_items(self.context, 'fake1', [public_item_ids[0]]) self.assertEqual(0, len(items)) items = db_api.get_public_items(self.context, 'fake', fakes.random_ec2_id('fake')) self.assertEqual(0, len(items)) items = db_api.get_public_items(self.context, 'fake0', []) self.assertEqual(0, len(items))
def test_get_public_items(self): self._setup_items() items = db_api.get_public_items(self.context, 'fake') self.assertEqual(2, len(items)) public_item_ids = [i['id'] for i in items] items = db_api.get_public_items(self.context, 'fake', public_item_ids) self.assertEqual(2, len(items)) items = db_api.get_public_items(self.context, 'fake', [public_item_ids[0]]) self.assertEqual(1, len(items)) items = db_api.get_public_items(self.context, 'fake', (public_item_ids[1],)) self.assertEqual(1, len(items)) items = db_api.get_public_items(self.context, 'fake1', [public_item_ids[0]]) self.assertEqual(0, len(items)) items = db_api.get_public_items(self.context, 'fake', fakes.random_ec2_id('fake')) self.assertEqual(0, len(items)) items = db_api.get_public_items(self.context, 'fake0', []) self.assertEqual(0, len(items))
def test_deregister_image(self): self._setup_model() # normal flow resp = self.execute('DeregisterImage', {'ImageId': fakes.ID_EC2_IMAGE_1}) self.assertThat(resp, matchers.DictMatches({'return': True})) self.db_api.delete_item.assert_called_once_with( mock.ANY, fakes.ID_EC2_IMAGE_1) self.glance.images.delete.assert_called_once_with( fakes.ID_OS_IMAGE_1) # deregister image which failed on asynchronously creation self.glance.reset_mock() image_id = fakes.random_ec2_id('ami') self.add_mock_db_items({'id': image_id, 'os_id': None, 'state': 'failed'}) resp = self.execute('DeregisterImage', {'ImageId': image_id}) self.assertThat(resp, matchers.DictMatches({'return': True})) self.db_api.delete_item.assert_called_with(mock.ANY, image_id) self.assertFalse(self.glance.images.delete.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_deregister_image(self): self._setup_model() # normal flow resp = self.execute('DeregisterImage', {'ImageId': fakes.ID_EC2_IMAGE_1}) self.assertThat(resp, matchers.DictMatches({'return': True})) self.db_api.delete_item.assert_called_once_with( mock.ANY, fakes.ID_EC2_IMAGE_1) self.glance.images.delete.assert_called_once_with(fakes.ID_OS_IMAGE_1) # deregister image which failed on asynchronously creation self.glance.reset_mock() image_id = fakes.random_ec2_id('ami') self.add_mock_db_items({ 'id': image_id, 'os_id': None, 'state': 'failed' }) resp = self.execute('DeregisterImage', {'ImageId': image_id}) self.assertThat(resp, matchers.DictMatches({'return': True})) self.db_api.delete_item.assert_called_with(mock.ANY, image_id) self.assertFalse(self.glance.images.delete.called)
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_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_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}))