From 2d82d413bee7f1ea9d3fa2f03915a6ff5207ad1f Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Wed, 9 Mar 2016 13:12:16 -0500 Subject: [PATCH] Move inventory environment loading to function Originally the inventory environment was loaded as part of the `main` function. While this worked, it was difficult to isolate environment information when using it for testing. The loading process for the environment is now in a standalone function, with basic tests around it. In order to facilitate testing the `load_environment` function directly, the Python path is altered in the test script to import the dynamic_inventory.py script. This change is also useful for future tests targeted at specific functions. Change-Id: Ic24a8bc068f39cbd56ec8563d7f0bb40690f63a3 --- playbooks/inventory/dynamic_inventory.py | 26 +++++++++++++++++------- tests/test_inventory.py | 20 ++++++++++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/playbooks/inventory/dynamic_inventory.py b/playbooks/inventory/dynamic_inventory.py index 21d3e009c8..4f550fc63c 100755 --- a/playbooks/inventory/dynamic_inventory.py +++ b/playbooks/inventory/dynamic_inventory.py @@ -898,11 +898,29 @@ def _check_config_settings(cidr_networks, config, container_skel): ) +def load_environment(config_path): + """Create an environment dictionary from config files + + :param config_path: ``str``path where the environment files are kept + """ + + environment = dict() + + # Load all YAML files found in the env.d directory + env_plugins = os.path.join(config_path, 'env.d') + + if os.path.isdir(env_plugins): + _extra_config(user_defined_config=environment, base_dir=env_plugins) + + return environment + + def load_user_configuration(config_path): """Create a user configuration dictionary from config files :param config_path: ``str`` path where the configuration files are kept """ + user_defined_config = dict() # Load the user defined configuration file @@ -936,13 +954,7 @@ def main(): user_defined_config = load_user_configuration(config_path) - environment = dict() - - # Load all YAML files found in the env.d directory - env_plugins = os.path.join(config_path, 'env.d') - - if os.path.isdir(env_plugins): - _extra_config(user_defined_config=environment, base_dir=env_plugins) + environment = load_environment(config_path) # Load existing inventory file if found dynamic_inventory_file = os.path.join( diff --git a/tests/test_inventory.py b/tests/test_inventory.py index 54bd3eb030..9c408fa7b6 100644 --- a/tests/test_inventory.py +++ b/tests/test_inventory.py @@ -261,6 +261,26 @@ class TestUserConfiguration(unittest.TestCase): self.assertIsInstance(self.loaded_user_configuration, dict) +class TestEnvironments(unittest.TestCase): + def setUp(self): + self.longMessage = True + self.loaded_environment = di.load_environment(TARGET_DIR) + + def test_loading_environment(self): + """Test that the environment can be loaded""" + self.assertIsInstance(self.loaded_environment, dict) + + def test_envd_read(self): + """Test that the env.d contents are inserted into the environment""" + expected_keys = [ + 'component_skel', + 'container_skel', + 'physical_skel', + ] + for key in expected_keys: + self.assertIn(key, self.loaded_environment) + + class TestDuplicateIps(unittest.TestCase): def setUp(self): # Allow custom assertion errors.