Debian Codegraph @

heatclient/tests/unit/test_events.py

raw symbols SCIP
1 # Copyright 2013 IBM Corp.
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 import testtools
18
19 from heatclient.common import utils
20 from heatclient.v1 import events
21
22
23 class EventManagerTest(testtools.TestCase):
24
25 def test_list_event(self):
26 stack_id = 'teststack',
27 resource_name = 'testresource'
28 manager = events.EventManager(None)
29 with mock.patch('heatclient.v1.events.EventManager._resolve_stack_id')\
30 as mock_re:
31 mock_re.return_value = 'teststack/abcd1234'
32
33 manager._list = mock.MagicMock()
34 manager.list(stack_id, resource_name)
35 # Make sure url is correct.
36 manager._list.assert_called_once_with(
37 '/stacks/teststack/abcd1234/'
38 'resources/testresource/events',
39 "events")
40 mock_re.assert_called_once_with(stack_id)
41
42 def test_list_event_with_unicode_resource_name(self):
43 stack_id = 'teststack',
44 resource_name = '\u5de5\u4f5c'
45 manager = events.EventManager(None)
46 with mock.patch('heatclient.v1.events.EventManager._resolve_stack_id')\
47 as mock_re:
48 mock_re.return_value = 'teststack/abcd1234'
49
50 manager._list = mock.MagicMock()
51 manager.list(stack_id, resource_name)
52 # Make sure url is correct.
53 manager._list.assert_called_once_with(
54 '/stacks/teststack/abcd1234/'
55 'resources/%E5%B7%A5%E4%BD%9C/'
56 'events', "events")
57 mock_re.assert_called_once_with(stack_id)
58
59 def test_list_event_with_none_resource_name(self):
60 stack_id = 'teststack',
61 manager = events.EventManager(None)
62 manager._list = mock.MagicMock()
63 manager.list(stack_id)
64 # Make sure url is correct.
65 manager._list.assert_called_once_with('/stacks/teststack/'
66 'events', "events")
67
68 def test_list_event_with_kwargs(self):
69 stack_id = 'teststack',
70 resource_name = 'testresource'
71 kwargs = {'limit': 2,
72 'marker': '6d6935f4-0ae5',
73 'filters': {
74 'resource_action': 'CREATE',
75 'resource_status': 'COMPLETE'
76 }}
77 manager = events.EventManager(None)
78 manager = events.EventManager(None)
79 with mock.patch('heatclient.v1.events.EventManager._resolve_stack_id')\
80 as mock_re:
81 mock_re.return_value = 'teststack/abcd1234'
82
83 manager._list = mock.MagicMock()
84 manager.list(stack_id, resource_name, **kwargs)
85 # Make sure url is correct.
86 self.assertEqual(1, manager._list.call_count)
87 args = manager._list.call_args
88 self.assertEqual(2, len(args[0]))
89 url, param = args[0]
90 self.assertEqual("events", param)
91 base_url, query_params = utils.parse_query_url(url)
92 expected_base_url = ('/stacks/teststack/abcd1234/'
93 'resources/testresource/events')
94 self.assertEqual(expected_base_url, base_url)
95 expected_query_dict = {'marker': ['6d6935f4-0ae5'],
96 'limit': ['2'],
97 'resource_action': ['CREATE'],
98 'resource_status': ['COMPLETE']}
99 self.assertEqual(expected_query_dict, query_params)
100 mock_re.assert_called_once_with(stack_id)
101
102 @mock.patch('heatclient.v1.events.EventManager._resolve_stack_id')
103 @mock.patch('heatclient.common.utils.get_response_body')
104 def test_get_event(self, mock_utils, mock_re):
105 fields = {'stack_id': 'teststack',
106 'resource_name': 'testresource',
107 'event_id': '1'}
108
109 class FakeAPI:
110 """Fake API and ensure request url is correct."""
111
112 def json_request(self, *args, **kwargs):
113 expect = ('GET',
114 '/stacks/teststack/abcd1234/resources'
115 '/testresource/events/1')
116 assert args == expect
117 return {}, {'event': []}
118
119 def get(self, *args, **kwargs):
120 pass
121
122 manager = events.EventManager(FakeAPI())
123 with mock.patch('heatclient.v1.events.Event'):
124 mock_utils.return_value = {'event': []}
125 mock_re.return_value = 'teststack/abcd1234'
126 manager.get(**fields)
127 mock_re.assert_called_once_with('teststack')
128
129 @mock.patch('heatclient.v1.events.EventManager._resolve_stack_id')
130 @mock.patch('heatclient.common.utils.get_response_body')
131 def test_get_event_with_unicode_resource_name(self, mock_utils, mock_re):
132 fields = {'stack_id': 'teststack',
133 'resource_name': '\u5de5\u4f5c',
134 'event_id': '1'}
135
136 class FakeAPI:
137 """Fake API and ensure request url is correct."""
138
139 def json_request(self, *args, **kwargs):
140 expect = ('GET',
141 '/stacks/teststack/abcd1234/resources'
142 '/%E5%B7%A5%E4%BD%9C/events/1')
143 assert args == expect
144 return {}, {'event': []}
145
146 def get(self, *args, **kwargs):
147 pass
148
149 manager = events.EventManager(FakeAPI())
150 with mock.patch('heatclient.v1.events.Event'):
151 mock_utils.return_value = {'event': []}
152 mock_re.return_value = 'teststack/abcd1234'
153 manager.get(**fields)
154 mock_re.assert_called_once_with('teststack')