-
Notifications
You must be signed in to change notification settings - Fork 325
/
test_combination.py
281 lines (264 loc) · 12.5 KB
/
test_combination.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# -*- encoding: utf-8 -*-
#
# Copyright © 2013 eNovance <licensing@enovance.com>
#
# Authors: Mehdi Abaakouk <mehdi.abaakouk@enovance.com>
#
# 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.
"""Tests for ceilometer/alarm/threshold_evaluation.py
"""
import mock
import uuid
from ceilometer.alarm.evaluator import combination
from ceilometer.storage import models
from ceilometerclient import exc
from ceilometerclient.v2 import alarms
from tests.alarm.evaluator import base
class TestEvaluate(base.TestEvaluatorBase):
EVALUATOR = combination.CombinationEvaluator
def prepare_alarms(self):
self.alarms = [
models.Alarm(name='or-alarm',
description='the or alarm',
type='combination',
enabled=True,
user_id='foobar',
project_id='snafu',
alarm_id=str(uuid.uuid4()),
state='insufficient data',
state_timestamp=None,
timestamp=None,
insufficient_data_actions=[],
ok_actions=[],
alarm_actions=[],
repeat_actions=False,
rule=dict(
alarm_ids=[
'9cfc3e51-2ff1-4b1d-ac01-c1bd4c6d0d1e',
'1d441595-d069-4e05-95ab-8693ba6a8302'],
operator='or',
)),
models.Alarm(name='and-alarm',
description='the and alarm',
type='combination',
enabled=True,
user_id='foobar',
project_id='snafu',
alarm_id=str(uuid.uuid4()),
state='insufficient data',
state_timestamp=None,
timestamp=None,
insufficient_data_actions=[],
ok_actions=[],
alarm_actions=[],
repeat_actions=False,
rule=dict(
alarm_ids=[
'b82734f4-9d06-48f3-8a86-fa59a0c99dc8',
'15a700e5-2fe8-4b3d-8c55-9e92831f6a2b'],
operator='and',
))
]
@staticmethod
def _get_alarm(state):
return alarms.Alarm(None, {'state': state})
def _combination_transition_reason(self, state):
return ['Transition to %(state)s due at least to one alarm in'
' 9cfc3e51-2ff1-4b1d-ac01-c1bd4c6d0d1e,'
'1d441595-d069-4e05-95ab-8693ba6a8302'
' in state %(state)s' % {'state': state},
'Transition to %(state)s due to all alarms'
' (b82734f4-9d06-48f3-8a86-fa59a0c99dc8,'
'15a700e5-2fe8-4b3d-8c55-9e92831f6a2b)'
' in state %(state)s' % {'state': state}]
def _combination_remaining_reason(self, state):
return ['Remaining as %(state)s due at least to one alarm in'
' 9cfc3e51-2ff1-4b1d-ac01-c1bd4c6d0d1e,'
'1d441595-d069-4e05-95ab-8693ba6a8302'
' in state %(state)s' % {'state': state},
'Remaining as %(state)s due to all alarms'
' (b82734f4-9d06-48f3-8a86-fa59a0c99dc8,'
'15a700e5-2fe8-4b3d-8c55-9e92831f6a2b)'
' in state %(state)s' % {'state': state}]
def test_retry_transient_api_failure(self):
with mock.patch('ceilometerclient.client.get_client',
return_value=self.api_client):
broken = exc.CommunicationError(message='broken')
self.api_client.alarms.get.side_effect = [
broken,
broken,
broken,
broken,
self._get_alarm('ok'),
self._get_alarm('ok'),
self._get_alarm('ok'),
self._get_alarm('ok'),
]
self._evaluate_all_alarms()
self._assert_all_alarms('insufficient data')
self._evaluate_all_alarms()
self._assert_all_alarms('ok')
def test_simple_insufficient(self):
self._set_all_alarms('ok')
with mock.patch('ceilometerclient.client.get_client',
return_value=self.api_client):
broken = exc.CommunicationError(message='broken')
self.api_client.alarms.get.side_effect = broken
self._evaluate_all_alarms()
self._assert_all_alarms('insufficient data')
expected = [mock.call(alarm.alarm_id, state='insufficient data')
for alarm in self.alarms]
update_calls = self.api_client.alarms.set_state.call_args_list
self.assertEqual(update_calls, expected)
expected = [mock.call(alarm,
'ok',
('%d alarms in %s are in unknown state' %
(2, ",".join(alarm.rule['alarm_ids']))))
for alarm in self.alarms]
self.assertEqual(self.notifier.notify.call_args_list, expected)
def test_to_ok_with_all_ok(self):
self._set_all_alarms('insufficient data')
with mock.patch('ceilometerclient.client.get_client',
return_value=self.api_client):
self.api_client.alarms.get.side_effect = [
self._get_alarm('ok'),
self._get_alarm('ok'),
self._get_alarm('ok'),
self._get_alarm('ok'),
]
self._evaluate_all_alarms()
expected = [mock.call(alarm.alarm_id, state='ok')
for alarm in self.alarms]
update_calls = self.api_client.alarms.set_state.call_args_list
self.assertEqual(update_calls, expected)
reasons = self._combination_transition_reason('ok')
expected = [mock.call(alarm, 'insufficient data', reason)
for alarm, reason in zip(self.alarms, reasons)]
self.assertEqual(self.notifier.notify.call_args_list, expected)
def test_to_ok_with_one_alarm(self):
self._set_all_alarms('alarm')
with mock.patch('ceilometerclient.client.get_client',
return_value=self.api_client):
self.api_client.alarms.get.side_effect = [
self._get_alarm('ok'),
self._get_alarm('ok'),
self._get_alarm('alarm'),
self._get_alarm('ok'),
]
self._evaluate_all_alarms()
expected = [mock.call(alarm.alarm_id, state='ok')
for alarm in self.alarms]
update_calls = self.api_client.alarms.set_state.call_args_list
self.assertEqual(update_calls, expected)
reasons = self._combination_transition_reason('ok')
expected = [mock.call(alarm, 'alarm', reason)
for alarm, reason in zip(self.alarms, reasons)]
self.assertEqual(self.notifier.notify.call_args_list, expected)
def test_to_alarm_with_all_alarm(self):
self._set_all_alarms('ok')
with mock.patch('ceilometerclient.client.get_client',
return_value=self.api_client):
self.api_client.alarms.get.side_effect = [
self._get_alarm('alarm'),
self._get_alarm('alarm'),
self._get_alarm('alarm'),
self._get_alarm('alarm'),
]
self._evaluate_all_alarms()
expected = [mock.call(alarm.alarm_id, state='alarm')
for alarm in self.alarms]
update_calls = self.api_client.alarms.set_state.call_args_list
self.assertEqual(update_calls, expected)
reasons = self._combination_transition_reason('alarm')
expected = [mock.call(alarm, 'ok', reason)
for alarm, reason in zip(self.alarms, reasons)]
self.assertEqual(self.notifier.notify.call_args_list, expected)
def test_to_alarm_with_one_ok(self):
self._set_all_alarms('ok')
with mock.patch('ceilometerclient.client.get_client',
return_value=self.api_client):
self.api_client.alarms.get.side_effect = [
self._get_alarm('ok'),
self._get_alarm('alarm'),
self._get_alarm('alarm'),
self._get_alarm('alarm'),
]
self._evaluate_all_alarms()
expected = [mock.call(alarm.alarm_id, state='alarm')
for alarm in self.alarms]
update_calls = self.api_client.alarms.set_state.call_args_list
self.assertEqual(update_calls, expected)
reasons = self._combination_transition_reason('alarm')
expected = [mock.call(alarm, 'ok', reason)
for alarm, reason in zip(self.alarms, reasons)]
self.assertEqual(self.notifier.notify.call_args_list, expected)
def test_to_unknown(self):
self._set_all_alarms('ok')
with mock.patch('ceilometerclient.client.get_client',
return_value=self.api_client):
broken = exc.CommunicationError(message='broken')
self.api_client.alarms.get.side_effect = [
broken,
self._get_alarm('ok'),
self._get_alarm('insufficient data'),
self._get_alarm('ok'),
]
self._evaluate_all_alarms()
expected = [mock.call(alarm.alarm_id, state='insufficient data')
for alarm in self.alarms]
update_calls = self.api_client.alarms.set_state.call_args_list
self.assertEqual(update_calls, expected)
reasons = ['1 alarms in'
' 9cfc3e51-2ff1-4b1d-ac01-c1bd4c6d0d1e,'
'1d441595-d069-4e05-95ab-8693ba6a8302'
' are in unknown state',
'1 alarms in'
' b82734f4-9d06-48f3-8a86-fa59a0c99dc8,'
'15a700e5-2fe8-4b3d-8c55-9e92831f6a2b'
' are in unknown state']
expected = [mock.call(alarm, 'ok', reason)
for alarm, reason in zip(self.alarms, reasons)]
self.assertEqual(self.notifier.notify.call_args_list, expected)
def test_no_state_change(self):
self._set_all_alarms('ok')
with mock.patch('ceilometerclient.client.get_client',
return_value=self.api_client):
self.api_client.alarms.get.side_effect = [
self._get_alarm('ok'),
self._get_alarm('ok'),
self._get_alarm('ok'),
self._get_alarm('ok'),
]
self._evaluate_all_alarms()
update_calls = self.api_client.alarms.set_state.call_args_list
self.assertEqual(update_calls, [])
self.assertEqual(self.notifier.notify.call_args_list, [])
def test_no_state_change_and_repeat_actions(self):
self.alarms[0].repeat_actions = True
self.alarms[1].repeat_actions = True
self._set_all_alarms('ok')
with mock.patch('ceilometerclient.client.get_client',
return_value=self.api_client):
self.api_client.alarms.get.side_effect = [
self._get_alarm('ok'),
self._get_alarm('ok'),
self._get_alarm('ok'),
self._get_alarm('ok'),
]
self._evaluate_all_alarms()
update_calls = self.api_client.alarms.set_state.call_args_list
self.assertEqual(update_calls, [])
reasons = self._combination_remaining_reason('ok')
expected = [mock.call(alarm, 'ok', reason)
for alarm, reason in zip(self.alarms, reasons)]
self.assertEqual(self.notifier.notify.call_args_list, expected)