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.