Files
home-services/tests/test_composectl.py
T

94 lines
3.3 KiB
Python

import shutil
import unittest
from pathlib import Path
from composectl import (
build_compose_command,
build_update_commands,
detect_runtime,
discover_compose_files,
discover_stack_names,
)
class ComposeCtlTests(unittest.TestCase):
def test_detect_runtime_returns_installed_backend(self):
runtime = detect_runtime()
expected = "docker" if shutil.which("docker") else "podman"
self.assertEqual(runtime, expected)
def test_detect_runtime_rejects_unavailable_explicit_runtime(self):
if shutil.which("docker"):
self.skipTest("Docker is available on this host")
with self.assertRaises(RuntimeError):
detect_runtime(preferred="docker")
def test_build_compose_command_uses_selected_runtime(self):
cmd = build_compose_command(
runtime="podman",
compose_file=Path("services/media_rhel1/compose.yaml"),
project_name="media_rhel1_services",
action="up",
)
self.assertEqual(
cmd,
[
"podman",
"compose",
"-f",
"services/media_rhel1/compose.yaml",
"-p",
"media_rhel1_services",
"up",
"-d",
"--remove-orphans",
],
)
def test_discover_compose_files_finds_existing_stacks(self):
stack_files = discover_compose_files(Path("."))
self.assertIn(Path("services/media_nas1/compose.yaml"), stack_files)
self.assertIn(Path("services/media_rhel1/compose.yaml"), stack_files)
def test_discover_stack_names_returns_inferred_stack_names(self):
stack_names = discover_stack_names(Path("."))
self.assertIn("media_nas1", stack_names)
self.assertIn("media_rhel1", stack_names)
def test_build_update_commands_returns_full_stack_refresh_sequence(self):
commands = build_update_commands(
runtime="podman",
compose_file=Path("services/media_rhel1/compose.yaml"),
project_name="media_rhel1_services",
)
self.assertEqual(
commands,
[
["podman", "compose", "-f", "services/media_rhel1/compose.yaml", "-p", "media_rhel1_services", "config"],
["podman", "compose", "-f", "services/media_rhel1/compose.yaml", "-p", "media_rhel1_services", "pull"],
["podman", "compose", "-f", "services/media_rhel1/compose.yaml", "-p", "media_rhel1_services", "up", "-d", "--remove-orphans"],
],
)
def test_build_update_commands_can_include_image_prune(self):
commands = build_update_commands(
runtime="docker",
compose_file=Path("services/media_nas1/compose.yaml"),
project_name="media_nas1_services",
prune=True,
)
self.assertEqual(commands[-1], ["docker", "image", "prune", "-f"])
def test_build_compose_command_can_validate_configuration(self):
command = build_compose_command(
runtime="docker",
compose_file=Path("services/media_nas1/compose.yaml"),
project_name="media_nas1_services",
action="config",
)
self.assertEqual(command[-1], "config")
if __name__ == "__main__":
unittest.main()