From 6c2e34e0ecaaa5c57a24811ebb8a422924537417 Mon Sep 17 00:00:00 2001 From: Dan Radez Date: Tue, 23 Aug 2016 16:22:40 -0400 Subject: converiting the deploy settings obj to a dict The deploy settings values are embedded in a dictionary inside a generic object. This patch makes the deploy settings object a dictionary so the values can be accessed directly without having to unnecessarily drill down through an empty object to the dict that holds the content intended to be managed by the deploy settings object. - adding tests to cover DeploySettings 100% Change-Id: I4ba625cd7b51cfb6c1f91c74f1d332d1e3dd9a8e Signed-off-by: Dan Radez --- build/rpm_specs/opnfv-apex-common.spec | 2 +- lib/python/apex/__init__.py | 2 +- lib/python/apex/deploy_env.py | 171 ------------------------------- lib/python/apex/deploy_settings.py | 178 +++++++++++++++++++++++++++++++++ tests/test_apex_deploy_env.py | 90 ----------------- tests/test_apex_deploy_settings.py | 103 +++++++++++++++++++ 6 files changed, 283 insertions(+), 263 deletions(-) delete mode 100644 lib/python/apex/deploy_env.py create mode 100644 lib/python/apex/deploy_settings.py delete mode 100644 tests/test_apex_deploy_env.py create mode 100644 tests/test_apex_deploy_settings.py diff --git a/build/rpm_specs/opnfv-apex-common.spec b/build/rpm_specs/opnfv-apex-common.spec index 39bcd289..b9125b36 100644 --- a/build/rpm_specs/opnfv-apex-common.spec +++ b/build/rpm_specs/opnfv-apex-common.spec @@ -65,7 +65,7 @@ install lib/utility-functions.sh %{buildroot}%{_var}/opt/opnfv/lib/ install lib/python/apex_python_utils.py %{buildroot}%{_var}/opt/opnfv/lib/python/ mkdir -p %{buildroot}%{python3_sitelib}/apex/ install lib/python/apex/__init__.py %{buildroot}%{python3_sitelib}/apex/ -install lib/python/apex/deploy_env.py %{buildroot}%{python3_sitelib}/apex/ +install lib/python/apex/deploy_settings.py %{buildroot}%{python3_sitelib}/apex/ install lib/python/apex/ip_utils.py %{buildroot}%{python3_sitelib}/apex/ install lib/python/apex/network_environment.py %{buildroot}%{python3_sitelib}/apex/ install lib/python/apex/network_settings.py %{buildroot}%{python3_sitelib}/apex/ diff --git a/lib/python/apex/__init__.py b/lib/python/apex/__init__.py index 5b158501..e1b8b547 100644 --- a/lib/python/apex/__init__.py +++ b/lib/python/apex/__init__.py @@ -9,5 +9,5 @@ from .network_settings import NetworkSettings -from .deploy_env import DeploySettings +from .deploy_settings import DeploySettings from .network_environment import NetworkEnvironment diff --git a/lib/python/apex/deploy_env.py b/lib/python/apex/deploy_env.py deleted file mode 100644 index 816dc114..00000000 --- a/lib/python/apex/deploy_env.py +++ /dev/null @@ -1,171 +0,0 @@ -############################################################################## -# Copyright (c) 2016 Michael Chapman (michapma@redhat.com) and others. -# -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# http://www.apache.org/licenses/LICENSE-2.0 -############################################################################## - - -import yaml -import logging - -REQ_DEPLOY_SETTINGS = ['sdn_controller', - 'odl_version', - 'sdn_l3', - 'tacker', - 'congress', - 'dataplane', - 'sfc', - 'vpn', - 'vpp'] - -OPT_DEPLOY_SETTINGS = ['performance', 'vsperf'] - -VALID_ROLES = ['Controller', 'Compute', 'ObjectStorage'] -VALID_PERF_OPTS = ['kernel', 'nova'] -VALID_DATAPLANES = ['ovs', 'ovs_dpdk', 'fdio'] - - -class DeploySettings: - """ - This class parses a APEX deploy settings yaml file into an object - - Currently the parsed object is dumped into a bash global definition file - for deploy.sh consumption. This object will later be used directly as - deployment script move to python. - """ - def __init__(self, filename): - with open(filename, 'r') as settings_file: - self.deploy_settings = yaml.load(settings_file) - self._validate_settings() - - def _validate_settings(self): - """ - Validates the deploy settings file provided - - DeploySettingsException will be raised if validation fails. - """ - - if 'deploy_options' not in self.deploy_settings: - raise DeploySettingsException("No deploy options provided in" - " deploy settings file") - if 'global_params' not in self.deploy_settings: - raise DeploySettingsException("No global options provided in" - " deploy settings file") - - deploy_options = self.deploy_settings['deploy_options'] - if not isinstance(deploy_options, dict): - raise DeploySettingsException("deploy_options should be a list") - - for setting, value in deploy_options.items(): - if setting not in REQ_DEPLOY_SETTINGS + OPT_DEPLOY_SETTINGS: - raise DeploySettingsException("Invalid deploy_option {} " - "specified".format(setting)) - if setting == 'dataplane': - if value not in VALID_DATAPLANES: - planes = ' '.join(VALID_DATAPLANES) - raise DeploySettingsException( - "Invalid dataplane {} specified. Valid dataplanes:" - " {}".format(value, planes)) - - for req_set in REQ_DEPLOY_SETTINGS: - if req_set not in deploy_options: - if req_set == 'dataplane': - self.deploy_settings['deploy_options'][req_set] = 'ovs' - else: - self.deploy_settings['deploy_options'][req_set] = False - - if 'performance' in deploy_options: - if not isinstance(deploy_options['performance'], dict): - raise DeploySettingsException("Performance deploy_option" - "must be a dictionary.") - for role, role_perf_sets in deploy_options['performance'].items(): - if role not in VALID_ROLES: - raise DeploySettingsException("Performance role {}" - "is not valid, choose" - "from {}".format( - role, - " ".join(VALID_ROLES) - )) - - for key in role_perf_sets: - if key not in VALID_PERF_OPTS: - raise DeploySettingsException("Performance option {} " - "is not valid, choose" - "from {}".format( - key, - " ".join( - VALID_PERF_OPTS) - )) - - def _dump_performance(self): - """ - Creates performance settings string for bash consumption. - - Output will be in the form of a list that can be iterated over in - bash, with each string being the direct input to the performance - setting script in the form to - facilitate modification of the correct image. - """ - bash_str = 'performance_options=(\n' - deploy_options = self.deploy_settings['deploy_options'] - for role, settings in deploy_options['performance'].items(): - for category, options in settings.items(): - for key, value in options.items(): - bash_str += "\"{} {} {} {}\"\n".format(role, - category, - key, - value) - bash_str += ')\n' - bash_str += '\n' - bash_str += 'performance_roles=(\n' - for role in self.deploy_settings['deploy_options']['performance']: - bash_str += role + '\n' - bash_str += ')\n' - bash_str += '\n' - - return bash_str - - def _dump_deploy_options_array(self): - """ - Creates deploy settings array in bash syntax. - """ - bash_str = '' - for key, value in self.deploy_settings['deploy_options'].items(): - if not isinstance(value, bool): - bash_str += "deploy_options_array[{}]=\"{}\"\n".format(key, - value) - else: - bash_str += "deploy_options_array[{}]={}\n".format(key, - value) - return bash_str - - def dump_bash(self, path=None): - """ - Prints settings for bash consumption. - - If optional path is provided, bash string will be written to the file - instead of stdout. - """ - bash_str = '' - for key, value in self.deploy_settings['global_params'].items(): - bash_str += "{}={}\n".format(key, value) - if 'performance' in self.deploy_settings['deploy_options']: - bash_str += self._dump_performance() - bash_str += self._dump_deploy_options_array() - - if path: - with open(path, 'w') as file: - file.write(bash_str) - else: - print(bash_str) - - -class DeploySettingsException(Exception): - def __init__(self, value): - self.value = value - - def __str__(self): - return self.value diff --git a/lib/python/apex/deploy_settings.py b/lib/python/apex/deploy_settings.py new file mode 100644 index 00000000..b70efdac --- /dev/null +++ b/lib/python/apex/deploy_settings.py @@ -0,0 +1,178 @@ +############################################################################## +# Copyright (c) 2016 Michael Chapman (michapma@redhat.com) and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + + +import yaml +import logging + +REQ_DEPLOY_SETTINGS = ['sdn_controller', + 'odl_version', + 'sdn_l3', + 'tacker', + 'congress', + 'dataplane', + 'sfc', + 'vpn', + 'vpp'] + +OPT_DEPLOY_SETTINGS = ['performance', 'vsperf'] + +VALID_ROLES = ['Controller', 'Compute', 'ObjectStorage'] +VALID_PERF_OPTS = ['kernel', 'nova'] +VALID_DATAPLANES = ['ovs', 'ovs_dpdk', 'fdio'] + + +class DeploySettings(dict): + """ + This class parses a APEX deploy settings yaml file into an object + + Currently the parsed object is dumped into a bash global definition file + for deploy.sh consumption. This object will later be used directly as + deployment script move to python. + """ + def __init__(self, filename): + init_dict = {} + if type(filename) is str: + with open(filename, 'r') as deploy_settings_file: + init_dict = yaml.load(deploy_settings_file) + else: + # assume input is a dict to build from + init_dict = filename + + super().__init__(init_dict) + self._validate_settings() + + def _validate_settings(self): + """ + Validates the deploy settings file provided + + DeploySettingsException will be raised if validation fails. + """ + + if 'deploy_options' not in self: + raise DeploySettingsException("No deploy options provided in" + " deploy settings file") + if 'global_params' not in self: + raise DeploySettingsException("No global options provided in" + " deploy settings file") + + deploy_options = self['deploy_options'] + if not isinstance(deploy_options, dict): + raise DeploySettingsException("deploy_options should be a list") + + for setting, value in deploy_options.items(): + if setting not in REQ_DEPLOY_SETTINGS + OPT_DEPLOY_SETTINGS: + raise DeploySettingsException("Invalid deploy_option {} " + "specified".format(setting)) + if setting == 'dataplane': + if value not in VALID_DATAPLANES: + planes = ' '.join(VALID_DATAPLANES) + raise DeploySettingsException( + "Invalid dataplane {} specified. Valid dataplanes:" + " {}".format(value, planes)) + + for req_set in REQ_DEPLOY_SETTINGS: + if req_set not in deploy_options: + if req_set == 'dataplane': + self['deploy_options'][req_set] = 'ovs' + else: + self['deploy_options'][req_set] = False + + if 'performance' in deploy_options: + if not isinstance(deploy_options['performance'], dict): + raise DeploySettingsException("Performance deploy_option" + "must be a dictionary.") + for role, role_perf_sets in deploy_options['performance'].items(): + if role not in VALID_ROLES: + raise DeploySettingsException("Performance role {}" + "is not valid, choose" + "from {}".format( + role, + " ".join(VALID_ROLES) + )) + + for key in role_perf_sets: + if key not in VALID_PERF_OPTS: + raise DeploySettingsException("Performance option {} " + "is not valid, choose" + "from {}".format( + key, + " ".join( + VALID_PERF_OPTS) + )) + + def _dump_performance(self): + """ + Creates performance settings string for bash consumption. + + Output will be in the form of a list that can be iterated over in + bash, with each string being the direct input to the performance + setting script in the form to + facilitate modification of the correct image. + """ + bash_str = 'performance_options=(\n' + deploy_options = self['deploy_options'] + for role, settings in deploy_options['performance'].items(): + for category, options in settings.items(): + for key, value in options.items(): + bash_str += "\"{} {} {} {}\"\n".format(role, + category, + key, + value) + bash_str += ')\n' + bash_str += '\n' + bash_str += 'performance_roles=(\n' + for role in self['deploy_options']['performance']: + bash_str += role + '\n' + bash_str += ')\n' + bash_str += '\n' + + return bash_str + + def _dump_deploy_options_array(self): + """ + Creates deploy settings array in bash syntax. + """ + bash_str = '' + for key, value in self['deploy_options'].items(): + if not isinstance(value, bool): + bash_str += "deploy_options_array[{}]=\"{}\"\n".format(key, + value) + else: + bash_str += "deploy_options_array[{}]={}\n".format(key, + value) + return bash_str + + def dump_bash(self, path=None): + """ + Prints settings for bash consumption. + + If optional path is provided, bash string will be written to the file + instead of stdout. + """ + bash_str = '' + for key, value in self['global_params'].items(): + bash_str += "{}={}\n".format(key, value) + if 'performance' in self['deploy_options']: + bash_str += self._dump_performance() + bash_str += self._dump_deploy_options_array() + + if path: + with open(path, 'w') as file: + file.write(bash_str) + else: + print(bash_str) + + +class DeploySettingsException(Exception): + def __init__(self, value): + self.value = value + + def __str__(self): + return self.value diff --git a/tests/test_apex_deploy_env.py b/tests/test_apex_deploy_env.py deleted file mode 100644 index 563bfd8f..00000000 --- a/tests/test_apex_deploy_env.py +++ /dev/null @@ -1,90 +0,0 @@ -############################################################################## -# Copyright (c) 2016 Dan Radez (Red Hat) -# -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# http://www.apache.org/licenses/LICENSE-2.0 -############################################################################## - -import io -# https://docs.python.org/3/library/io.html - -from apex.deploy_env import DeploySettings -from apex.deploy_env import DeploySettingsException - -from nose.tools import assert_equal -from nose.tools import assert_raises - -deploy_files = ('deploy_settings.yaml', - 'os-nosdn-nofeature-noha.yaml', - 'os-nosdn-ovs-noha.yaml', - 'os-ocl-nofeature-ha.yaml', - 'os-odl_l2-sdnvpn-ha.yaml', - 'os-odl_l3-nofeature-ha.yaml', - 'os-nosdn-nofeature-ha.yaml', - 'os-nosdn-ovs-ha.yaml', - 'os-nosdn-performance-ha.yaml', - 'os-odl_l2-nofeature-ha.yaml', - 'os-odl_l2-sfc-noha.yaml', - 'os-onos-nofeature-ha.yaml', - 'os-onos-sfc-ha.yaml') - -test_deploy_content = ( - 'global_params:', - 'deploy_options: string', - """deploy_options: string -global_params:""", - """global_params: -deploy_options: - error: error -""", - """global_params: -deploy_options: - performance: string -""", - """global_params: -deploy_options: - dataplane: invalid -""", - """global_params: -deploy_options: - performance: - Controller: - error: error -""",) - - -class TestIpUtils(object): - @classmethod - def setup_class(klass): - """This method is run once for each class before any tests are run""" - - @classmethod - def teardown_class(klass): - """This method is run once for each class _after_ all tests are run""" - - def setUp(self): - """This method is run once before _each_ test method is executed""" - - def teardown(self): - """This method is run once after _each_ test method is executed""" - - def test_init(self): - for f in deploy_files: - ds = DeploySettings('../config/deploy/{}'.format(f)) - - def test__validate_settings(self): - for c in test_deploy_content: - f = open('/tmp/apex_deploy_test_file', 'w') - f.write(c) - f.close() - assert_raises(DeploySettingsException, - DeploySettings, '/tmp/apex_deploy_test_file') - - def test_dump_bash(self): - # the performance file has the most use of the function - # so using that as the test case - ds = DeploySettings('../config/deploy/os-nosdn-performance-ha.yaml') - assert_equal(ds.dump_bash(), None) - assert_equal(ds.dump_bash(path='/dev/null'), None) diff --git a/tests/test_apex_deploy_settings.py b/tests/test_apex_deploy_settings.py new file mode 100644 index 00000000..1e26b287 --- /dev/null +++ b/tests/test_apex_deploy_settings.py @@ -0,0 +1,103 @@ +############################################################################## +# Copyright (c) 2016 Dan Radez (Red Hat) +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +import io +# https://docs.python.org/3/library/io.html + +from apex.deploy_settings import DeploySettings +from apex.deploy_settings import DeploySettingsException + +from nose.tools import assert_equal +from nose.tools import assert_raises +from nose.tools import assert_is_instance + +deploy_files = ('deploy_settings.yaml', + 'os-nosdn-nofeature-noha.yaml', + 'os-nosdn-ovs-noha.yaml', + 'os-ocl-nofeature-ha.yaml', + 'os-odl_l2-sdnvpn-ha.yaml', + 'os-odl_l3-nofeature-ha.yaml', + 'os-nosdn-nofeature-ha.yaml', + 'os-nosdn-ovs-ha.yaml', + 'os-nosdn-performance-ha.yaml', + 'os-odl_l2-nofeature-ha.yaml', + 'os-odl_l2-sfc-noha.yaml', + 'os-onos-nofeature-ha.yaml', + 'os-onos-sfc-ha.yaml') + +test_deploy_content = ( + 'global_params:', + 'deploy_options: string', + """deploy_options: string +global_params:""", + """global_params: +deploy_options: + error: error +""", + """global_params: +deploy_options: + performance: string +""", + """global_params: +deploy_options: + dataplane: invalid +""", + """global_params: +deploy_options: + performance: + Controller: + error: error +""", + """global_params: +deploy_options: + performance: + InvalidRole: + error: error +""",) + + +class TestIpUtils(object): + @classmethod + def setup_class(klass): + """This method is run once for each class before any tests are run""" + + @classmethod + def teardown_class(klass): + """This method is run once for each class _after_ all tests are run""" + + def setUp(self): + """This method is run once before _each_ test method is executed""" + + def teardown(self): + """This method is run once after _each_ test method is executed""" + + def test_init(self): + for f in deploy_files: + ds = DeploySettings('../config/deploy/{}'.format(f)) + ds = DeploySettings(ds) + + def test__validate_settings(self): + for c in test_deploy_content: + f = open('/tmp/apex_deploy_test_file', 'w') + f.write(c) + f.close() + assert_raises(DeploySettingsException, + DeploySettings, '/tmp/apex_deploy_test_file') + + def test_dump_bash(self): + # the performance file has the most use of the function + # so using that as the test case + ds = DeploySettings('../config/deploy/os-nosdn-performance-ha.yaml') + assert_equal(ds.dump_bash(), None) + assert_equal(ds.dump_bash(path='/dev/null'), None) + + def test_exception(sefl): + e = DeploySettingsException("test") + print(e) + assert_is_instance(e, DeploySettingsException) -- cgit 1.2.3-korg