Debian Codegraph @

monascaclient/tests/v2_0/shell/test_notifications.py

raw
1 # Copyright 2017 FUJITSU LIMITED
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 from unittest import mock
16
17 from oslotest import base
18
19 from monascaclient.osc import migration as migr
20 from monascaclient.v2_0 import notifications
21 from monascaclient.v2_0 import shell
22
23
24 class FakeV2Client(object):
25
26 def __init__(self):
27 super(FakeV2Client, self).__init__()
28 self.notifications = mock.Mock(spec=notifications.NotificationsManager)
29
30
31 class TestNotificationsShellV2(base.BaseTestCase):
32
33 @mock.patch('monascaclient.osc.migration.make_client')
34 def test_notification_create_email(self, mc):
35 mc.return_value = c = FakeV2Client()
36
37 raw_args = ['email1', 'EMAIL', '[email protected]']
38 name, cmd_clazz = migr.create_command_class('do_notification_create',
39 shell)
40 cmd = cmd_clazz(mock.Mock(), mock.Mock())
41
42 parser = cmd.get_parser(name)
43 parsed_args = parser.parse_args(raw_args)
44 cmd.run(parsed_args)
45
46 data = {'name': 'email1',
47 'type': 'EMAIL',
48 'address': '[email protected]'}
49
50 c.notifications.create.assert_called_once_with(**data)
51
52 @mock.patch('monascaclient.osc.migration.make_client')
53 def test_notification_create_webhook(self, mc):
54 mc.return_value = c = FakeV2Client()
55
56 raw_args = ['mypost', 'WEBHOOK', 'http://localhost:8080']
57 name, cmd_clazz = migr.create_command_class('do_notification_create',
58 shell)
59 cmd = cmd_clazz(mock.Mock(), mock.Mock())
60
61 parser = cmd.get_parser(name)
62 parsed_args = parser.parse_args(raw_args)
63 cmd.run(parsed_args)
64
65 data = {'name': 'mypost',
66 'type': 'WEBHOOK',
67 'address': 'http://localhost:8080'}
68
69 c.notifications.create.assert_called_once_with(**data)
70
71 @mock.patch('monascaclient.osc.migration.make_client')
72 def test_good_notifications_patch(self, mc):
73 args = '--type EMAIL --address [email protected] --period 0'
74 data = {'type': 'EMAIL',
75 'address': '[email protected]',
76 'period': 0}
77 self._patch_test(mc, args, data)
78
79 @mock.patch('monascaclient.osc.migration.make_client')
80 def test_good_notifications_patch_just_name(self, mc):
81 name = 'fred'
82 args = '--name ' + name
83 data = {'name': name}
84 self._patch_test(mc, args, data)
85
86 @mock.patch('monascaclient.osc.migration.make_client')
87 def test_good_notifications_patch_just_address(self, mc):
88 address = '[email protected]'
89 args = '--address ' + address
90 data = {'address': address}
91 self._patch_test(mc, args, data)
92
93 @mock.patch('monascaclient.osc.migration.make_client')
94 def test_good_notifications_patch_just_period(self, mc):
95 period = 0
96 args = '--period ' + str(period)
97 data = {'period': period}
98 self._patch_test(mc, args, data)
99
100 @mock.patch('monascaclient.osc.migration.make_client')
101 def test_good_notifications_patch_recurring_email(self, mc):
102 args = '--type EMAIL --address [email protected] --period 60'
103 data = {'type': 'EMAIL',
104 'address': '[email protected]',
105 'period': 60}
106 self._patch_test(mc, args, data)
107
108 @mock.patch('monascaclient.osc.migration.make_client')
109 def test_good_notifications_update(self, mc):
110 mc.return_value = c = FakeV2Client()
111
112 id_str = '0495340b-58fd-4e1c-932b-5e6f9cc96491'
113 raw_args = ('{0} notification_updated_name '
114 'EMAIL [email protected] 0').format(id_str).split(' ')
115 name, cmd_clazz = migr.create_command_class('do_notification_update',
116 shell)
117 cmd = cmd_clazz(mock.Mock(), mock.Mock())
118
119 parser = cmd.get_parser(name)
120 parsed_args = parser.parse_args(raw_args)
121
122 cmd.run(parsed_args)
123
124 data = {
125 'name': 'notification_updated_name',
126 'type': 'EMAIL',
127 'address': '[email protected]',
128 'period': 0,
129 'notification_id': id_str
130 }
131
132 c.notifications.update.assert_called_once_with(**data)
133
134 @staticmethod
135 def _patch_test(mc, args, data):
136 mc.return_value = c = FakeV2Client()
137
138 id_str = '0495340b-58fd-4e1c-932b-5e6f9cc96490'
139 raw_args = '{0} {1}'.format(id_str, args).split(' ')
140 name, cmd_clazz = migr.create_command_class('do_notification_patch',
141 shell)
142 cmd = cmd_clazz(mock.Mock(), mock.Mock())
143
144 parser = cmd.get_parser(name)
145 parsed_args = parser.parse_args(raw_args)
146 cmd.run(parsed_args)
147
148 # add notification_id to data
149 data['notification_id'] = id_str
150
151 c.notifications.patch.assert_called_once_with(**data)