Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion python/envgene/envgenehelper/business_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,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:
Expand Down
152 changes: 152 additions & 0 deletions python/envgene/envgenehelper/test_business_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
from .business_helper import getEnvDefinition
import pytest
import logging


# Enable logging ONLY for this file
@pytest.fixture(autouse=True)
def enable_logging():
logging.basicConfig(level=logging.INFO, force=True)


test_logger = logging.getLogger("test_logger")


# ===================== TEST 1 =====================
def test_inventory_added_when_missing(monkeypatch):
input_yaml = {"applications": []}

monkeypatch.setattr(
"envgenehelper.business_helper.getEnvDefinitionPath",
lambda x: "dummy_path"
)
monkeypatch.setattr(
"envgenehelper.business_helper.check_file_exists",
lambda x: True
)
monkeypatch.setattr(
"envgenehelper.business_helper.openYaml",
lambda x: input_yaml.copy()
)

test_logger.info("\n===== TEST 1: Inventory Missing =====")
test_logger.info("BEFORE: %s", input_yaml)
test_logger.info("Inventory BEFORE? %s", "inventory" in input_yaml)

result = getEnvDefinition("test_env")

test_logger.info("AFTER: %s", result)
test_logger.info("Inventory AFTER? %s", "inventory" in result)
test_logger.info("Inventory content: %s", result.get("inventory"))

assert "inventory" in result
assert result["inventory"] == {}


# ===================== TEST 2 =====================
def test_inventory_not_overridden(monkeypatch):
input_yaml = {"inventory": {"a": 1}}

monkeypatch.setattr(
"envgenehelper.business_helper.getEnvDefinitionPath",
lambda x: "dummy_path"
)
monkeypatch.setattr(
"envgenehelper.business_helper.check_file_exists",
lambda x: True
)
monkeypatch.setattr(
"envgenehelper.business_helper.openYaml",
lambda x: input_yaml
)

test_logger.info("\n===== TEST 2: Inventory Exists =====")
test_logger.info("BEFORE: %s", input_yaml)

result = getEnvDefinition("test_env")

test_logger.info("AFTER: %s", result)
test_logger.info("Inventory unchanged? %s", result["inventory"] == input_yaml["inventory"])

assert result == input_yaml
assert result["inventory"] is input_yaml["inventory"]


# ===================== TEST 3 =====================
def test_file_not_found(monkeypatch):
monkeypatch.setattr(
"envgenehelper.business_helper.getEnvDefinitionPath",
lambda x: "dummy_path"
)
monkeypatch.setattr(
"envgenehelper.business_helper.check_file_exists",
lambda x: False
)

test_logger.info("\n===== TEST 3: File Not Found =====")

with pytest.raises(ReferenceError) as exc:
getEnvDefinition("test_env")

test_logger.info("Exception: %s", str(exc.value))


# ===================== TEST 4 =====================
def test_env_dir_fallback(monkeypatch):
input_yaml = {"applications": []}

monkeypatch.setattr(
"envgenehelper.business_helper.get_current_env_dir_from_env_vars",
lambda: "env_from_vars"
)
monkeypatch.setattr(
"envgenehelper.business_helper.getEnvDefinitionPath",
lambda x: "dummy_path"
)
monkeypatch.setattr(
"envgenehelper.business_helper.check_file_exists",
lambda x: True
)
monkeypatch.setattr(
"envgenehelper.business_helper.openYaml",
lambda x: input_yaml.copy()
)

test_logger.info("\n===== TEST 4: env_dir Fallback =====")
test_logger.info("env_dir BEFORE: None")

result = getEnvDefinition(None)

test_logger.info("AFTER: %s", result)
test_logger.info("Inventory added? %s", "inventory" in result)

assert "inventory" in result


# ===================== TEST 5 =====================
def test_empty_yaml(monkeypatch):
input_yaml = {}

monkeypatch.setattr(
"envgenehelper.business_helper.getEnvDefinitionPath",
lambda x: "dummy"
)
monkeypatch.setattr(
"envgenehelper.business_helper.check_file_exists",
lambda x: True
)
monkeypatch.setattr(
"envgenehelper.business_helper.openYaml",
lambda x: input_yaml.copy()
)

test_logger.info("\n===== TEST 5: Empty YAML =====")
test_logger.info("BEFORE: %s", input_yaml)

result = getEnvDefinition("test_env")

test_logger.info("AFTER: %s", result)
test_logger.info("Inventory added? %s", "inventory" in result)
test_logger.info("Inventory content: %s", result.get("inventory"))

assert result["inventory"] == {}
1 change: 0 additions & 1 deletion schemas/env-definition.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@
}
},
"required": [
"inventory",
"envTemplate"
]
}
Loading