Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

Fix docker runner test cases to work without docker client being present (#1188)

Ritwik G968f23bbf2ae
diff --git a/runner/src/unstract/runner/clients/test_docker.py b/runner/src/unstract/runner/clients/test_docker.py
index 82c3bcf0f..1c1418b00 100644
--- a/runner/src/unstract/runner/clients/test_docker.py
+++ b/runner/src/unstract/runner/clients/test_docker.py
@@ -20,7 +20,13 @@ def docker_container():
@pytest.fixture
-def docker_client():
+def docker_client(mocker):
+ # Mock the DockerClient.from_env to avoid connecting to a real Docker daemon
+ mock_client = MagicMock()
+ mocker.patch(f"{DOCKER_MODULE}.DockerClient.from_env", return_value=mock_client)
+ # Mock the private login method to avoid any actual login attempts
+ mocker.patch(f"{DOCKER_MODULE}.Client._Client__private_login")
+
image_name = "test-image"
image_tag = "latest"
logger = logging.getLogger("test-logger")
@@ -73,12 +79,10 @@ def test_client_init(mocker):
def test_get_image_exists(docker_client, mocker):
"""Test the __image_exists method."""
- # Mock the client object
- mock_client = mocker.patch.object(docker_client, "client")
# Create a mock for the 'images' attribute
mock_images = mocker.MagicMock()
# Attach the mock to the client object
- mock_client.images = mock_images
+ docker_client.client.images = mock_images
# Patch the 'get' method of the 'images' attribute
mock_images.get.side_effect = ImageNotFound("Image not found")
@@ -88,16 +92,19 @@ def test_get_image_exists(docker_client, mocker):
def test_get_image(docker_client, mocker):
"""Test the get_image method."""
- # Patch the client object to control its behavior
- mock_client = mocker.patch.object(docker_client, "client")
- # Patch the images attribute of the client to control its behavior
+ # Create a mock for the 'images' attribute
mock_images = mocker.MagicMock()
- mock_client.images = mock_images
+ # Attach the mock to the client object
+ docker_client.client.images = mock_images
+ # Create a mock for the 'api' attribute
+ mock_api = mocker.MagicMock()
+ # Attach the mock to the client object
+ docker_client.client.api = mock_api
# Case 1: Image exists
- mock_images.get.side_effect = MagicMock() # Mock that image exists
+ mock_images.get.side_effect = None # Mock that image exists
assert docker_client.get_image() == "test-image:latest"
- mock_images.get.assert_called_once_with("test-image:latest") # Ensure get is called
+ mock_images.get.assert_called_with("test-image:latest") # Ensure get is called
# Case 2: Image does not exist
mock_images.get.side_effect = ImageNotFound(
@@ -106,7 +113,7 @@ def test_get_image(docker_client, mocker):
mock_pull = mocker.patch.object(docker_client.client.api, "pull") # Patch pull method
mock_pull.return_value = iter([{"status": "pulling"}]) # Simulate pull process
assert docker_client.get_image() == "test-image:latest"
- mock_pull.assert_called_once_with(
+ mock_api.pull.assert_called_with(
repository="test-image",
tag="latest",
stream=True,
@@ -175,8 +182,9 @@ def test_get_container_run_config_without_mount(docker_client, mocker):
def test_run_container(docker_client, mocker):
"""Test the run_container method."""
- # Patch the client object to control its behavior
- mock_client = mocker.patch.object(docker_client, "client")
+ # Create a mock for the containers.run method
+ mock_container = mocker.MagicMock()
+ docker_client.client.containers.run.return_value = mock_container
config = {
"name": "test-image",
@@ -192,8 +200,9 @@ def test_run_container(docker_client, mocker):
"mounts": [],
}
- assert isinstance(docker_client.run_container(config), DockerContainer)
- mock_client.containers.run.assert_called_once_with(**config)
+ result = docker_client.run_container(config)
+ assert isinstance(result, DockerContainer)
+ docker_client.client.containers.run.assert_called_once_with(**config)
def test_get_image_for_sidecar(docker_client_with_sidecar, mocker):