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
5 # http://www.apache.org/licenses/LICENSE-2.0
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
14 from toscaparser.tests.base import TestCase
15 import translator.common.utils
18 class CommonUtilsTest(TestCase):
20 MemoryUnit = translator.common.utils.MemoryUnit
21 cmpUtils = translator.common.utils.CompareUtils
22 yamlUtils = translator.common.utils.YamlUtils
23 UrlUtils = translator.common.utils.UrlUtils
25 def test_convert_unit_size_to_num(self):
28 expected_output = 1000
29 output = self.MemoryUnit.convert_unit_size_to_num(size, num_to_convert)
30 self.assertEqual(output, expected_output)
34 expected_output = 40000
35 output = self.MemoryUnit.convert_unit_size_to_num(size, num_to_convert)
36 self.assertEqual(output, expected_output)
41 output = self.MemoryUnit.convert_unit_size_to_num(size, num_to_convert)
42 self.assertEqual(output, expected_output)
44 def test_validate_unit(self):
46 exp_msg = ('Provided unit "{0}" is not valid. The valid units are '
47 '{1}').format(unit, self.MemoryUnit.UNIT_SIZE_DICT.keys())
49 self.MemoryUnit.validate_unit(unit)
50 except Exception as err:
52 isinstance(err, ValueError))
53 self.assertEqual(exp_msg, err.__str__())
55 def test_unit_size_conversion_to_GNU_standard(self):
58 converted_unit = self.MemoryUnit.validate_unit(unit)
59 self.assertEqual(converted_unit, standard_unit)
63 converted_unit = self.MemoryUnit.validate_unit(unit)
64 self.assertEqual(converted_unit, standard_unit)
68 converted_unit = self.MemoryUnit.validate_unit(unit)
69 self.assertEqual(converted_unit, standard_unit)
73 converted_unit = self.MemoryUnit.validate_unit(unit)
74 self.assertEqual(converted_unit, standard_unit)
78 converted_unit = self.MemoryUnit.validate_unit(unit)
79 self.assertEqual(converted_unit, standard_unit)
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)
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))
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'}))
99 def test_compareutils_reorder(self):
100 dic = {'output': {'website_url': {'value': {'get_attr':
101 ['server', 'networks',
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',
108 'allowed_values': [1, 2, 4, 8],
109 'server3': {'depends_on': ['server1', 'server2']}}
110 self.assertEqual(reordered_dic, self.cmpUtils.reorder(dic))
112 def test_compareutils_diff_dicts_both_null(self):
116 self.cmpUtils.diff_dicts(expected, provided))
118 def test_compareutils_diff_dicts_one_null(self):
119 expected = {'keyname': 'userkey'}
122 {self.cmpUtils.MISMATCH_VALUE1_LABEL: {'keyname': 'userkey'},
123 self.cmpUtils.MISMATCH_VALUE2_LABEL: None},
124 self.cmpUtils.diff_dicts(expected, provided))
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']}}
131 {'server3': {'keyname':
132 {self.cmpUtils.MISMATCH_VALUE1_LABEL: 'userkey',
133 self.cmpUtils.MISMATCH_VALUE2_LABEL: None}}},
134 self.cmpUtils.diff_dicts(expected, provided))
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'}}
141 {'server3': {'keyname':
142 {self.cmpUtils.MISMATCH_VALUE1_LABEL: None,
143 self.cmpUtils.MISMATCH_VALUE2_LABEL: 'userkey'}}},
144 self.cmpUtils.diff_dicts(expected, provided))
146 def test_compareutils_diff_dicts_value_diff(self):
151 {'get_attr': ['server', 'networks', 'private', 0]}}},
152 'server3': {'depends_on': ['server2', 'server1']}}
157 {'get_attr': ['server', 'networks', 'public', 0]}}},
158 'server3': {'depends_on': ['server2', 'server1']}}
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))
170 def test_yamlutils_get_dict_missing_file(self):
171 self.assertIsNone(self.yamlUtils.get_dict('./no_file.yaml'))
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')
178 {'tosca_definitions_version': 'tosca_simple_yaml_1_0',
180 'RSYSLOG is the Rocket-fast SYStem for LOG processing.\n',
182 {'tosca.nodes.SoftwareComponent.Rsyslog':
183 {'derived_from': 'tosca.nodes.SoftwareComponent',
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))
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))
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')
209 {'tosca_definitions_version': 'tosca_simple_yaml_1_0',
211 'RSYSLOG is the Rocket-fast SYStem for LOG processing.\n',
213 {'tosca.nodes.SoftwareComponent.Rsyslog':
214 {'derived_from': 'tosca.nodes.SoftwareComponent',
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))
224 def test_assert_value_is_num(self):
226 output = translator.common.utils.str_to_num(value)
227 self.assertEqual(value, output)
229 def test_urlutils_validate_url(self):
230 self.assertTrue(self.UrlUtils.validate_url("http://www.github.com/"))
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"))