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

ref(commands): Improving commands to use starting services and started #212

Merged
merged 3 commits into from
Jan 16, 2025
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
16 changes: 13 additions & 3 deletions devservices/commands/list_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def list_services(args: Namespace) -> None:
coderoot = get_coderoot()
services = get_local_services(coderoot)
state = State()
running_services = state.get_service_entries(StateTables.STARTED_SERVICES)
starting_services = set(state.get_service_entries(StateTables.STARTING_SERVICES))
started_services = set(state.get_service_entries(StateTables.STARTED_SERVICES))
running_services = starting_services.union(started_services)
IanWoodard marked this conversation as resolved.
Show resolved Hide resolved

if not services:
console.warning("No services found")
Expand All @@ -47,10 +49,18 @@ def list_services(args: Namespace) -> None:
console.info("Running services:")

for service in services_to_show:
status = "running" if service.name in running_services else "stopped"
active_modes = state.get_active_modes_for_service(
status = "stopped"
if service.name in starting_services:
status = "starting"
elif service.name in started_services:
status = "running"
active_starting_modes = state.get_active_modes_for_service(
service.name, StateTables.STARTING_SERVICES
)
active_started_modes = state.get_active_modes_for_service(
service.name, StateTables.STARTED_SERVICES
)
active_modes = active_starting_modes or active_started_modes
console.info(f"- {service.name}")
console.info(f" modes: {active_modes}")
console.info(f" status: {status}")
Expand Down
4 changes: 3 additions & 1 deletion devservices/commands/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def logs(args: Namespace) -> None:
mode_dependencies = modes[mode_to_use]

state = State()
running_services = state.get_service_entries(StateTables.STARTED_SERVICES)
starting_services = set(state.get_service_entries(StateTables.STARTING_SERVICES))
started_services = set(state.get_service_entries(StateTables.STARTED_SERVICES))
running_services = starting_services.union(started_services)
if service.name not in running_services:
console.warning(f"Service {service.name} is not running")
return
Expand Down
120 changes: 101 additions & 19 deletions tests/commands/test_list_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,82 @@
import pytest

from devservices.commands.list_services import list_services
from devservices.utils.state import State
from devservices.utils.state import StateTables
from testing.utils import create_config_file


def test_list_running_services(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
@mock.patch("devservices.utils.state.State.get_service_entries")
@mock.patch("devservices.utils.state.State.get_active_modes_for_service")
def test_list_running_services_starting(
mock_get_active_modes_for_service: mock.Mock,
mock_get_service_entries: mock.Mock,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
with mock.patch(
"devservices.commands.list_services.get_coderoot",
return_value=str(tmp_path / "code"),
), mock.patch("devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")):
state = State()
state.update_service_entry(
"example-service", "default", StateTables.STARTED_SERVICES
with (
mock.patch(
"devservices.commands.list_services.get_coderoot",
return_value=str(tmp_path / "code"),
),
mock.patch("devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")),
):
mock_get_service_entries.side_effect = [["example-service"], []]
mock_get_active_modes_for_service.side_effect = [["default"], []]
config = {
"x-sentry-service-config": {
"version": 0.1,
"service_name": "example-service",
"dependencies": {
"redis": {"description": "Redis"},
"clickhouse": {"description": "Clickhouse"},
},
"modes": {"default": ["redis", "clickhouse"]},
},
"services": {
"redis": {"image": "redis:6.2.14-alpine"},
"clickhouse": {
"image": "altinity/clickhouse-server:23.8.11.29.altinitystable"
},
},
}
create_config_file(tmp_path / "code" / "example-service", config)

args = Namespace(service_name=None, all=False)
list_services(args)

mock_get_service_entries.assert_has_calls(
[
mock.call(StateTables.STARTING_SERVICES),
mock.call(StateTables.STARTED_SERVICES),
]
)

# Capture the printed output
captured = capsys.readouterr()

assert (
captured.out
== f"Running services:\n- example-service\n modes: ['default']\n status: starting\n location: {tmp_path / 'code' / 'example-service'}\n"
)


@mock.patch("devservices.utils.state.State.get_service_entries")
@mock.patch("devservices.utils.state.State.get_active_modes_for_service")
def test_list_running_services_started(
mock_get_active_modes_for_service: mock.Mock,
mock_get_service_entries: mock.Mock,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
with (
mock.patch(
"devservices.commands.list_services.get_coderoot",
return_value=str(tmp_path / "code"),
),
mock.patch("devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")),
):
mock_get_service_entries.side_effect = [[], ["example-service"]]
mock_get_active_modes_for_service.side_effect = [[], ["default"]]
config = {
"x-sentry-service-config": {
"version": 0.1,
Expand All @@ -45,6 +105,13 @@ def test_list_running_services(
args = Namespace(service_name=None, all=False)
list_services(args)

mock_get_service_entries.assert_has_calls(
[
mock.call(StateTables.STARTING_SERVICES),
mock.call(StateTables.STARTED_SERVICES),
]
)

# Capture the printed output
captured = capsys.readouterr()

Expand All @@ -54,15 +121,23 @@ def test_list_running_services(
)


def test_list_all_services(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
with mock.patch(
"devservices.commands.list_services.get_coderoot",
return_value=str(tmp_path / "code"),
), mock.patch("devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")):
state = State()
state.update_service_entry(
"example-service", "default", StateTables.STARTED_SERVICES
)
@mock.patch("devservices.utils.state.State.get_service_entries")
@mock.patch("devservices.utils.state.State.get_active_modes_for_service")
def test_list_all_services(
mock_get_active_modes_for_service: mock.Mock,
mock_get_service_entries: mock.Mock,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
with (
mock.patch(
"devservices.commands.list_services.get_coderoot",
return_value=str(tmp_path / "code"),
),
mock.patch("devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")),
):
mock_get_service_entries.side_effect = [[], ["example-service"]]
mock_get_active_modes_for_service.side_effect = [[], ["default"]]
config = {
"x-sentry-service-config": {
"version": 0.1,
Expand All @@ -85,6 +160,13 @@ def test_list_all_services(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -
args = Namespace(service_name=None, all=True)
list_services(args)

mock_get_service_entries.assert_has_calls(
[
mock.call(StateTables.STARTING_SERVICES),
mock.call(StateTables.STARTED_SERVICES),
]
)

# Capture the printed output
captured = capsys.readouterr()

Expand Down
23 changes: 21 additions & 2 deletions tests/commands/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from devservices.exceptions import ConfigError
from devservices.exceptions import ServiceNotFoundError
from devservices.utils.services import Service
from devservices.utils.state import StateTables


@mock.patch("devservices.commands.logs.get_docker_compose_commands_to_run")
Expand Down Expand Up @@ -54,7 +55,16 @@ def test_logs_no_specified_service_not_running(
logs(args)

mock_find_matching_service.assert_called_once_with(None)
mock_get_service_entries.assert_called_once()
mock_get_service_entries.assert_has_calls(
[
mock.call(
StateTables.STARTING_SERVICES,
),
mock.call(
StateTables.STARTED_SERVICES,
),
]
)
mock_install_and_verify_dependencies.assert_not_called()
mock_get_docker_compose_commands_to_run.assert_not_called()

Expand Down Expand Up @@ -125,7 +135,16 @@ def test_logs_no_specified_service_success(
logs(args)

mock_find_matching_service.assert_called_once_with(None)
mock_get_service_entries.assert_called_once()
mock_get_service_entries.assert_has_calls(
[
mock.call(
StateTables.STARTING_SERVICES,
),
mock.call(
StateTables.STARTED_SERVICES,
),
]
)
mock_install_and_verify_dependencies.assert_called_once()
mock_run_cmd.assert_called_once_with(
[
Expand Down
Loading