From 21d415b4ce5788148b3f95757e5ead7e302a4fdc Mon Sep 17 00:00:00 2001 From: Ian Woodard <17186604+IanWoodard@users.noreply.github.com> Date: Mon, 11 Nov 2024 18:01:59 -0800 Subject: [PATCH 1/2] fix(logs): Fixing bug in logs command, adding tests --- devservices/commands/logs.py | 4 +-- tests/commands/test_logs.py | 67 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/commands/test_logs.py diff --git a/devservices/commands/logs.py b/devservices/commands/logs.py index 8fc9ec3..00965e2 100644 --- a/devservices/commands/logs.py +++ b/devservices/commands/logs.py @@ -42,8 +42,8 @@ def logs(args: Namespace) -> None: state = State() running_services = state.get_started_services() - if service_name not in running_services: - console.warning(f"Service {service_name} is not running") + if service.name not in running_services: + console.warning(f"Service {service.name} is not running") return try: diff --git a/tests/commands/test_logs.py b/tests/commands/test_logs.py new file mode 100644 index 0000000..54708d2 --- /dev/null +++ b/tests/commands/test_logs.py @@ -0,0 +1,67 @@ +from __future__ import annotations + +import subprocess +from argparse import Namespace +from pathlib import Path +from unittest import mock + +import pytest + +from devservices.commands.logs import logs +from devservices.configs.service_config import Dependency +from devservices.configs.service_config import ServiceConfig +from devservices.utils.services import Service + + +@mock.patch("devservices.commands.logs.run_docker_compose_command") +@mock.patch("devservices.commands.logs.find_matching_service") +@mock.patch("devservices.utils.state.State.get_started_services") +@mock.patch("devservices.commands.logs.install_and_verify_dependencies") +def test_logs_no_specified_service_success( + mock_install_and_verify_dependencies: mock.Mock, + mock_get_started_services: mock.Mock, + mock_find_matching_service: mock.Mock, + mock_run_docker_compose_command: mock.Mock, + capsys: pytest.CaptureFixture[str], + tmp_path: Path, +) -> None: + args = Namespace(service_name=None) + mock_service = Service( + name="example-service", + config=ServiceConfig( + version=0.1, + service_name="example-service", + dependencies={ + "redis": Dependency(description="Redis"), + "clickhouse": Dependency(description="Clickhouse"), + }, + modes={"default": ["redis", "clickhouse"]}, + ), + repo_path=str(tmp_path / "example-service"), + ) + mock_install_and_verify_dependencies.return_value = {} + mock_get_started_services.return_value = ["example-service"] + mock_find_matching_service.return_value = mock_service + mock_run_docker_compose_command.return_value = [ + subprocess.CompletedProcess( + args=["docker", "compose", "logs", "redis", "clickhouse"], + returncode=0, + stdout="redis and clickhouse log output", + ) + ] + + logs(args) + + mock_install_and_verify_dependencies.assert_called_once() + mock_get_started_services.assert_called_once() + mock_find_matching_service.assert_called_once_with(None) + mock_run_docker_compose_command.assert_called_once_with( + mock_service, + "logs", + ["redis", "clickhouse"], + {}, + options=["-n", "100"], + ) + + captured = capsys.readouterr() + assert captured.out.endswith("redis and clickhouse log output\n") From 233469bb9cb250c4c2ab3d69060e91bc2fc87dc6 Mon Sep 17 00:00:00 2001 From: Ian Woodard <17186604+IanWoodard@users.noreply.github.com> Date: Mon, 11 Nov 2024 18:08:38 -0800 Subject: [PATCH 2/2] Improving test coverage --- tests/commands/test_logs.py | 44 +++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/tests/commands/test_logs.py b/tests/commands/test_logs.py index 54708d2..6b1fd5d 100644 --- a/tests/commands/test_logs.py +++ b/tests/commands/test_logs.py @@ -13,6 +13,46 @@ from devservices.utils.services import Service +@mock.patch("devservices.commands.logs.run_docker_compose_command") +@mock.patch("devservices.commands.logs.find_matching_service") +@mock.patch("devservices.utils.state.State.get_started_services") +@mock.patch("devservices.commands.logs.install_and_verify_dependencies") +def test_logs_no_specified_service_not_running( + mock_install_and_verify_dependencies: mock.Mock, + mock_get_started_services: mock.Mock, + mock_find_matching_service: mock.Mock, + mock_run_docker_compose_command: mock.Mock, + capsys: pytest.CaptureFixture[str], + tmp_path: Path, +) -> None: + args = Namespace(service_name=None) + mock_service = Service( + name="example-service", + config=ServiceConfig( + version=0.1, + service_name="example-service", + dependencies={ + "redis": Dependency(description="Redis"), + "clickhouse": Dependency(description="Clickhouse"), + }, + modes={"default": ["redis", "clickhouse"]}, + ), + repo_path=str(tmp_path / "example-service"), + ) + mock_get_started_services.return_value = [] + mock_find_matching_service.return_value = mock_service + + logs(args) + + mock_find_matching_service.assert_called_once_with(None) + mock_get_started_services.assert_called_once() + mock_install_and_verify_dependencies.assert_not_called() + mock_run_docker_compose_command.assert_not_called() + + captured = capsys.readouterr() + assert "Service example-service is not running" in captured.out + + @mock.patch("devservices.commands.logs.run_docker_compose_command") @mock.patch("devservices.commands.logs.find_matching_service") @mock.patch("devservices.utils.state.State.get_started_services") @@ -52,9 +92,9 @@ def test_logs_no_specified_service_success( logs(args) - mock_install_and_verify_dependencies.assert_called_once() - mock_get_started_services.assert_called_once() mock_find_matching_service.assert_called_once_with(None) + mock_get_started_services.assert_called_once() + mock_install_and_verify_dependencies.assert_called_once() mock_run_docker_compose_command.assert_called_once_with( mock_service, "logs",