def test_allowed_instances_more_than_1(self): # Pretend that 1 instance exists for this tenant self.mock.StubOutWithMock(query.Query, "count") query.Query.count().AndReturn(1) # Allow up to 3 instances to be created default_quotas = [{"tenant_id": "12345", "hard_limit": 3, "resource": "instances"}] self.mock.StubOutWithMock(models.Quota, "find_all") models.Quota.find_all(tenant_id="12345", deleted=False).AndReturn(default_quotas) self.mock.ReplayAll() # Check if we are allowed to create 2 instances allowed = quota.allowed_instances(self.DUMMY_CONTEXT, 2) self.assertTrue(allowed == 2, "Expected 2 allowed instance, instead got %s" % allowed)
def test_allowed_instances_exceeds_quota(self): """ Ensure that a 0 is returned when instance count == quota limit """ # Pretend that 1 instance exists for this tenant self.mock.StubOutWithMock(query.Query, "count") query.Query.count().AndReturn(3) # Allow up to 3 instances to be created default_quotas = [{"tenant_id": "12345", "hard_limit": 3, "resource": "instances"}] self.mock.StubOutWithMock(models.Quota, "find_all") models.Quota.find_all(tenant_id="12345", deleted=False).AndReturn(default_quotas) self.mock.ReplayAll() # Check if we are allowed to create 1 instance allowed = quota.allowed_instances(self.DUMMY_CONTEXT, 1) self.assertTrue(allowed == 0, "Expected 0 allowed instance, instead got %s" % allowed)
def _check_instance_quota(self, context, count=1): num_instances = quota.allowed_instances(context, count) LOG.debug('number of instances allowed to create %s' % num_instances) if num_instances < count: tid = context.tenant if num_instances <= 0: msg = _("Cannot create any more instances of this type.") else: msg = (_("Can only create %s more instances of this type.") % num_instances) LOG.warn(_("Quota exceeded for %(tid)s," " tried to create %(count)s instances. %(msg)s"), locals()) raise exception.QuotaError("InstanceLimitExceeded") return num_instances
def test_allowed_instances_truncated(self): """ Ensure that the request for an instance count that exceeds quota gets truncated to the maximum allowed """ # Pretend that 1 instance exists for this tenant self.mock.StubOutWithMock(query.Query, "count") query.Query.count().AndReturn(1) # Allow up to 3 instances to be created default_quotas = [{"tenant_id": "12345", "hard_limit": 3, "resource": "instances"}] self.mock.StubOutWithMock(models.Quota, "find_all") models.Quota.find_all(tenant_id="12345", deleted=False).AndReturn(default_quotas) self.mock.ReplayAll() # Check if we are allowed to create 3 instances allowed = quota.allowed_instances(self.DUMMY_CONTEXT, 3) self.assertTrue(allowed == 2, "Expected 2 allowed instance, instead got %s" % allowed)
def test_allowed_instances(self): """Tests that given a quota on instances, the number of intances allowed to be created is calculated appropriately""" # Pretend that 1 instance exists for this tenant self.mock.StubOutWithMock(query.Query, "count") query.Query.count().AndReturn(1) # Allow up to 3 instances to be created default_quotas = [{"tenant_id": "12345", "hard_limit": 3, "resource": "instances"}] self.mock.StubOutWithMock(models.Quota, "find_all") models.Quota.find_all(tenant_id="12345", deleted=False).AndReturn(default_quotas) self.mock.ReplayAll() # Check if we are allowed to create 1 instance allowed = quota.allowed_instances(self.DUMMY_CONTEXT, 1) self.assertTrue(allowed == 1, "Expected 1 allowed instance, instead got %s" % allowed)