mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 22:37:29 +00:00
The published Production guide of svc-bkp-nfs-2-local deployed only that role, so the web-app-docs CLI replay (PR 3103 Backup·NFS#0) ran no svc-storage-nfs-server task, nothing created /srv/nfs and the backup unit stopped with 'ERROR: /srv/nfs missing'. guide_companions adds the NFS server to the guide and its replay. 🎯 Fix: 85 % 💥 Break: 5 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: 100 % 🏆 Score: 81 🚦 Gate: ⛔ fix 85 % < 90 Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
106 lines
3.6 KiB
Python
106 lines
3.6 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from cli.build.docs.readme.generate import generate_readme
|
|
from utils.cache.files import PROJECT_ROOT
|
|
from utils.roles.mapping import (
|
|
ROLE_FILE_TASKS_MAIN,
|
|
ROLE_FILE_TEMPL_COMPOSE,
|
|
ROLE_FILE_VARS_MAIN,
|
|
)
|
|
|
|
ROLE = "web-app-sample"
|
|
READS_COUNTRY = 'SAMPLE_COUNTRY: "{{ service_provider.company.address.country }}"\n'
|
|
COUNTRY_VARS = (
|
|
'\\"service_provider\\": {\\"company\\": {\\"address\\": '
|
|
'{\\"country\\": \\"$COUNTRY\\"}}}, '
|
|
)
|
|
|
|
|
|
def _production_block(files: dict[str, str]) -> str:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
role_dir = Path(tmp) / ROLE
|
|
for rel, content in files.items():
|
|
path = role_dir / rel
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(content, encoding="utf-8")
|
|
text, _ = generate_readme(role_dir, ROLE)
|
|
return text.split("### Production", 1)[1].split("## Credits", 1)[0]
|
|
|
|
|
|
class TestServiceProviderCountry(unittest.TestCase):
|
|
def test_stack_role_reading_the_country_in_vars_asks_for_it(self) -> None:
|
|
block = _production_block(
|
|
{ROLE_FILE_TEMPL_COMPOSE: "", ROLE_FILE_VARS_MAIN: READS_COUNTRY}
|
|
)
|
|
|
|
self.assertIn('COUNTRY="<your-country>"', block)
|
|
self.assertIn('-e COUNTRY="$COUNTRY"', block)
|
|
self.assertIn(COUNTRY_VARS, block)
|
|
|
|
def test_role_reading_the_country_in_tasks_asks_for_it(self) -> None:
|
|
block = _production_block(
|
|
{ROLE_FILE_TEMPL_COMPOSE: "", ROLE_FILE_TASKS_MAIN: READS_COUNTRY}
|
|
)
|
|
|
|
self.assertIn('COUNTRY="<your-country>"', block)
|
|
self.assertIn(COUNTRY_VARS, block)
|
|
|
|
def test_host_role_reading_the_country_asks_for_it(self) -> None:
|
|
block = _production_block({ROLE_FILE_VARS_MAIN: READS_COUNTRY})
|
|
|
|
self.assertIn("--host localhost", block)
|
|
self.assertIn('COUNTRY="<your-country>"', block)
|
|
self.assertIn(COUNTRY_VARS, block)
|
|
|
|
def test_the_production_commands_parse_as_bash(self) -> None:
|
|
for files in (
|
|
{ROLE_FILE_TEMPL_COMPOSE: "", ROLE_FILE_VARS_MAIN: READS_COUNTRY},
|
|
{ROLE_FILE_VARS_MAIN: READS_COUNTRY},
|
|
):
|
|
block = _production_block(files)
|
|
scripts = re.findall(r"```bash\n(.*?)```", block, re.DOTALL)
|
|
self.assertTrue(scripts)
|
|
for script in scripts:
|
|
checked = subprocess.run(
|
|
["bash", "-n"],
|
|
input=script,
|
|
text=True,
|
|
capture_output=True,
|
|
check=False,
|
|
)
|
|
self.assertEqual(checked.returncode, 0, checked.stderr)
|
|
|
|
def test_role_rendering_the_country_only_in_a_template_does_not_ask(
|
|
self,
|
|
) -> None:
|
|
block = _production_block(
|
|
{
|
|
ROLE_FILE_TEMPL_COMPOSE: "",
|
|
"templates/imprint.html.j2": READS_COUNTRY,
|
|
}
|
|
)
|
|
|
|
self.assertNotIn("COUNTRY", block)
|
|
self.assertNotIn("service_provider", block)
|
|
|
|
|
|
class TestGuideCompanions(unittest.TestCase):
|
|
def test_backup_nfs_guide_deploys_the_export_it_backs_up(self) -> None:
|
|
role = "svc-bkp-nfs-2-local"
|
|
text, _ = generate_readme(PROJECT_ROOT / "roles" / role, role)
|
|
block = text.split("### Production", 1)[1].split("## Credits", 1)[0]
|
|
apps = re.search(r"^APP=(\S+)$", block, re.MULTILINE)
|
|
|
|
self.assertIsNotNone(apps)
|
|
self.assertEqual(apps.group(1).split(","), [role, "svc-storage-nfs-server"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|