From f46f142f64cbef054d45da1dd440cfa3536a7108 Mon Sep 17 00:00:00 2001 From: Kaiyan Sheng Date: Fri, 15 Jul 2016 15:43:11 -0600 Subject: [PATCH] Patch-notification failed with period=0 Patching from webhook notification to email notification fails when period is changed from 60 to 0 because when set period to be 0, the if statement "if args.period" returns false. Add unit tests for notification-patch and notification-update Change-Id: Id767514814e1f1ff72fb01da098dfef81c98f616 (cherry picked from commit f745813366578c467e20922d676cc203460f7369) --- monascaclient/tests/test_shell.py | 62 ++++++++++++++++++++++++++++++- monascaclient/v2_0/shell.py | 4 +- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/monascaclient/tests/test_shell.py b/monascaclient/tests/test_shell.py index 76a9f93..267a6f0 100644 --- a/monascaclient/tests/test_shell.py +++ b/monascaclient/tests/test_shell.py @@ -1,4 +1,4 @@ -# (C) Copyright 2014-2016 Hewlett Packard Enterprise Development Company LP +# (C) Copyright 2014-2016 Hewlett Packard Enterprise Development LP # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -279,6 +279,66 @@ class ShellTestMonascaCommands(ShellBase): retvalue = self.shell(argstr) self.assertRegexpMatches(retvalue, "id") + def test_good_notifications_patch(self): + self._script_keystone_client() + + id_str = '0495340b-58fd-4e1c-932b-5e6f9cc96490' + resp = fakes.FakeHTTPResponse( + 201, + 'Created', + {'location': 'http://no.where/v2.0/notification-methods'}, + None) + http.HTTPClient.json_request( + 'PATCH', + '/notification-methods/' + id_str, + data={'type': 'EMAIL', + 'address': 'john.doe@hpe.com', + 'period': 0}, + headers={'X-Auth-Key': 'password', + 'X-Auth-User': 'username'}).AndReturn((resp, 'id')) + self.m.ReplayAll() + + argstring = 'notification-patch {0} --type EMAIL --address' \ + ' john.doe@hpe.com --period 0'.format(id_str) + retvalue = self.shell(argstring) + self.assertRegexpMatches(retvalue, "id") + + def test_bad_notifications_patch(self): + self._script_keystone_client() + + id_str = '0495340b-58fd-4e1c-932b-5e6f9cc96490' + argstring = 'notification-patch {0} --type EMAIL --address' \ + ' john.doe@hpe.com --period 60'.format(id_str) + self.m.ReplayAll() + + retvalue = self.shell(argstring) + self.assertRegexpMatches(retvalue, "^Invalid") + + def test_good_notifications_update(self): + self._script_keystone_client() + + id_str = '0495340b-58fd-4e1c-932b-5e6f9cc96491' + resp = fakes.FakeHTTPResponse( + 201, + 'Created', + {'location': 'http://no.where/v2.0/notification-methods'}, + None) + http.HTTPClient.json_request( + 'PUT', + '/notification-methods/' + id_str, + data={'name': 'notification_updated_name', + 'type': 'EMAIL', + 'address': 'john.doe@hpe.com', + 'period': 0}, + headers={'X-Auth-Key': 'password', + 'X-Auth-User': 'username'}).AndReturn((resp, 'id')) + self.m.ReplayAll() + + argstring = 'notification-update {0} notification_updated_name ' \ + 'EMAIL john.doe@hpe.com 0'.format(id_str) + retvalue = self.shell(argstring) + self.assertRegexpMatches(retvalue, "id") + def test_good_alarm_definition_update(self): self._script_keystone_client() diff --git a/monascaclient/v2_0/shell.py b/monascaclient/v2_0/shell.py index 1edca86..897cd1a 100644 --- a/monascaclient/v2_0/shell.py +++ b/monascaclient/v2_0/shell.py @@ -1,4 +1,4 @@ -# (C) Copyright 2014-2016 Hewlett Packard Enterprise Development Company LP +# (C) Copyright 2014-2016 Hewlett Packard Enterprise Development LP # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -693,7 +693,7 @@ def do_notification_patch(mc, args): fields['type'] = args.type if args.address: fields['address'] = args.address - if args.period: + if args.period or args.period == 0: if args.type and not _validate_notification_period( args.period, args.type.upper()): return