From 235e1776df8df84c5607f037e9211d7f2b7b69d7 Mon Sep 17 00:00:00 2001 From: Ian Woodard <17186604+IanWoodard@users.noreply.github.com> Date: Thu, 19 Dec 2024 09:35:30 -0600 Subject: [PATCH] Fixing tests --- devservices/commands/purge.py | 9 ++++--- tests/commands/test_purge.py | 48 +++++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/devservices/commands/purge.py b/devservices/commands/purge.py index 7b5bf8c..c65999d 100644 --- a/devservices/commands/purge.py +++ b/devservices/commands/purge.py @@ -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) with Status( lambda: console.warning("Stopping all running devservices containers"), diff --git a/tests/commands/test_purge.py b/tests/commands/test_purge.py index 51760f0..1007f45 100644 --- a/tests/commands/test_purge.py +++ b/tests/commands/test_purge.py @@ -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", @@ -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 ( @@ -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( @@ -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( @@ -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(