Commit d69b3095 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

Add details in exceptions raised in issue_commission

Exceptions regarding limit violations now carry details
as specified in CommissionValueException
parent d1e55896
......@@ -47,19 +47,32 @@ class NoEntityError(CommissionException):
pass
@register_exception
class NoQuantityError(CommissionException):
class CommissionValueException(CommissionException):
def __init__(self, *args, **kw):
super(CommissionValueException, self).__init__(*args, **kw)
kwargs = self.kwargs
self.source = kwargs['source']
self.target = kwargs['target']
self.resource = kwargs['resource']
self.requested = kwargs['requested']
self.current = kwargs['current']
self.limit = kwargs['limit']
@register_exception
class NoQuantityError(CommissionValueException):
pass
@register_exception
class NoCapacityError(CommissionException):
class NoCapacityError(CommissionValueException):
pass
@register_exception
class ExportLimitError(CommissionException):
class ExportLimitError(CommissionValueException):
pass
@register_exception
class ImportLimitError(CommissionException):
class ImportLimitError(CommissionValueException):
pass
@register_exception
......
......@@ -659,53 +659,78 @@ class QuotaholderDjangoDBCallpoint(Callpoint):
if quantity < 0:
release = 1
# Source limits checks
try:
h = db_get_holding(entity=entity, resource=resource,
for_update=True)
except Holding.DoesNotExist:
m = ("There is not enough quantity "
"to allocate from in %s.%s" % (entity, resource))
raise NoQuantityError(m)
raise NoQuantityError(m,
source=entity, target=target,
resource=resource, requested=quantity,
current=0, limit=0)
hp = h.policy
if (hp.export_limit is not None and
h.exporting + quantity > hp.export_limit):
if hp.export_limit is not None:
current = h.exporting
limit = hp.export_limit
if current + quantity > limit:
m = ("Export limit reached for %s.%s" % (entity, resource))
raise ExportLimitError(m)
raise ExportLimitError(m,
source=entity, target=target,
resource=resource, requested=quantity,
current=current, limit=limit)
if hp.quantity is not None:
available = (+ hp.quantity + h.imported + h.returned
- h.exporting - h.releasing)
if available - quantity < 0:
limit = hp.quantity
current = (+ h.exporting + h.releasing
- h.imported - h.returned)
if current + quantity > limit:
m = ("There is not enough quantity "
"to allocate from in %s.%s" % (entity, resource))
raise NoQuantityError(m)
raise NoQuantityError(m,
source=entity, target=target,
resource=resource, requested=quantity,
current=current, limit=limit)
# Target limits checks
try:
th = db_get_holding(entity=target, resource=resource,
for_update=True)
except Holding.DoesNotExist:
m = ("There is not enough capacity "
"to allocate into in %s.%s" % (target, resource))
raise NoCapacityError(m)
raise NoCapacityError(m,
source=entity, target=target,
resource=resource, requested=quantity,
current=0, limit=0)
tp = th.policy
if (tp.import_limit is not None and
th.importing + quantity > tp.import_limit):
if tp.import_limit is not None:
limit = tp.import_limit
current = th.importing
if current + quantity > limit:
m = ("Import limit reached for %s.%s" % (target, resource))
raise ImportLimitError(m)
raise ImportLimitError(m,
source=entity, target=target,
resource=resource, requested=quantity,
current=current, limit=limit)
if tp.capacity is not None:
capacity = (+ tp.capacity + th.exported + th.released
- th.importing - th.returning)
limit = tp.capacity
current = (+ th.importing + th.returning
- th.exported - th.released)
if capacity - quantity < 0:
if current + quantity > limit:
m = ("There is not enough capacity "
"to allocate into in %s.%s" % (target, resource))
raise NoCapacityError(m)
raise NoCapacityError(m,
source=entity, target=target,
resource=resource, requested=quantity,
current=current, limit=limit)
Provision.objects.create( serial = commission,
entity = e,
......
......@@ -37,7 +37,12 @@ from config import rand_string
from config import printf
from synnefo.lib.commissioning import CallError
from synnefo.lib.quotaholder.api import InvalidDataError, NoEntityError
from synnefo.lib.quotaholder.api import (
InvalidDataError,
InvalidKeyError, NoEntityError,
NoQuantityError, NoCapacityError,
ExportLimitError, ImportLimitError,
DuplicateError)
from synnefo.lib.quotaholder.api.quotaholder import (
Name, Key, Quantity, Capacity, ImportLimit, ExportLimit, Resource, Flags,
Imported, Exported, Returned, Released)
......@@ -215,8 +220,9 @@ class QHAPITest(QHTestCase):
self.assertEqual(r, [(e, resource) + limits1 +
DEFAULT_HOLDING + (f,)])
def new_quota(self, entity, key, resource):
limits = self.rand_limits()
def new_quota(self, entity, key, resource, limits=None):
if limits is None:
limits = self.rand_limits()
f = self.rand_flags()
r = self.qh.set_quota(
set_quota=[(entity, resource, key) + limits + (f,)])
......@@ -293,7 +299,7 @@ class QHAPITest(QHTestCase):
(e0, resource1, 0, None, 5, 5)
+ DEFAULT_HOLDING + (0,)])
def test_009_commissions(self):
def test_0090_commissions(self):
e0, k0 = self.new_entity()
e1, k1 = self.new_entity()
resource = self.rand_resource()
......@@ -318,6 +324,69 @@ class QHAPITest(QHTestCase):
r = self.qh.get_pending_commissions(clientkey=self.client)
self.assertEqual(r, [])
def test_0091_commissions_exceptions(self):
es1, ks1 = self.new_entity()
es2, ks2 = self.new_entity()
et1, kt1 = self.new_entity()
et2, kt2 = self.new_entity()
resource = self.rand_resource()
self.new_quota(es1, ks1, resource, (10, 5, 5, 15))
self.new_quota(es2, ks2, resource, (10, 5, 5, 10))
self.new_quota(et1, kt1, resource, (0, 15, 3, 20))
self.new_quota(et2, kt2, resource, (0, 15, 20, 20))
try:
self.qh.issue_commission(clientkey=self.client, target=et1, key=kt1,
name='something',
provisions=[(es1, resource, 12)])
except NoQuantityError, e:
self.assertEqual(e.source, es1)
self.assertEqual(e.target, et1)
self.assertEqual(e.resource, resource)
self.assertEqual(e.limit, 10)
self.assertEqual(e.requested, 12)
self.assertEqual(e.current, 0)
r = self.qh.issue_commission(clientkey=self.client, target=et1,
key=kt1,
name='something',
provisions=[(es1, resource, 2)])
self.assertGreater(r, 0)
try:
self.qh.issue_commission(clientkey=self.client, target=et1, key=kt1,
name='something',
provisions=[(es1, resource, 2)])
except ImportLimitError, e:
self.assertEqual(e.source, es1)
self.assertEqual(e.target, et1)
self.assertEqual(e.resource, resource)
self.assertEqual(e.limit, 3)
self.assertEqual(e.requested, 2)
self.assertEqual(e.current, 2)
r = self.qh.issue_commission(clientkey=self.client, target=et2,
key=kt2,
name='something',
provisions=[(es2, resource, 9)])
self.assertGreater(r, 0)
try:
self.qh.issue_commission(clientkey=self.client, target=et2,
key=kt2,
name='something',
provisions=[(es2, resource, 1),
(es1, resource, 2)])
except NoCapacityError, e:
self.assertEqual(e.source, es1)
self.assertEqual(e.target, et2)
self.assertEqual(e.resource, resource)
self.assertEqual(e.limit, 10)
self.assertEqual(e.requested, 2)
# 9 actual + 1 from the first provision
self.assertEqual(e.current, 10)
def test_010_list_holdings(self):
e0, k0 = ('list_holdings_one', '1')
e1, k1 = ('list_holdings_two', '1')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment