fix: lowercase container name filter for case-sensitive docker ps
- get_container_id() and get_container_name() now lowercase the instance name before passing to docker ps --filter, because Docker container names are lowercase internally and the filter is case-sensitive. This caused container_id to never be captured when instance.name contained uppercase chars (e.g. 'Headquarter'), breaking terminal WebSocket connections. - Also guard proc.stdout being None in start_cloudflared_tunnel(). - Add unit tests for get_container_id and get_container_name. Quality gates: pytest (14 passed), python clean
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
"""Unit tests for docker service utilities."""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from src.services.docker import get_container_id, get_container_name
|
||||
|
||||
|
||||
class TestGetContainerId:
|
||||
"""Tests for get_container_id."""
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_lowercases_name_for_filter(self, mock_run) -> None:
|
||||
"""Docker ps name filter is case-sensitive; we must lowercase."""
|
||||
mock_run.return_value = MagicMock(returncode=0, stdout="abc123\n")
|
||||
|
||||
result = get_container_id("MyContainer-ABC")
|
||||
|
||||
assert result == "abc123"
|
||||
call_args = mock_run.call_args[0][0]
|
||||
# The filter must use lowercase
|
||||
assert "name=mycontainer-abc" in call_args
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_returns_none_when_not_found(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(returncode=0, stdout="")
|
||||
|
||||
result = get_container_id("missing")
|
||||
|
||||
assert result is None
|
||||
|
||||
|
||||
class TestGetContainerName:
|
||||
"""Tests for get_container_name."""
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_lowercases_name_for_filter(self, mock_run) -> None:
|
||||
"""Docker ps name filter is case-sensitive; we must lowercase."""
|
||||
mock_run.return_value = MagicMock(returncode=0, stdout="mycontainer-abc\n")
|
||||
|
||||
result = get_container_name("MyContainer-ABC")
|
||||
|
||||
assert result == "mycontainer-abc"
|
||||
call_args = mock_run.call_args[0][0]
|
||||
assert "name=mycontainer-abc" in call_args
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_returns_none_when_not_found(self, mock_run) -> None:
|
||||
mock_run.return_value = MagicMock(returncode=0, stdout="")
|
||||
|
||||
result = get_container_name("missing")
|
||||
|
||||
assert result is None
|
||||
Reference in New Issue
Block a user