Use timeutils.utcnow in alarm threshold evaluation

This change use timeutils.utcnow() in alarm threshold evaluation
instead of datetime.utcnow() to allow mock the the now datetime

It also add a test to check the behavior of bound_duration method.

Change-Id: I970e63da33e17018ed4fef31d0082803f6e0fb29
This commit is contained in:
Mehdi Abaakouk 2013-08-23 09:32:29 +02:00
parent c0acc9797e
commit 3deb7397ba
2 changed files with 20 additions and 1 deletions

View File

@ -22,6 +22,7 @@ import operator
from oslo.config import cfg
from ceilometer.openstack.common import log
from ceilometer.openstack.common import timeutils
from ceilometerclient import client as ceiloclient
from ceilometer.openstack.common.gettextutils import _
@ -90,7 +91,7 @@ class Evaluator(object):
@classmethod
def _bound_duration(cls, alarm, constraints):
"""Bound the duration of the statistics query."""
now = datetime.datetime.utcnow()
now = timeutils.utcnow()
window = (alarm.period *
(alarm.evaluation_periods + cls.look_back))
start = now - datetime.timedelta(seconds=window)

View File

@ -17,10 +17,12 @@
# under the License.
"""Tests for ceilometer/alarm/threshold_evaluation.py
"""
import datetime
import mock
import uuid
from ceilometer.alarm import threshold_evaluation
from ceilometer.openstack.common import timeutils
from ceilometer.storage import models
from ceilometer.tests import base
from ceilometerclient import exc
@ -61,6 +63,10 @@ class TestEvaluate(base.TestCase):
self.evaluator = threshold_evaluation.Evaluator(self.notifier)
self.evaluator.assign_alarms(self.alarms)
def tearDown(self):
super(TestEvaluate, self).tearDown()
timeutils.utcnow.override_time = None
@staticmethod
def _get_stat(attr, value):
return statistics.Statistics(None, {attr: value})
@ -273,3 +279,15 @@ class TestEvaluate(base.TestCase):
expected = [mock.call(alarm, 'insufficient data', reason)
for alarm, reason in zip(self.alarms, reasons)]
self.assertEqual(self.notifier.notify.call_args_list, expected)
def test_bound_duration(self):
timeutils.utcnow.override_time = datetime.datetime(2012, 7, 2, 10, 45)
constraint = self.evaluator._bound_duration(self.alarms[0], [])
self.assertEqual(constraint, [
{'field': 'timestamp',
'op': 'le',
'value': timeutils.utcnow().isoformat()},
{'field': 'timestamp',
'op': 'ge',
'value': '2012-07-02T10:39:00'},
])