Skip to content

Commit

Permalink
Make sure quota error message is displayed in overview page.
Browse files Browse the repository at this point in the history
Fixes bug 1040799.

This commit fixes the bug that error message "Unable to retrieve quota
information" detected during rendering nova/overview page is displayed
after moving to the next page.

Change-Id: I647a02a216b803e6fd17d4849437a77b680f84af
  • Loading branch information
amotoki committed Aug 30, 2012
1 parent b177299 commit 856083a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
33 changes: 32 additions & 1 deletion horizon/dashboards/nova/overview/tests.py
Expand Up @@ -37,11 +37,14 @@ class UsageViewTests(test.TestCase):
def test_usage(self):
now = timezone.now()
usage_obj = api.nova.Usage(self.usages.first())
quotas = self.quota_usages.first()
self.mox.StubOutWithMock(api, 'usage_get')
self.mox.StubOutWithMock(api.nova, 'tenant_quota_usages')
api.usage_get(IsA(http.HttpRequest), self.tenant.id,
datetime.datetime(now.year, now.month, 1, 0, 0, 0),
Func(usage.almost_now)) \
.AndReturn(usage_obj)
api.nova.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(quotas)
self.mox.ReplayAll()

res = self.client.get(reverse('horizon:nova:overview:index'))
Expand All @@ -66,45 +69,73 @@ def test_unauthorized(self):
def test_usage_csv(self):
now = timezone.now()
usage_obj = api.nova.Usage(self.usages.first())
quotas = self.quota_usages.first()
self.mox.StubOutWithMock(api, 'usage_get')
self.mox.StubOutWithMock(api.nova, 'tenant_quota_usages')
timestamp = datetime.datetime(now.year, now.month, 1, 0, 0, 0)
api.usage_get(IsA(http.HttpRequest),
self.tenant.id,
timestamp,
Func(usage.almost_now)) \
.AndReturn(usage_obj)
api.nova.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(quotas)

self.mox.ReplayAll()
res = self.client.get(reverse('horizon:nova:overview:index') +
"?format=csv")
self.assertTemplateUsed(res, 'nova/overview/usage.csv')
self.assertTrue(isinstance(res.context['usage'], usage.TenantUsage))

def test_usage_exception(self):
def test_usage_exception_usage(self):
now = timezone.now()
quotas = self.quota_usages.first()
self.mox.StubOutWithMock(api, 'usage_get')
self.mox.StubOutWithMock(api.nova, 'tenant_quota_usages')
timestamp = datetime.datetime(now.year, now.month, 1, 0, 0, 0)
api.usage_get(IsA(http.HttpRequest),
self.tenant.id,
timestamp,
Func(usage.almost_now)) \
.AndRaise(self.exceptions.nova)
api.nova.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(quotas)
self.mox.ReplayAll()

res = self.client.get(reverse('horizon:nova:overview:index'))
self.assertTemplateUsed(res, 'nova/overview/usage.html')
self.assertEqual(res.context['usage'].usage_list, [])

def test_usage_exception_quota(self):
now = timezone.now()
usage_obj = api.nova.Usage(self.usages.first())
self.mox.StubOutWithMock(api, 'usage_get')
self.mox.StubOutWithMock(api.nova, 'tenant_quota_usages')
timestamp = datetime.datetime(now.year, now.month, 1, 0, 0, 0)
api.usage_get(IsA(http.HttpRequest),
self.tenant.id,
timestamp,
Func(usage.almost_now)) \
.AndReturn(usage_obj)
api.nova.tenant_quota_usages(IsA(http.HttpRequest))\
.AndRaise(self.exceptions.nova)
self.mox.ReplayAll()

res = self.client.get(reverse('horizon:nova:overview:index'))
self.assertTemplateUsed(res, 'nova/overview/usage.html')
self.assertEqual(res.context['usage'].quotas, {})

