'Tests that an allocation that includes more than one resource
provider can be created, listed and deleted properly.
Bug #1707669 highlighted a situation that arose when attempting to
remove part of an allocation for a source host during a resize
operation where the exiting allocation was not being properly
deleted.'

def test_multi_provider_allocation(self):

    cn_source = objects.ResourceProvider(context=self.ctx, uuid=uuidsentinel.cn_source, name=uuidsentinel.cn_source)
    cn_source.create()
    cn_dest = objects.ResourceProvider(context=self.ctx, uuid=uuidsentinel.cn_dest, name=uuidsentinel.cn_dest)
    cn_dest.create()
    for cn in (cn_source, cn_dest):
        cpu_inv = objects.Inventory(context=self.ctx, resource_provider=cn, resource_class=fields.ResourceClass.VCPU, total=24, reserved=0, min_unit=1, max_unit=24, step_size=1, allocation_ratio=16.0)
        ram_inv = objects.Inventory(context=self.ctx, resource_provider=cn, resource_class=fields.ResourceClass.MEMORY_MB, total=1024, reserved=0, min_unit=64, max_unit=1024, step_size=64, allocation_ratio=1.5)
        inv_list = objects.InventoryList(context=self.ctx, objects=[cpu_inv, ram_inv])
        cn.set_inventory(inv_list)
    alloc_list = objects.AllocationList(context=self.ctx, objects=[objects.Allocation(context=self.ctx, consumer_id=uuidsentinel.instance, resource_provider=cn_source, resource_class=fields.ResourceClass.VCPU, used=1), objects.Allocation(context=self.ctx, consumer_id=uuidsentinel.instance, resource_provider=cn_source, resource_class=fields.ResourceClass.MEMORY_MB, used=256), objects.Allocation(context=self.ctx, consumer_id=uuidsentinel.instance, resource_provider=cn_dest, resource_class=fields.ResourceClass.VCPU, used=1), objects.Allocation(context=self.ctx, consumer_id=uuidsentinel.instance, resource_provider=cn_dest, resource_class=fields.ResourceClass.MEMORY_MB, used=256)])
    alloc_list.create_all()
    src_allocs = objects.AllocationList.get_all_by_resource_provider_uuid(self.ctx, cn_source.uuid)
    self.assertEqual(2, len(src_allocs))
    dest_allocs = objects.AllocationList.get_all_by_resource_provider_uuid(self.ctx, cn_dest.uuid)
    self.assertEqual(2, len(dest_allocs))
    consumer_allocs = objects.AllocationList.get_all_by_consumer_id(self.ctx, uuidsentinel.instance)
    self.assertEqual(4, len(consumer_allocs))
    new_alloc_list = objects.AllocationList(context=self.ctx, objects=[objects.Allocation(context=self.ctx, consumer_id=uuidsentinel.instance, resource_provider=cn_dest, resource_class=fields.ResourceClass.VCPU, used=1), objects.Allocation(context=self.ctx, consumer_id=uuidsentinel.instance, resource_provider=cn_dest, resource_class=fields.ResourceClass.MEMORY_MB, used=256)])
    new_alloc_list.create_all()
    src_allocs = objects.AllocationList.get_all_by_resource_provider_uuid(self.ctx, cn_source.uuid)
    self.assertEqual(0, len(src_allocs))
    dest_allocs = objects.AllocationList.get_all_by_resource_provider_uuid(self.ctx, cn_dest.uuid)
    self.assertEqual(2, len(dest_allocs))
    consumer_allocs = objects.AllocationList.get_all_by_consumer_id(self.ctx, uuidsentinel.instance)
    self.assertEqual(2, len(consumer_allocs))
