converiting the deploy settings obj to a dict 99/19399/4
authorDan Radez <dradez@redhat.com>
Tue, 23 Aug 2016 20:22:40 +0000 (16:22 -0400)
committerDan Radez <dradez@redhat.com>
Tue, 23 Aug 2016 22:50:33 +0000 (18:50 -0400)
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 <dradez@redhat.com>
build/rpm_specs/opnfv-apex-common.spec
lib/python/apex/__init__.py
lib/python/apex/deploy_settings.py [moved from lib/python/apex/deploy_env.py with 87% similarity]
tests/test_apex_deploy_settings.py [moved from tests/test_apex_deploy_env.py with 86% similarity]

index 39bcd28..b9125b3 100644 (file)
@@ -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/
index 5b15850..e1b8b54 100644 (file)
@@ -9,5 +9,5 @@
 
 
 from .network_settings import NetworkSettings
-from .deploy_env import DeploySettings
+from .deploy_settings import DeploySettings
 from .network_environment import NetworkEnvironment
similarity index 87%
rename from lib/python/apex/deploy_env.py
rename to lib/python/apex/deploy_settings.py
index 816dc11..b70efda 100644 (file)
@@ -28,7 +28,7 @@ VALID_PERF_OPTS = ['kernel', 'nova']
 VALID_DATAPLANES = ['ovs', 'ovs_dpdk', 'fdio']
 
 
-class DeploySettings:
+class DeploySettings(dict):
     """
     This class parses a APEX deploy settings yaml file into an object
 
@@ -37,9 +37,16 @@ class DeploySettings:
     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()
+        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):
         """
@@ -48,14 +55,14 @@ class DeploySettings:
         DeploySettingsException will be raised if validation fails.
         """
 
-        if 'deploy_options' not in self.deploy_settings:
+        if 'deploy_options' not in self:
             raise DeploySettingsException("No deploy options provided in"
                                           " deploy settings file")
-        if 'global_params' not in self.deploy_settings:
+        if 'global_params' not in self:
             raise DeploySettingsException("No global options provided in"
                                           " deploy settings file")
 
-        deploy_options = self.deploy_settings['deploy_options']
+        deploy_options = self['deploy_options']
         if not isinstance(deploy_options, dict):
             raise DeploySettingsException("deploy_options should be a list")
 
@@ -73,9 +80,9 @@ class DeploySettings:
         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'
+                    self['deploy_options'][req_set] = 'ovs'
                 else:
-                    self.deploy_settings['deploy_options'][req_set] = False
+                    self['deploy_options'][req_set] = False
 
         if 'performance' in deploy_options:
             if not isinstance(deploy_options['performance'], dict):
@@ -110,7 +117,7 @@ class DeploySettings:
         facilitate modification of the correct image.
         """
         bash_str = 'performance_options=(\n'
-        deploy_options = self.deploy_settings['deploy_options']
+        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():
@@ -121,7 +128,7 @@ class DeploySettings:
         bash_str += ')\n'
         bash_str += '\n'
         bash_str += 'performance_roles=(\n'
-        for role in self.deploy_settings['deploy_options']['performance']:
+        for role in self['deploy_options']['performance']:
             bash_str += role + '\n'
         bash_str += ')\n'
         bash_str += '\n'
@@ -133,7 +140,7 @@ class DeploySettings:
         Creates deploy settings array in bash syntax.
         """
         bash_str = ''
-        for key, value in self.deploy_settings['deploy_options'].items():
+        for key, value in self['deploy_options'].items():
             if not isinstance(value, bool):
                 bash_str += "deploy_options_array[{}]=\"{}\"\n".format(key,
                                                                        value)
@@ -150,9 +157,9 @@ class DeploySettings:
         instead of stdout.
         """
         bash_str = ''
-        for key, value in self.deploy_settings['global_params'].items():
+        for key, value in self['global_params'].items():
             bash_str += "{}={}\n".format(key, value)
-        if 'performance' in self.deploy_settings['deploy_options']:
+        if 'performance' in self['deploy_options']:
             bash_str += self._dump_performance()
         bash_str += self._dump_deploy_options_array()
 
similarity index 86%
rename from tests/test_apex_deploy_env.py
rename to tests/test_apex_deploy_settings.py
index 563bfd8..1e26b28 100644 (file)
 import io
 # https://docs.python.org/3/library/io.html
 
-from apex.deploy_env import DeploySettings
-from apex.deploy_env import DeploySettingsException
+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',
@@ -52,6 +53,12 @@ deploy_options:
   performance:
     Controller:
       error: error
+""",
+    """global_params:
+deploy_options:
+  performance:
+    InvalidRole:
+      error: error
 """,)
 
 
@@ -73,6 +80,7 @@ class TestIpUtils(object):
     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:
@@ -88,3 +96,8 @@ class TestIpUtils(object):
         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)