Пример #1
0
    def test_get_by_specified_consumer_type(self):
        ct = ct_obj.ConsumerType(self.ctx, name='INSTANCE')
        ct.create()
        consumer_id = uuidutils.generate_uuid()
        c = c_obj.Consumer(self.ctx,
                           uuid=consumer_id,
                           project=self.project_obj,
                           user=self.user_obj,
                           consumer_type_id=ct.id)
        c.create()
        # This will add a consumer with the consumer type INSTANCE
        # and the default project and user external_ids
        da = copy.deepcopy(tb.DISK_ALLOCATION)
        da['consumer_id'] = c.uuid
        self._make_allocation(tb.DISK_INVENTORY, da)

        # Verify we filter the INSTANCE type correctly. Note: this will also
        # work if filtering is broken (if it's not filtering at all)
        usages = usage_obj.get_by_consumer_type(self.ctx,
                                                self.project_obj.external_id,
                                                consumer_type=ct.name)
        self.assertEqual(1, len(usages))
        usage = usages[0]
        self.assertEqual(ct.name, usage.consumer_type)
        self.assertEqual(1, usage.consumer_count)
        self.assertEqual(orc.DISK_GB, usage.resource_class)
        self.assertEqual(2, usage.usage)
        # Verify we get nothing back if we filter on a different consumer
        # type that does not exist (will not work if filtering is broken)
        usages = usage_obj.get_by_consumer_type(self.ctx,
                                                self.project_obj.external_id,
                                                consumer_type='BOGUS')
        self.assertEqual(0, len(usages))
Пример #2
0
    def test_get_by_all_consumer_type(self):
        # This will add a consumer with the default consumer type UNKNOWN
        db_rp, _ = self._make_allocation(tb.DISK_INVENTORY, tb.DISK_ALLOCATION)
        # Make another allocation with a different consumer type
        ct = ct_obj.ConsumerType(self.ctx, name='FOO')
        ct.create()
        consumer_id = uuidutils.generate_uuid()
        c = c_obj.Consumer(self.ctx,
                           uuid=consumer_id,
                           project=self.project_obj,
                           user=self.user_obj,
                           consumer_type_id=ct.id)
        c.create()
        self.allocate_from_provider(db_rp, orc.DISK_GB, 2, consumer=c)

        # Verify we get usages back for both consumer types with 'all'
        usages = usage_obj.get_by_consumer_type(self.ctx,
                                                self.project_obj.external_id,
                                                consumer_type='all')
        self.assertEqual(1, len(usages))
        usage = usages[0]
        self.assertEqual('all', usage.consumer_type)
        self.assertEqual(2, usage.consumer_count)
        self.assertEqual(orc.DISK_GB, usage.resource_class)
        self.assertEqual(4, usage.usage)
Пример #3
0
    def test_get_by_name_and_id(self):

        ct = ct_obj.ConsumerType(self.context, name='MIGRATION')
        ct.create()

        named_ct = ct_obj.ConsumerType.get_by_name(self.context, 'MIGRATION')
        self.assertEqual(ct.id, named_ct.id)

        id_ct = ct_obj.ConsumerType.get_by_id(self.context, ct.id)
        self.assertEqual(ct.name, id_ct.name)
Пример #4
0
def get_or_create_consumer_type_id(ctx, name):
    """Tries to fetch the provided consumer_type and creates a new one if it
    does not exist.

    :param ctx: The request context.
    :param name: The name of the consumer type.
    :returns: The id of the ConsumerType object.
    """
    try:
        return ctx.ct_cache.id_from_string(name)
    except exception.ConsumerTypeNotFound:
        cons_type = consumer_type_obj.ConsumerType(ctx, name=name)
        try:
            cons_type.create()
            return cons_type.id
        except exception.ConsumerTypeExists:
            # another thread created concurrently, so try again
            return get_or_create_consumer_type_id(ctx, name)
