Fix simple_tenant_usage's handing of future end times

Fixes bug #1043999

simple_tenant_usage returns the usage in the range between the supplied
start and end query parameters. The _hours_for() method calculates the
number of hours the instance was running in this range, even if the end time
is in the future.

By default, python-novaclient requests usage between 4 weeks ago and tomorrow.
This means we a report which accounts for 24 hours of extra running time for
each currently running instance. Fix this by clamping the period of the
returned report at the current time.

Change-Id: I61c211fd85b729aaeaac6ef24644ee19eb5bb6b0
This commit is contained in:
RongzeZhu 2012-09-02 18:43:56 +08:00
parent 84c62b35ff
commit 869443f41c
2 changed files with 27 additions and 6 deletions

View File

@ -218,6 +218,9 @@ class SimpleTenantUsageController(object):
authorize_list(context)
(period_start, period_stop, detailed) = self._get_datetime_range(req)
now = timeutils.utcnow()
if period_stop > now:
period_stop = now
usages = self._tenant_usages_for_period(context,
period_start,
period_stop,
@ -233,6 +236,9 @@ class SimpleTenantUsageController(object):
authorize_show(context, {'project_id': tenant_id})
(period_start, period_stop, ignore) = self._get_datetime_range(req)
now = timeutils.utcnow()
if period_stop > now:
period_stop = now
usage = self._tenant_usages_for_period(context,
period_start,
period_stop,

View File

@ -41,8 +41,9 @@ ROOT_GB = 10
EPHEMERAL_GB = 20
MEMORY_MB = 1024
VCPUS = 2
STOP = timeutils.utcnow()
START = STOP - datetime.timedelta(hours=HOURS)
NOW = timeutils.utcnow()
START = NOW - datetime.timedelta(hours=HOURS)
STOP = NOW
def fake_instance_type_get(self, context, instance_type_id):
@ -93,10 +94,10 @@ class SimpleTenantUsageTest(test.TestCase):
'faketenant_1',
is_admin=False)
def test_verify_index(self):
def _test_verify_index(self, start, stop):
req = webob.Request.blank(
'/v2/faketenant_0/os-simple-tenant-usage?start=%s&end=%s' %
(START.isoformat(), STOP.isoformat()))
(start.isoformat(), stop.isoformat()))
req.method = "GET"
req.headers["content-type"] = "application/json"
@ -117,6 +118,20 @@ class SimpleTenantUsageTest(test.TestCase):
SERVERS * VCPUS * HOURS)
self.assertFalse(usages[i].get('server_usages'))
def test_verify_index(self):
self._test_verify_index(START, STOP)
def test_verify_index_futrue_end_time(self):
future = NOW + datetime.timedelta(hours=HOURS)
self._test_verify_index(START, future)
def test_verify_show(self):
self._test_verify_show(START, STOP)
def test_verify_show_futrue_end_time(self):
future = NOW + datetime.timedelta(hours=HOURS)
self._test_verify_show(START, future)
def _get_tenant_usages(self, detailed=''):
req = webob.Request.blank(
'/v2/faketenant_0/os-simple-tenant-usage?'
@ -149,12 +164,12 @@ class SimpleTenantUsageTest(test.TestCase):
for i in xrange(TENANTS):
self.assertEqual(usages[i].get('server_usages'), None)
def test_verify_show(self):
def _test_verify_show(self, start, stop):
tenant_id = 0
req = webob.Request.blank(
'/v2/faketenant_0/os-simple-tenant-usage/'
'faketenant_%s?start=%s&end=%s' %
(tenant_id, START.isoformat(), STOP.isoformat()))
(tenant_id, start.isoformat(), stop.isoformat()))
req.method = "GET"
req.headers["content-type"] = "application/json"