mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-30 07:18:09 +02:00
Extend repair scripts with env-file support and unit tests
- Added detect_env_file() to both sys-ctl-rpr-docker-soft and sys-ctl-rpr-docker-hard * prefer .env, fallback to .env/env * append --env-file parameter automatically - Refactored soft script to use compose_cmd() for consistent command building - Adjusted error recovery path in soft script to also respect env-file - Extended unit tests for soft script to cover env-file priority and restart commands - Added new unit tests for hard script verifying env-file priority, cwd handling, and --only filter logic Ref: https://chatgpt.com/share/68ad7b30-7510-800f-8172-56f03a2f40f5
This commit is contained in:
@@ -42,6 +42,38 @@ class TestRepairDockerSoft(unittest.TestCase):
|
||||
)
|
||||
self.assertEqual(s.normalize_services_arg([], ""), [])
|
||||
|
||||
def test_detect_env_file_priority(self):
|
||||
s = self.script
|
||||
base = "/proj"
|
||||
# Monkeypatch os.path.isfile
|
||||
old_isfile = s.os.path.isfile
|
||||
try:
|
||||
def fake_isfile(path):
|
||||
# Only .env exists
|
||||
return path == f"{base}/.env"
|
||||
s.os.path.isfile = fake_isfile
|
||||
self.assertEqual(s.detect_env_file(base), f"{base}/.env")
|
||||
|
||||
# Only .env/env exists
|
||||
def fake_isfile2(path):
|
||||
return path == f"{base}/.env/env"
|
||||
s.os.path.isfile = fake_isfile2
|
||||
self.assertEqual(s.detect_env_file(base), f"{base}/.env/env")
|
||||
|
||||
# Both exist -> prefer .env
|
||||
def fake_isfile3(path):
|
||||
return path in (f"{base}/.env", f"{base}/.env/env")
|
||||
s.os.path.isfile = fake_isfile3
|
||||
self.assertEqual(s.detect_env_file(base), f"{base}/.env")
|
||||
|
||||
# Neither exists
|
||||
def fake_isfile4(path):
|
||||
return False
|
||||
s.os.path.isfile = fake_isfile4
|
||||
self.assertIsNone(s.detect_env_file(base))
|
||||
finally:
|
||||
s.os.path.isfile = old_isfile
|
||||
|
||||
def test_wait_while_manipulation_running_respects_timeout(self):
|
||||
s = self.script
|
||||
calls = {"checks": 0, "sleeps": 0}
|
||||
@@ -77,7 +109,7 @@ class TestRepairDockerSoft(unittest.TestCase):
|
||||
s.time.sleep = old_sleep
|
||||
s.time.time = old_time
|
||||
|
||||
def test_main_restarts_and_counts_errors(self):
|
||||
def test_main_restarts_and_counts_errors_and_envfile_usage(self):
|
||||
s = self.script
|
||||
cmd_log = []
|
||||
|
||||
@@ -92,25 +124,55 @@ class TestRepairDockerSoft(unittest.TestCase):
|
||||
return []
|
||||
|
||||
def fake_find_docker_compose(path):
|
||||
# Compose-Projekte: app1, db -> vorhanden; "other" -> nicht vorhanden
|
||||
if path.endswith("/app1") or path.endswith("/db"):
|
||||
return str(Path(path) / "docker-compose.yml")
|
||||
return None
|
||||
|
||||
# Steuere die detect_env_file-Antwort:
|
||||
# - Für app1 existiert nur .env/env
|
||||
# - Für db existiert .env
|
||||
def fake_detect_env_file(project_path: str):
|
||||
if project_path.endswith("/app1"):
|
||||
return f"{project_path}/.env/env"
|
||||
if project_path.endswith("/db"):
|
||||
return f"{project_path}/.env"
|
||||
return None
|
||||
|
||||
old_print_bash = s.print_bash
|
||||
old_find = s.find_docker_compose_file
|
||||
old_detect = s.detect_env_file
|
||||
try:
|
||||
s.print_bash = fake_print_bash
|
||||
s.find_docker_compose_file = fake_find_docker_compose # <-- jetzt gleicher Name!
|
||||
s.find_docker_compose_file = fake_find_docker_compose
|
||||
s.detect_env_file = fake_detect_env_file
|
||||
|
||||
errors = s.main("/BASE", manipulation_services=[], timeout=None)
|
||||
# one error expected for "other" (no compose file)
|
||||
self.assertEqual(errors, 1)
|
||||
|
||||
restart_cmds = [c for c in cmd_log if "docker-compose -p" in c and " restart" in c]
|
||||
self.assertTrue(any('cd "/BASE/app1"' in c and 'docker-compose -p "app1" restart' in c for c in restart_cmds))
|
||||
self.assertTrue(any('cd "/BASE/db"' in c and 'docker-compose -p "db" restart' in c for c in restart_cmds))
|
||||
restart_cmds = [c for c in cmd_log if ' docker-compose' in c and " restart" in c]
|
||||
# app1: --env-file "/BASE/app1/.env/env" + -p "app1"
|
||||
self.assertTrue(any(
|
||||
'cd "/BASE/app1"' in c and
|
||||
'--env-file "/BASE/app1/.env/env"' in c and
|
||||
'-p "app1"' in c and
|
||||
' restart' in c
|
||||
for c in restart_cmds
|
||||
))
|
||||
# db: --env-file "/BASE/db/.env" + -p "db"
|
||||
self.assertTrue(any(
|
||||
'cd "/BASE/db"' in c and
|
||||
'--env-file "/BASE/db/.env"' in c and
|
||||
'-p "db"' in c and
|
||||
' restart' in c
|
||||
for c in restart_cmds
|
||||
))
|
||||
finally:
|
||||
s.print_bash = old_print_bash
|
||||
s.find_docker_compose_file = old_find
|
||||
s.detect_env_file = old_detect
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
Reference in New Issue
Block a user