Merge "1st & last sample timestamps in Resource representation"

This commit is contained in:
Jenkins 2014-01-08 06:17:07 +00:00 committed by Gerrit Code Review
commit 4f009afbfd
2 changed files with 60 additions and 5 deletions

View File

@ -987,8 +987,11 @@ class Resource(_Base):
user_id = wtypes.text user_id = wtypes.text
"The ID of the user who created the resource or updated it last" "The ID of the user who created the resource or updated it last"
timestamp = datetime.datetime first_sample_timestamp = datetime.datetime
"UTC date and time of the last update to any meter for the resource" "UTC date & time of the first sample associated with the resource"
last_sample_timestamp = datetime.datetime
"UTC date & time of the last sample associated with the resource"
metadata = {wtypes.text: wtypes.text} metadata = {wtypes.text: wtypes.text}
"Arbitrary metadata associated with the resource" "Arbitrary metadata associated with the resource"

View File

@ -23,6 +23,7 @@ import json
import logging import logging
import testscenarios import testscenarios
from ceilometer.openstack.common import timeutils
from ceilometer.publisher import rpc from ceilometer.publisher import rpc
from ceilometer import sample from ceilometer import sample
from ceilometer.tests.api.v2 import FunctionalTest from ceilometer.tests.api.v2 import FunctionalTest
@ -40,7 +41,19 @@ class TestListResources(FunctionalTest,
data = self.get_json('/resources') data = self.get_json('/resources')
self.assertEqual([], data) self.assertEqual([], data)
@staticmethod
def _isotime(timestamp):
# drop TZ specifier
return unicode(timeutils.isotime(timestamp))[:-1]
def _verify_sample_timestamps(self, res, first, last):
self.assertTrue('first_sample_timestamp' in res)
self.assertEqual(res['first_sample_timestamp'], self._isotime(first))
self.assertTrue('last_sample_timestamp' in res)
self.assertEqual(res['last_sample_timestamp'], self._isotime(last))
def test_instance_no_metadata(self): def test_instance_no_metadata(self):
timestamp = datetime.datetime(2012, 7, 2, 10, 40)
sample1 = sample.Sample( sample1 = sample.Sample(
'instance', 'instance',
'cumulative', 'cumulative',
@ -49,7 +62,7 @@ class TestListResources(FunctionalTest,
'user-id', 'user-id',
'project-id', 'project-id',
'resource-id', 'resource-id',
timestamp=datetime.datetime(2012, 7, 2, 10, 40), timestamp=timestamp,
resource_metadata=None, resource_metadata=None,
source='test', source='test',
) )
@ -61,8 +74,13 @@ class TestListResources(FunctionalTest,
data = self.get_json('/resources') data = self.get_json('/resources')
self.assertEqual(1, len(data)) self.assertEqual(1, len(data))
self._verify_sample_timestamps(data[0], timestamp, timestamp)
def test_instances(self): def test_instances(self):
timestamps = {
'resource-id': datetime.datetime(2012, 7, 2, 10, 40),
'resource-id-alternate': datetime.datetime(2012, 7, 2, 10, 41),
}
sample1 = sample.Sample( sample1 = sample.Sample(
'instance', 'instance',
'cumulative', 'cumulative',
@ -71,7 +89,7 @@ class TestListResources(FunctionalTest,
'user-id', 'user-id',
'project-id', 'project-id',
'resource-id', 'resource-id',
timestamp=datetime.datetime(2012, 7, 2, 10, 40), timestamp=timestamps['resource-id'],
resource_metadata={'display_name': 'test-server', resource_metadata={'display_name': 'test-server',
'tag': 'self.sample', 'tag': 'self.sample',
}, },
@ -91,7 +109,7 @@ class TestListResources(FunctionalTest,
'user-id', 'user-id',
'project-id', 'project-id',
'resource-id-alternate', 'resource-id-alternate',
timestamp=datetime.datetime(2012, 7, 2, 10, 41), timestamp=timestamps['resource-id-alternate'],
resource_metadata={'display_name': 'test-server', resource_metadata={'display_name': 'test-server',
'tag': 'self.sample2', 'tag': 'self.sample2',
}, },
@ -105,6 +123,40 @@ class TestListResources(FunctionalTest,
data = self.get_json('/resources') data = self.get_json('/resources')
self.assertEqual(2, len(data)) self.assertEqual(2, len(data))
for res in data:
timestamp = timestamps.get(res['resource_id'])
self._verify_sample_timestamps(res, timestamp, timestamp)
def test_instance_multiple_samples(self):
timestamps = [
datetime.datetime(2012, 7, 2, 10, 40),
datetime.datetime(2012, 7, 2, 10, 41),
datetime.datetime(2012, 7, 2, 10, 42),
]
for timestamp in timestamps:
datapoint = sample.Sample(
'instance',
'cumulative',
'',
1,
'user-id',
'project-id',
'resource-id',
timestamp=timestamp,
resource_metadata={'display_name': 'test-server',
'tag': 'self.sample',
},
source='test',
)
msg = rpc.meter_message_from_counter(
datapoint,
self.CONF.publisher_rpc.metering_secret,
)
self.conn.record_metering_data(msg)
data = self.get_json('/resources')
self.assertEqual(1, len(data))
self._verify_sample_timestamps(data[0], timestamps[0], timestamps[-1])
def test_instances_one(self): def test_instances_one(self):
sample1 = sample.Sample( sample1 = sample.Sample(