Spaces:
Configuration error
Configuration error
Download tests/unit/test_docker_runtime.py from flyingdarkdev/README: direct link, hf CLI and curl.
- Browser
- Download file 2.21 kB
-
https://huggingface.co/spaces/flyingdarkdev/README/resolve/main/tests/unit/test_docker_runtime.py
- Command line
-
hf download hf://spaces/flyingdarkdev/README/tests/unit/test_docker_runtime.py
-
curl -L -o test_docker_runtime.py https://huggingface.co/spaces/flyingdarkdev/README/resolve/main/tests/unit/test_docker_runtime.py
2.21 kB
| from unittest.mock import MagicMock, patch | |
| import pytest | |
| from openhands.core.config import AppConfig | |
| from openhands.events import EventStream | |
| from openhands.runtime.impl.docker.docker_runtime import DockerRuntime | |
| def mock_docker_client(): | |
| with patch('docker.from_env') as mock_client: | |
| container_mock = MagicMock() | |
| container_mock.status = 'running' | |
| container_mock.attrs = { | |
| 'Config': { | |
| 'Env': ['port=12345', 'VSCODE_PORT=54321'], | |
| 'ExposedPorts': {'12345/tcp': {}, '54321/tcp': {}}, | |
| } | |
| } | |
| mock_client.return_value.containers.get.return_value = container_mock | |
| mock_client.return_value.containers.run.return_value = container_mock | |
| # Mock version info for BuildKit check | |
| mock_client.return_value.version.return_value = { | |
| 'Version': '20.10.0', | |
| 'Components': [{'Name': 'Engine', 'Version': '20.10.0'}], | |
| } # Ensure version is >= 18.09 | |
| yield mock_client.return_value | |
| def config(): | |
| config = AppConfig() | |
| config.sandbox.keep_runtime_alive = False | |
| return config | |
| def event_stream(): | |
| return MagicMock(spec=EventStream) | |
| def test_container_stopped_when_keep_runtime_alive_false( | |
| mock_stop_containers, mock_docker_client, config, event_stream | |
| ): | |
| # Arrange | |
| runtime = DockerRuntime(config, event_stream, sid='test-sid') | |
| runtime.container = mock_docker_client.containers.get.return_value | |
| # Act | |
| runtime.close() | |
| # Assert | |
| mock_stop_containers.assert_called_once_with('openhands-runtime-test-sid') | |
| def test_container_not_stopped_when_keep_runtime_alive_true( | |
| mock_stop_containers, mock_docker_client, config, event_stream | |
| ): | |
| # Arrange | |
| config.sandbox.keep_runtime_alive = True | |
| runtime = DockerRuntime(config, event_stream, sid='test-sid') | |
| runtime.container = mock_docker_client.containers.get.return_value | |
| # Act | |
| runtime.close() | |
| # Assert | |
| mock_stop_containers.assert_not_called() | |