diff --git a/monasca_api/tests/test_ad_repository.py b/monasca_api/tests/test_ad_repository.py index 7f58a5ec3..05d6f6fe2 100644 --- a/monasca_api/tests/test_ad_repository.py +++ b/monasca_api/tests/test_ad_repository.py @@ -1,5 +1,6 @@ # Copyright 2015 Cray # Copyright 2016 FUJITSU LIMITED +# (C) Copyright 2016 Hewlett Packard Enterprise Development Company LP # # 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 @@ -433,8 +434,16 @@ class TestAlarmDefinitionRepoDB(testtools.TestCase, fixtures.TestWithFixtures): [], [], None, None, True) - from monasca_api.common.repositories import exceptions + self.repo.update_or_patch_alarm_definition('bob', '234', + None, None, + None, False, + None, None, + None, None, + match_by, None, + False) + + from monasca_api.common.repositories import exceptions self.assertRaises(exceptions.InvalidUpdateException, self.repo.update_or_patch_alarm_definition, 'bob', '234', @@ -445,6 +454,15 @@ class TestAlarmDefinitionRepoDB(testtools.TestCase, fixtures.TestWithFixtures): None, None, False) + self.assertRaises(exceptions.InvalidUpdateException, + self.repo.update_or_patch_alarm_definition, + 'bob', '234', + '90% CPU', None, + sub_expr_list, False, + description, alarm_actions, + None, None, + 'update_match_by', 'LOW') + self.repo.delete_alarm_definition('bob', '234') self.assertRaises(exceptions.DoesNotExistException, diff --git a/monasca_api/v2/reference/alarm_definitions.py b/monasca_api/v2/reference/alarm_definitions.py index 7985e9e07..ff3ac1a84 100644 --- a/monasca_api/v2/reference/alarm_definitions.py +++ b/monasca_api/v2/reference/alarm_definitions.py @@ -1,4 +1,4 @@ -# Copyright 2014,2016 Hewlett Packard Enterprise Development Company, L.P. +# (C) Copyright 2014,2016 Hewlett Packard Enterprise Development Company LP # # 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 diff --git a/monasca_tempest_tests/services/monasca_client.py b/monasca_tempest_tests/services/monasca_client.py index 3f0ee18ff..57ec86f25 100644 --- a/monasca_tempest_tests/services/monasca_client.py +++ b/monasca_tempest_tests/services/monasca_client.py @@ -107,20 +107,12 @@ class MonascaClient(rest_client.RestClient): resp, response_body = self.get(uri) return resp, json.loads(response_body) - def update_notification_method(self, - id, - name=None, - type=None, - address=None, - period=None): + def update_notification_method(self, id, name, type, address, period=None): uri = 'notification-methods/' + id request_body = {} - if name is not None: - request_body['name'] = name - if type is not None: - request_body['type'] = type - if address is not None: - request_body['address'] = address + request_body['name'] = name + request_body['type'] = type + request_body['address'] = address if period is not None: request_body['period'] = period resp, response_body = self.put(uri, json.dumps(request_body)) @@ -168,35 +160,27 @@ class MonascaClient(rest_client.RestClient): resp, response_body = self.delete(uri) return resp, response_body - def update_alarm_definition(self, id, name, expression, description=None, - actions_enabled=None, match_by=None, - severity=None, alarm_actions=None, - ok_actions=None, undetermined_actions=None, + def update_alarm_definition(self, id, name, expression, description, + actions_enabled, match_by, + severity, alarm_actions, + ok_actions, undetermined_actions, **kwargs): uri = 'alarm-definitions/' + id request_body = {} request_body['name'] = name request_body['expression'] = expression - - if description is not None: - request_body['description'] = description - if actions_enabled is not None: - request_body['actions_enabled'] = actions_enabled - if match_by is not None: - request_body['match_by'] = match_by - if severity is not None: - request_body['severity'] = severity - if alarm_actions is not None: - request_body['alarm_actions'] = alarm_actions - if ok_actions is not None: - request_body['ok_actions'] = ok_actions - if undetermined_actions is not None: - request_body['undetermined_actions'] = undetermined_actions + request_body['description'] = description + request_body['actions_enabled'] = actions_enabled + request_body['match_by'] = match_by + request_body['severity'] = severity + request_body['alarm_actions'] = alarm_actions + request_body['ok_actions'] = ok_actions + request_body['undetermined_actions'] = undetermined_actions for key, value in kwargs.iteritems(): request_body[key] = value - resp, response_body = self.patch(uri, json.dumps(request_body)) + resp, response_body = self.put(uri, json.dumps(request_body)) return resp, json.loads(response_body) def patch_alarm_definition(self, @@ -305,3 +289,38 @@ class MonascaClient(rest_client.RestClient): uri = uri + query_params resp, response_body = self.get(uri) return resp, json.loads(response_body) + + # For Negative Tests + def update_alarm_definition_with_no_ok_actions(self, id, name, + expression, description, + actions_enabled, match_by, + severity, alarm_actions, + undetermined_actions, + **kwargs): + uri = 'alarm-definitions/' + id + request_body = {} + request_body['name'] = name + request_body['expression'] = expression + request_body['description'] = description + request_body['actions_enabled'] = actions_enabled + request_body['match_by'] = match_by + request_body['severity'] = severity + request_body['alarm_actions'] = alarm_actions + request_body['undetermined_actions'] = undetermined_actions + + for key, value in kwargs.iteritems(): + request_body[key] = value + + resp, response_body = self.put(uri, json.dumps(request_body)) + return resp, json.loads(response_body) + + def update_notification_method_with_no_address(self, id, name, type, + period=None): + uri = 'notification-methods/' + id + request_body = {} + request_body['name'] = name + request_body['type'] = type + if period is not None: + request_body['period'] = period + resp, response_body = self.put(uri, json.dumps(request_body)) + return resp, json.loads(response_body) diff --git a/monasca_tempest_tests/tests/api/test_alarm_definitions.py b/monasca_tempest_tests/tests/api/test_alarm_definitions.py index 4d75a8b02..45035780b 100644 --- a/monasca_tempest_tests/tests/api/test_alarm_definitions.py +++ b/monasca_tempest_tests/tests/api/test_alarm_definitions.py @@ -702,19 +702,26 @@ class TestAlarmDefinitions(base.BaseMonascaTest): @test.attr(type="gate") def test_update_alarm_definition(self): + notification_name = data_utils.rand_name('notification-') + notification_type = 'EMAIL' + address = 'root@localhost' + + resp, response_body = self.monasca_client.create_notification_method( + notification_name, type=notification_type, address=address) + notification_id = self._verify_create_notification_method( + resp, response_body, notification_name, notification_type, address) + response_body_list = self._create_alarm_definitions( expression=None, number_of_definitions=1) # Update alarm definition updated_name = data_utils.rand_name('updated_name') updated_description = 'updated description' updated_expression = "max(cpu.system_perc) < 0" + resp, response_body = self.monasca_client.update_alarm_definition( - id=str(response_body_list[0]['id']), - name=updated_name, - expression=updated_expression, - description=updated_description, - actions_enabled='true' - ) + str(response_body_list[0]['id']), updated_name, updated_expression, + updated_description, True, response_body_list[0]['match_by'], + 'LOW', [notification_id], [notification_id], [notification_id]) self.assertEqual(200, resp.status) self._verify_update_patch_alarm_definition(response_body, updated_name, updated_expression, @@ -739,10 +746,24 @@ class TestAlarmDefinitions(base.BaseMonascaTest): updated_match_by = ['hostname'] self.assertRaises(exceptions.UnprocessableEntity, self.monasca_client.update_alarm_definition, - id=response_body_list[0]['id'], name=name, - expression=expression, - description=description, actions_enabled='true', - match_by=updated_match_by) + response_body_list[0]['id'], name, expression, + description, 'true', updated_match_by, 'LOW', None, + None, None) + + @test.attr(type="gate") + @test.attr(type=['negative']) + def test_update_alarm_definition_with_no_ok_actions(self): + response_body_list = self._create_alarm_definitions( + expression=None, number_of_definitions=1) + name = response_body_list[0]['name'] + expression = response_body_list[0]['expression'] + description = response_body_list[0]['description'] + updated_match_by = ['hostname'] + self.assertRaises( + exceptions.UnprocessableEntity, + self.monasca_client.update_alarm_definition_with_no_ok_actions, + response_body_list[0]['id'], name, expression, description, + 'true', updated_match_by, 'LOW', None, None) @test.attr(type="gate") def test_update_notification_in_alarm_definition(self): @@ -763,13 +784,9 @@ class TestAlarmDefinitions(base.BaseMonascaTest): # Update alarm definition update_alarm_def_name = data_utils.rand_name('monitoring_alarm_update') resp, response_body = self.monasca_client.update_alarm_definition( - response_body_list[0]['id'], - name=update_alarm_def_name, - expression=expression, - actions_enabled='true', - alarm_actions=[notification_id], - ok_actions=[notification_id], - undetermined_actions=[notification_id]) + response_body_list[0]['id'], update_alarm_def_name, expression, + 'description', True, response_body_list[0]['match_by'], 'LOW', + [notification_id], [notification_id], [notification_id]) self.assertEqual(200, resp.status) self._verify_update_patch_alarm_definition(response_body, update_alarm_def_name, diff --git a/monasca_tempest_tests/tests/api/test_notification_methods.py b/monasca_tempest_tests/tests/api/test_notification_methods.py index 44c0f486f..fe2d53598 100644 --- a/monasca_tempest_tests/tests/api/test_notification_methods.py +++ b/monasca_tempest_tests/tests/api/test_notification_methods.py @@ -570,6 +570,24 @@ class TestNotificationMethods(base.BaseMonascaTest): self.monasca_client.delete_notification_method(id) self.assertEqual(204, resp.status) + @test.attr(type="gate") + @test.attr(type=['negative']) + def test_update_notification_method_with_no_address(self): + name = data_utils.rand_name('notification-') + notification = helpers.create_notification(name=name) + resp, response_body = self.monasca_client.create_notifications( + notification) + id = response_body['id'] + self.assertEqual(201, resp.status) + self.assertRaises( + (exceptions.BadRequest, exceptions.UnprocessableEntity), + self.monasca_client.update_notification_method_with_no_address, id, + name="test_update_notification_method_name", + type=response_body['type']) + resp, response_body = \ + self.monasca_client.delete_notification_method(id) + self.assertEqual(204, resp.status) + @test.attr(type="gate") def test_create_and_delete_notification_method(self): notification = helpers.create_notification()