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

feat(stop): Add more context for what is going on underneath the hood for stop #149

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 14 additions & 2 deletions devservices/commands/down.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import concurrent.futures
import os
import subprocess
from argparse import _SubParsersAction
from argparse import ArgumentParser
from argparse import Namespace
Expand All @@ -13,6 +14,7 @@
from devservices.constants import DEVSERVICES_DEPENDENCIES_CACHE_DIR
from devservices.constants import DEVSERVICES_DEPENDENCIES_CACHE_DIR_KEY
from devservices.constants import DEVSERVICES_DIR_NAME
from devservices.constants import DOCKER_COMPOSE_COMMAND_LENGTH
from devservices.exceptions import DependencyError
from devservices.exceptions import DockerComposeError
from devservices.utils.console import Console
Expand Down Expand Up @@ -82,7 +84,7 @@ def down(args: Namespace) -> None:
service, remote_dependencies
)
try:
_down(service, remote_dependencies, mode_dependencies)
_down(service, remote_dependencies, mode_dependencies, status)
except DockerComposeError as dce:
capture_exception(dce)
status.failure(f"Failed to stop {service.name}: {dce.stderr}")
Expand All @@ -93,10 +95,20 @@ def down(args: Namespace) -> None:
state.remove_started_service(service.name)


def _bring_down_dependency(
cmd: list[str], current_env: dict[str, str], status: Status
) -> subprocess.CompletedProcess[str]:
# TODO: Get rid of these constants, we need a smarter way to determine the containers being brought down
for dependency in cmd[DOCKER_COMPOSE_COMMAND_LENGTH:]:
status.info(f"Stopping {dependency}")
return run_cmd(cmd, current_env)


def _down(
service: Service,
remote_dependencies: set[InstalledRemoteDependency],
mode_dependencies: list[str],
status: Status,
) -> None:
relative_local_dependency_directory = os.path.relpath(
os.path.join(DEVSERVICES_DEPENDENCIES_CACHE_DIR, DEPENDENCY_CONFIG_VERSION),
Expand Down Expand Up @@ -124,7 +136,7 @@ def _down(

with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [
executor.submit(run_cmd, cmd, current_env)
executor.submit(_bring_down_dependency, cmd, current_env, status)
for cmd in docker_compose_commands
]
for future in concurrent.futures.as_completed(futures):
Expand Down
1 change: 1 addition & 0 deletions devservices/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
DEVSERVICES_DEPENDENCIES_CACHE_DIR = os.path.join(DEVSERVICES_CACHE_DIR, "dependencies")
DEVSERVICES_DEPENDENCIES_CACHE_DIR_KEY = "DEVSERVICES_DEPENDENCIES_CACHE_DIR"
STATE_DB_FILE = os.path.join(DEVSERVICES_LOCAL_DIR, "state")
DOCKER_COMPOSE_COMMAND_LENGTH = 7

DEPENDENCY_CONFIG_VERSION = "v1"
DEPENDENCY_GIT_PARTIAL_CLONE_CONFIG_OPTIONS = {
Expand Down
15 changes: 13 additions & 2 deletions tests/commands/test_down.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
),
)
@mock.patch("devservices.utils.state.State.remove_started_service")
def test_down_simple(
mock_remove_started_service: mock.Mock, mock_run: mock.Mock, tmp_path: Path
def test_up_simple(
mock_remove_started_service: mock.Mock,
mock_run: mock.Mock,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
with mock.patch(
"devservices.commands.down.DEVSERVICES_DEPENDENCIES_CACHE_DIR",
Expand Down Expand Up @@ -91,6 +94,10 @@ def test_down_simple(

mock_remove_started_service.assert_called_with("example-service")

captured = capsys.readouterr()
assert "Stopping clickhouse" in captured.out.strip()
assert "Stopping redis" in captured.out.strip()


@mock.patch("devservices.utils.docker_compose.subprocess.run")
@mock.patch("devservices.utils.state.State.remove_started_service")
Expand Down Expand Up @@ -140,3 +147,7 @@ def test_down_error(
)

mock_remove_started_service.assert_not_called()

captured = capsys.readouterr()
assert "Stopping clickhouse" not in captured.out.strip()
assert "Stopping redis" not in captured.out.strip()