From d376c37fd627764484e80be7010a7eefed9eaa4a Mon Sep 17 00:00:00 2001 From: Martin Kopec Date: Fri, 21 Dec 2018 13:02:53 +0000 Subject: [PATCH] Add unit test for profile feature The patch adds a new unit test for profile feature. The patch also converts all overrides values read from profile.yaml to string as this is the data type of overrides. Change-Id: I795b9b900b6274e689ba64b9721fb63e49696dea --- config_tempest/profile.py | 17 +++++++++-- config_tempest/tests/test_profile.py | 45 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/config_tempest/profile.py b/config_tempest/profile.py index e9ee22e0..2646a5c2 100644 --- a/config_tempest/profile.py +++ b/config_tempest/profile.py @@ -33,6 +33,18 @@ def _convert_remove_append(options): return converted +def _read_yaml_file(path): + """Read a .yaml file. + + :param path: path to the profile.yaml file + :type path: string + :return: profile arguments + :rtype: dict + """ + with open(path, 'r') as stream: + return yaml.load(stream) + + def read_profile_file(path): """Read python-tempestconf arguments from a .yaml file. @@ -41,8 +53,7 @@ def read_profile_file(path): :return: profile arguments :rtype: dict """ - with open(path, 'r') as stream: - profile_args = yaml.load(stream) + profile_args = _read_yaml_file(path) # convert overrides, to a list of tuples (s, k, v) overrides = [] if 'overrides' in profile_args: @@ -51,7 +62,7 @@ def read_profile_file(path): v = profile_args['overrides'][key] if isinstance(v, list): v = ','.join(v) - overrides.append((s, k, v)) + overrides.append((s, k, str(v))) profile_args['overrides'] = overrides # convert remove remove = [] diff --git a/config_tempest/tests/test_profile.py b/config_tempest/tests/test_profile.py index 3b22b64d..2f4ca9e2 100644 --- a/config_tempest/tests/test_profile.py +++ b/config_tempest/tests/test_profile.py @@ -13,6 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. +import mock + from config_tempest import profile from config_tempest.tests.base import BaseConfigTempestTest @@ -30,3 +32,46 @@ class TestProfile(BaseConfigTempestTest): ] out_data = profile._convert_remove_append(in_data) self.assertItemsEqual(expected, out_data) + + @mock.patch('config_tempest.profile._read_yaml_file') + def test_read_profile_file(self, mock_read_yaml): + profile_data = { + 'create': True, + 'overrides': { + 'auth.use_dynamic_credentials': True + }, + 'append': { + 'network-feature-enabled.api_extensions': 'ext', + 'identity-feature-enabled.api_extensions': ['ext1', 'ext2'], + 'compute-feature-enabled.api_extensions': 'ext3,ext4' + }, + 'remove': { + 'network-feature-enabled.api_extensions': 'dvr', + 'identity-feature-enabled.api_extensions': ['dvr1', 'dvr2'], + 'compute-feature-enabled.api_extensions': 'dvr3,dvr4' + }, + 'network-id': 'network_id', + 'out': './etc/tempest.conf' + } + mock_read_yaml.return_value = profile_data + ret_dict = profile.read_profile_file('path') + expected = { + 'create': True, + 'remove': [ + 'network-feature-enabled.api_extensions=dvr', + 'identity-feature-enabled.api_extensions=dvr1,dvr2', + 'compute-feature-enabled.api_extensions=dvr3,dvr4' + ], + 'network-id': 'network_id', + 'overrides': [('auth', 'use_dynamic_credentials', 'True')], + 'append': [ + 'network-feature-enabled.api_extensions=ext', + 'identity-feature-enabled.api_extensions=ext1,ext2', + 'compute-feature-enabled.api_extensions=ext3,ext4' + ], + 'out': './etc/tempest.conf' + } + for key in ['create', 'network-id', 'out']: + self.assertEqual(expected[key], ret_dict[key]) + for key in ['remove', 'overrides', 'append']: + self.assertListEqual(sorted(expected[key]), sorted(ret_dict[key]))