Removes direct access of timeutils.override_time
The attribute "override_time" is a private attribute which should only be set through "timeutils.set_time_override()". There are a number of places in Ceilometer which the override_time attribute is set directly and such behavior should be prohibited. However the "set_time_override()" function along with "override_time" in timeutils is planned to be removed, therefore we no longer need to use it. Change-Id: I6f3a5e0bb4e4c448f369dd5ead0e1525fd2262e6 Related-Bug: #1266962
This commit is contained in:
parent
e587980bf5
commit
5722c168f8
|
@ -203,8 +203,9 @@ class Sample(Base):
|
|||
resource_id = Column(String(255), ForeignKey('resource.id'))
|
||||
resource_metadata = Column(JSONEncodedDict())
|
||||
volume = Column(Float(53))
|
||||
timestamp = Column(PreciseTimestamp(), default=timeutils.utcnow)
|
||||
recorded_at = Column(PreciseTimestamp(), default=timeutils.utcnow)
|
||||
timestamp = Column(PreciseTimestamp(), default=lambda: timeutils.utcnow())
|
||||
recorded_at = Column(PreciseTimestamp(),
|
||||
default=lambda: timeutils.utcnow())
|
||||
message_signature = Column(String(1000))
|
||||
message_id = Column(String(1000))
|
||||
sources = relationship("Source", secondary=lambda: sourceassoc)
|
||||
|
@ -278,13 +279,14 @@ class Alarm(Base):
|
|||
name = Column(Text)
|
||||
type = Column(String(50))
|
||||
description = Column(Text)
|
||||
timestamp = Column(PreciseTimestamp, default=timeutils.utcnow)
|
||||
timestamp = Column(PreciseTimestamp, default=lambda: timeutils.utcnow())
|
||||
|
||||
user_id = Column(String(255))
|
||||
project_id = Column(String(255))
|
||||
|
||||
state = Column(String(255))
|
||||
state_timestamp = Column(PreciseTimestamp, default=timeutils.utcnow)
|
||||
state_timestamp = Column(PreciseTimestamp,
|
||||
default=lambda: timeutils.utcnow())
|
||||
|
||||
ok_actions = Column(JSONEncodedDict)
|
||||
alarm_actions = Column(JSONEncodedDict)
|
||||
|
@ -308,7 +310,7 @@ class AlarmChange(Base):
|
|||
user_id = Column(String(255), ForeignKey('user.id'))
|
||||
type = Column(String(20))
|
||||
detail = Column(Text)
|
||||
timestamp = Column(PreciseTimestamp, default=timeutils.utcnow)
|
||||
timestamp = Column(PreciseTimestamp, default=lambda: timeutils.utcnow())
|
||||
|
||||
|
||||
class EventType(Base):
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
import mock
|
||||
|
||||
from ceilometer.openstack.common import test
|
||||
from ceilometer.openstack.common import timeutils
|
||||
|
||||
|
||||
class TestEvaluatorBase(test.BaseTestCase):
|
||||
|
@ -32,10 +31,6 @@ class TestEvaluatorBase(test.BaseTestCase):
|
|||
self.evaluator = self.EVALUATOR(self.notifier)
|
||||
self.prepare_alarms()
|
||||
|
||||
def tearDown(self):
|
||||
super(TestEvaluatorBase, self).tearDown()
|
||||
timeutils.utcnow.override_time = None
|
||||
|
||||
@staticmethod
|
||||
def prepare_alarms(self):
|
||||
self.alarms = []
|
||||
|
|
|
@ -49,7 +49,8 @@ class TestEvaluatorBaseClass(test.BaseTestCase):
|
|||
mock.MagicMock(), mock.MagicMock())
|
||||
self.assertTrue(self.called)
|
||||
|
||||
def test_base_time_constraints(self):
|
||||
@mock.patch.object(timeutils, 'utcnow')
|
||||
def test_base_time_constraints(self, mock_utcnow):
|
||||
alarm = mock.MagicMock()
|
||||
alarm.time_constraints = [
|
||||
{'name': 'test',
|
||||
|
@ -64,16 +65,17 @@ class TestEvaluatorBaseClass(test.BaseTestCase):
|
|||
'timezone': ''},
|
||||
]
|
||||
cls = evaluator.Evaluator
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 1, 12, 0, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 1, 12, 0, 0)
|
||||
self.assertTrue(cls.within_time_constraint(alarm))
|
||||
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 2, 1, 0, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 2, 1, 0, 0)
|
||||
self.assertTrue(cls.within_time_constraint(alarm))
|
||||
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 2, 5, 0, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 2, 5, 0, 0)
|
||||
self.assertFalse(cls.within_time_constraint(alarm))
|
||||
|
||||
def test_base_time_constraints_complex(self):
|
||||
@mock.patch.object(timeutils, 'utcnow')
|
||||
def test_base_time_constraints_complex(self, mock_utcnow):
|
||||
alarm = mock.MagicMock()
|
||||
alarm.time_constraints = [
|
||||
{'name': 'test',
|
||||
|
@ -87,38 +89,39 @@ class TestEvaluatorBaseClass(test.BaseTestCase):
|
|||
cls = evaluator.Evaluator
|
||||
|
||||
# test minutes inside
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 3, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 5, 3, 3, 0)
|
||||
self.assertTrue(cls.within_time_constraint(alarm))
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 31, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 5, 3, 31, 0)
|
||||
self.assertTrue(cls.within_time_constraint(alarm))
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 57, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 5, 3, 57, 0)
|
||||
self.assertTrue(cls.within_time_constraint(alarm))
|
||||
|
||||
# test minutes outside
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 2, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 5, 3, 2, 0)
|
||||
self.assertFalse(cls.within_time_constraint(alarm))
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 4, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 5, 3, 4, 0)
|
||||
self.assertFalse(cls.within_time_constraint(alarm))
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 58, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 5, 3, 58, 0)
|
||||
self.assertFalse(cls.within_time_constraint(alarm))
|
||||
|
||||
# test hours inside
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 5, 3, 31, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 5, 3, 31, 0)
|
||||
self.assertTrue(cls.within_time_constraint(alarm))
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 5, 5, 31, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 5, 5, 31, 0)
|
||||
self.assertTrue(cls.within_time_constraint(alarm))
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 5, 11, 31, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 5, 11, 31, 0)
|
||||
self.assertTrue(cls.within_time_constraint(alarm))
|
||||
|
||||
# test hours outside
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 5, 1, 31, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 5, 1, 31, 0)
|
||||
self.assertFalse(cls.within_time_constraint(alarm))
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 5, 4, 31, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 5, 4, 31, 0)
|
||||
self.assertFalse(cls.within_time_constraint(alarm))
|
||||
timeutils.set_time_override(datetime.datetime(2014, 1, 5, 12, 31, 0))
|
||||
mock_utcnow.return_value = datetime.datetime(2014, 1, 5, 12, 31, 0)
|
||||
self.assertFalse(cls.within_time_constraint(alarm))
|
||||
|
||||
def test_base_time_constraints_timezone(self):
|
||||
@mock.patch.object(timeutils, 'utcnow')
|
||||
def test_base_time_constraints_timezone(self, mock_utcnow):
|
||||
alarm = mock.MagicMock()
|
||||
alarm.time_constraints = [
|
||||
{'name': 'test',
|
||||
|
@ -132,8 +135,8 @@ class TestEvaluatorBaseClass(test.BaseTestCase):
|
|||
tzinfo=pytz.timezone('Europe/Ljubljana'))
|
||||
dt_us = datetime.datetime(2014, 1, 1, 12, 0, 0,
|
||||
tzinfo=pytz.timezone('US/Eastern'))
|
||||
timeutils.set_time_override(dt_eu.astimezone(pytz.UTC))
|
||||
mock_utcnow.return_value = dt_eu.astimezone(pytz.UTC)
|
||||
self.assertTrue(cls.within_time_constraint(alarm))
|
||||
|
||||
timeutils.set_time_override(dt_us.astimezone(pytz.UTC))
|
||||
mock_utcnow.return_value = dt_us.astimezone(pytz.UTC)
|
||||
self.assertFalse(cls.within_time_constraint(alarm))
|
||||
|
|
|
@ -310,7 +310,8 @@ class TestEvaluate(base.TestEvaluatorBase):
|
|||
|
||||
self.assertEqual(expected, self.notifier.notify.call_args_list)
|
||||
|
||||
def test_state_change_inside_time_constraint(self):
|
||||
@mock.patch.object(timeutils, 'utcnow')
|
||||
def test_state_change_inside_time_constraint(self, mock_utcnow):
|
||||
self._set_all_alarms('insufficient data')
|
||||
self.alarms[0].time_constraints = [
|
||||
{'name': 'test',
|
||||
|
@ -322,9 +323,9 @@ class TestEvaluate(base.TestEvaluatorBase):
|
|||
self.alarms[1].time_constraints = self.alarms[0].time_constraints
|
||||
dt = datetime.datetime(2014, 1, 1, 12, 0, 0,
|
||||
tzinfo=pytz.timezone('Europe/Ljubljana'))
|
||||
mock_utcnow.return_value = dt.astimezone(pytz.UTC)
|
||||
with mock.patch('ceilometerclient.client.get_client',
|
||||
return_value=self.api_client):
|
||||
timeutils.set_time_override(dt.astimezone(pytz.UTC))
|
||||
self.api_client.alarms.get.side_effect = [
|
||||
self._get_alarm('ok'),
|
||||
self._get_alarm('ok'),
|
||||
|
@ -348,7 +349,8 @@ class TestEvaluate(base.TestEvaluatorBase):
|
|||
in zip(self.alarms, reasons, reason_datas)]
|
||||
self.assertEqual(expected, self.notifier.notify.call_args_list)
|
||||
|
||||
def test_no_state_change_outside_time_constraint(self):
|
||||
@mock.patch.object(timeutils, 'utcnow')
|
||||
def test_no_state_change_outside_time_constraint(self, mock_utcnow):
|
||||
self._set_all_alarms('insufficient data')
|
||||
self.alarms[0].time_constraints = [
|
||||
{'name': 'test',
|
||||
|
@ -360,9 +362,9 @@ class TestEvaluate(base.TestEvaluatorBase):
|
|||
self.alarms[1].time_constraints = self.alarms[0].time_constraints
|
||||
dt = datetime.datetime(2014, 1, 1, 15, 0, 0,
|
||||
tzinfo=pytz.timezone('Europe/Ljubljana'))
|
||||
mock_utcnow.return_value = dt.astimezone(pytz.UTC)
|
||||
with mock.patch('ceilometerclient.client.get_client',
|
||||
return_value=self.api_client):
|
||||
timeutils.set_time_override(dt.astimezone(pytz.UTC))
|
||||
self.api_client.alarms.get.side_effect = [
|
||||
self._get_alarm('ok'),
|
||||
self._get_alarm('ok'),
|
||||
|
|
|
@ -322,16 +322,17 @@ class TestEvaluate(base.TestEvaluatorBase):
|
|||
alarm = self.alarms[0]
|
||||
if exclude_outliers is not None:
|
||||
alarm.rule['exclude_outliers'] = exclude_outliers
|
||||
timeutils.utcnow.override_time = datetime.datetime(2012, 7, 2, 10, 45)
|
||||
constraint = self.evaluator._bound_duration(alarm, [])
|
||||
self.assertEqual([
|
||||
{'field': 'timestamp',
|
||||
'op': 'le',
|
||||
'value': timeutils.utcnow().isoformat()},
|
||||
{'field': 'timestamp',
|
||||
'op': 'ge',
|
||||
'value': start},
|
||||
], constraint)
|
||||
with mock.patch.object(timeutils, 'utcnow') as mock_utcnow:
|
||||
mock_utcnow.return_value = datetime.datetime(2012, 7, 2, 10, 45)
|
||||
constraint = self.evaluator._bound_duration(alarm, [])
|
||||
self.assertEqual([
|
||||
{'field': 'timestamp',
|
||||
'op': 'le',
|
||||
'value': timeutils.utcnow().isoformat()},
|
||||
{'field': 'timestamp',
|
||||
'op': 'ge',
|
||||
'value': start},
|
||||
], constraint)
|
||||
|
||||
def test_bound_duration_outlier_exclusion_defaulted(self):
|
||||
self._do_test_bound_duration('2012-07-02T10:39:00')
|
||||
|
@ -448,7 +449,8 @@ class TestEvaluate(base.TestEvaluatorBase):
|
|||
def test_simple_alarm_no_clear_without_outlier_exclusion(self):
|
||||
self. _do_test_simple_alarm_clear_outlier_exclusion(False)
|
||||
|
||||
def test_state_change_inside_time_constraint(self):
|
||||
@mock.patch.object(timeutils, 'utcnow')
|
||||
def test_state_change_inside_time_constraint(self, mock_utcnow):
|
||||
self._set_all_alarms('ok')
|
||||
self.alarms[0].time_constraints = [
|
||||
{'name': 'test',
|
||||
|
@ -460,9 +462,9 @@ class TestEvaluate(base.TestEvaluatorBase):
|
|||
self.alarms[1].time_constraints = self.alarms[0].time_constraints
|
||||
dt = datetime.datetime(2014, 1, 1, 12, 0, 0,
|
||||
tzinfo=pytz.timezone('Europe/Ljubljana'))
|
||||
mock_utcnow.return_value = dt.astimezone(pytz.UTC)
|
||||
with mock.patch('ceilometerclient.client.get_client',
|
||||
return_value=self.api_client):
|
||||
timeutils.set_time_override(dt.astimezone(pytz.UTC))
|
||||
# the following part based on test_simple_insufficient
|
||||
self.api_client.statistics.list.return_value = []
|
||||
self._evaluate_all_alarms()
|
||||
|
@ -485,7 +487,8 @@ class TestEvaluate(base.TestEvaluatorBase):
|
|||
for alarm in self.alarms]
|
||||
self.assertEqual(expected, self.notifier.notify.call_args_list)
|
||||
|
||||
def test_no_state_change_outside_time_constraint(self):
|
||||
@mock.patch.object(timeutils, 'utcnow')
|
||||
def test_no_state_change_outside_time_constraint(self, mock_utcnow):
|
||||
self._set_all_alarms('ok')
|
||||
self.alarms[0].time_constraints = [
|
||||
{'name': 'test',
|
||||
|
@ -497,9 +500,9 @@ class TestEvaluate(base.TestEvaluatorBase):
|
|||
self.alarms[1].time_constraints = self.alarms[0].time_constraints
|
||||
dt = datetime.datetime(2014, 1, 1, 15, 0, 0,
|
||||
tzinfo=pytz.timezone('Europe/Ljubljana'))
|
||||
mock_utcnow.return_value = dt.astimezone(pytz.UTC)
|
||||
with mock.patch('ceilometerclient.client.get_client',
|
||||
return_value=self.api_client):
|
||||
timeutils.set_time_override(dt.astimezone(pytz.UTC))
|
||||
self.api_client.statistics.list.return_value = []
|
||||
self._evaluate_all_alarms()
|
||||
self._assert_all_alarms('ok')
|
||||
|
|
|
@ -42,7 +42,10 @@ class TestCoordinate(test.BaseTestCase):
|
|||
group='alarm')
|
||||
self.api_client = mock.Mock()
|
||||
self.override_start = datetime.datetime(2012, 7, 2, 10, 45)
|
||||
timeutils.utcnow.override_time = self.override_start
|
||||
patcher = mock.patch.object(timeutils, 'utcnow')
|
||||
self.addCleanup(patcher.stop)
|
||||
self.mock_utcnow = patcher.start()
|
||||
self.mock_utcnow.return_value = self.override_start
|
||||
self.partition_coordinator = coordination.PartitionCoordinator()
|
||||
self.partition_coordinator.coordination_rpc = mock.Mock()
|
||||
#add extra logger to check exception conditions and logged content
|
||||
|
@ -52,7 +55,6 @@ class TestCoordinate(test.BaseTestCase):
|
|||
|
||||
def tearDown(self):
|
||||
super(TestCoordinate, self).tearDown()
|
||||
timeutils.utcnow.override_time = None
|
||||
# clean up the logger
|
||||
coordination.LOG.logger.removeHandler(self.str_handler)
|
||||
self.output.close()
|
||||
|
@ -112,7 +114,7 @@ class TestCoordinate(test.BaseTestCase):
|
|||
|
||||
def _advance_time(self, factor):
|
||||
delta = datetime.timedelta(seconds=self.test_interval * factor)
|
||||
timeutils.utcnow.override_time += delta
|
||||
self.mock_utcnow.return_value = timeutils.utcnow() + delta
|
||||
|
||||
def _younger_by(self, offset):
|
||||
return self.partition_coordinator.this.priority + offset
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
import datetime
|
||||
import logging
|
||||
|
||||
import mock
|
||||
import webtest.app
|
||||
|
||||
from ceilometer.openstack.common import timeutils
|
||||
|
@ -38,7 +39,10 @@ class TestListEvents(FunctionalTest,
|
|||
|
||||
def setUp(self):
|
||||
super(TestListEvents, self).setUp()
|
||||
timeutils.utcnow.override_time = datetime.datetime(2014, 2, 11, 16, 42)
|
||||
patcher = mock.patch.object(timeutils, 'utcnow')
|
||||
self.addCleanup(patcher.stop)
|
||||
self.mock_utcnow = patcher.start()
|
||||
self.mock_utcnow.return_value = datetime.datetime(2014, 2, 11, 16, 42)
|
||||
self.sample1 = sample.Sample(
|
||||
'instance',
|
||||
'cumulative',
|
||||
|
@ -85,8 +89,7 @@ class TestListEvents(FunctionalTest,
|
|||
data = self.get_json('/meters/instance')
|
||||
self.assertEqual(2, len(data))
|
||||
for s in data:
|
||||
self.assertEqual(timeutils.utcnow.override_time.isoformat(),
|
||||
s['recorded_at'])
|
||||
self.assertEqual(timeutils.utcnow().isoformat(), s['recorded_at'])
|
||||
|
||||
def test_all_trailing_slash(self):
|
||||
data = self.get_json('/meters/instance/')
|
||||
|
|
|
@ -176,8 +176,9 @@ class RelationshipTest(scenarios.DBTestBase):
|
|||
# don't want to automatically inherit all the Meter setup.
|
||||
database_connection = 'sqlite://'
|
||||
|
||||
def test_clear_metering_data_meta_tables(self):
|
||||
timeutils.utcnow.override_time = datetime.datetime(2012, 7, 2, 10, 45)
|
||||
@patch.object(timeutils, 'utcnow')
|
||||
def test_clear_metering_data_meta_tables(self, mock_utcnow):
|
||||
mock_utcnow.return_value = datetime.datetime(2012, 7, 2, 10, 45)
|
||||
self.conn.clear_expired_metering_data(3 * 60)
|
||||
|
||||
session = self.conn._engine_facade.get_session()
|
||||
|
@ -190,8 +191,9 @@ class RelationshipTest(scenarios.DBTestBase):
|
|||
.group_by(sql_models.Sample.id)
|
||||
)).count())
|
||||
|
||||
def test_clear_metering_data_associations(self):
|
||||
timeutils.utcnow.override_time = datetime.datetime(2012, 7, 2, 10, 45)
|
||||
@patch.object(timeutils, 'utcnow')
|
||||
def test_clear_metering_data_associations(self, mock_utcnow):
|
||||
mock_utcnow.return_value = datetime.datetime(2012, 7, 2, 10, 45)
|
||||
self.conn.clear_expired_metering_data(3 * 60)
|
||||
|
||||
session = self.conn._engine_facade.get_session()
|
||||
|
|
|
@ -22,6 +22,8 @@
|
|||
|
||||
import datetime
|
||||
|
||||
import mock
|
||||
|
||||
from ceilometer.openstack.common import timeutils
|
||||
from ceilometer.publisher import utils
|
||||
from ceilometer import sample
|
||||
|
@ -56,8 +58,10 @@ class DBTestBase(tests_db.TestBase):
|
|||
|
||||
def setUp(self):
|
||||
super(DBTestBase, self).setUp()
|
||||
timeutils.set_time_override(
|
||||
datetime.datetime(2015, 7, 2, 10, 39))
|
||||
patcher = mock.patch.object(timeutils, 'utcnow')
|
||||
self.addCleanup(patcher.stop)
|
||||
self.mock_utcnow = patcher.start()
|
||||
self.mock_utcnow.return_value = datetime.datetime(2015, 7, 2, 10, 39)
|
||||
self.prepare_data()
|
||||
|
||||
def prepare_data(self):
|
||||
|
@ -654,7 +658,7 @@ class RawSampleTest(DBTestBase,
|
|||
if self.CONF.database.connection.startswith('mongodb://'):
|
||||
return
|
||||
|
||||
timeutils.utcnow.override_time = datetime.datetime(2012, 7, 2, 10, 45)
|
||||
self.mock_utcnow.return_value = datetime.datetime(2012, 7, 2, 10, 45)
|
||||
self.conn.clear_expired_metering_data(3 * 60)
|
||||
f = storage.SampleFilter(meter='instance')
|
||||
results = list(self.conn.get_samples(f))
|
||||
|
@ -672,7 +676,7 @@ class RawSampleTest(DBTestBase,
|
|||
if self.CONF.database.connection.startswith('mongodb://'):
|
||||
return
|
||||
|
||||
timeutils.utcnow.override_time = datetime.datetime(2010, 7, 2, 10, 45)
|
||||
self.mock_utcnow.return_value = datetime.datetime(2010, 7, 2, 10, 45)
|
||||
self.conn.clear_expired_metering_data(3 * 60)
|
||||
f = storage.SampleFilter(meter='instance')
|
||||
results = list(self.conn.get_samples(f))
|
||||
|
@ -718,7 +722,7 @@ class RawSampleTest(DBTestBase,
|
|||
)
|
||||
|
||||
self.conn.create_alarm(alarm)
|
||||
timeutils.utcnow.override_time = datetime.datetime(2012, 7, 2, 10, 45)
|
||||
self.mock_utcnow.return_value = datetime.datetime(2012, 7, 2, 10, 45)
|
||||
self.conn.clear_expired_metering_data(5)
|
||||
f = storage.SampleFilter(meter='instance')
|
||||
results = list(self.conn.get_samples(f))
|
||||
|
|
Loading…
Reference in New Issue