mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-06-25 11:45:32 +02:00
Adapted tests and solved bugs
This commit is contained in:
parent
a2e6c9881a
commit
0d8027c908
@ -53,7 +53,10 @@ def main():
|
|||||||
with output_file.open("w", encoding="utf-8") as f:
|
with output_file.open("w", encoding="utf-8") as f:
|
||||||
yaml.dump(result, f, sort_keys=False)
|
yaml.dump(result, f, sort_keys=False)
|
||||||
|
|
||||||
print(f"✅ Generated: {output_file.relative_to(cwd)}")
|
try:
|
||||||
|
print(f"✅ Generated: {output_file.relative_to(cwd)}")
|
||||||
|
except ValueError:
|
||||||
|
print(f"✅ Generated: {output_file}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
@ -4,43 +4,52 @@ import tempfile
|
|||||||
import shutil
|
import shutil
|
||||||
import yaml
|
import yaml
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest.mock import patch
|
import subprocess
|
||||||
import importlib.util
|
|
||||||
|
|
||||||
class TestGenerateDefaultApplications(unittest.TestCase):
|
class TestGenerateDefaultApplications(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
# Determine script location
|
# Create temp role structure
|
||||||
self.script_path = Path(__file__).resolve().parent.parent.parent / "cli" / "generate_default_applications.py"
|
|
||||||
spec = importlib.util.spec_from_file_location("generate_default_applications", self.script_path)
|
|
||||||
self.gda = importlib.util.module_from_spec(spec)
|
|
||||||
spec.loader.exec_module(self.gda)
|
|
||||||
|
|
||||||
# Setup fake Ansible role structure
|
|
||||||
self.temp_dir = Path(tempfile.mkdtemp())
|
self.temp_dir = Path(tempfile.mkdtemp())
|
||||||
self.roles_dir = self.temp_dir / "roles"
|
self.roles_dir = self.temp_dir / "roles"
|
||||||
self.output_file = self.temp_dir / "group_vars" / "all" / "11_applications.yml"
|
self.roles_dir.mkdir()
|
||||||
(self.roles_dir / "docker-testapp" / "vars").mkdir(parents=True, exist_ok=True)
|
|
||||||
(self.roles_dir / "docker-testapp" / "tasks").mkdir(parents=True, exist_ok=True)
|
|
||||||
|
|
||||||
# Populate vars/main.yml and vars/configuration.yml
|
# Sample role
|
||||||
(self.roles_dir / "docker-testapp" / "vars" / "main.yml").write_text("application_id: testapp\n")
|
self.sample_role = self.roles_dir / "docker-testapp"
|
||||||
(self.roles_dir / "docker-testapp" / "vars" / "configuration.yml").write_text("foo: bar\nbaz: 123\n")
|
(self.sample_role / "vars").mkdir(parents=True)
|
||||||
(self.roles_dir / "docker-testapp" / "tasks" / "main.yml").write_text("# dummy task")
|
|
||||||
|
# Write application_id and configuration
|
||||||
|
(self.sample_role / "vars" / "main.yml").write_text("application_id: testapp\n")
|
||||||
|
(self.sample_role / "vars" / "configuration.yml").write_text("foo: bar\nbaz: 123\n")
|
||||||
|
|
||||||
|
# Output file path
|
||||||
|
self.output_file = self.temp_dir / "group_vars" / "all" / "11_applications.yml"
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
shutil.rmtree(self.temp_dir)
|
shutil.rmtree(self.temp_dir)
|
||||||
|
|
||||||
def test_extracts_and_writes_configuration(self):
|
def test_script_generates_expected_yaml(self):
|
||||||
self.gda.generate_default_applications(
|
script_path = Path(__file__).resolve().parent.parent.parent / "cli" / "generate_default_applications.py"
|
||||||
roles_dir=self.roles_dir,
|
|
||||||
output_file=self.output_file
|
result = subprocess.run(
|
||||||
|
[
|
||||||
|
"python3", str(script_path),
|
||||||
|
"--roles-dir", str(self.roles_dir),
|
||||||
|
"--output-file", str(self.output_file)
|
||||||
|
],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertTrue(self.output_file.exists())
|
self.assertEqual(result.returncode, 0, msg=result.stderr)
|
||||||
result = yaml.safe_load(self.output_file.read_text())
|
self.assertTrue(self.output_file.exists(), "Output file was not created.")
|
||||||
self.assertIn("testapp", result)
|
|
||||||
self.assertEqual(result["testapp"]["foo"], "bar")
|
data = yaml.safe_load(self.output_file.read_text())
|
||||||
self.assertEqual(result["testapp"]["baz"], 123)
|
self.assertIn("default_applications", data)
|
||||||
|
self.assertIn("testapp", data["default_applications"])
|
||||||
|
self.assertEqual(data["default_applications"]["testapp"]["foo"], "bar")
|
||||||
|
self.assertEqual(data["default_applications"]["testapp"]["baz"], 123)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
@ -114,9 +114,11 @@ class TestGenerateVaultedCredentials(unittest.TestCase):
|
|||||||
override_value = "custom-override-value"
|
override_value = "custom-override-value"
|
||||||
override_key = "credentials.shared_secret"
|
override_key = "credentials.shared_secret"
|
||||||
|
|
||||||
# Patch vault encryption to just return the plaintext prefixed as mock
|
# 👇 Patch die Methode innerhalb des importierten Moduls gvc
|
||||||
with patch("generate_vaulted_credentials.encrypt_with_vault") as mock_encrypt:
|
with patch.object(gvc, "encrypt_with_vault") as mock_encrypt, \
|
||||||
mock_encrypt.side_effect = lambda val, name, **kwargs: f"$ANSIBLE_VAULT;1.1;AES256\n{val}"
|
patch("builtins.input", return_value="n"):
|
||||||
|
mock_encrypt.side_effect = lambda val, name, *_args, **_kwargs: f"$ANSIBLE_VAULT;1.1;AES256\n{val}"
|
||||||
|
|
||||||
updated = gvc.apply_schema_to_inventory(
|
updated = gvc.apply_schema_to_inventory(
|
||||||
schema=schema_data,
|
schema=schema_data,
|
||||||
inventory_data=inventory_data,
|
inventory_data=inventory_data,
|
||||||
@ -129,5 +131,6 @@ class TestGenerateVaultedCredentials(unittest.TestCase):
|
|||||||
actual = updated["applications"]["demoapp"]["credentials"]["shared_secret"]
|
actual = updated["applications"]["demoapp"]["credentials"]["shared_secret"]
|
||||||
self.assertIn(override_value, str(actual), "The override value was not used during encryption.")
|
self.assertIn(override_value, str(actual), "The override value was not used during encryption.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user