-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(logs): Fixing bug in logs command, adding tests (#137)
* fix(logs): Fixing bug in logs command, adding tests * Improving test coverage
- Loading branch information
1 parent
1f9e62f
commit f42b680
Showing
2 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
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_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") | ||
@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_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", | ||
["redis", "clickhouse"], | ||
{}, | ||
options=["-n", "100"], | ||
) | ||
|
||
captured = capsys.readouterr() | ||
assert captured.out.endswith("redis and clickhouse log output\n") |