File: test_alarm_manager.py

package info (click to toggle)
python-aodhclient 3.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 596 kB
  • sloc: python: 2,812; sh: 34; makefile: 24
file content (93 lines) | stat: -rw-r--r-- 3,396 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#
#    Copyright IBM 2016. All rights reserved
#
#    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 testtools
from unittest import mock

from aodhclient.v2 import alarm


class AlarmManagerTest(testtools.TestCase):

    def setUp(self):
        super().setUp()
        self.client = mock.Mock()
        self.alarms = {
            'event_alarm': {
                'gnocchi_aggregation_by_metrics_threshold_rule': {},
                'gnocchi_resources_threshold_rule': {},
                'name': 'event_alarm',
                'gnocchi_aggregation_by_resources_threshold_rule': {},
                'event_rule': {},
                'type': 'event'}
        }
        self.results = {
            "result1": {
                "event_rule": {}
            },
        }

    @mock.patch.object(alarm.AlarmManager, '_get')
    def test_list(self, mock_am):
        am = alarm.AlarmManager(self.client)
        am.list()
        mock_am.assert_called_with('v2/alarms')

    @mock.patch.object(alarm.AlarmManager, '_post')
    def test_query(self, mock_am):
        am = alarm.AlarmManager(self.client)
        query = '{"=": {"type": "event"}}'
        am.query(query)
        url = 'v2/query/alarms'
        expected_value = ('{"filter": "{\\"=\\": {\\"type\\":'
                          ' \\"event\\"}}"}')
        headers_value = {'Content-Type': "application/json"}
        mock_am.assert_called_with(
            url,
            data=expected_value,
            headers=headers_value)

    @mock.patch.object(alarm.AlarmManager, '_get')
    def test_list_with_filters(self, mock_am):
        am = alarm.AlarmManager(self.client)
        filters = dict(type='gnocchi_resources_threshold', severity='low')
        am.list(filters=filters)
        expected_url = (
            "v2/alarms?q.field=severity&q.op=eq&q.value=low&"
            "q.field=type&q.op=eq&q.value=gnocchi_resources_threshold")
        mock_am.assert_called_with(expected_url)

    @mock.patch.object(alarm.AlarmManager, '_get')
    def test_get(self, mock_am):
        am = alarm.AlarmManager(self.client)
        am.get('01919bbd-8b0e-451c-be28-abe250ae9b1b')
        mock_am.assert_called_with(
            'v2/alarms/01919bbd-8b0e-451c-be28-abe250ae9b1b')

    @mock.patch.object(alarm.AlarmManager, '_delete')
    def test_delete(self, mock_am):
        am = alarm.AlarmManager(self.client)
        am.delete('01919bbd-8b0e-451c-be28-abe250ae9b1b')
        mock_am.assert_called_with(
            'v2/alarms/01919bbd-8b0e-451c-be28-abe250ae9b1b')

    def test_clean_rules_event_alarm(self):
        am = alarm.AlarmManager(self.client)
        alarm_value = self.alarms.get('event_alarm')
        am._clean_rules('event', alarm_value)
        alarm_value.pop('type')
        alarm_value.pop('name')
        result = self.results.get("result1")
        self.assertEqual(alarm_value, result)