| 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 argparse |
| 17 |
from unittest import mock |
| 18 |
|
| 19 |
import testtools |
| 20 |
|
| 21 |
from aodhclient.v2 import alarm_cli |
| 22 |
|
| 23 |
|
| 24 |
class CliAlarmCreateTest(testtools.TestCase): |
| 25 |
|
| 26 |
def setUp(self): |
| 27 |
super().setUp() |
| 28 |
self.app = mock.Mock() |
| 29 |
self.parser = mock.Mock() |
| 30 |
self.cli_alarm_create = ( |
| 31 |
alarm_cli.CliAlarmCreate(self.app, self.parser)) |
| 32 |
|
| 33 |
@mock.patch.object(argparse.ArgumentParser, 'error') |
| 34 |
def test_validate_args_gnocchi_resources_threshold(self, mock_arg): |
| 35 |
# Cover the test case of the method _validate_args for |
| 36 |
# gnocchi_resources_threshold |
| 37 |
parser = self.cli_alarm_create.get_parser('aodh alarm create') |
| 38 |
test_parsed_args = parser.parse_args([ |
| 39 |
'--name', 'gnocchi_resources_threshold_test', |
| 40 |
'--type', 'gnocchi_resources_threshold', |
| 41 |
'--metric', 'cpu', |
| 42 |
'--aggregation-method', 'last', |
| 43 |
'--resource-type', 'generic', |
| 44 |
'--threshold', '80' |
| 45 |
]) |
| 46 |
self.cli_alarm_create._validate_args(test_parsed_args) |
| 47 |
mock_arg.assert_called_once_with( |
| 48 |
'gnocchi_resources_threshold requires --metric, ' |
| 49 |
'--threshold, --resource-id, --resource-type and ' |
| 50 |
'--aggregation-method') |
| 51 |
|
| 52 |
@mock.patch.object(argparse.ArgumentParser, 'error') |
| 53 |
def test_validate_args_threshold(self, mock_arg): |
| 54 |
# Cover the test case of the method _validate_args for |
| 55 |
# threshold |
| 56 |
parser = self.cli_alarm_create.get_parser('aodh alarm create') |
| 57 |
test_parsed_args = parser.parse_args([ |
| 58 |
'--name', 'threshold_test', |
| 59 |
'--type', 'threshold', |
| 60 |
'--threshold', '80' |
| 61 |
]) |
| 62 |
self.cli_alarm_create._validate_args(test_parsed_args) |
| 63 |
mock_arg.assert_called_once_with( |
| 64 |
'Threshold alarm requires -m/--meter-name and ' |
| 65 |
'--threshold parameters. Meter name can be ' |
| 66 |
'found in Ceilometer') |
| 67 |
|
| 68 |
@mock.patch.object(argparse.ArgumentParser, 'error') |
| 69 |
def test_validate_args_composite(self, mock_arg): |
| 70 |
# Cover the test case of the method _validate_args for |
| 71 |
# composite |
| 72 |
parser = self.cli_alarm_create.get_parser('aodh alarm create') |
| 73 |
test_parsed_args = parser.parse_args([ |
| 74 |
'--name', 'composite_test', |
| 75 |
'--type', 'composite' |
| 76 |
]) |
| 77 |
self.cli_alarm_create._validate_args(test_parsed_args) |
| 78 |
mock_arg.assert_called_once_with( |
| 79 |
'Composite alarm requires --composite-rule parameter') |
| 80 |
|
| 81 |
@mock.patch.object(argparse.ArgumentParser, 'error') |
| 82 |
def test_validate_args_gno_agg_by_resources_threshold(self, mock_arg): |
| 83 |
# Cover the test case of the method _validate_args for |
| 84 |
# gnocchi_aggregation_by_resources_threshold |
| 85 |
parser = self.cli_alarm_create.get_parser('aodh alarm create') |
| 86 |
test_parsed_args = parser.parse_args([ |
| 87 |
'--name', 'gnocchi_aggregation_by_resources_threshold_test', |
| 88 |
'--type', 'gnocchi_aggregation_by_resources_threshold', |
| 89 |
'--metric', 'cpu', |
| 90 |
'--aggregation-method', 'last', |
| 91 |
'--resource-type', 'generic', |
| 92 |
'--threshold', '80' |
| 93 |
]) |
| 94 |
self.cli_alarm_create._validate_args(test_parsed_args) |
| 95 |
mock_arg.assert_called_once_with( |
| 96 |
'gnocchi_aggregation_by_resources_threshold requires ' |
| 97 |
'--metric, --threshold, --aggregation-method, --query and ' |
| 98 |
'--resource-type') |
| 99 |
|
| 100 |
@mock.patch.object(argparse.ArgumentParser, 'error') |
| 101 |
def test_validate_args_gno_agg_by_metrics_threshold(self, mock_arg): |
| 102 |
# Cover the test case of the method _validate_args for |
| 103 |
# gnocchi_aggregation_by_metrics_threshold |
| 104 |
parser = self.cli_alarm_create.get_parser('aodh alarm create') |
| 105 |
test_parsed_args = parser.parse_args([ |
| 106 |
'--name', 'gnocchi_aggregation_by_metrics_threshold_test', |
| 107 |
'--type', 'gnocchi_aggregation_by_metrics_threshold', |
| 108 |
'--resource-type', 'generic', |
| 109 |
'--threshold', '80' |
| 110 |
]) |
| 111 |
self.cli_alarm_create._validate_args(test_parsed_args) |
| 112 |
mock_arg.assert_called_once_with( |
| 113 |
'gnocchi_aggregation_by_metrics_threshold requires ' |
| 114 |
'--metric, --threshold and --aggregation-method') |
| 115 |
|
| 116 |
@mock.patch.object(argparse.ArgumentParser, 'error') |
| 117 |
def test_validate_args_prometheus(self, mock_arg): |
| 118 |
# Cover the test case of the method _validate_args for |
| 119 |
# prometheus |
| 120 |
parser = self.cli_alarm_create.get_parser('aodh alarm create') |
| 121 |
test_parsed_args = parser.parse_args([ |
| 122 |
'--name', 'prom_test', |
| 123 |
'--type', 'prometheus', |
| 124 |
'--comparison-operator', 'gt', |
| 125 |
'--threshold', '666', |
| 126 |
]) |
| 127 |
self.cli_alarm_create._validate_args(test_parsed_args) |
| 128 |
mock_arg.assert_called_once_with( |
| 129 |
'Prometheus alarm requires --query and --threshold parameters.') |
| 130 |
|
| 131 |
def test_alarm_from_args(self): |
| 132 |
# The test case to cover the method _alarm_from_args |
| 133 |
parser = self.cli_alarm_create.get_parser('aodh alarm create') |
| 134 |
test_parsed_args = parser.parse_args([ |
| 135 |
'--type', 'event', |
| 136 |
'--name', 'alarm_from_args_test', |
| 137 |
'--project-id', '01919bbd-8b0e-451c-be28-abe250ae9b1b', |
| 138 |
'--user-id', '01919bbd-8b0e-451c-be28-abe250ae9c1c', |
| 139 |
'--description', 'For Test', |
| 140 |
'--state', 'ok', |
| 141 |
'--severity', 'critical', |
| 142 |
'--enabled', 'True', |
| 143 |
'--alarm-action', 'http://something/alarm', |
| 144 |
'--ok-action', 'http://something/ok', |
| 145 |
'--repeat-action', 'True', |
| 146 |
'--insufficient-data-action', |
| 147 |
'http://something/insufficient', |
| 148 |
'--time-constraint', |
| 149 |
'name=cons1;start="0 11 * * *";duration=300;description=desc1', |
| 150 |
'--evaluation-periods', '60', |
| 151 |
'--comparison-operator', 'le', |
| 152 |
'--threshold', '80', |
| 153 |
'--event-type', 'event', |
| 154 |
'--query', 'resource=fake-resource-id', |
| 155 |
'--granularity', '60', |
| 156 |
'--aggregation-method', 'last', |
| 157 |
'--metric', 'cpu', |
| 158 |
'--resource-id', '01919bbd-8b0e-451c-be28-abe250ae9c1c', |
| 159 |
'--resource-type', 'generic', |
| 160 |
'--stack-id', '0809ab348-8b0e-451c-be28-abe250ae9c1c', |
| 161 |
'--pool-id', '79832aabf-343ba-be28-abe250ae9c1c', |
| 162 |
'--autoscaling-group-id', 'abe250ae9c1c-79832aabf-343ba-be28' |
| 163 |
]) |
| 164 |
|
| 165 |
# Output for the test |
| 166 |
alarm = { |
| 167 |
'name': 'alarm_from_args_test', |
| 168 |
'project_id': '01919bbd-8b0e-451c-be28-abe250ae9b1b', |
| 169 |
'user_id': '01919bbd-8b0e-451c-be28-abe250ae9c1c', |
| 170 |
'description': 'For Test', |
| 171 |
'state': 'ok', |
| 172 |
'severity': 'critical', |
| 173 |
'enabled': True, |
| 174 |
'alarm_actions': ['http://something/alarm'], |
| 175 |
'ok_actions': ['http://something/ok'], |
| 176 |
'insufficient_data_actions': |
| 177 |
['http://something/insufficient'], |
| 178 |
'time_constraints': [{'description': 'desc1', |
| 179 |
'duration': '300', |
| 180 |
'name': 'cons1', |
| 181 |
'start': '0 11 * * *'}], |
| 182 |
'repeat_actions': True, |
| 183 |
'event_rule': { |
| 184 |
'event_type': 'event', |
| 185 |
'query': [{'field': 'resource', |
| 186 |
'op': 'eq', |
| 187 |
'type': '', |
| 188 |
'value': 'fake-resource-id'}] |
| 189 |
}, |
| 190 |
'threshold_rule': {'comparison_operator': 'le', |
| 191 |
'evaluation_periods': 60, |
| 192 |
'query': [{'field': 'resource', |
| 193 |
'op': 'eq', |
| 194 |
'type': '', |
| 195 |
'value': 'fake-resource-id'}], |
| 196 |
'threshold': 80.0}, |
| 197 |
'prometheus_rule': {'comparison_operator': 'le', |
| 198 |
'query': [{'field': 'resource', |
| 199 |
'op': 'eq', |
| 200 |
'type': '', |
| 201 |
'value': 'fake-resource-id'}], |
| 202 |
'threshold': 80.0}, |
| 203 |
'gnocchi_resources_threshold_rule': { |
| 204 |
'granularity': '60', |
| 205 |
'metric': 'cpu', |
| 206 |
'aggregation_method': 'last', |
| 207 |
'evaluation_periods': 60, |
| 208 |
'resource_id': '01919bbd-8b0e-451c-be28-abe250ae9c1c', |
| 209 |
'comparison_operator': 'le', |
| 210 |
'threshold': 80.0, |
| 211 |
'resource_type': 'generic' |
| 212 |
}, |
| 213 |
'gnocchi_aggregation_by_metrics_threshold_rule': { |
| 214 |
'granularity': '60', |
| 215 |
'aggregation_method': 'last', |
| 216 |
'evaluation_periods': 60, |
| 217 |
'comparison_operator': 'le', |
| 218 |
'threshold': 80.0, |
| 219 |
'metrics': ['cpu'], |
| 220 |
}, |
| 221 |
'loadbalancer_member_health_rule': { |
| 222 |
'autoscaling_group_id': 'abe250ae9c1c-79832aabf-343ba-be28', |
| 223 |
'pool_id': '79832aabf-343ba-be28-abe250ae9c1c', |
| 224 |
'stack_id': '0809ab348-8b0e-451c-be28-abe250ae9c1c' |
| 225 |
}, |
| 226 |
'gnocchi_aggregation_by_resources_threshold_rule': { |
| 227 |
'granularity': '60', |
| 228 |
'metric': 'cpu', |
| 229 |
'aggregation_method': 'last', |
| 230 |
'evaluation_periods': 60, |
| 231 |
'comparison_operator': 'le', |
| 232 |
'threshold': 80.0, |
| 233 |
'query': [{'field': 'resource', |
| 234 |
'op': 'eq', |
| 235 |
'type': '', |
| 236 |
'value': 'fake-resource-id'}], |
| 237 |
'resource_type': 'generic' |
| 238 |
}, |
| 239 |
'composite_rule': None, |
| 240 |
'type': 'event' |
| 241 |
} |
| 242 |
alarm_rep = self.cli_alarm_create._alarm_from_args(test_parsed_args) |
| 243 |
self.assertEqual(alarm, alarm_rep) |
| 244 |
|
| 245 |
def test_alarm_from_args_for_prometheus(self): |
| 246 |
# The test case to cover the method _alarm_from_args |
| 247 |
parser = self.cli_alarm_create.get_parser('aodh alarm create') |
| 248 |
test_parsed_args = parser.parse_args([ |
| 249 |
'--name', 'alarm_prom', |
| 250 |
'--type', 'prometheus', |
| 251 |
'--comparison-operator', 'gt', |
| 252 |
'--threshold', '666', |
| 253 |
'--query', r'some_metric{some_label="some_value"}' |
| 254 |
]) |
| 255 |
|
| 256 |
prom_rule = {'comparison_operator': 'gt', |
| 257 |
'query': r'some_metric{some_label="some_value"}', |
| 258 |
'threshold': 666.0} |
| 259 |
|
| 260 |
alarm_rep = self.cli_alarm_create._alarm_from_args(test_parsed_args) |
| 261 |
self.assertEqual(prom_rule, alarm_rep['prometheus_rule']) |
| 262 |
|
| 263 |
def test_validate_time_constraint(self): |
| 264 |
starts = ['0 11 * * *', ' 0 11 * * * ', |
| 265 |
'"0 11 * * *"', '\'0 11 * * *\''] |
| 266 |
for start in starts: |
| 267 |
string = 'name=const1;start=%s;duration=1' % start |
| 268 |
expected = dict(name='const1', |
| 269 |
start='0 11 * * *', |
| 270 |
duration='1') |
| 271 |
self.assertEqual( |
| 272 |
expected, |
| 273 |
self.cli_alarm_create.validate_time_constraint(string)) |
| 274 |
|
| 275 |
def test_validate_time_constraint_with_bad_format(self): |
| 276 |
string = 'name=const2;start="0 11 * * *";duration:2' |
| 277 |
self.assertRaises(argparse.ArgumentTypeError, |
| 278 |
self.cli_alarm_create.validate_time_constraint, |
| 279 |
string) |