Debian Codegraph @

aodhclient/tests/functional/test_alarm_history.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from oslo_serialization import jsonutils
14 from oslo_utils import uuidutils
15
16 from aodhclient.tests.functional import base
17
18
19 class AlarmHistoryTest(base.ClientTestBase):
20
21 def test_help(self):
22 self.aodh("help", params="alarm-history show")
23 self.aodh("help", params="alarm-history search")
24
25 def test_alarm_history_scenario(self):
26
27 PROJECT_ID = uuidutils.generate_uuid()
28 RESOURCE_ID = uuidutils.generate_uuid()
29
30 result = self.aodh('alarm',
31 params=("create "
32 "--type gnocchi_resources_threshold "
33 "--name history1 --metric cpu_util "
34 "--threshold 5 "
35 "--resource-id %s --resource-type generic "
36 "--aggregation-method last "
37 "--project-id %s"
38 % (RESOURCE_ID, PROJECT_ID)))
39 alarm = self.details_multiple(result)[0]
40 ALARM_ID = alarm['alarm_id']
41 result = self.aodh('alarm',
42 params=("create "
43 "--type gnocchi_resources_threshold "
44 "--name history2 --metric cpu_util "
45 "--threshold 10 "
46 "--resource-id %s --resource-type generic "
47 "--aggregation-method last "
48 "--project-id %s"
49 % (RESOURCE_ID, PROJECT_ID)))
50 alarm = self.details_multiple(result)[0]
51 ALARM_ID2 = alarm['alarm_id']
52
53 # LIST WITH PAGINATION
54 # list with limit
55 result = self.aodh('alarm-history',
56 params=("show %s --limit 1" % ALARM_ID))
57 alarm_list = self.parser.listing(result)
58 self.assertEqual(1, len(alarm_list))
59 # list with sort key=timestamp, dir=asc
60 result = self.aodh('alarm-history',
61 params=("show %s --sort timestamp:asc" % ALARM_ID))
62 alarm_history_list = self.parser.listing(result)
63 timestamp = [r['timestamp'] for r in alarm_history_list]
64 sorted_timestamp = sorted(timestamp)
65 self.assertEqual(sorted_timestamp, timestamp)
66 # list with sort key=type dir = desc and key=timestamp, dir=asc
67 result = self.aodh('alarm-history',
68 params=("show %s --sort type:desc "
69 "--sort timestamp:asc" % ALARM_ID))
70 alarm_history_list = self.parser.listing(result)
71 creation = alarm_history_list.pop(-1)
72 timestamp = [r['timestamp'] for r in alarm_history_list]
73 sorted_timestamp = sorted(timestamp)
74 self.assertEqual(sorted_timestamp, timestamp)
75 self.assertEqual('creation', creation['type'])
76
77 # TEST FIELDS
78 result = self.aodh(
79 'alarm-history', params=("show %s" % ALARM_ID))
80 history = self.parser.listing(result)[0]
81 for key in ["timestamp", "type", "detail", "event_id"]:
82 self.assertIn(key, history)
83
84 # SHOW
85 result = self.aodh(
86 'alarm-history', params=("show %s" % ALARM_ID))
87 history = self.parser.listing(result)[0]
88 self.assertEqual('creation', history['type'])
89 self.assertEqual('history1',
90 jsonutils.loads(history['detail'])['name'])
91
92 result = self.aodh(
93 'alarm-history', params=("show %s" % ALARM_ID2))
94 history = self.parser.listing(result)[0]
95 self.assertEqual('creation', history['type'])
96 self.assertEqual('history2',
97 jsonutils.loads(history['detail'])['name'])
98
99 # SEARCH ALL
100 result = self.aodh('alarm-history', params=("search"))
101 self.assertIn(ALARM_ID,
102 [r['alarm_id'] for r in self.parser.listing(result)])
103 self.assertIn(ALARM_ID2,
104 [r['alarm_id'] for r in self.parser.listing(result)])
105
106 # SEARCH
107 result = self.aodh('alarm-history',
108 params=("search --query "
109 "alarm_id=%s"
110 % ALARM_ID))
111 history = self.parser.listing(result)[0]
112 self.assertEqual(ALARM_ID, history["alarm_id"])
113 self.assertEqual('creation', history['type'])
114 self.assertEqual('history1',
115 jsonutils.loads(history['detail'])['name'])
116
117 # CLEANUP
118 self.aodh('alarm', params="delete %s" % ALARM_ID)
119 self.aodh('alarm', params="delete %s" % ALARM_ID2)