Merge "Fixes duplicated names in alarm time constraints"

This commit is contained in:
Jenkins 2014-03-25 10:31:33 +00:00 committed by Gerrit Code Review
commit 55cbe746da
2 changed files with 35 additions and 0 deletions

View File

@ -1732,6 +1732,12 @@ class Alarm(_Base):
if not alarms:
raise EntityNotFound(_('Alarm'), id)
tc_names = [tc.name for tc in alarm.time_constraints]
if len(tc_names) > len(set(tc_names)):
error = _("Time constraint names must be "
"unique for a given alarm.")
raise ClientSideError(error)
return alarm
@staticmethod

View File

@ -338,6 +338,35 @@ class TestAlarms(FunctionalTest,
alarms = list(self.conn.get_alarms())
self.assertEqual(4, len(alarms))
def test_post_duplicate_time_constraint_name(self):
json = {
'name': 'added_alarm_duplicate_constraint_name',
'type': 'threshold',
'time_constraints': [
{
'name': 'testcons',
'start': '* 11 * * *',
'duration': 10
},
{
'name': 'testcons',
'start': '* * * * *',
'duration': 20
}
],
'threshold_rule': {
'meter_name': 'ameter',
'threshold': 300.0
}
}
resp = self.post_json('/alarms', params=json, expect_errors=True,
status=400, headers=self.auth_headers)
self.assertEqual(
"Time constraint names must be unique for a given alarm.",
resp.json['error_message']['faultstring'])
alarms = list(self.conn.get_alarms())
self.assertEqual(4, len(alarms))
def test_post_invalid_alarm_time_constraint_duration(self):
json = {
'name': 'added_alarm_invalid_constraint_duration',