Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(logs): Fixing bug in logs command, adding tests #137

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions devservices/commands/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

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")

Check warning on line 46 in devservices/commands/logs.py

View check run for this annotation

Codecov / codecov/patch

devservices/commands/logs.py#L46

Added line #L46 was not covered by tests
return

try:
Expand Down
67 changes: 67 additions & 0 deletions tests/commands/test_logs.py
Original file line number Diff line number Diff line change
@@ -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")