def test_usage_default_tenant(self):
now = timezone.now()
usage_obj = api.nova.Usage(self.usages.first())
quotas = self.quota_usages.first()
self.mox.StubOutWithMock(api, 'usage_get')
self.mox.StubOutWithMock(api.nova, 'tenant_quota_usages')
timestamp = datetime.datetime(now.year, now.month, 1, 0, 0, 0)
api.usage_get(IsA(http.HttpRequest),
self.tenant.id,
timestamp,
Func(usage.almost_now)) \
.AndReturn(usage_obj)
api.nova.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(quotas)
self.mox.ReplayAll()

res = self.client.get(reverse('horizon:nova:overview:index'))
Expand Down
6 changes: 6 additions & 0 deletions horizon/dashboards/syspanel/overview/tests.py
Expand Up @@ -36,16 +36,19 @@

class UsageViewTests(test.BaseAdminViewTests):
@test.create_stubs({api: ('usage_list',),
api.nova: ('tenant_quota_usages',),
api.keystone: ('tenant_list',)})
def test_usage(self):
now = timezone.now()
usage_obj = api.nova.Usage(self.usages.first())
quotas = self.quota_usages.first()
api.keystone.tenant_list(IsA(http.HttpRequest), admin=True) \
.AndReturn(self.tenants.list())
api.usage_list(IsA(http.HttpRequest),
datetime.datetime(now.year, now.month, 1, 0, 0, 0),
Func(usage.almost_now)) \
.AndReturn([usage_obj])
api.nova.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(quotas)
self.mox.ReplayAll()
res = self.client.get(reverse('horizon:syspanel:overview:index'))
self.assertTemplateUsed(res, 'syspanel/overview/usage.html')
Expand All @@ -65,16 +68,19 @@ def test_usage(self):
usage_obj.total_local_gb_usage))

@test.create_stubs({api: ('usage_list',),
api.nova: ('tenant_quota_usages',),
api.keystone: ('tenant_list',)})
def test_usage_csv(self):
now = timezone.now()
usage_obj = api.nova.Usage(self.usages.first())
quotas = self.quota_usages.first()
api.keystone.tenant_list(IsA(http.HttpRequest), admin=True) \
.AndReturn(self.tenants.list())
api.usage_list(IsA(http.HttpRequest),
datetime.datetime(now.year, now.month, 1, 0, 0, 0),
Func(usage.almost_now)) \
.AndReturn([usage_obj])
api.nova.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(quotas)
self.mox.ReplayAll()
csv_url = reverse('horizon:syspanel:overview:index') + "?format=csv"
res = self.client.get(csv_url)
Expand Down
16 changes: 7 additions & 9 deletions horizon/usage/base.py
Expand Up @@ -30,6 +30,7 @@ def __init__(self, request, tenant_id=None):
self.request = request
self.summary = {}
self.usage_list = []
self.quotas = {}

@property
def today(self):
Expand Down Expand Up @@ -104,15 +105,12 @@ def summarize(self, start, end):
self.summary.setdefault(key, 0)
self.summary[key] += value

def quota(self):
if not hasattr(self, "_quotas"):
try:
self._quotas = api.nova.tenant_quota_usages(self.request)
except:
self._quotas = {}
exceptions.handle(self.request,
_("Unable to retrieve quota information."))
return self._quotas
def get_quotas(self):
try:
self.quotas = api.nova.tenant_quota_usages(self.request)
except:
exceptions.handle(self.request,
_("Unable to retrieve quota information."))

def csv_link(self):
form = self.get_form()
Expand Down
1 change: 1 addition & 0 deletions horizon/usage/views.py
Expand Up @@ -31,6 +31,7 @@ def get_data(self):
tenant_id = self.kwargs.get('tenant_id', self.request.user.tenant_id)
self.usage = self.usage_class(self.request, tenant_id)
self.usage.summarize(*self.usage.get_date_range())
self.usage.get_quotas()
self.kwargs['usage'] = self.usage
return self.usage.usage_list

Expand Down

0 comments on commit 856083a

Please sign in to comment.