Correct alarm timestamp field in unittest code
This patch corrects some code in unit test which relates to timestamp issue. timestamp and state_timestamp fields will always be set, but test code sometimes sets them to None, which will affect source code to consider the scenario timestamp is None, in fact, this would never happen in API call. This patch adds a type check for timestamp and state_timestamp in alarm.storage.models.Alarm, to prevent contributors to accidently set these two fields to None. Change-Id: I035b4f0f9c1ff7f4158c72acfddaf02e55891086
This commit is contained in:
parent
f856d3c915
commit
5b8c72c3ea
@ -17,6 +17,9 @@
|
||||
"""Model classes for use in the storage API.
|
||||
"""
|
||||
|
||||
import datetime
|
||||
|
||||
from ceilometer.openstack.common.gettextutils import _
|
||||
from ceilometer.storage import base
|
||||
|
||||
|
||||
@ -60,6 +63,11 @@ class Alarm(base.Model):
|
||||
timestamp, user_id, project_id, state, state_timestamp,
|
||||
ok_actions, alarm_actions, insufficient_data_actions,
|
||||
repeat_actions, rule, time_constraints):
|
||||
if not isinstance(timestamp, datetime.datetime):
|
||||
raise TypeError(_("timestamp should be datetime object"))
|
||||
if not isinstance(state_timestamp, datetime.datetime):
|
||||
raise TypeError(_("state_timestamp should be datetime object"))
|
||||
|
||||
base.Model.__init__(
|
||||
self,
|
||||
alarm_id=alarm_id,
|
||||
|
@ -14,8 +14,10 @@
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
"""Tests for ceilometer/alarm/threshold_evaluation.py
|
||||
|
||||
"""Tests for ceilometer/alarm/evaluator/combination.py
|
||||
"""
|
||||
|
||||
import datetime
|
||||
import uuid
|
||||
|
||||
@ -28,6 +30,7 @@ import pytz
|
||||
from ceilometer.alarm.evaluator import combination
|
||||
from ceilometer.alarm.storage import models
|
||||
from ceilometer.tests.alarm.evaluator import base
|
||||
from ceilometer.tests import constants
|
||||
|
||||
|
||||
class TestEvaluate(base.TestEvaluatorBase):
|
||||
@ -43,8 +46,8 @@ class TestEvaluate(base.TestEvaluatorBase):
|
||||
project_id='snafu',
|
||||
alarm_id=str(uuid.uuid4()),
|
||||
state='insufficient data',
|
||||
state_timestamp=None,
|
||||
timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
insufficient_data_actions=[],
|
||||
ok_actions=[],
|
||||
alarm_actions=[],
|
||||
@ -64,8 +67,8 @@ class TestEvaluate(base.TestEvaluatorBase):
|
||||
project_id='snafu',
|
||||
alarm_id=str(uuid.uuid4()),
|
||||
state='insufficient data',
|
||||
state_timestamp=None,
|
||||
timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
insufficient_data_actions=[],
|
||||
ok_actions=[],
|
||||
alarm_actions=[],
|
||||
|
@ -30,6 +30,7 @@ from six import moves
|
||||
from ceilometer.alarm.evaluator import threshold
|
||||
from ceilometer.alarm.storage import models
|
||||
from ceilometer.tests.alarm.evaluator import base
|
||||
from ceilometer.tests import constants
|
||||
|
||||
|
||||
class TestEvaluate(base.TestEvaluatorBase):
|
||||
@ -45,8 +46,8 @@ class TestEvaluate(base.TestEvaluatorBase):
|
||||
project_id='snafu',
|
||||
alarm_id=str(uuid.uuid4()),
|
||||
state='insufficient data',
|
||||
state_timestamp=None,
|
||||
timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
insufficient_data_actions=[],
|
||||
ok_actions=[],
|
||||
alarm_actions=[],
|
||||
@ -73,8 +74,8 @@ class TestEvaluate(base.TestEvaluatorBase):
|
||||
user_id='foobar',
|
||||
project_id='snafu',
|
||||
state='insufficient data',
|
||||
state_timestamp=None,
|
||||
timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
insufficient_data_actions=[],
|
||||
ok_actions=[],
|
||||
alarm_actions=[],
|
||||
|
@ -28,6 +28,7 @@ from six import moves
|
||||
from ceilometer.alarm.partition import coordination
|
||||
from ceilometer.alarm.storage import models
|
||||
from ceilometer.tests import base as tests_base
|
||||
from ceilometer.tests import constants
|
||||
|
||||
|
||||
class MockLoggingHandler(logging.Handler):
|
||||
@ -116,8 +117,8 @@ class TestCoordinate(tests_base.BaseTestCase):
|
||||
description='',
|
||||
repeat_actions=False,
|
||||
state='insufficient data',
|
||||
state_timestamp=None,
|
||||
timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
ok_actions=[],
|
||||
alarm_actions=[],
|
||||
insufficient_data_actions=[],
|
||||
|
@ -30,6 +30,7 @@ from six import moves
|
||||
from ceilometer.alarm.storage import models
|
||||
from ceilometer import messaging
|
||||
from ceilometer.tests.api import v2
|
||||
from ceilometer.tests import constants
|
||||
from ceilometer.tests import db as tests_db
|
||||
|
||||
|
||||
@ -55,8 +56,8 @@ class TestAlarms(v2.FunctionalTest,
|
||||
alarm_id='a',
|
||||
description='a',
|
||||
state='insufficient data',
|
||||
state_timestamp=None,
|
||||
timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
ok_actions=[],
|
||||
insufficient_data_actions=[],
|
||||
alarm_actions=[],
|
||||
@ -83,8 +84,8 @@ class TestAlarms(v2.FunctionalTest,
|
||||
alarm_id='b',
|
||||
description='b',
|
||||
state='insufficient data',
|
||||
state_timestamp=None,
|
||||
timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
ok_actions=[],
|
||||
insufficient_data_actions=[],
|
||||
alarm_actions=[],
|
||||
@ -109,8 +110,8 @@ class TestAlarms(v2.FunctionalTest,
|
||||
alarm_id='c',
|
||||
description='c',
|
||||
state='insufficient data',
|
||||
state_timestamp=None,
|
||||
timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
ok_actions=[],
|
||||
insufficient_data_actions=[],
|
||||
alarm_actions=[],
|
||||
@ -135,8 +136,8 @@ class TestAlarms(v2.FunctionalTest,
|
||||
alarm_id='d',
|
||||
description='d',
|
||||
state='insufficient data',
|
||||
state_timestamp=None,
|
||||
timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
ok_actions=[],
|
||||
insufficient_data_actions=[],
|
||||
alarm_actions=[],
|
||||
@ -212,8 +213,8 @@ class TestAlarms(v2.FunctionalTest,
|
||||
alarm_id='d',
|
||||
description='d',
|
||||
state='ok',
|
||||
state_timestamp=None,
|
||||
timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
ok_actions=[],
|
||||
insufficient_data_actions=[],
|
||||
alarm_actions=[],
|
||||
@ -262,8 +263,8 @@ class TestAlarms(v2.FunctionalTest,
|
||||
alarm_id='d',
|
||||
description='d',
|
||||
state='insufficient data',
|
||||
state_timestamp=None,
|
||||
timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
ok_actions=[],
|
||||
insufficient_data_actions=[],
|
||||
alarm_actions=[],
|
||||
|
17
ceilometer/tests/constants.py
Normal file
17
ceilometer/tests/constants.py
Normal file
@ -0,0 +1,17 @@
|
||||
# Copyright 2014 Huawei Technologies Co., Ltd.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import datetime
|
||||
|
||||
MIN_DATETIME = datetime.datetime(datetime.MINYEAR, 1, 1)
|
@ -112,3 +112,47 @@ class TestTraitModel(testbase.BaseTestCase):
|
||||
event_models.Trait.TEXT_TYPE, 10)
|
||||
self.assertEqual("10", v)
|
||||
self.assertIsInstance(v, str)
|
||||
|
||||
|
||||
class TestClassModel(testbase.BaseTestCase):
|
||||
|
||||
ALARM = {
|
||||
'alarm_id': '503490ea-ee9e-40d6-9cad-93a71583f4b2',
|
||||
'enabled': True,
|
||||
'type': 'threshold',
|
||||
'name': 'alarm-test',
|
||||
'description': 'alarm-test-description',
|
||||
'timestamp': None,
|
||||
'user_id': '5c76351f5fef4f6490d1048355094ca3',
|
||||
'project_id': 'd83ed14a457141fc8661b4dcb3fd883d',
|
||||
'state': "insufficient data",
|
||||
'state_timestamp': None,
|
||||
'ok_actions': [],
|
||||
'alarm_actions': [],
|
||||
'insufficient_data_actions': [],
|
||||
'repeat_actions': False,
|
||||
'time_constraints': [],
|
||||
'rule': {
|
||||
'comparison_operator': 'lt',
|
||||
'threshold': 34,
|
||||
'statistic': 'max',
|
||||
'evaluation_periods': 1,
|
||||
'period': 60,
|
||||
'meter_name': 'cpu_util',
|
||||
'query': []
|
||||
}
|
||||
}
|
||||
|
||||
def test_timestamp_cannot_be_none(self):
|
||||
self.ALARM['timestamp'] = None
|
||||
self.ALARM['state_timestamp'] = datetime.datetime.utcnow()
|
||||
self.assertRaises(TypeError,
|
||||
alarm_models.Alarm.__init__,
|
||||
**self.ALARM)
|
||||
|
||||
def test_state_timestamp_cannot_be_none(self):
|
||||
self.ALARM['timestamp'] = datetime.datetime.utcnow()
|
||||
self.ALARM['state_timestamp'] = None
|
||||
self.assertRaises(TypeError,
|
||||
alarm_models.Alarm.__init__,
|
||||
**self.ALARM)
|
||||
|
@ -19,6 +19,7 @@ import mock
|
||||
|
||||
from ceilometer.publisher import utils
|
||||
from ceilometer import sample
|
||||
from ceilometer.tests import constants
|
||||
from ceilometer.tests import db as tests_db
|
||||
from ceilometer.tests.storage import test_storage_scenarios
|
||||
|
||||
@ -96,7 +97,7 @@ class CompatibilityTest(test_storage_scenarios.DBTestBase,
|
||||
enabled=True,
|
||||
name='old-alert',
|
||||
description='old-alert',
|
||||
timestamp=None,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
meter_name='cpu',
|
||||
user_id='me',
|
||||
project_id='and-da-boys',
|
||||
@ -106,7 +107,7 @@ class CompatibilityTest(test_storage_scenarios.DBTestBase,
|
||||
evaluation_periods=1,
|
||||
period=60,
|
||||
state="insufficient data",
|
||||
state_timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
ok_actions=[],
|
||||
alarm_actions=['http://nowhere/alarms'],
|
||||
insufficient_data_actions=[],
|
||||
|
@ -34,6 +34,7 @@ from ceilometer.publisher import utils
|
||||
from ceilometer import sample
|
||||
from ceilometer import storage
|
||||
from ceilometer.storage import base
|
||||
from ceilometer.tests import constants
|
||||
from ceilometer.tests import db as tests_db
|
||||
|
||||
|
||||
@ -654,17 +655,16 @@ class RawSampleTest(DBTestBase,
|
||||
def test_clear_metering_data_with_alarms(self):
|
||||
# NOTE(jd) Override this test in MongoDB because our code doesn't clear
|
||||
# the collections, this is handled by MongoDB TTL feature.
|
||||
|
||||
alarm = alarm_models.Alarm(alarm_id='r3d',
|
||||
enabled=True,
|
||||
type='threshold',
|
||||
name='red-alert',
|
||||
description='my red-alert',
|
||||
timestamp=None,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
user_id='user-id',
|
||||
project_id='project-id',
|
||||
state="insufficient data",
|
||||
state_timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
ok_actions=[],
|
||||
alarm_actions=['http://nowhere/alarms'],
|
||||
insufficient_data_actions=[],
|
||||
@ -2311,11 +2311,11 @@ class AlarmTestBase(DBTestBase):
|
||||
type='threshold',
|
||||
name='red-alert',
|
||||
description='my red-alert',
|
||||
timestamp=None,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
user_id='me',
|
||||
project_id='and-da-boys',
|
||||
state="insufficient data",
|
||||
state_timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
ok_actions=[],
|
||||
alarm_actions=['http://nowhere/alarms'],
|
||||
insufficient_data_actions=[],
|
||||
@ -2339,11 +2339,11 @@ class AlarmTestBase(DBTestBase):
|
||||
type='threshold',
|
||||
name='orange-alert',
|
||||
description='a orange',
|
||||
timestamp=None,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
user_id='me',
|
||||
project_id='and-da-boys',
|
||||
state="insufficient data",
|
||||
state_timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
ok_actions=[],
|
||||
alarm_actions=['http://nowhere/alarms'],
|
||||
insufficient_data_actions=[],
|
||||
@ -2365,11 +2365,11 @@ class AlarmTestBase(DBTestBase):
|
||||
type='threshold',
|
||||
name='yellow-alert',
|
||||
description='yellow',
|
||||
timestamp=None,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
user_id='me',
|
||||
project_id='and-da-boys',
|
||||
state="insufficient data",
|
||||
state_timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
ok_actions=[],
|
||||
alarm_actions=['http://nowhere/alarms'],
|
||||
insufficient_data_actions=[],
|
||||
@ -2451,11 +2451,11 @@ class AlarmTest(AlarmTestBase,
|
||||
type='threshold',
|
||||
name='llu',
|
||||
description='llu',
|
||||
timestamp=None,
|
||||
timestamp=constants.MIN_DATETIME,
|
||||
user_id='bla',
|
||||
project_id='ffo',
|
||||
state="insufficient data",
|
||||
state_timestamp=None,
|
||||
state_timestamp=constants.MIN_DATETIME,
|
||||
ok_actions=[],
|
||||
alarm_actions=[],
|
||||
insufficient_data_actions=[],
|
||||
@ -2516,7 +2516,6 @@ class AlarmTestPagination(AlarmTestBase,
|
||||
self.assertEqual(len(alarms), 2)
|
||||
|
||||
def test_get_alarm_paginate(self):
|
||||
|
||||
self.add_some_alarms()
|
||||
|
||||
pagination = base.Pagination(limit=4, marker_value='yellow-alert')
|
||||
|
Loading…
Reference in New Issue
Block a user