| 1 |
# |
| 2 |
# Copyright IBM 2016. All rights reserved |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
import testtools |
| 17 |
from unittest import mock |
| 18 |
|
| 19 |
from aodhclient.v2 import alarm |
| 20 |
|
| 21 |
|
| 22 |
class AlarmManagerTest(testtools.TestCase): |
| 23 |
|
| 24 |
def setUp(self): |
| 25 |
super().setUp() |
| 26 |
self.client = mock.Mock() |
| 27 |
self.alarms = { |
| 28 |
'event_alarm': { |
| 29 |
'gnocchi_aggregation_by_metrics_threshold_rule': {}, |
| 30 |
'gnocchi_resources_threshold_rule': {}, |
| 31 |
'name': 'event_alarm', |
| 32 |
'gnocchi_aggregation_by_resources_threshold_rule': {}, |
| 33 |
'event_rule': {}, |
| 34 |
'type': 'event'} |
| 35 |
} |
| 36 |
self.results = { |
| 37 |
"result1": { |
| 38 |
"event_rule": {} |
| 39 |
}, |
| 40 |
} |
| 41 |
|
| 42 |
@mock.patch.object(alarm.AlarmManager, '_get') |
| 43 |
def test_list(self, mock_am): |
| 44 |
am = alarm.AlarmManager(self.client) |
| 45 |
am.list() |
| 46 |
mock_am.assert_called_with('v2/alarms') |
| 47 |
|
| 48 |
@mock.patch.object(alarm.AlarmManager, '_post') |
| 49 |
def test_query(self, mock_am): |
| 50 |
am = alarm.AlarmManager(self.client) |
| 51 |
query = '{"=": {"type": "event"}}' |
| 52 |
am.query(query) |
| 53 |
url = 'v2/query/alarms' |
| 54 |
expected_value = ('{"filter": "{\\"=\\": {\\"type\\":' |
| 55 |
' \\"event\\"}}"}') |
| 56 |
headers_value = {'Content-Type': "application/json"} |
| 57 |
mock_am.assert_called_with( |
| 58 |
url, |
| 59 |
data=expected_value, |
| 60 |
headers=headers_value) |
| 61 |
|
| 62 |
@mock.patch.object(alarm.AlarmManager, '_get') |
| 63 |
def test_list_with_filters(self, mock_am): |
| 64 |
am = alarm.AlarmManager(self.client) |
| 65 |
filters = dict(type='gnocchi_resources_threshold', severity='low') |
| 66 |
am.list(filters=filters) |
| 67 |
expected_url = ( |
| 68 |
"v2/alarms?q.field=severity&q.op=eq&q.value=low&" |
| 69 |
"q.field=type&q.op=eq&q.value=gnocchi_resources_threshold") |
| 70 |
mock_am.assert_called_with(expected_url) |
| 71 |
|
| 72 |
@mock.patch.object(alarm.AlarmManager, '_get') |
| 73 |
def test_get(self, mock_am): |
| 74 |
am = alarm.AlarmManager(self.client) |
| 75 |
am.get('01919bbd-8b0e-451c-be28-abe250ae9b1b') |
| 76 |
mock_am.assert_called_with( |
| 77 |
'v2/alarms/01919bbd-8b0e-451c-be28-abe250ae9b1b') |
| 78 |
|
| 79 |
@mock.patch.object(alarm.AlarmManager, '_delete') |
| 80 |
def test_delete(self, mock_am): |
| 81 |
am = alarm.AlarmManager(self.client) |
| 82 |
am.delete('01919bbd-8b0e-451c-be28-abe250ae9b1b') |
| 83 |
mock_am.assert_called_with( |
| 84 |
'v2/alarms/01919bbd-8b0e-451c-be28-abe250ae9b1b') |
| 85 |
|
| 86 |
def test_clean_rules_event_alarm(self): |
| 87 |
am = alarm.AlarmManager(self.client) |
| 88 |
alarm_value = self.alarms.get('event_alarm') |
| 89 |
am._clean_rules('event', alarm_value) |
| 90 |
alarm_value.pop('type') |
| 91 |
alarm_value.pop('name') |
| 92 |
result = self.results.get("result1") |
| 93 |
self.assertEqual(alarm_value, result) |