mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-09-09 03:37:37 +02:00
Restructured CLI logic
This commit is contained in:
56
tests/unit/cli/generate/defaults/test_applications.py
Normal file
56
tests/unit/cli/generate/defaults/test_applications.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import os
|
||||
import unittest
|
||||
import tempfile
|
||||
import shutil
|
||||
import yaml
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
|
||||
|
||||
class TestGenerateDefaultApplications(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Create temp role structure
|
||||
self.temp_dir = Path(tempfile.mkdtemp())
|
||||
self.roles_dir = self.temp_dir / "roles"
|
||||
self.roles_dir.mkdir()
|
||||
|
||||
# Sample role
|
||||
self.sample_role = self.roles_dir / "web-app-testapp"
|
||||
(self.sample_role / "vars").mkdir(parents=True)
|
||||
(self.sample_role / "config").mkdir(parents=True)
|
||||
|
||||
# Write application_id and configuration
|
||||
(self.sample_role / "vars" / "main.yml").write_text("application_id: testapp\n")
|
||||
(self.sample_role / "config" / "main.yml").write_text("foo: bar\nbaz: 123\n")
|
||||
|
||||
# Output file path
|
||||
self.output_file = self.temp_dir / "group_vars" / "all" / "04_applications.yml"
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.temp_dir)
|
||||
|
||||
def test_script_generates_expected_yaml(self):
|
||||
script_path = Path(__file__).resolve().parent.parent.parent.parent.parent.parent / "cli/generate/defaults/applications.py"
|
||||
|
||||
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.assertEqual(result.returncode, 0, msg=result.stderr)
|
||||
self.assertTrue(self.output_file.exists(), "Output file was not created.")
|
||||
|
||||
data = yaml.safe_load(self.output_file.read_text())
|
||||
self.assertIn("defaults_applications", data)
|
||||
self.assertIn("testapp", data["defaults_applications"])
|
||||
self.assertEqual(data["defaults_applications"]["testapp"]["foo"], "bar")
|
||||
self.assertEqual(data["defaults_applications"]["testapp"]["baz"], 123)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Reference in New Issue
Block a user