| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 1 | # Copyright 2017 iIntel Research and Development Ireland Limited |
| 2 | # ************************************************************** |
| 3 | |
| 4 | # This file is part of OSM Monitoring module |
| 5 | # All Rights Reserved to Intel Corporation |
| 6 | |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | # not use this file except in compliance with the License. You may obtain |
| 9 | # a copy of the License at |
| 10 | |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | # License for the specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | |
| 19 | # For those usages not covered by the Apache License, Version 2.0 please |
| 20 | # contact: helena.mcgough@intel.com or adrian.hoban@intel.com |
| 21 | ## |
| 22 | """Tests for all alarm request message keys.""" |
| 23 | |
| 24 | import json |
| 25 | |
| 26 | import logging |
| 27 | |
| 28 | import unittest |
| 29 | |
| 30 | import mock |
| 31 | |
| 32 | from plugins.OpenStack.Aodh import alarming as alarm_req |
| 33 | from plugins.OpenStack.common import Common |
| 34 | |
| 35 | __author__ = "Helena McGough" |
| 36 | |
| 37 | log = logging.getLogger(__name__) |
| 38 | |
| 39 | auth_token = mock.ANY |
| 40 | endpoint = mock.ANY |
| 41 | |
| 42 | |
| 43 | class Response(object): |
| 44 | """Mock a response message class.""" |
| 45 | |
| 46 | def __init__(self, result): |
| 47 | """Initialise the response text and status code.""" |
| 48 | self.text = json.dumps(result) |
| 49 | self.status_code = "MOCK_STATUS_CODE" |
| 50 | |
| 51 | |
| 52 | class TestAlarming(unittest.TestCase): |
| 53 | """Tests for alarming class functions.""" |
| 54 | |
| 55 | def setUp(self): |
| 56 | """Setup for tests.""" |
| 57 | super(TestAlarming, self).setUp() |
| 58 | self.alarming = alarm_req.Alarming() |
| 59 | self.alarming.common = Common() |
| 60 | |
| 61 | @mock.patch.object(alarm_req.Alarming, "check_payload") |
| 62 | @mock.patch.object(alarm_req.Alarming, "check_for_metric") |
| 63 | @mock.patch.object(Common, "_perform_request") |
| 64 | def test_config_invalid_alarm_req(self, perf_req, check_metric, check_pay): |
| 65 | """Test configure an invalid alarm request.""" |
| 66 | # Configuring with invalid alarm name results in failure |
| 67 | values = {"alarm_name": "my_alarm", |
| 68 | "metric_name": "my_metric", |
| 69 | "resource_uuid": "my_r_id"} |
| 70 | self.alarming.configure_alarm(endpoint, auth_token, values) |
| 71 | perf_req.assert_not_called |
| 72 | perf_req.reset_mock() |
| 73 | |
| 74 | # Correct alarm_name will check for metric in Gnocchi |
| 75 | # If there isn't one an alarm won;t be created |
| 76 | values = {"alarm_name": "disk_write_ops", |
| 77 | "metric_name": "disk_write_ops", |
| 78 | "resource_uuid": "my_r_id"} |
| 79 | |
| 80 | check_metric.return_value = None |
| 81 | |
| 82 | self.alarming.configure_alarm(endpoint, auth_token, values) |
| 83 | perf_req.assert_not_called |
| 84 | |
| 85 | @mock.patch.object(alarm_req.Alarming, "check_payload") |
| 86 | @mock.patch.object(alarm_req.Alarming, "check_for_metric") |
| 87 | @mock.patch.object(Common, "_perform_request") |
| 88 | def test_config_valid_alarm_req(self, perf_req, check_metric, check_pay): |
| 89 | """Test config a valid alarm.""" |
| 90 | # Correct alarm_name will check for metric in Gnocchi |
| 91 | # And conform that the payload is configured correctly |
| 92 | values = {"alarm_name": "disk_write_ops", |
| 93 | "metric_name": "disk_write_ops", |
| 94 | "resource_uuid": "my_r_id"} |
| 95 | |
| 96 | check_metric.return_value = "my_metric_id" |
| 97 | check_pay.return_value = "my_payload" |
| 98 | |
| 99 | self.alarming.configure_alarm(endpoint, auth_token, values) |
| 100 | perf_req.assert_called_with( |
| 101 | "<ANY>/v2/alarms/", auth_token, |
| 102 | req_type="post", payload="my_payload") |
| 103 | |
| 104 | @mock.patch.object(Common, "_perform_request") |
| 105 | def test_delete_alarm_req(self, perf_req): |
| 106 | """Test delete alarm request.""" |
| 107 | self.alarming.delete_alarm(endpoint, auth_token, "my_alarm_id") |
| 108 | |
| 109 | perf_req.assert_called_with( |
| 110 | "<ANY>/v2/alarms/my_alarm_id", auth_token, req_type="delete") |
| 111 | |
| 112 | @mock.patch.object(Common, "_perform_request") |
| 113 | def test_invalid_list_alarm_req(self, perf_req): |
| 114 | """Test invalid list alarm_req.""" |
| 115 | # Request will not be performed with out a resoure_id |
| 116 | list_details = {"mock_details": "invalid_details"} |
| 117 | self.alarming.list_alarms(endpoint, auth_token, list_details) |
| 118 | |
| 119 | perf_req.assert_not_called |
| 120 | |
| 121 | @mock.patch.object(Common, "_perform_request") |
| 122 | def test_valid_list_alarm_req(self, perf_req): |
| 123 | """Test valid list alarm request.""" |
| 124 | # Minimum requirement for an alarm list is resource_id |
| 125 | list_details = {"resource_uuid": "mock_r_id"} |
| 126 | self.alarming.list_alarms(endpoint, auth_token, list_details) |
| 127 | |
| 128 | perf_req.assert_called_with( |
| 129 | "<ANY>/v2/alarms/", auth_token, req_type="get") |
| 130 | perf_req.reset_mock() |
| 131 | |
| 132 | # Check list with alarm_name defined |
| 133 | list_details = {"resource_uuid": "mock_r_id", |
| 134 | "alarm_name": "my_alarm", |
| 135 | "severity": "critical"} |
| 136 | self.alarming.list_alarms(endpoint, auth_token, list_details) |
| 137 | |
| 138 | perf_req.assert_called_with( |
| 139 | "<ANY>/v2/alarms/", auth_token, req_type="get") |
| 140 | |
| 141 | @mock.patch.object(Common, "_perform_request") |
| 142 | def test_ack_alarm_req(self, perf_req): |
| 143 | """Test update alarm state for acknowledge alarm request.""" |
| 144 | self.alarming.update_alarm_state(endpoint, auth_token, "my_alarm_id") |
| 145 | |
| 146 | perf_req.assert_called_with( |
| 147 | "<ANY>/v2/alarms/my_alarm_id/state", auth_token, req_type="put", |
| 148 | payload=json.dumps("ok")) |
| 149 | |
| 150 | @mock.patch.object(alarm_req.Alarming, "check_payload") |
| 151 | @mock.patch.object(Common, "_perform_request") |
| 152 | def test_update_alarm_invalid(self, perf_req, check_pay): |
| 153 | """Test update alarm with invalid get response.""" |
| 154 | values = {"alarm_uuid": "my_alarm_id"} |
| 155 | |
| 156 | self.alarming.update_alarm(endpoint, auth_token, values) |
| 157 | |
| 158 | perf_req.assert_called_with(mock.ANY, auth_token, req_type="get") |
| 159 | check_pay.assert_not_called |
| 160 | |
| 161 | @mock.patch.object(alarm_req.Alarming, "check_payload") |
| 162 | @mock.patch.object(Common, "_perform_request") |
| 163 | def test_update_alarm_invalid_payload(self, perf_req, check_pay): |
| 164 | """Test update alarm with invalid payload.""" |
| 165 | resp = Response({"name": "my_alarm", |
| 166 | "state": "alarm", |
| 167 | "gnocchi_resources_threshold_rule": |
| 168 | {"resource_id": "my_resource_id", |
| 169 | "metric": "my_metric"}}) |
| 170 | perf_req.return_value = resp |
| 171 | check_pay.return_value = None |
| 172 | values = {"alarm_uuid": "my_alarm_id"} |
| 173 | |
| 174 | self.alarming.update_alarm(endpoint, auth_token, values) |
| 175 | |
| 176 | perf_req.assert_called_with(mock.ANY, auth_token, req_type="get") |
| 177 | self.assertEqual(perf_req.call_count, 1) |
| 178 | |
| 179 | @mock.patch.object(alarm_req.Alarming, "check_payload") |
| 180 | @mock.patch.object(Common, "_perform_request") |
| 181 | def test_update_alarm_valid(self, perf_req, check_pay): |
| 182 | """Test valid update alarm request.""" |
| 183 | resp = Response({"name": "my_alarm", |
| 184 | "state": "alarm", |
| 185 | "gnocchi_resources_threshold_rule": |
| 186 | {"resource_id": "my_resource_id", |
| 187 | "metric": "my_metric"}}) |
| 188 | perf_req.return_value = resp |
| 189 | values = {"alarm_uuid": "my_alarm_id"} |
| 190 | |
| 191 | self.alarming.update_alarm(endpoint, auth_token, values) |
| 192 | |
| 193 | check_pay.assert_called_with(values, "my_metric", "my_resource_id", |
| 194 | "my_alarm", alarm_state="alarm") |
| 195 | |
| 196 | self.assertEqual(perf_req.call_count, 2) |
| 197 | # Second call is the update request |
| 198 | perf_req.assert_called_with( |
| 199 | '<ANY>/v2/alarms/my_alarm_id', auth_token, |
| 200 | req_type="put", payload=check_pay.return_value) |
| 201 | |
| 202 | def test_check_valid_payload(self): |
| 203 | """Test the check payload function for a valid payload.""" |
| 204 | values = {"severity": "warning", |
| 205 | "statistic": "COUNT", |
| 206 | "threshold_value": 12, |
| 207 | "operation": "GT"} |
| 208 | payload = self.alarming.check_payload( |
| 209 | values, "my_metric", "r_id", "alarm_name") |
| 210 | |
| 211 | self.assertEqual( |
| 212 | json.loads(payload), {"name": "alarm_name", |
| 213 | "gnocchi_resources_threshold_rule": |
| 214 | {"resource_id": "r_id", |
| 215 | "metric": "my_metric", |
| 216 | "comparison_operator": "gt", |
| 217 | "aggregation_method": "count", |
| 218 | "threshold": 12, |
| 219 | "resource_type": "generic"}, |
| 220 | "severity": "low", |
| 221 | "state": "ok", |
| 222 | "type": "gnocchi_resources_threshold"}) |
| 223 | |
| 224 | def test_check_valid_state_payload(self): |
| 225 | """Test the check payload function for a valid payload with state.""" |
| 226 | values = {"severity": "warning", |
| 227 | "statistic": "COUNT", |
| 228 | "threshold_value": 12, |
| 229 | "operation": "GT"} |
| 230 | payload = self.alarming.check_payload( |
| 231 | values, "my_metric", "r_id", "alarm_name", alarm_state="alarm") |
| 232 | |
| 233 | self.assertEqual( |
| 234 | json.loads(payload), {"name": "alarm_name", |
| 235 | "gnocchi_resources_threshold_rule": |
| 236 | {"resource_id": "r_id", |
| 237 | "metric": "my_metric", |
| 238 | "comparison_operator": "gt", |
| 239 | "aggregation_method": "count", |
| 240 | "threshold": 12, |
| 241 | "resource_type": "generic"}, |
| 242 | "severity": "low", |
| 243 | "state": "alarm", |
| 244 | "type": "gnocchi_resources_threshold"}) |
| 245 | |
| 246 | def test_check_invalid_payload(self): |
| 247 | """Test the check payload function for an invalid payload.""" |
| 248 | values = {"alarm_values": "mock_invalid_details"} |
| 249 | payload = self.alarming.check_payload( |
| 250 | values, "my_metric", "r_id", "alarm_name") |
| 251 | |
| 252 | self.assertEqual(payload, None) |
| 253 | |
| 254 | @mock.patch.object(Common, "_perform_request") |
| 255 | def test_get_alarm_state(self, perf_req): |
| 256 | """Test the get alarm state function.""" |
| 257 | self.alarming.get_alarm_state(endpoint, auth_token, "alarm_id") |
| 258 | |
| 259 | perf_req.assert_called_with( |
| 260 | "<ANY>/v2/alarms/alarm_id/state", auth_token, req_type="get") |
| 261 | |
| 262 | @mock.patch.object(Common, "get_endpoint") |
| 263 | @mock.patch.object(Common, "_perform_request") |
| 264 | def test_check_for_metric(self, perf_req, get_endpoint): |
| 265 | """Test the check for metric function.""" |
| 266 | get_endpoint.return_value = "gnocchi_endpoint" |
| 267 | |
| 268 | self.alarming.check_for_metric(auth_token, "metric_name", "r_id") |
| 269 | |
| 270 | perf_req.assert_called_with( |
| 271 | "gnocchi_endpoint/v1/metric/", auth_token, req_type="get") |