Replace datetime of time_constraints by aware object
Error occurs in alarm evaluation when timezone in time_constraints is specified, because astimezone() cannot be applied to a native datetime. Making datetime to non-native (aware object) will solve the problem. Change-Id: Ic0de4a5b32865bbe330c2936f14adf59168eefe5 Closes-bug: #1342676
This commit is contained in:
parent
5ad1ca6efa
commit
667e464312
@ -93,7 +93,7 @@ class Evaluator(object):
|
|||||||
if not alarm.time_constraints:
|
if not alarm.time_constraints:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
now_utc = timeutils.utcnow()
|
now_utc = timeutils.utcnow().replace(tzinfo=pytz.utc)
|
||||||
for tc in alarm.time_constraints:
|
for tc in alarm.time_constraints:
|
||||||
tz = pytz.timezone(tc['timezone']) if tc['timezone'] else None
|
tz = pytz.timezone(tc['timezone']) if tc['timezone'] else None
|
||||||
now_tz = now_utc.astimezone(tz) if tz else now_utc
|
now_tz = now_utc.astimezone(tz) if tz else now_utc
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
"""
|
"""
|
||||||
import datetime
|
import datetime
|
||||||
import mock
|
import mock
|
||||||
import pytz
|
|
||||||
|
|
||||||
from ceilometer.alarm import evaluator
|
from ceilometer.alarm import evaluator
|
||||||
from ceilometer.openstack.common import test
|
from ceilometer.openstack.common import test
|
||||||
@ -122,6 +121,9 @@ class TestEvaluatorBaseClass(test.BaseTestCase):
|
|||||||
@mock.patch.object(timeutils, 'utcnow')
|
@mock.patch.object(timeutils, 'utcnow')
|
||||||
def test_base_time_constraints_timezone(self, mock_utcnow):
|
def test_base_time_constraints_timezone(self, mock_utcnow):
|
||||||
alarm = mock.MagicMock()
|
alarm = mock.MagicMock()
|
||||||
|
cls = evaluator.Evaluator
|
||||||
|
mock_utcnow.return_value = datetime.datetime(2014, 1, 1, 11, 0, 0)
|
||||||
|
|
||||||
alarm.time_constraints = [
|
alarm.time_constraints = [
|
||||||
{'name': 'test',
|
{'name': 'test',
|
||||||
'description': 'test',
|
'description': 'test',
|
||||||
@ -129,13 +131,13 @@ class TestEvaluatorBaseClass(test.BaseTestCase):
|
|||||||
'duration': 10800, # 3 hours
|
'duration': 10800, # 3 hours
|
||||||
'timezone': 'Europe/Ljubljana'}
|
'timezone': 'Europe/Ljubljana'}
|
||||||
]
|
]
|
||||||
cls = evaluator.Evaluator
|
|
||||||
dt_eu = datetime.datetime(2014, 1, 1, 12, 0, 0,
|
|
||||||
tzinfo=pytz.timezone('Europe/Ljubljana'))
|
|
||||||
dt_us = datetime.datetime(2014, 1, 1, 12, 0, 0,
|
|
||||||
tzinfo=pytz.timezone('US/Eastern'))
|
|
||||||
mock_utcnow.return_value = dt_eu.astimezone(pytz.UTC)
|
|
||||||
self.assertTrue(cls.within_time_constraint(alarm))
|
self.assertTrue(cls.within_time_constraint(alarm))
|
||||||
|
|
||||||
mock_utcnow.return_value = dt_us.astimezone(pytz.UTC)
|
alarm.time_constraints = [
|
||||||
|
{'name': 'test2',
|
||||||
|
'description': 'test2',
|
||||||
|
'start': '0 11 * * *', # daily at 11:00
|
||||||
|
'duration': 10800, # 3 hours
|
||||||
|
'timezone': 'US/Eastern'}
|
||||||
|
]
|
||||||
self.assertFalse(cls.within_time_constraint(alarm))
|
self.assertFalse(cls.within_time_constraint(alarm))
|
||||||
|
Loading…
Reference in New Issue
Block a user