Synchronise the openstack bugs
[parser.git] / tosca2heat / heat-translator / translator / tests / test_utils.py
1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #    not use this file except in compliance with the License. You may obtain
3 #    a copy of the License at
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #    Unless required by applicable law or agreed to in writing, software
8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #    License for the specific language governing permissions and limitations
11 #    under the License.
12
13 import os
14 from toscaparser.tests.base import TestCase
15 import translator.common.utils
16
17
18 class CommonUtilsTest(TestCase):
19
20     MemoryUnit = translator.common.utils.MemoryUnit
21     cmpUtils = translator.common.utils.CompareUtils
22     yamlUtils = translator.common.utils.YamlUtils
23     UrlUtils = translator.common.utils.UrlUtils
24
25     def test_convert_unit_size_to_num(self):
26         size = '1 TB'
27         num_to_convert = 'GB'
28         expected_output = 1000
29         output = self.MemoryUnit.convert_unit_size_to_num(size, num_to_convert)
30         self.assertEqual(output, expected_output)
31
32         size = '40 GB'
33         num_to_convert = 'MB'
34         expected_output = 40000
35         output = self.MemoryUnit.convert_unit_size_to_num(size, num_to_convert)
36         self.assertEqual(output, expected_output)
37
38         size = '20 B'
39         num_to_convert = None
40         expected_output = 20
41         output = self.MemoryUnit.convert_unit_size_to_num(size, num_to_convert)
42         self.assertEqual(output, expected_output)
43
44     def test_validate_unit(self):
45         unit = 'AB'
46         exp_msg = ('Provided unit "{0}" is not valid. The valid units are '
47                    '{1}').format(unit, self.MemoryUnit.UNIT_SIZE_DICT.keys())
48         try:
49             self.MemoryUnit.validate_unit(unit)
50         except Exception as err:
51             self.assertTrue(
52                 isinstance(err, ValueError))
53             self.assertEqual(exp_msg, err.__str__())
54
55     def test_unit_size_conversion_to_GNU_standard(self):
56         unit = 'gB'
57         standard_unit = 'GB'
58         converted_unit = self.MemoryUnit.validate_unit(unit)
59         self.assertEqual(converted_unit, standard_unit)
60
61         unit = 'KB'
62         standard_unit = 'kB'
63         converted_unit = self.MemoryUnit.validate_unit(unit)
64         self.assertEqual(converted_unit, standard_unit)
65
66         unit = 'kb'
67         standard_unit = 'kB'
68         converted_unit = self.MemoryUnit.validate_unit(unit)
69         self.assertEqual(converted_unit, standard_unit)
70
71         unit = 'kB'
72         standard_unit = 'kB'
73         converted_unit = self.MemoryUnit.validate_unit(unit)
74         self.assertEqual(converted_unit, standard_unit)
75
76         unit = 'MIB'
77         standard_unit = 'MiB'
78         converted_unit = self.MemoryUnit.validate_unit(unit)
79         self.assertEqual(converted_unit, standard_unit)
80
81     def test_str_to_num_value_error(self):
82         str_to_convert = '55063.000000'
83         expected_output = 55063.0
84         output = translator.common.utils.str_to_num(str_to_convert)
85         self.assertEqual(output, expected_output)
86
87     def test_compare_dicts_unequal(self):
88         dict1 = {'allowed_values': [1, 2, 4, 8],
89                  'server3': {'depends_on': ['server1', 'server2']}}
90         dict2 = {'allowed_values': [1, 2, 4, 8],
91                  'server3': {'depends_on': ['server2', 'server1']}}
92         self.assertFalse(self.cmpUtils.compare_dicts(dict1, dict2))
93
94     def test_dicts_equivalent_empty_dicts(self):
95         self.assertTrue(self.cmpUtils.compare_dicts(None, None))
96         self.assertFalse(self.cmpUtils.compare_dicts(None, {}))
97         self.assertFalse(self.cmpUtils.compare_dicts(None, {'x': '2'}))
98
99     def test_compareutils_reorder(self):
100         dic = {'output': {'website_url': {'value': {'get_attr':
101                                                     ['server', 'networks',
102                                                      'private', 0]}}},
103                'allowed_values': [2, 8, 1, 4],
104                'server3': {'depends_on': ['server2', 'server1']}}
105         reordered_dic = {'output': {'website_url': {'value': {'get_attr':
106                                                     ['server', 'networks',
107                                                      'private', 0]}}},
108                          'allowed_values': [1, 2, 4, 8],
109                          'server3': {'depends_on': ['server1', 'server2']}}
110         self.assertEqual(reordered_dic, self.cmpUtils.reorder(dic))
111
112     def test_compareutils_diff_dicts_both_null(self):
113         expected = None
114         provided = None
115         self.assertEqual({},
116                          self.cmpUtils.diff_dicts(expected, provided))
117
118     def test_compareutils_diff_dicts_one_null(self):
119         expected = {'keyname': 'userkey'}
120         provided = None
121         self.assertEqual(
122             {self.cmpUtils.MISMATCH_VALUE1_LABEL: {'keyname': 'userkey'},
123              self.cmpUtils.MISMATCH_VALUE2_LABEL: None},
124             self.cmpUtils.diff_dicts(expected, provided))
125
126     def test_compareutils_diff_dicts_missing_key(self):
127         expected = {'server3': {'depends_on': ['server1', 'server2'],
128                                 'keyname': 'userkey'}}
129         provided = {'server3': {'depends_on': ['server2', 'server1']}}
130         self.assertEqual(
131             {'server3': {'keyname':
132              {self.cmpUtils.MISMATCH_VALUE1_LABEL: 'userkey',
133               self.cmpUtils.MISMATCH_VALUE2_LABEL: None}}},
134             self.cmpUtils.diff_dicts(expected, provided))
135
136     def test_compareutils_diff_dicts_missing_key_other_dict(self):
137         expected = {'server3': {'depends_on': ['server1', 'server2']}}
138         provided = {'server3': {'depends_on': ['server2', 'server1'],
139                                 'keyname': 'userkey'}}
140         self.assertEqual(
141             {'server3': {'keyname':
142              {self.cmpUtils.MISMATCH_VALUE1_LABEL: None,
143               self.cmpUtils.MISMATCH_VALUE2_LABEL: 'userkey'}}},
144             self.cmpUtils.diff_dicts(expected, provided))
145
146     def test_compareutils_diff_dicts_value_diff(self):
147         expected = \
148             {'output':
149              {'website_url':
150               {'value':
151                {'get_attr': ['server', 'networks', 'private', 0]}}},
152              'server3': {'depends_on': ['server2', 'server1']}}
153         provided = \
154             {'output':
155              {'website_url':
156               {'value':
157                {'get_attr': ['server', 'networks', 'public', 0]}}},
158              'server3': {'depends_on': ['server2', 'server1']}}
159         self.assertEqual(
160             {'output':
161              {'website_url':
162               {'value':
163                {'get_attr':
164                 {self.cmpUtils.MISMATCH_VALUE1_LABEL:
165                  ['server', 'networks', 'private', 0],
166                  self.cmpUtils.MISMATCH_VALUE2_LABEL:
167                  ['server', 'networks', 'public', 0]}}}}},
168             self.cmpUtils.diff_dicts(expected, provided))
169
170     def test_yamlutils_get_dict_missing_file(self):
171         self.assertIsNone(self.yamlUtils.get_dict('./no_file.yaml'))
172
173     def test_yamlutils_get_dict(self):
174         yaml_file = os.path.join(
175             os.path.dirname(os.path.abspath(__file__)),
176             '../tests/data/custom_types/rsyslog.yaml')
177         dict = \
178             {'tosca_definitions_version': 'tosca_simple_yaml_1_0',
179              'description':
180              'RSYSLOG is the Rocket-fast SYStem for LOG processing.\n',
181              'node_types':
182              {'tosca.nodes.SoftwareComponent.Rsyslog':
183               {'derived_from': 'tosca.nodes.SoftwareComponent',
184                'requirements':
185                [{'log_endpoint':
186                  {'capability': 'tosca.capabilities.Endpoint',
187                   'node': 'tosca.nodes.SoftwareComponent.Logstash',
188                   'relationship': 'tosca.relationships.ConnectsTo'}}]}}}
189         self.assertEqual(dict, self.yamlUtils.get_dict(yaml_file))
190
191     def test_yamlutils_compare_yamls(self):
192         yaml_file1 = os.path.join(
193             os.path.dirname(os.path.abspath(__file__)),
194             '../tests/data/custom_types/kibana.yaml')
195         yaml_file2 = os.path.join(
196             os.path.dirname(os.path.abspath(__file__)),
197             '../tests/data/custom_types/collectd.yaml')
198         self.assertTrue(self.yamlUtils.compare_yamls(yaml_file1, yaml_file1))
199         self.assertFalse(self.yamlUtils.compare_yamls(yaml_file1, yaml_file2))
200
201     def test_yamlutils_compare_yaml_dict(self):
202         yaml_file1 = os.path.join(
203             os.path.dirname(os.path.abspath(__file__)),
204             '../tests/data/custom_types/rsyslog.yaml')
205         yaml_file2 = os.path.join(
206             os.path.dirname(os.path.abspath(__file__)),
207             '../tests/data/custom_types/collectd.yaml')
208         dict = \
209             {'tosca_definitions_version': 'tosca_simple_yaml_1_0',
210              'description':
211              'RSYSLOG is the Rocket-fast SYStem for LOG processing.\n',
212              'node_types':
213              {'tosca.nodes.SoftwareComponent.Rsyslog':
214               {'derived_from': 'tosca.nodes.SoftwareComponent',
215                'requirements':
216                [{'log_endpoint':
217                  {'capability': 'tosca.capabilities.Endpoint',
218                   'node': 'tosca.nodes.SoftwareComponent.Logstash',
219                   'relationship': 'tosca.relationships.ConnectsTo'}}]}}}
220         self.assertEqual({}, self.cmpUtils.diff_dicts(
221             self.yamlUtils.get_dict(yaml_file1), dict))
222         self.assertFalse(self.yamlUtils.compare_yaml_dict(yaml_file2, dict))
223
224     def test_assert_value_is_num(self):
225         value = 1
226         output = translator.common.utils.str_to_num(value)
227         self.assertEqual(value, output)
228
229     def test_urlutils_validate_url(self):
230         self.assertTrue(self.UrlUtils.validate_url("http://www.github.com/"))
231         self.assertTrue(
232             self.UrlUtils.validate_url("https://github.com:81/a/2/a.b"))
233         self.assertTrue(self.UrlUtils.validate_url("ftp://github.com"))
234         self.assertFalse(self.UrlUtils.validate_url("github.com"))
235         self.assertFalse(self.UrlUtils.validate_url("123"))
236         self.assertFalse(self.UrlUtils.validate_url("a/b/c"))