Disable specifying alarm itself in combination rule

Currently, update combination alarm can specify itself in alarm_ids,
however, that will cause endless loop when evaluating this alarm.

This patch adds a check to disable such behavior.

Change-Id: I63b465c59fe39206ae04b0736482d1ffd7bf59c4
Closes-Bug: #1304419
(cherry picked from commit de8176fa73)
This commit is contained in:
ZhiQiang Fan 2014-04-09 06:27:28 +08:00 committed by ZhiQiang Fan
parent 94c12fd76c
commit 82eed26214
2 changed files with 31 additions and 0 deletions

View File

@ -1925,6 +1925,13 @@ class AlarmController(rest.RestController):
_("Alarm with name=%s exists") % data.name,
status_code=409)
# should check if there is any circle in the dependency, but for
# efficiency reason, here only check alarm cannot depend on itself
if data.type == 'combination':
if self._id in data.combination_rule.alarm_ids:
raise ClientSideError(_('Cannot specify alarm %s itself in '
'combination rule') % self._id)
old_alarm = Alarm.from_db_model(alarm_in).as_dict(storage.models.Alarm)
updated_alarm = data.as_dict(storage.models.Alarm)
try:

View File

@ -1344,6 +1344,30 @@ class TestAlarms(FunctionalTest,
'Alarm with name=name1 exists',
resp.json['error_message']['faultstring'])
def test_put_alarm_combination_cannot_specify_itself(self):
json = {
'name': 'name4',
'type': 'combination',
'combination_rule': {
'alarm_ids': ['d'],
}
}
data = self.get_json('/alarms',
q=[{'field': 'name',
'value': 'name4',
}])
self.assertEqual(1, len(data))
alarm_id = data[0]['alarm_id']
resp = self.put_json('/alarms/%s' % alarm_id,
expect_errors=True, status=400,
params=json,
headers=self.auth_headers)
msg = 'Cannot specify alarm %s itself in combination rule' % alarm_id
self.assertEqual(msg, resp.json['error_message']['faultstring'])
def test_delete_alarm(self):
data = self.get_json('/alarms')
self.assertEqual(4, len(data))