From 667e4643122761b899a0824662a644ba7253cef8 Mon Sep 17 00:00:00 2001 From: Rikimaru Honjo Date: Thu, 17 Jul 2014 19:59:23 +0900 Subject: [PATCH] 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 --- ceilometer/alarm/evaluator/__init__.py | 2 +- ceilometer/tests/alarm/evaluator/test_base.py | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/ceilometer/alarm/evaluator/__init__.py b/ceilometer/alarm/evaluator/__init__.py index 03329b1d5..f09f478f3 100644 --- a/ceilometer/alarm/evaluator/__init__.py +++ b/ceilometer/alarm/evaluator/__init__.py @@ -93,7 +93,7 @@ class Evaluator(object): if not alarm.time_constraints: return True - now_utc = timeutils.utcnow() + now_utc = timeutils.utcnow().replace(tzinfo=pytz.utc) for tc in alarm.time_constraints: tz = pytz.timezone(tc['timezone']) if tc['timezone'] else None now_tz = now_utc.astimezone(tz) if tz else now_utc diff --git a/ceilometer/tests/alarm/evaluator/test_base.py b/ceilometer/tests/alarm/evaluator/test_base.py index 3a0b62b67..1aa6f24a2 100644 --- a/ceilometer/tests/alarm/evaluator/test_base.py +++ b/ceilometer/tests/alarm/evaluator/test_base.py @@ -18,7 +18,6 @@ """ import datetime import mock -import pytz from ceilometer.alarm import evaluator from ceilometer.openstack.common import test @@ -122,6 +121,9 @@ class TestEvaluatorBaseClass(test.BaseTestCase): @mock.patch.object(timeutils, 'utcnow') def test_base_time_constraints_timezone(self, mock_utcnow): alarm = mock.MagicMock() + cls = evaluator.Evaluator + mock_utcnow.return_value = datetime.datetime(2014, 1, 1, 11, 0, 0) + alarm.time_constraints = [ {'name': 'test', 'description': 'test', @@ -129,13 +131,13 @@ class TestEvaluatorBaseClass(test.BaseTestCase): 'duration': 10800, # 3 hours '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)) - 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))