Debian Codegraph @

heatclient/tests/unit/test_utils.py

raw symbols SCIP
1 # Copyright 2012 OpenStack Foundation
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import os
17 from unittest import mock
18 from urllib import request
19
20 import testtools
21
22 from heatclient.common import utils
23 from heatclient import exc
24 from heatclient.v1 import resources as hc_res
25
26
27 class ShellTest(testtools.TestCase):
28
29 def test_format_parameter_none(self):
30 self.assertEqual({}, utils.format_parameters(None))
31
32 def test_format_parameters(self):
33 p = utils.format_parameters([
34 'InstanceType=m1.large;DBUsername=wp;'
35 'DBPassword=verybadpassword;KeyName=heat_key;'
36 'LinuxDistribution=F17'])
37 self.assertEqual({'InstanceType': 'm1.large',
38 'DBUsername': 'wp',
39 'DBPassword': 'verybadpassword',
40 'KeyName': 'heat_key',
41 'LinuxDistribution': 'F17'
42 }, p)
43
44 def test_format_parameters_split(self):
45 p = utils.format_parameters([
46 'KeyName=heat_key;'
47 'DnsSecKey=hsgx1m31PbamNF4WEcHlwjIlCGgifOdoB58/wwC7a4oAONQ/fDV5ct'
48 'qrYBoLlKHhTfkyQEw9iVScKYZbbMtMNg==;'
49 'UpstreamDNS=8.8.8.8'])
50 self.assertEqual({'KeyName': 'heat_key',
51 'DnsSecKey': 'hsgx1m31PbamNF4WEcHlwjIlCGgifOdoB58/ww'
52 'C7a4oAONQ/fDV5ctqrYBoLlKHhTfkyQEw9iVScKYZbbMtMNg==',
53 'UpstreamDNS': '8.8.8.8'}, p)
54
55 def test_format_parameters_multiple(self):
56 p = utils.format_parameters([
57 'KeyName=heat_key',
58 'DnsSecKey=hsgx1m31PbamNF4WEcHlwjIlCGgifOdoB58/wwC7a4oAONQ/fDV5ct'
59 'qrYBoLlKHhTfkyQEw9iVScKYZbbMtMNg==',
60 'UpstreamDNS=8.8.8.8'])
61 self.assertEqual({'KeyName': 'heat_key',
62 'DnsSecKey': 'hsgx1m31PbamNF4WEcHlwjIlCGgifOdoB58/ww'
63 'C7a4oAONQ/fDV5ctqrYBoLlKHhTfkyQEw9iVScKYZbbMtMNg==',
64 'UpstreamDNS': '8.8.8.8'}, p)
65
66 def test_format_parameters_multiple_semicolon_values(self):
67 p = utils.format_parameters([
68 'KeyName=heat_key',
69 'DnsSecKey=hsgx1m31;PbaNF4WEcHlwj;IlCGgfOdoB;58/ww7a4oAO;NQ/fD==',
70 'UpstreamDNS=8.8.8.8'])
71 self.assertEqual({'KeyName': 'heat_key',
72 'DnsSecKey': 'hsgx1m31;PbaNF4WEcHlwj;IlCGgfOdoB;58/'
73 'ww7a4oAO;NQ/fD==',
74 'UpstreamDNS': '8.8.8.8'}, p)
75
76 def test_format_parameters_parse_semicolon_false(self):
77 p = utils.format_parameters(
78 ['KeyName=heat_key;UpstreamDNS=8.8.8.8;a=b'],
79 parse_semicolon=False)
80 self.assertEqual({'KeyName': 'heat_key;UpstreamDNS=8.8.8.8;a=b'}, p)
81
82 def test_format_parameters_multiple_values_per_pamaters(self):
83 p = utils.format_parameters([
84 'status=COMPLETE',
85 'status=FAILED'])
86 self.assertIn('status', p)
87 self.assertIn('COMPLETE', p['status'])
88 self.assertIn('FAILED', p['status'])
89
90 def test_format_parameter_bad_parameter(self):
91 params = ['KeyName=heat_key;UpstreamDNS8.8.8.8']
92 ex = self.assertRaises(exc.CommandError,
93 utils.format_parameters, params)
94 self.assertEqual('Malformed parameter(UpstreamDNS8.8.8.8). '
95 'Use the key=value format.', str(ex))
96
97 def test_format_multiple_bad_parameter(self):
98 params = ['KeyName=heat_key', 'UpstreamDNS8.8.8.8']
99 ex = self.assertRaises(exc.CommandError,
100 utils.format_parameters, params)
101 self.assertEqual('Malformed parameter(UpstreamDNS8.8.8.8). '
102 'Use the key=value format.', str(ex))
103
104 def test_link_formatter(self):
105 self.assertEqual('', utils.link_formatter(None))
106 self.assertEqual('', utils.link_formatter([]))
107 self.assertEqual(
108 'http://foo.example.com\nhttp://bar.example.com',
109 utils.link_formatter([
110 {'href': 'http://foo.example.com'},
111 {'href': 'http://bar.example.com'}]))
112 self.assertEqual(
113 'http://foo.example.com (a)\nhttp://bar.example.com (b)',
114 utils.link_formatter([
115 {'href': 'http://foo.example.com', 'rel': 'a'},
116 {'href': 'http://bar.example.com', 'rel': 'b'}]))
117 self.assertEqual(
118 '\n',
119 utils.link_formatter([
120 {'hrf': 'http://foo.example.com'},
121 {}]))
122
123 def test_resource_nested_identifier(self):
124 rsrc_info = {'resource_name': 'aresource',
125 'links': [{'href': 'http://foo/name/id/resources/0',
126 'rel': 'self'},
127 {'href': 'http://foo/name/id',
128 'rel': 'stack'},
129 {'href': 'http://foo/n_name/n_id',
130 'rel': 'nested'}]}
131 rsrc = hc_res.Resource(manager=None, info=rsrc_info)
132 self.assertEqual('n_name/n_id', utils.resource_nested_identifier(rsrc))
133
134 def test_resource_nested_identifier_none(self):
135 rsrc_info = {'resource_name': 'aresource',
136 'links': [{'href': 'http://foo/name/id/resources/0',
137 'rel': 'self'},
138 {'href': 'http://foo/name/id',
139 'rel': 'stack'}]}
140 rsrc = hc_res.Resource(manager=None, info=rsrc_info)
141 self.assertIsNone(utils.resource_nested_identifier(rsrc))
142
143 def test_json_formatter(self):
144 self.assertEqual('null', utils.json_formatter(None))
145 self.assertEqual('{}', utils.json_formatter({}))
146 self.assertEqual('{\n "foo": "bar"\n}',
147 utils.json_formatter({"foo": "bar"}))
148 self.assertEqual('{\n "Uni": "test\u2665"\n}',
149 utils.json_formatter({"Uni": "test\u2665"}))
150
151 def test_yaml_formatter(self):
152 self.assertEqual('null\n...\n', utils.yaml_formatter(None))
153 self.assertEqual('{}\n', utils.yaml_formatter({}))
154 self.assertEqual('foo: bar\n',
155 utils.yaml_formatter({"foo": "bar"}))
156
157 def test_text_wrap_formatter(self):
158 self.assertEqual('', utils.text_wrap_formatter(None))
159 self.assertEqual('', utils.text_wrap_formatter(''))
160 self.assertEqual('one two three',
161 utils.text_wrap_formatter('one two three'))
162 self.assertEqual(
163 'one two three four five six seven eight nine ten eleven\ntwelve',
164 utils.text_wrap_formatter(
165 'one two three four five six seven '
166 'eight nine ten eleven twelve'))
167
168 def test_newline_list_formatter(self):
169 self.assertEqual('', utils.newline_list_formatter(None))
170 self.assertEqual('', utils.newline_list_formatter([]))
171 self.assertEqual('one\ntwo',
172 utils.newline_list_formatter(['one', 'two']))
173
174 def test_event_log_formatter(self):
175 event1 = {'event_time': '2015-09-28T12:12:12',
176 'id': '123456789',
177 'resource_name': 'res_name',
178 'resource_status': 'CREATE_IN_PROGRESS',
179 'resource_status_reason': 'CREATE started'}
180 event2 = {'event_time': '2015-09-28T12:12:22',
181 'id': '123456789',
182 'resource_name': 'res_name',
183 'resource_status': 'CREATE_COMPLETE',
184 'resource_status_reason': 'CREATE completed'}
185 events_list = [hc_res.Resource(manager=None, info=event1),
186 hc_res.Resource(manager=None, info=event2)]
187
188 expected = ('2015-09-28 12:12:12 [res_name]: '
189 'CREATE_IN_PROGRESS CREATE started\n'
190 '2015-09-28 12:12:22 [res_name]: '
191 'CREATE_COMPLETE CREATE completed')
192 self.assertEqual(expected, utils.event_log_formatter(events_list))
193 self.assertEqual('', utils.event_log_formatter([]))
194
195 def test_event_log_formatter_resource_path_old_heat(self):
196
197 events = [{
198 'resource_name': 'nested',
199 'event_time': '2016-09-05T04:10:24Z',
200 'links': [{
201 'href': 'http://192.0.2.1:8004/v1/t/stacks/'
202 'nested/1bed5d4d-41d6-4451-b274-c073ebee375d',
203 'rel': 'stack'
204 }],
205 'logical_resource_id': 'nested',
206 'resource_status': 'CREATE_IN_PROGRESS',
207 'resource_status_reason': 'Stack CREATE started',
208 'physical_resource_id': '1bed5d4d-41d6-4451-b274-c073ebee375d',
209 }, {
210 'resource_name': 'rg1',
211 'event_time': '2016-09-05T04:10:24Z',
212 'links': [{
213 'href': 'http://192.0.2.1:8004/v1/t/stacks/'
214 'nested/1bed5d4d-41d6-4451-b274-c073ebee375d',
215 'rel': 'stack'
216 }],
217 'logical_resource_id': 'rg1',
218 'resource_status': 'CREATE_IN_PROGRESS',
219 'resource_status_reason': 'state changed',
220 'physical_resource_id': None, # note the None from old heat
221 'id': '375c49ae-cefb-4fb3-8f4d-1d5f1b9e3e5d'
222 }, {
223 'resource_name': 'nested-rg1-m4zxcs4pra6t',
224 'event_time': '2016-09-05T04:10:24Z',
225 'links': [{
226 'href': 'http://192.0.2.1:8004/v1/t/stacks/'
227 'nested-rg1-m4zxcs4pra6t/'
228 '3400bbad-a825-4226-ac23-c607846420db',
229 'rel': 'stack'
230 }],
231 'logical_resource_id': 'nested-rg1-m4zxcs4pra6t',
232 'resource_status': 'CREATE_IN_PROGRESS',
233 'resource_status_reason': 'Stack CREATE started',
234 'physical_resource_id': '3400bbad-a825-4226-ac23-c607846420db',
235 'id': '7e521c84-cd35-4f4c-b0de-962bd3cc40a8'
236 }, {
237 'resource_name': '1',
238 'event_time': '2016-09-05T04:10:24Z',
239 'links': [{
240 'href': 'http://192.0.2.1:8004/v1/t/stacks/'
241 'nested-rg1-m4zxcs4pra6t/'
242 '3400bbad-a825-4226-ac23-c607846420db',
243 'rel': 'stack'
244 }],
245 'logical_resource_id': '1',
246 'resource_status': 'CREATE_IN_PROGRESS',
247 'resource_status_reason': 'state changed',
248 'physical_resource_id': None, # note the None from old heat
249 'id': 'c6186c16-94ef-4214-a11a-7e3cc8a17f82'
250 }]
251
252 events_list = [hc_res.Resource(manager=None, info=event)
253 for event in events]
254
255 expected = '''\
256 2016-09-05 04:10:24Z [nested]: \
257 CREATE_IN_PROGRESS Stack CREATE started
258 2016-09-05 04:10:24Z [nested.rg1]: \
259 CREATE_IN_PROGRESS state changed
260 2016-09-05 04:10:24Z [nested-rg1-m4zxcs4pra6t]: \
261 CREATE_IN_PROGRESS Stack CREATE started
262 2016-09-05 04:10:24Z [nested-rg1-m4zxcs4pra6t.1]: \
263 CREATE_IN_PROGRESS state changed'''
264 self.assertEqual(expected, utils.event_log_formatter(events_list))
265
266 def test_event_log_formatter_resource_path(self):
267
268 events = [{
269 'resource_name': 'nested',
270 'event_time': '2016-09-05T04:10:24Z',
271 'links': [{
272 'href': 'http://192.0.2.1:8004/v1/t/stacks/'
273 'nested/1bed5d4d-41d6-4451-b274-c073ebee375d',
274 'rel': 'stack'
275 }],
276 'logical_resource_id': 'nested',
277 'resource_status': 'CREATE_IN_PROGRESS',
278 'resource_status_reason': 'Stack CREATE started',
279 'physical_resource_id': '1bed5d4d-41d6-4451-b274-c073ebee375d',
280 }, {
281 'resource_name': 'rg1',
282 'event_time': '2016-09-05T04:10:24Z',
283 'links': [{
284 'href': 'http://192.0.2.1:8004/v1/t/stacks/'
285 'nested/1bed5d4d-41d6-4451-b274-c073ebee375d',
286 'rel': 'stack'
287 }],
288 'logical_resource_id': 'rg1',
289 'resource_status': 'CREATE_IN_PROGRESS',
290 'resource_status_reason': 'state changed',
291 'physical_resource_id': 'nested-rg1-m4zxcs4pra6t',
292 'id': '375c49ae-cefb-4fb3-8f4d-1d5f1b9e3e5d'
293 }, {
294 'resource_name': 'nested-rg1-m4zxcs4pra6t',
295 'event_time': '2016-09-05T04:10:24Z',
296 'links': [{
297 'href': 'http://192.0.2.1:8004/v1/t/stacks/'
298 'nested-rg1-m4zxcs4pra6t/'
299 '3400bbad-a825-4226-ac23-c607846420db',
300 'rel': 'stack'
301 }],
302 'logical_resource_id': 'nested-rg1-m4zxcs4pra6t',
303 'resource_status': 'CREATE_IN_PROGRESS',
304 'resource_status_reason': 'Stack CREATE started',
305 'physical_resource_id': '3400bbad-a825-4226-ac23-c607846420db',
306 'id': '7e521c84-cd35-4f4c-b0de-962bd3cc40a8'
307 }, {
308 'resource_name': '1',
309 'event_time': '2016-09-05T04:10:24Z',
310 'links': [{
311 'href': 'http://192.0.2.1:8004/v1/t/stacks/'
312 'nested-rg1-m4zxcs4pra6t/'
313 '3400bbad-a825-4226-ac23-c607846420db',
314 'rel': 'stack'
315 }],
316 'logical_resource_id': '1',
317 'resource_status': 'CREATE_IN_PROGRESS',
318 'resource_status_reason': 'state changed',
319 'physical_resource_id': 'nested-rg1-m4zxcs4pra6t-1-z6sgpq54n6e7',
320 'id': 'c6186c16-94ef-4214-a11a-7e3cc8a17f82'
321 }]
322
323 events_list = [hc_res.Resource(manager=None, info=event)
324 for event in events]
325
326 expected = '''\
327 2016-09-05 04:10:24Z [nested]: \
328 CREATE_IN_PROGRESS Stack CREATE started
329 2016-09-05 04:10:24Z [nested.rg1]: \
330 CREATE_IN_PROGRESS state changed
331 2016-09-05 04:10:24Z [nested.rg1]: \
332 CREATE_IN_PROGRESS Stack CREATE started
333 2016-09-05 04:10:24Z [nested.rg1.1]: \
334 CREATE_IN_PROGRESS state changed'''
335 self.assertEqual(expected, utils.event_log_formatter(events_list))
336
337
338 class ShellTestParameterFiles(testtools.TestCase):
339
340 def test_format_parameter_file_none(self):
341 self.assertEqual({}, utils.format_parameter_file(None))
342
343 def test_format_parameter_file(self):
344 tmpl_file = '/opt/stack/template.yaml'
345 contents = 'DBUsername=wp\nDBPassword=verybadpassword'
346 utils.read_url_content = mock.MagicMock()
347 utils.read_url_content.return_value = ('DBUsername=wp\n'
348 'DBPassword=verybadpassword')
349
350 p = utils.format_parameter_file([
351 'env_file1=test_file1'], tmpl_file)
352 self.assertEqual({'env_file1': contents
353 }, p)
354
355 def test_format_parameter_file_no_template(self):
356 tmpl_file = None
357 contents = 'DBUsername=wp\nDBPassword=verybadpassword'
358 utils.read_url_content = mock.MagicMock()
359 utils.read_url_content.return_value = ('DBUsername=wp\n'
360 'DBPassword=verybadpassword')
361 p = utils.format_parameter_file([
362 'env_file1=test_file1'], tmpl_file)
363 self.assertEqual({'env_file1': contents
364 }, p)
365
366 def test_format_all_parameters(self):
367 tmpl_file = '/opt/stack/template.yaml'
368 contents = 'DBUsername=wp\nDBPassword=verybadpassword'
369 params = ['KeyName=heat_key;UpstreamDNS=8.8.8.8']
370 utils.read_url_content = mock.MagicMock()
371 utils.read_url_content.return_value = ('DBUsername=wp\n'
372 'DBPassword=verybadpassword')
373 p = utils.format_all_parameters(params, [
374 'env_file1=test_file1'], template_file=tmpl_file)
375 self.assertEqual({'KeyName': 'heat_key',
376 'UpstreamDNS': '8.8.8.8',
377 'env_file1': contents}, p)
378
379
380 class TestURLFunctions(testtools.TestCase):
381
382 def setUp(self):
383 super().setUp()
384 self.m = mock.MagicMock()
385
386 self.addCleanup(self.m.UnsetStubs)
387
388 def test_normalise_file_path_to_url_relative(self):
389 self.assertEqual(
390 'file://' + request.pathname2url('%s/foo' % os.getcwd()),
391 utils.normalise_file_path_to_url(
392 'foo'))
393
394 def test_normalise_file_path_to_url_absolute(self):
395 self.assertEqual(
396 'file:///tmp/foo',
397 utils.normalise_file_path_to_url(
398 '/tmp/foo'))
399
400 def test_normalise_file_path_to_url_file(self):
401 self.assertEqual(
402 'file:///tmp/foo',
403 utils.normalise_file_path_to_url(
404 'file:///tmp/foo'))
405
406 def test_normalise_file_path_to_url_http(self):
407 self.assertEqual(
408 'http://localhost/foo',
409 utils.normalise_file_path_to_url(
410 'http://localhost/foo'))
411
412 def test_get_template_url(self):
413 tmpl_file = '/opt/stack/template.yaml'
414 tmpl_url = 'file:///opt/stack/template.yaml'
415 self.assertEqual(utils.get_template_url(tmpl_file, None),
416 tmpl_url)
417 self.assertEqual(utils.get_template_url(None, tmpl_url),
418 tmpl_url)
419 self.assertIsNone(utils.get_template_url(None, None))
420
421 def test_base_url_for_url(self):
422 self.assertEqual(
423 'file:///foo/bar',
424 utils.base_url_for_url(
425 'file:///foo/bar/baz'))
426 self.assertEqual(
427 'file:///foo/bar',
428 utils.base_url_for_url(
429 'file:///foo/bar/baz.txt'))
430 self.assertEqual(
431 'file:///foo/bar',
432 utils.base_url_for_url(
433 'file:///foo/bar/'))
434 self.assertEqual(
435 'file:///',
436 utils.base_url_for_url(
437 'file:///'))
438 self.assertEqual(
439 'file:///',
440 utils.base_url_for_url(
441 'file:///foo'))
442
443 self.assertEqual(
444 'http://foo/bar',
445 utils.base_url_for_url(
446 'http://foo/bar/'))
447 self.assertEqual(
448 'http://foo/bar',
449 utils.base_url_for_url(
450 'http://foo/bar/baz.template'))