mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 19:01:54 +00:00
111 lines
3.9 KiB
Python
111 lines
3.9 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
import yaml
|
|
|
|
from utils.valid_deploy_id import ValidDeployId
|
|
|
|
|
|
class TestValidDeployId(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
# Uses real repo roles/ via ValidDeployId internal repo-root resolution
|
|
self.validator = ValidDeployId()
|
|
|
|
# pick a real application id from roles for positive tests
|
|
self.assertTrue(
|
|
self.validator.valid_ids,
|
|
"Expected at least one application id discovered from repo roles/",
|
|
)
|
|
self.existing_app = sorted(self.validator.valid_ids)[0]
|
|
|
|
# and a guaranteed non-existing id for negative tests
|
|
self.missing_app = "this-app-id-should-not-exist-xyz-123"
|
|
|
|
def _write_ini_inventory(self, content: str) -> str:
|
|
fd, path = tempfile.mkstemp(suffix=".ini")
|
|
os.close(fd)
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
f.write(content)
|
|
return path
|
|
|
|
def _write_yaml_inventory(self, data) -> str:
|
|
fd, path = tempfile.mkstemp(suffix=".yml")
|
|
os.close(fd)
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
yaml.safe_dump(data, f)
|
|
return path
|
|
|
|
def test_valid_in_roles_and_ini_inventory(self) -> None:
|
|
ini_content = f"""
|
|
[servers]
|
|
{self.existing_app},otherhost
|
|
"""
|
|
inv = self._write_ini_inventory(ini_content)
|
|
result = self.validator.validate(inv, [self.existing_app])
|
|
self.assertEqual(
|
|
result, {}, "app should be valid when in roles and ini inventory"
|
|
)
|
|
|
|
def test_missing_in_roles(self) -> None:
|
|
ini_content = f"""
|
|
[servers]
|
|
{self.missing_app}
|
|
"""
|
|
inv = self._write_ini_inventory(ini_content)
|
|
result = self.validator.validate(inv, [self.missing_app])
|
|
expected = {self.missing_app: {"in_roles": False, "in_inventory": True}}
|
|
self.assertEqual(result, expected)
|
|
|
|
def test_missing_in_inventory_ini(self) -> None:
|
|
ini_content = """
|
|
[servers]
|
|
otherhost
|
|
"""
|
|
inv = self._write_ini_inventory(ini_content)
|
|
result = self.validator.validate(inv, [self.existing_app])
|
|
expected = {self.existing_app: {"in_roles": True, "in_inventory": False}}
|
|
self.assertEqual(result, expected)
|
|
|
|
def test_missing_both_ini(self) -> None:
|
|
ini_content = """
|
|
[servers]
|
|
otherhost
|
|
"""
|
|
inv = self._write_ini_inventory(ini_content)
|
|
result = self.validator.validate(inv, [self.missing_app])
|
|
expected = {self.missing_app: {"in_roles": False, "in_inventory": False}}
|
|
self.assertEqual(result, expected)
|
|
|
|
def test_valid_in_roles_and_yaml_inventory(self) -> None:
|
|
data = {self.existing_app: {"hosts": ["localhost"]}}
|
|
inv = self._write_yaml_inventory(data)
|
|
result = self.validator.validate(inv, [self.existing_app])
|
|
self.assertEqual(result, {}, "app should be valid in roles and yaml inventory")
|
|
|
|
def test_missing_in_roles_yaml(self) -> None:
|
|
data = {self.missing_app: {}}
|
|
inv = self._write_yaml_inventory(data)
|
|
result = self.validator.validate(inv, [self.missing_app])
|
|
expected = {self.missing_app: {"in_roles": False, "in_inventory": True}}
|
|
self.assertEqual(result, expected)
|
|
|
|
def test_missing_in_inventory_yaml(self) -> None:
|
|
data = {"group": {"other": {}}}
|
|
inv = self._write_yaml_inventory(data)
|
|
result = self.validator.validate(inv, [self.existing_app])
|
|
expected = {self.existing_app: {"in_roles": True, "in_inventory": False}}
|
|
self.assertEqual(result, expected)
|
|
|
|
def test_missing_both_yaml(self) -> None:
|
|
data = {}
|
|
inv = self._write_yaml_inventory(data)
|
|
result = self.validator.validate(inv, [self.missing_app])
|
|
expected = {self.missing_app: {"in_roles": False, "in_inventory": False}}
|
|
self.assertEqual(result, expected)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|