From 0cafa87cb7f7f10112fcf6f888d7a6d53f5fb663 Mon Sep 17 00:00:00 2001 From: Ritwik G Date: Fri, 14 Mar 2025 21:16:58 +0530 Subject: [PATCH 1/2] Fix runner test cases to work without docker client being present --- .../unstract/runner/clients/test_docker.py | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/runner/src/unstract/runner/clients/test_docker.py b/runner/src/unstract/runner/clients/test_docker.py index c275e15e0..8b7624f65 100644 --- a/runner/src/unstract/runner/clients/test_docker.py +++ b/runner/src/unstract/runner/clients/test_docker.py @@ -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") @@ -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") @@ -78,27 +82,25 @@ 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_images.get.side_effect = ImageNotFound("Image not found") # Mock that image doesn't exist + 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, @@ -161,8 +163,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", @@ -178,8 +181,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__": From 94141d838252c1967192ee0da8d0e97b527a2354 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Mar 2025 15:48:42 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- runner/src/unstract/runner/clients/test_docker.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/runner/src/unstract/runner/clients/test_docker.py b/runner/src/unstract/runner/clients/test_docker.py index 8b7624f65..70fa3afa5 100644 --- a/runner/src/unstract/runner/clients/test_docker.py +++ b/runner/src/unstract/runner/clients/test_docker.py @@ -24,7 +24,7 @@ def docker_client(mocker): 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") @@ -97,7 +97,9 @@ def test_get_image(docker_client, mocker): 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_images.get.side_effect = ImageNotFound( + "Image not found" + ) # Mock that image doesn't exist mock_api.pull.return_value = iter([{"status": "pulling"}]) # Simulate pull process assert docker_client.get_image() == "test-image:latest" mock_api.pull.assert_called_with(