diff --git a/python/envgene/envgenehelper/business_helper.py b/python/envgene/envgenehelper/business_helper.py index 86879dd10..2afb11162 100644 --- a/python/envgene/envgenehelper/business_helper.py +++ b/python/envgene/envgenehelper/business_helper.py @@ -174,7 +174,12 @@ def getEnvDefinition(env_dir = None): env_definition_path = getEnvDefinitionPath(env_dir) if not check_file_exists(env_definition_path): raise ReferenceError(f"Environment definition for env {env_dir} is not found in {env_definition_path}") - return openYaml(env_definition_path) + env_definition_yaml = openYaml(env_definition_path) + if 'inventory' not in env_definition_yaml: + logger.warning(f"'inventory' section is not found in env_definition.yml for env {env_dir}. Adding empty 'inventory' section to avoid errors in plugins.") + env_definition_yaml['inventory'] = {} + return env_definition_yaml + def getEnvDefinitionPath(env_dir) -> str: diff --git a/python/envgene/envgenehelper/test_business_helper.py b/python/envgene/envgenehelper/test_business_helper.py new file mode 100644 index 000000000..4c5e78b9c --- /dev/null +++ b/python/envgene/envgenehelper/test_business_helper.py @@ -0,0 +1,92 @@ +from .business_helper import getEnvDefinition +import pytest +import logging + +@pytest.fixture(scope="session", autouse=True) +def log_start(): + logging.basicConfig(level=logging.INFO, force=True) + +@pytest.fixture(scope="session", autouse=True) +def log_end(request): + yield + if request.session.testsfailed == 0: + logging.info("===== test_business_helper.py : All test cases passed successfully =====") + + +test_cases = [ + { + "name": "Inventory Missing", + "input_yaml": {"applications": []}, + "file_exists": True, + "expected_inventory": {}, + "expect_exception": None, + "env_dir": "test_env", + }, + { + "name": "Inventory Exists", + "input_yaml": {"inventory": {"a": 1}}, + "file_exists": True, + "expected_inventory": {"a": 1}, + "expect_exception": None, + "env_dir": "test_env", + }, + { + "name": "File Not Found", + "input_yaml": None, + "file_exists": False, + "expected_inventory": None, + "expect_exception": ReferenceError, + "env_dir": "test_env", + }, + { + "name": "Env Dir Fallback", + "input_yaml": {"applications": []}, + "file_exists": True, + "expected_inventory": {}, + "expect_exception": None, + "env_dir": None, + }, + { + "name": "Empty YAML", + "input_yaml": {}, + "file_exists": True, + "expected_inventory": {}, + "expect_exception": None, + "env_dir": "test_env", + }, +] + +@pytest.mark.parametrize( + "case", + test_cases, + ids=[case["name"] for case in test_cases] +) +def test_get_env_definition(case, monkeypatch): + monkeypatch.setattr( + "envgenehelper.business_helper.getEnvDefinitionPath", + lambda x: "dummy_path" + ) + monkeypatch.setattr( + "envgenehelper.business_helper.check_file_exists", + lambda x: case["file_exists"] + ) + if case["input_yaml"] is not None: + monkeypatch.setattr( + "envgenehelper.business_helper.openYaml", + lambda x: case["input_yaml"].copy() + ) + monkeypatch.setattr( + "envgenehelper.business_helper.get_current_env_dir_from_env_vars", + lambda: "env_from_vars" + ) + if case["expect_exception"]: + with pytest.raises(case["expect_exception"]): + getEnvDefinition(case["env_dir"]) + return + + result = getEnvDefinition(case["env_dir"]) + assert "inventory" in result + assert result["inventory"] == case["expected_inventory"] + + if case["name"] == "Inventory Exists": + assert result["inventory"] == case["input_yaml"]["inventory"] diff --git a/schemas/env-definition.schema.json b/schemas/env-definition.schema.json index 7e4fbf01f..4fa19635c 100644 --- a/schemas/env-definition.schema.json +++ b/schemas/env-definition.schema.json @@ -342,7 +342,6 @@ } }, "required": [ - "inventory", "envTemplate" ] }