/ server / tests / conftest.py
conftest.py
 1  # Copyright 2025 Alibaba Group Holding Ltd.
 2  #
 3  # Licensed under the Apache License, Version 2.0 (the "License");
 4  # you may not use this file except in compliance with the License.
 5  # You may obtain a copy of the License at
 6  #
 7  #     http://www.apache.org/licenses/LICENSE-2.0
 8  #
 9  # Unless required by applicable law or agreed to in writing, software
10  # distributed under the License is distributed on an "AS IS" BASIS,
11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  # See the License for the specific language governing permissions and
13  # limitations under the License.
14  
15  import os
16  from pathlib import Path
17  
18  import pytest
19  from fastapi.testclient import TestClient
20  from unittest.mock import MagicMock
21  
22  TEST_CONFIG_PATH = Path(__file__).resolve().parent / "testdata" / "config.toml"
23  os.environ.setdefault("SANDBOX_CONFIG_PATH", str(TEST_CONFIG_PATH))
24  
25  # Prevent real Docker connections during tests by mocking docker.from_env
26  import docker  # noqa: E402
27  
28  _mock_docker_client = MagicMock()
29  _mock_docker_client.containers.list.return_value = []
30  docker.from_env = lambda: _mock_docker_client  # type: ignore
31  
32  from opensandbox_server.main import app  # noqa: E402
33  
34  
35  @pytest.fixture(scope="session")
36  def test_api_key() -> str:
37      return "test-api-key-12345"
38  
39  
40  @pytest.fixture(scope="function")
41  def client() -> TestClient:
42      return TestClient(app)
43  
44  
45  @pytest.fixture(scope="function")
46  def auth_headers(test_api_key: str) -> dict:
47      return {"OPEN-SANDBOX-API-KEY": test_api_key}
48  
49  
50  @pytest.fixture(scope="session")
51  def sample_sandbox_request() -> dict:
52      return {
53          "image": {"uri": "python:3.11"},
54          "timeout": 3600,
55          "resourceLimits": {"cpu": "500m", "memory": "512Mi"},
56          "env": {"DEBUG": "true", "LOG_LEVEL": "info"},
57          "metadata": {"name": "Test Sandbox", "project": "test-project"},
58          "entrypoint": ["python", "-c", "print('Hello from sandbox')"],
59      }