-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(purge): Adding state clear functionality to purge (#125)
* feat(purge): Adding state clear functionality to purge * Adding docker daemon check for purge * Adding docker tests * Adding a test with no running containers * Addressing review feedback * Addressing review feedback
- Loading branch information
1 parent
5c7343c
commit 06b683a
Showing
7 changed files
with
214 additions
and
13 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
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
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 |
---|---|---|
@@ -1,35 +1,132 @@ | ||
from __future__ import annotations | ||
|
||
import builtins | ||
from argparse import Namespace | ||
from pathlib import Path | ||
from unittest import mock | ||
|
||
from devservices.commands.purge import purge | ||
from devservices.utils.state import State | ||
|
||
|
||
def test_purge_no_cache(tmp_path: Path) -> None: | ||
with mock.patch( | ||
"devservices.commands.purge.DEVSERVICES_CACHE_DIR", | ||
str(tmp_path / ".devservices-cache"), | ||
@mock.patch("devservices.commands.purge.stop_all_running_containers") | ||
def test_purge_not_confirmed( | ||
mock_stop_all_running_containers: mock.Mock, tmp_path: Path | ||
) -> None: | ||
with ( | ||
mock.patch( | ||
"devservices.commands.purge.DEVSERVICES_CACHE_DIR", | ||
str(tmp_path / ".devservices-cache"), | ||
), | ||
mock.patch("devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")), | ||
mock.patch.object(builtins, "input", lambda _: "no"), | ||
): | ||
args = Namespace() | ||
purge(args) | ||
|
||
mock_stop_all_running_containers.assert_not_called() | ||
|
||
def test_purge_with_cache(tmp_path: Path) -> None: | ||
with mock.patch( | ||
"devservices.commands.purge.DEVSERVICES_CACHE_DIR", | ||
str(tmp_path / ".devservices-cache"), | ||
|
||
@mock.patch("devservices.commands.purge.stop_all_running_containers") | ||
def test_purge_with_cache_and_state_and_no_running_containers_confirmed( | ||
mock_stop_all_running_containers: mock.Mock, tmp_path: Path | ||
) -> None: | ||
with ( | ||
mock.patch( | ||
"devservices.commands.purge.DEVSERVICES_CACHE_DIR", | ||
str(tmp_path / ".devservices-cache"), | ||
), | ||
mock.patch("devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")), | ||
mock.patch.object(builtins, "input", lambda _: "yes"), | ||
mock.patch( | ||
"devservices.utils.docker.check_docker_daemon_running", return_value=None | ||
), | ||
): | ||
# Create a cache file to test purging | ||
cache_dir = tmp_path / ".devservices-cache" | ||
cache_dir.mkdir(parents=True, exist_ok=True) | ||
cache_file = tmp_path / ".devservices-cache" / "test.txt" | ||
cache_file.write_text("This is a test cache file.") | ||
|
||
state = State() | ||
state.add_started_service("test-service", "test-mode") | ||
|
||
assert cache_file.exists() | ||
assert state.get_started_services() == ["test-service"] | ||
|
||
args = Namespace() | ||
purge(args) | ||
|
||
assert not cache_file.exists() | ||
assert state.get_started_services() == [] | ||
|
||
mock_stop_all_running_containers.assert_called_once() | ||
|
||
|
||
@mock.patch("devservices.commands.purge.stop_all_running_containers") | ||
def test_purge_with_cache_and_state_and_running_containers_confirmed( | ||
mock_stop_all_running_containers: mock.Mock, tmp_path: Path | ||
) -> None: | ||
with ( | ||
mock.patch( | ||
"devservices.commands.purge.DEVSERVICES_CACHE_DIR", | ||
str(tmp_path / ".devservices-cache"), | ||
), | ||
mock.patch("devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")), | ||
mock.patch.object(builtins, "input", lambda _: "yes"), | ||
mock.patch( | ||
"devservices.utils.docker.check_docker_daemon_running", return_value=None | ||
), | ||
): | ||
# Create a cache file to test purging | ||
cache_dir = tmp_path / ".devservices-cache" | ||
cache_dir.mkdir(parents=True, exist_ok=True) | ||
cache_file = tmp_path / ".devservices-cache" / "test.txt" | ||
cache_file.write_text("This is a test cache file.") | ||
|
||
state = State() | ||
state.add_started_service("test-service", "test-mode") | ||
|
||
assert cache_file.exists() | ||
assert state.get_started_services() == ["test-service"] | ||
|
||
args = Namespace() | ||
purge(args) | ||
|
||
assert not cache_file.exists() | ||
assert state.get_started_services() == [] | ||
|
||
mock_stop_all_running_containers.assert_called_once() | ||
|
||
|
||
@mock.patch("devservices.commands.purge.stop_all_running_containers") | ||
def test_purge_with_cache_and_state_and_running_containers_not_confirmed( | ||
mock_stop_all_running_containers: mock.Mock, tmp_path: Path | ||
) -> None: | ||
with ( | ||
mock.patch( | ||
"devservices.commands.purge.DEVSERVICES_CACHE_DIR", | ||
str(tmp_path / ".devservices-cache"), | ||
), | ||
mock.patch("devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")), | ||
mock.patch.object(builtins, "input", lambda _: "no"), | ||
mock.patch( | ||
"devservices.utils.docker.check_docker_daemon_running", return_value=None | ||
), | ||
): | ||
# Create a cache file to test purging | ||
cache_dir = tmp_path / ".devservices-cache" | ||
cache_dir.mkdir(parents=True, exist_ok=True) | ||
cache_file = tmp_path / ".devservices-cache" / "test.txt" | ||
cache_file.write_text("This is a test cache file.") | ||
|
||
state = State() | ||
state.add_started_service("test-service", "test-mode") | ||
|
||
args = Namespace() | ||
purge(args) | ||
|
||
assert cache_file.exists() | ||
assert state.get_started_services() == ["test-service"] | ||
|
||
mock_stop_all_running_containers.assert_not_called() |
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,47 @@ | ||
from __future__ import annotations | ||
|
||
import subprocess | ||
from unittest import mock | ||
|
||
from devservices.utils.docker import stop_all_running_containers | ||
|
||
|
||
@mock.patch("subprocess.check_output") | ||
@mock.patch("subprocess.run") | ||
@mock.patch("devservices.utils.docker.check_docker_daemon_running") | ||
def test_stop_all_running_containers_none_running( | ||
mock_check_docker_daemon_running: mock.Mock, | ||
mock_run: mock.Mock, | ||
mock_check_output: mock.Mock, | ||
) -> None: | ||
mock_check_docker_daemon_running.return_value = None | ||
mock_check_output.return_value = b"" | ||
stop_all_running_containers() | ||
mock_check_docker_daemon_running.assert_called_once() | ||
mock_check_output.assert_called_once_with( | ||
["docker", "ps", "-q"], stderr=subprocess.DEVNULL | ||
) | ||
mock_run.assert_not_called() | ||
|
||
|
||
@mock.patch("subprocess.check_output") | ||
@mock.patch("subprocess.run") | ||
@mock.patch("devservices.utils.docker.check_docker_daemon_running") | ||
def test_stop_all_running_containers( | ||
mock_check_docker_daemon_running: mock.Mock, | ||
mock_run: mock.Mock, | ||
mock_check_output: mock.Mock, | ||
) -> None: | ||
mock_check_docker_daemon_running.return_value = None | ||
mock_check_output.return_value = b"container1\ncontainer2\n" | ||
stop_all_running_containers() | ||
mock_check_docker_daemon_running.assert_called_once() | ||
mock_check_output.assert_called_once_with( | ||
["docker", "ps", "-q"], stderr=subprocess.DEVNULL | ||
) | ||
mock_run.assert_called_once_with( | ||
["docker", "stop", "container1", "container2"], | ||
check=True, | ||
stdout=subprocess.DEVNULL, | ||
stderr=subprocess.DEVNULL, | ||
) |