Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions runner/src/unstract/runner/clients/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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")
Expand Down Expand Up @@ -63,12 +69,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")

Expand All @@ -78,27 +82,27 @@ 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(
"Image not found"
) # Mock that image doesn't exist
mock_pull = mocker.patch.object(
docker_client.client.api, "pull"
) # Patch pull method
mock_pull.return_value = iter([{"status": "pulling"}]) # Simulate pull process
mock_api.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,
Expand Down Expand Up @@ -161,8 +165,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",
Expand All @@ -178,8 +183,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)


if __name__ == "__main__":
Expand Down