File: test_alarm_history.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 (119 lines) | stat: -rw-r--r-- 5,290 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#    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.

from oslo_serialization import jsonutils
from oslo_utils import uuidutils

from aodhclient.tests.functional import base


class AlarmHistoryTest(base.ClientTestBase):

    def test_help(self):
        self.aodh("help", params="alarm-history show")
        self.aodh("help", params="alarm-history search")

    def test_alarm_history_scenario(self):

        PROJECT_ID = uuidutils.generate_uuid()
        RESOURCE_ID = uuidutils.generate_uuid()

        result = self.aodh('alarm',
                           params=("create "
                                   "--type gnocchi_resources_threshold "
                                   "--name history1 --metric cpu_util "
                                   "--threshold 5 "
                                   "--resource-id %s --resource-type generic "
                                   "--aggregation-method last "
                                   "--project-id %s"
                                   % (RESOURCE_ID, PROJECT_ID)))
        alarm = self.details_multiple(result)[0]
        ALARM_ID = alarm['alarm_id']
        result = self.aodh('alarm',
                           params=("create "
                                   "--type gnocchi_resources_threshold "
                                   "--name history2 --metric cpu_util "
                                   "--threshold 10 "
                                   "--resource-id %s --resource-type generic "
                                   "--aggregation-method last "
                                   "--project-id %s"
                                   % (RESOURCE_ID, PROJECT_ID)))
        alarm = self.details_multiple(result)[0]
        ALARM_ID2 = alarm['alarm_id']

        # LIST WITH PAGINATION
        # list with limit
        result = self.aodh('alarm-history',
                           params=("show %s --limit 1" % ALARM_ID))
        alarm_list = self.parser.listing(result)
        self.assertEqual(1, len(alarm_list))
        # list with sort key=timestamp, dir=asc
        result = self.aodh('alarm-history',
                           params=("show %s --sort timestamp:asc" % ALARM_ID))
        alarm_history_list = self.parser.listing(result)
        timestamp = [r['timestamp'] for r in alarm_history_list]
        sorted_timestamp = sorted(timestamp)
        self.assertEqual(sorted_timestamp, timestamp)
        # list with sort key=type dir = desc and key=timestamp, dir=asc
        result = self.aodh('alarm-history',
                           params=("show %s --sort type:desc "
                                   "--sort timestamp:asc" % ALARM_ID))
        alarm_history_list = self.parser.listing(result)
        creation = alarm_history_list.pop(-1)
        timestamp = [r['timestamp'] for r in alarm_history_list]
        sorted_timestamp = sorted(timestamp)
        self.assertEqual(sorted_timestamp, timestamp)
        self.assertEqual('creation', creation['type'])

        # TEST FIELDS
        result = self.aodh(
            'alarm-history', params=("show %s" % ALARM_ID))
        history = self.parser.listing(result)[0]
        for key in ["timestamp", "type", "detail", "event_id"]:
            self.assertIn(key, history)

        # SHOW
        result = self.aodh(
            'alarm-history', params=("show %s" % ALARM_ID))
        history = self.parser.listing(result)[0]
        self.assertEqual('creation', history['type'])
        self.assertEqual('history1',
                         jsonutils.loads(history['detail'])['name'])

        result = self.aodh(
            'alarm-history', params=("show %s" % ALARM_ID2))
        history = self.parser.listing(result)[0]
        self.assertEqual('creation', history['type'])
        self.assertEqual('history2',
                         jsonutils.loads(history['detail'])['name'])

        # SEARCH ALL
        result = self.aodh('alarm-history', params=("search"))
        self.assertIn(ALARM_ID,
                      [r['alarm_id'] for r in self.parser.listing(result)])
        self.assertIn(ALARM_ID2,
                      [r['alarm_id'] for r in self.parser.listing(result)])

        # SEARCH
        result = self.aodh('alarm-history',
                           params=("search --query "
                                   "alarm_id=%s"
                                   % ALARM_ID))
        history = self.parser.listing(result)[0]
        self.assertEqual(ALARM_ID, history["alarm_id"])
        self.assertEqual('creation', history['type'])
        self.assertEqual('history1',
                         jsonutils.loads(history['detail'])['name'])

        # CLEANUP
        self.aodh('alarm', params="delete %s" % ALARM_ID)
        self.aodh('alarm', params="delete %s" % ALARM_ID2)