Debian Codegraph @

heatclient/tests/unit/test_resources.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 from urllib import parse
19
20 from heatclient.common import utils
21 from heatclient.v1 import resources
22
23
24 class FakeAPI:
25 """Fake API and ensure request url is correct."""
26
27 def __init__(self, expect, key):
28 self.expect = expect
29 self.key = key
30
31 def get(self, *args, **kwargs):
32 assert ('GET', args[0]) == self.expect
33
34 def json_request(self, *args, **kwargs):
35 assert args == self.expect
36 ret = self.key and {self.key: []} or {}
37 return {}, {self.key: ret}
38
39 def raw_request(self, *args, **kwargs):
40 assert args == self.expect
41 return {}
42
43 def head(self, url, **kwargs):
44 return self.json_request("HEAD", url, **kwargs)
45
46 def post(self, url, **kwargs):
47 return self.json_request("POST", url, **kwargs)
48
49 def put(self, url, **kwargs):
50 return self.json_request("PUT", url, **kwargs)
51
52 def delete(self, url, **kwargs):
53 return self.raw_request("DELETE", url, **kwargs)
54
55 def patch(self, url, **kwargs):
56 return self.json_request("PATCH", url, **kwargs)
57
58
59 class ResourceManagerTest(testtools.TestCase):
60
61 def _base_test(self, func, fields, expect, key):
62 manager = resources.ResourceManager(FakeAPI(expect, key))
63
64 with mock.patch.object(manager, '_resolve_stack_id') as mock_rslv, \
65 mock.patch.object(utils, 'get_response_body') as mock_resp:
66 mock_resp.return_value = {key: key and {key: []} or {}}
67 mock_rslv.return_value = 'teststack/abcd1234'
68
69 getattr(manager, func)(**fields)
70
71 mock_resp.assert_called_once_with(mock.ANY)
72 mock_rslv.assert_called_once_with('teststack')
73
74 def test_get(self):
75 fields = {'stack_id': 'teststack',
76 'resource_name': 'testresource'}
77 expect = ('GET',
78 '/stacks/teststack/abcd1234/resources'
79 '/testresource')
80 key = 'resource'
81
82 self._base_test('get', fields, expect, key)
83
84 def test_get_with_attr(self):
85 fields = {'stack_id': 'teststack',
86 'resource_name': 'testresource',
87 'with_attr': ['attr_a', 'attr_b']}
88 expect = ('GET',
89 '/stacks/teststack/abcd1234/resources'
90 '/testresource?with_attr=attr_a&with_attr=attr_b')
91 key = 'resource'
92
93 self._base_test('get', fields, expect, key)
94
95 def test_get_with_unicode_resource_name(self):
96 fields = {'stack_id': 'teststack',
97 'resource_name': '\u5de5\u4f5c'}
98 expect = ('GET',
99 '/stacks/teststack/abcd1234/resources'
100 '/%E5%B7%A5%E4%BD%9C')
101 key = 'resource'
102
103 self._base_test('get', fields, expect, key)
104
105 def _test_list(self, fields, expect):
106 key = 'resources'
107
108 class FakeResponse:
109 def json(self):
110 return {key: {}}
111
112 class FakeClient:
113 def get(self, *args, **kwargs):
114 assert args[0] == expect
115 return FakeResponse()
116
117 manager = resources.ResourceManager(FakeClient())
118 manager.list(**fields)
119
120 def test_list(self):
121 self._test_list(
122 fields={'stack_id': 'teststack'},
123 expect='/stacks/teststack/resources')
124
125 def test_list_nested(self):
126 self._test_list(
127 fields={'stack_id': 'teststack', 'nested_depth': '99'},
128 expect='/stacks/teststack/resources?%s' % parse.urlencode({
129 'nested_depth': 99
130 }, True)
131 )
132
133 def test_list_filtering(self):
134 self._test_list(
135 fields={'stack_id': 'teststack', 'filters': {'name': 'rsc_1'}},
136 expect='/stacks/teststack/resources?%s' % parse.urlencode({
137 'name': 'rsc_1'
138 }, True)
139 )
140
141 def test_list_detail(self):
142 self._test_list(
143 fields={'stack_id': 'teststack', 'with_detail': 'True'},
144 expect='/stacks/teststack/resources?%s' % parse.urlencode({
145 'with_detail': True,
146 }, True)
147 )
148
149 def test_metadata(self):
150 fields = {'stack_id': 'teststack',
151 'resource_name': 'testresource'}
152 expect = ('GET',
153 '/stacks/teststack/abcd1234/resources'
154 '/testresource/metadata')
155 key = 'metadata'
156
157 self._base_test('metadata', fields, expect, key)
158
159 def test_generate_template(self):
160 fields = {'resource_name': 'testresource'}
161 expect = ('GET', '/resource_types/testresource/template')
162 key = None
163
164 class FakeAPI:
165 """Fake API and ensure request url is correct."""
166
167 def get(self, *args, **kwargs):
168 assert ('GET', args[0]) == expect
169
170 def json_request(self, *args, **kwargs):
171 assert args == expect
172 ret = key and {key: []} or {}
173 return {}, {key: ret}
174
175 manager = resources.ResourceManager(FakeAPI())
176
177 with mock.patch.object(utils, 'get_response_body') as mock_resp:
178 mock_resp.return_value = {key: key and {key: []} or {}}
179
180 manager.generate_template(**fields)
181
182 mock_resp.assert_called_once_with(mock.ANY)
183
184 def test_signal(self):
185 fields = {'stack_id': 'teststack',
186 'resource_name': 'testresource',
187 'data': 'Some content'}
188 expect = ('POST',
189 '/stacks/teststack/abcd1234/resources'
190 '/testresource/signal')
191 key = 'signal'
192
193 self._base_test('signal', fields, expect, key)
194
195 def test_mark_unhealthy(self):
196 fields = {'stack_id': 'teststack',
197 'resource_name': 'testresource',
198 'mark_unhealthy': 'True',
199 'resource_status_reason': 'Anything'}
200 expect = ('PATCH',
201 '/stacks/teststack/abcd1234/resources'
202 '/testresource')
203 key = 'mark_unhealthy'
204
205 self._base_test('mark_unhealthy', fields, expect, key)
206
207
208 class ResourceStackNameTest(testtools.TestCase):
209
210 def test_stack_name(self):
211 resource = resources.Resource(None, {"links": [{
212 "href": "http://heat.example.com:8004/foo/12/resources/foobar",
213 "rel": "self"
214 }, {
215 "href": "http://heat.example.com:8004/foo/12",
216 "rel": "stack"
217 }]})
218 self.assertEqual('foo', resource.stack_name)
219
220 def test_stack_name_no_links(self):
221 resource = resources.Resource(None, {})
222 self.assertIsNone(resource.stack_name)