def test_allowed_snapshots_more_than_1(self): # Pretend that 1 snapshot exists for this tenant self.mock.StubOutWithMock(query.Query, "count") query.Query.count().AndReturn(1) # Allow up to 5 snapshots to be created default_quotas = [{"tenant_id": "12345", "hard_limit": 5, "resource": "snapshots"}] 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 snapshot allowed = quota.allowed_snapshots(self.DUMMY_CONTEXT, 3) self.assertTrue(allowed == 3, "Expected 3 allowed snapshot, instead got %s" % allowed)
def _check_snapshot_quota(self, context, count=1): num_snapshots = quota.allowed_snapshots(context, count) LOG.debug('number of snapshots allowed to create %s' % num_snapshots) if num_snapshots < count: tid = context.tenant if num_snapshots <= 0: msg = _("Cannot create any more snapshots of this type.") else: msg = (_("Can only create %s more snapshots of this type.") % num_snapshots) LOG.warn(_("Quota exceeded for %(tid)s," " tried to create %(count)s snapshots. %(msg)s"), locals()) raise exception.QuotaError("InstanceLimitExceeded") return num_snapshots
def test_allowed_snapshots_truncated(self): """ Ensure that the request for an snapshot count that exceeds quota gets truncated to the maximum allowed """ # Pretend that 1 snapshot exists for this tenant self.mock.StubOutWithMock(query.Query, "count") query.Query.count().AndReturn(1) # Allow up to 5 snapshots to be created default_quotas = [{"tenant_id": "12345", "hard_limit": 5, "resource": "snapshots"}] 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 snapshot allowed = quota.allowed_snapshots(self.DUMMY_CONTEXT, 5) self.assertTrue(allowed == 4, "Expected 4 allowed snapshots, instead got %s" % allowed)
def test_allowed_snapshots(self): """Tests that given a quota on snapshots, the number of snapshots allowed to be created is calculated appropriately""" # Pretend that 1 snapshot exists for this tenant self.mock.StubOutWithMock(query.Query, "count") query.Query.count().AndReturn(1) # Allow up to 5 snapshots to be created default_quotas = [{"tenant_id": "12345", "hard_limit": 5, "resource": "snapshots"}] 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 snapshot allowed = quota.allowed_snapshots(self.DUMMY_CONTEXT, 1) self.assertTrue(allowed == 1, "Expected 1 allowed snapshot, instead got %s" % allowed)