Пример #5
0
    def test_multi_provider_allocation(self):
        """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.
        """
        cn_source = self._create_provider('cn_source')
        cn_dest = self._create_provider('cn_dest')

        # Add same inventory to both source and destination host
        for cn in (cn_source, cn_dest):
            tb.add_inventory(cn, orc.VCPU, 24,
                             allocation_ratio=16.0)
            tb.add_inventory(cn, orc.MEMORY_MB, 1024,
                             min_unit=64,
                             max_unit=1024,
                             step_size=64,
                             allocation_ratio=1.5)

        # Create an INSTANCE consumer type
        ct = ct_obj.ConsumerType(self.ctx, name='INSTANCE')
        ct.create()
        # Save consumer type id for later confirmation.
        ct_id = ct.id

        # Create a consumer representing the instance
        inst_consumer = consumer_obj.Consumer(
            self.ctx, uuid=uuidsentinel.instance, user=self.user_obj,
            project=self.project_obj, consumer_type_id=ct_id)
        inst_consumer.create()

        # Now create an allocation that represents a move operation where the
        # scheduler has selected cn_dest as the target host and created a
        # "doubled-up" allocation for the duration of the move operation
        alloc_list = [
            alloc_obj.Allocation(
                consumer=inst_consumer,
                resource_provider=cn_source,
                resource_class=orc.VCPU,
                used=1),
            alloc_obj.Allocation(
                consumer=inst_consumer,
                resource_provider=cn_source,
                resource_class=orc.MEMORY_MB,
                used=256),
            alloc_obj.Allocation(
                consumer=inst_consumer,
                resource_provider=cn_dest,
                resource_class=orc.VCPU,
                used=1),
            alloc_obj.Allocation(
                consumer=inst_consumer,
                resource_provider=cn_dest,
                resource_class=orc.MEMORY_MB,
                used=256),
        ]
        alloc_obj.replace_all(self.ctx, alloc_list)

        src_allocs = alloc_obj.get_all_by_resource_provider(
            self.ctx, cn_source)

        self.assertEqual(2, len(src_allocs))

        dest_allocs = alloc_obj.get_all_by_resource_provider(self.ctx, cn_dest)

        self.assertEqual(2, len(dest_allocs))

        consumer_allocs = alloc_obj.get_all_by_consumer_id(
            self.ctx, uuidsentinel.instance)

        self.assertEqual(4, len(consumer_allocs))

        # Validate that when we create an allocation for a consumer that we
        # delete any existing allocation and replace it with what the new.
        # Here, we're emulating the step that occurs on confirm_resize() where
        # the source host pulls the existing allocation for the instance and
        # removes any resources that refer to itself and saves the allocation
        # back to placement
        new_alloc_list = [
            alloc_obj.Allocation(
                consumer=inst_consumer,
                resource_provider=cn_dest,
                resource_class=orc.VCPU,
                used=1),
            alloc_obj.Allocation(
                consumer=inst_consumer,
                resource_provider=cn_dest,
                resource_class=orc.MEMORY_MB,
                used=256),
        ]
        alloc_obj.replace_all(self.ctx, new_alloc_list)

        src_allocs = alloc_obj.get_all_by_resource_provider(
            self.ctx, cn_source)

        self.assertEqual(0, len(src_allocs))

        dest_allocs = alloc_obj.get_all_by_resource_provider(
            self.ctx, cn_dest)

        self.assertEqual(2, len(dest_allocs))

        consumer_allocs = alloc_obj.get_all_by_consumer_id(
            self.ctx, uuidsentinel.instance)

        self.assertEqual(2, len(consumer_allocs))

        # check the allocations have the expected INSTANCE consumer type
        self.assertEqual(ct_id, consumer_allocs[0].consumer.consumer_type_id)
        self.assertEqual(ct_id, consumer_allocs[1].consumer.consumer_type_id)
Пример #6
0
    def test_duplicate_create(self):
        ct = ct_obj.ConsumerType(self.context, name='MIGRATION')
        ct.create()

        ct2 = ct_obj.ConsumerType(self.context, name='MIGRATION')
        self.assertRaises(exception.ConsumerTypeExists, ct2.create)