'We set up two compute nodes with VCPU and MEMORY_MB only, a shared
resource provider having DISK_GB inventory, and associate all of them
with an aggregate. We then call the _get_all_with_shared() function to
ensure that we get back the two compute node resource provider records.'

def test_get_all_with_shared(self):

    agg_uuid = uuidsentinel.agg
    cn1_uuid = uuidsentinel.cn1
    cn1 = objects.ResourceProvider(self.ctx, name='cn1', uuid=cn1_uuid)
    cn1.create()
    cn2_uuid = uuidsentinel.cn2
    cn2 = objects.ResourceProvider(self.ctx, name='cn2', uuid=cn2_uuid)
    cn2.create()
    for cn in (cn1, cn2):
        vcpu = objects.Inventory(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)
        memory_mb = objects.Inventory(resource_provider=cn, resource_class=fields.ResourceClass.MEMORY_MB, total=1024, reserved=0, min_unit=64, max_unit=1024, step_size=1, allocation_ratio=1.5)
        inv_list = objects.InventoryList(objects=[vcpu, memory_mb])
        cn.set_inventory(inv_list)
    ss_uuid = uuidsentinel.ss
    ss = objects.ResourceProvider(self.ctx, name='shared    storage', uuid=ss_uuid)
    ss.create()
    disk_gb = objects.Inventory(resource_provider=ss, resource_class=fields.ResourceClass.DISK_GB, total=2000, reserved=0, min_unit=10, max_unit=100, step_size=1, allocation_ratio=1.0)
    inv_list = objects.InventoryList(objects=[disk_gb])
    ss.set_inventory(inv_list)
    t = objects.Trait.get_by_name(self.ctx, 'MISC_SHARES_VIA_AGGREGATE')
    ss.set_traits(objects.TraitList(objects=[t]))
    resources = self._requested_resources()
    got_rps = rp_obj._get_all_with_shared(self.ctx, resources)
    got_ids = [rp.id for rp in got_rps]
    self.assertEqual([], got_ids)
    cn1.set_aggregates([agg_uuid])
    cn2.set_aggregates([agg_uuid])
    ss.set_aggregates([agg_uuid])
    got_rps = rp_obj._get_all_with_shared(self.ctx, resources)
    got_ids = [rp.id for rp in got_rps]
    self.assertEqual([cn1.id, cn2.id], got_ids)
    cn3_uuid = uuidsentinel.cn3
    cn3 = objects.ResourceProvider(self.ctx, name='cn3', uuid=cn3_uuid)
    cn3.create()
    vcpu = objects.Inventory(resource_provider=cn3, resource_class=fields.ResourceClass.VCPU, total=24, reserved=0, min_unit=1, max_unit=24, step_size=1, allocation_ratio=1.0)
    memory_mb = objects.Inventory(resource_provider=cn3, resource_class=fields.ResourceClass.MEMORY_MB, total=1024, reserved=0, min_unit=64, max_unit=1024, step_size=1, allocation_ratio=1.0)
    disk_gb = objects.Inventory(resource_provider=cn3, resource_class=fields.ResourceClass.DISK_GB, total=500, reserved=0, min_unit=10, max_unit=500, step_size=10, allocation_ratio=1.0)
    inv_list = objects.InventoryList(objects=[vcpu, memory_mb, disk_gb])
    cn3.set_inventory(inv_list)
    got_rps = rp_obj._get_all_with_shared(self.ctx, resources)
    got_ids = [rp.id for rp in got_rps]
    self.assertEqual([cn1.id, cn2.id, cn3.id], got_ids)
    vcpu_alloc = objects.Allocation(resource_provider=cn3, resource_class=fields.ResourceClass.VCPU, consumer_id=uuidsentinel.consumer, used=24)
    memory_mb_alloc = objects.Allocation(resource_provider=cn3, resource_class=fields.ResourceClass.MEMORY_MB, consumer_id=uuidsentinel.consumer, used=1024)
    alloc_list = objects.AllocationList(self.ctx, objects=[vcpu_alloc, memory_mb_alloc])
    alloc_list.create_all()
    got_rps = rp_obj._get_all_with_shared(self.ctx, resources)
    got_ids = [rp.id for rp in got_rps]
    self.assertEqual([cn1.id, cn2.id], got_ids)
    for x in range(3):
        memory_mb_alloc = objects.Allocation(resource_provider=cn2, resource_class=fields.ResourceClass.MEMORY_MB, consumer_id=getattr(uuidsentinel, ('consumer%d' % x)), used=512)
        alloc_list = objects.AllocationList(self.ctx, objects=[memory_mb_alloc])
        alloc_list.create_all()
    got_rps = rp_obj._get_all_with_shared(self.ctx, resources)
    got_ids = [rp.id for rp in got_rps]
    self.assertEqual([cn1.id], got_ids)
    cn4_uuid = uuidsentinel.cn4
    cn4 = objects.ResourceProvider(self.ctx, name='cn4', uuid=cn4_uuid)
    cn4.create()
    cn5_uuid = uuidsentinel.cn5
    cn5 = objects.ResourceProvider(self.ctx, name='cn5', uuid=cn5_uuid)
    cn5.create()
    for cn in (cn4, cn5):
        vcpu = objects.Inventory(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)
        memory_mb = objects.Inventory(resource_provider=cn, resource_class=fields.ResourceClass.MEMORY_MB, total=1024, reserved=0, min_unit=64, max_unit=1024, step_size=1, allocation_ratio=1.5)
        inv_list = objects.InventoryList(objects=[vcpu, memory_mb])
        cn.set_inventory(inv_list)
    ns_uuid = uuidsentinel.ns
    ns = objects.ResourceProvider(self.ctx, name='non_shared    storage', uuid=ns_uuid)
    ns.create()
    disk_gb = objects.Inventory(resource_provider=ns, resource_class=fields.ResourceClass.DISK_GB, total=2000, reserved=0, min_unit=10, max_unit=100, step_size=1, allocation_ratio=1.0)
    inv_list = objects.InventoryList(objects=[disk_gb])
    ns.set_inventory(inv_list)
    agg2_uuid = uuidsentinel.agg2
    cn4.set_aggregates([agg2_uuid])
    cn5.set_aggregates([agg2_uuid])
    ns.set_aggregates([agg2_uuid])
    got_rps = rp_obj._get_all_with_shared(self.ctx, resources)
    got_ids = [rp.id for rp in got_rps]
    self.assertEqual([cn1.id], got_ids)
