Skip to content

Commit

Permalink
Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IanWoodard committed Dec 19, 2024
1 parent 16b8fb2 commit 235e177
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
9 changes: 6 additions & 3 deletions devservices/commands/purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ def purge(_args: Namespace) -> None:
state.clear_state()
try:
devservices_containers = get_matching_containers(DEVSERVICES_ORCHESTRATOR_LABEL)
except DockerError as e:
console.failure(f"Failed to get devservices containers {e}")
except DockerDaemonNotRunningError as e:
console.warning(str(e))
return
except DockerError as de:
console.failure(f"Failed to get devservices containers {de.stderr}")
exit(1)
try:
devservices_volumes = get_volumes_for_containers(devservices_containers)
except DockerError as e:
console.failure(f"Failed to get devservices volumes {e}")
console.failure(f"Failed to get devservices volumes {e.stderr}")
exit(1)

Check warning on line 52 in devservices/commands/purge.py

View check run for this annotation

Codecov / codecov/patch

devservices/commands/purge.py#L50-L52

Added lines #L50 - L52 were not covered by tests
with Status(
lambda: console.warning("Stopping all running devservices containers"),
Expand Down
48 changes: 29 additions & 19 deletions tests/commands/test_purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
import pytest

from devservices.commands.purge import purge
from devservices.constants import DEVSERVICES_ORCHESTRATOR_LABEL
from devservices.exceptions import DockerDaemonNotRunningError
from devservices.exceptions import DockerError
from devservices.utils.state import State


@mock.patch("devservices.commands.purge.stop_matching_containers")
@mock.patch("devservices.commands.purge.get_matching_containers")
@mock.patch("devservices.commands.purge.stop_containers")
@mock.patch("devservices.commands.purge.subprocess.run")
def test_purge_docker_daemon_not_running(
mock_run: mock.Mock,
mock_stop_matching_containers: mock.Mock,
mock_stop_containers: mock.Mock,
mock_get_matching_containers: mock.Mock,
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
) -> None:
mock_stop_matching_containers.side_effect = DockerDaemonNotRunningError()
mock_get_matching_containers.side_effect = DockerDaemonNotRunningError()
with (
mock.patch(
"devservices.commands.purge.DEVSERVICES_CACHE_DIR",
Expand Down Expand Up @@ -50,25 +53,30 @@ def test_purge_docker_daemon_not_running(
assert not cache_file.exists()
assert state.get_started_services() == []

mock_stop_matching_containers.assert_called_once()
mock_get_matching_containers.assert_called_once_with(
DEVSERVICES_ORCHESTRATOR_LABEL
)
mock_stop_containers.assert_not_called()
mock_run.assert_not_called()

captured = capsys.readouterr()
assert (
"The docker daemon is not running, no containers to stop"
"Unable to connect to the docker daemon. Is the docker daemon running?"
in captured.out.strip()
)


@mock.patch("devservices.commands.purge.stop_matching_containers")
@mock.patch("devservices.commands.purge.get_matching_containers")
@mock.patch("devservices.commands.purge.stop_containers")
@mock.patch("devservices.commands.purge.subprocess.run")
def test_purge_docker_daemon_docker_error(
mock_run: mock.Mock,
mock_stop_matching_containers: mock.Mock,
mock_stop_containers: mock.Mock,
mock_get_matching_containers: mock.Mock,
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
) -> None:
mock_stop_matching_containers.side_effect = DockerError(
mock_get_matching_containers.side_effect = DockerError(
"command", 1, "output", "stderr"
)
with (
Expand Down Expand Up @@ -101,20 +109,20 @@ def test_purge_docker_daemon_docker_error(
assert not cache_file.exists()
assert state.get_started_services() == []

mock_stop_matching_containers.assert_called_once()
mock_get_matching_containers.assert_called_once_with(
DEVSERVICES_ORCHESTRATOR_LABEL
)
mock_stop_containers.assert_not_called()
mock_run.assert_not_called()

captured = capsys.readouterr()
assert (
"Failed to stop running devservices containers stderr"
in captured.out.strip()
)
assert "Failed to get devservices containers stderr" in captured.out.strip()


@mock.patch("devservices.commands.purge.stop_matching_containers")
@mock.patch("devservices.commands.purge.stop_containers")
@mock.patch("devservices.commands.purge.subprocess.run")
def test_purge_with_cache_and_state_and_no_running_containers(
mock_run: mock.Mock, mock_stop_matching_containers: mock.Mock, tmp_path: Path
mock_run: mock.Mock, mock_stop_containers: mock.Mock, tmp_path: Path
) -> None:
with (
mock.patch(
Expand Down Expand Up @@ -148,14 +156,14 @@ def test_purge_with_cache_and_state_and_no_running_containers(
assert not cache_file.exists()
assert state.get_started_services() == []

mock_stop_matching_containers.assert_called_once()
mock_stop_containers.assert_called_once_with([], should_remove=True)
mock_run.assert_not_called()


@mock.patch("devservices.commands.purge.stop_matching_containers")
@mock.patch("devservices.commands.purge.stop_containers")
@mock.patch("devservices.commands.purge.subprocess.run")
def test_purge_with_cache_and_state_and_running_containers_with_networks(
mock_run: mock.Mock, mock_stop_matching_containers: mock.Mock, tmp_path: Path
mock_run: mock.Mock, mock_stop_containers: mock.Mock, tmp_path: Path
) -> None:
with (
mock.patch(
Expand Down Expand Up @@ -189,7 +197,9 @@ def test_purge_with_cache_and_state_and_running_containers_with_networks(
assert not cache_file.exists()
assert state.get_started_services() == []

mock_stop_matching_containers.assert_called_once()
mock_stop_containers.assert_called_once_with(
["abc", "def", "ghe"], should_remove=True
)
mock_run.assert_has_calls(
[
mock.call(
Expand Down

0 comments on commit 235e177

Please sign in to comment.