|
10 | 10 | import os |
11 | 11 | import tempfile |
12 | 12 | import uuid |
13 | | -from contextlib import asynccontextmanager |
14 | 13 | from unittest.mock import Mock, patch |
15 | 14 |
|
16 | 15 | import pytest |
17 | | -from forge.actors.coder import SandboxedPythonCoder |
18 | 16 |
|
19 | | -from monarch.actor import this_proc |
| 17 | +from forge.actors.coder import _SandboxedPythonCoder |
20 | 18 |
|
21 | 19 |
|
22 | | -@asynccontextmanager |
23 | | -async def create_mock_coder( |
24 | | - execute_stdout="hello world\n", |
25 | | - execute_returncode=0, |
26 | | - execute_stderr="", |
27 | | - import_fails=False, |
28 | | - create_fails=False, |
29 | | -): |
30 | | - """Context manager that creates a mocked SandboxedPythonCoder.""" |
| 20 | +@pytest.mark.asyncio |
| 21 | +async def test_coder_success(): |
| 22 | + """Test successful execution.""" |
31 | 23 | unique_id = str(uuid.uuid4())[:8] |
32 | 24 | container_name = f"test_sandbox_{unique_id}" |
33 | 25 |
|
34 | 26 | with tempfile.NamedTemporaryFile(suffix=".sqsh", delete=False) as temp_image: |
35 | 27 | image_path = temp_image.name |
36 | 28 |
|
37 | | - coder = None |
| 29 | + def mock_subprocess_run(*args, **kwargs): |
| 30 | + """Mock subprocess.run for testing.""" |
| 31 | + cmd = args[0] if args else kwargs.get("args", []) |
| 32 | + cmd_str = " ".join(cmd) if isinstance(cmd, list) else str(cmd) |
| 33 | + |
| 34 | + if "import" in cmd_str: |
| 35 | + result = Mock() |
| 36 | + result.returncode = 0 |
| 37 | + result.stderr = "" |
| 38 | + return result |
| 39 | + elif "remove" in cmd_str: |
| 40 | + result = Mock() |
| 41 | + result.returncode = 0 |
| 42 | + return result |
| 43 | + elif "create" in cmd_str: |
| 44 | + result = Mock() |
| 45 | + result.returncode = 0 |
| 46 | + result.stderr = "" |
| 47 | + return result |
| 48 | + elif "start" in cmd_str: |
| 49 | + result = Mock() |
| 50 | + result.returncode = 0 |
| 51 | + result.stdout = "Hello World\n" |
| 52 | + result.stderr = "" |
| 53 | + return result |
| 54 | + else: |
| 55 | + raise ValueError(f"Unexpected subprocess call: {cmd_str}") |
| 56 | + |
38 | 57 | try: |
39 | | - with patch("subprocess.run") as mock_run: |
40 | | - |
41 | | - def mock_subprocess_run(*args, **kwargs): |
42 | | - cmd = args[0] |
43 | | - if "import" in cmd: |
44 | | - result = Mock() |
45 | | - if import_fails: |
46 | | - result.returncode = 1 |
47 | | - result.stderr = "Failed to import image: network error" |
48 | | - else: |
49 | | - result.returncode = 0 |
50 | | - result.stderr = "" |
51 | | - return result |
52 | | - elif "remove" in cmd: |
53 | | - result = Mock() |
54 | | - result.returncode = 0 |
55 | | - return result |
56 | | - elif "create" in cmd: |
57 | | - result = Mock() |
58 | | - if create_fails: |
59 | | - result.returncode = 1 |
60 | | - result.stderr = "Failed to create container: no space" |
61 | | - else: |
62 | | - result.returncode = 0 |
63 | | - result.stderr = "" |
64 | | - return result |
65 | | - elif "start" in cmd: |
66 | | - result = Mock() |
67 | | - result.returncode = execute_returncode |
68 | | - result.stdout = execute_stdout |
69 | | - result.stderr = execute_stderr |
70 | | - return result |
71 | | - else: |
72 | | - raise ValueError(f"Unexpected subprocess call: {cmd}") |
73 | | - |
74 | | - mock_run.side_effect = mock_subprocess_run |
75 | | - |
76 | | - coder = this_proc().spawn( |
77 | | - f"coder_{uuid.uuid1()}", |
78 | | - SandboxedPythonCoder, |
79 | | - "docker://python:3.10", |
80 | | - image_path, |
81 | | - container_name, |
| 58 | + with patch( |
| 59 | + "forge.actors.coder.subprocess.run", side_effect=mock_subprocess_run |
| 60 | + ): |
| 61 | + coder = _SandboxedPythonCoder( |
| 62 | + docker_image="docker://python:3.10", |
| 63 | + sqsh_image_path=image_path, |
| 64 | + container_name=container_name, |
82 | 65 | ) |
83 | 66 |
|
84 | | - yield coder, mock_run |
85 | | - |
| 67 | + await coder.setup() |
| 68 | + result, _ = await coder.execute(code="print('Hello World')") |
| 69 | + assert result == "Hello World\n" |
86 | 70 | finally: |
87 | | - if coder: |
88 | | - await SandboxedPythonCoder.shutdown(coder) |
89 | | - |
90 | 71 | if os.path.exists(image_path): |
91 | 72 | os.unlink(image_path) |
92 | 73 |
|
93 | 74 |
|
94 | | -@pytest.mark.timeout(10) |
95 | | -@pytest.mark.asyncio |
96 | | -async def test_coder_success(): |
97 | | - """Test successful execution.""" |
98 | | - async with create_mock_coder(execute_stdout="Hello World\n") as (coder, _): |
99 | | - await coder.setup.call_one() |
100 | | - result, _ = await coder.execute.call_one(code="print('Hello World')") |
101 | | - assert result == "Hello World\n" |
102 | | - |
103 | | - |
104 | | -@pytest.mark.timeout(10) |
105 | 75 | @pytest.mark.asyncio |
106 | 76 | async def test_coder_execution_failure(): |
107 | 77 | """Test execution failure.""" |
108 | | - async with create_mock_coder( |
109 | | - execute_returncode=1, execute_stderr="SyntaxError: invalid syntax" |
110 | | - ) as (coder, _): |
111 | | - await coder.setup.call_one() |
112 | | - output, err = await coder.execute.call_one(code="invalid syntax") |
113 | | - assert "SyntaxError" in err |
| 78 | + unique_id = str(uuid.uuid4())[:8] |
| 79 | + container_name = f"test_sandbox_{unique_id}" |
| 80 | + |
| 81 | + with tempfile.NamedTemporaryFile(suffix=".sqsh", delete=False) as temp_image: |
| 82 | + image_path = temp_image.name |
| 83 | + |
| 84 | + def mock_subprocess_run(*args, **kwargs): |
| 85 | + """Mock subprocess.run for testing.""" |
| 86 | + cmd = args[0] if args else kwargs.get("args", []) |
| 87 | + cmd_str = " ".join(cmd) if isinstance(cmd, list) else str(cmd) |
| 88 | + |
| 89 | + if "import" in cmd_str: |
| 90 | + result = Mock() |
| 91 | + result.returncode = 0 |
| 92 | + result.stderr = "" |
| 93 | + return result |
| 94 | + elif "remove" in cmd_str: |
| 95 | + result = Mock() |
| 96 | + result.returncode = 0 |
| 97 | + return result |
| 98 | + elif "create" in cmd_str: |
| 99 | + result = Mock() |
| 100 | + result.returncode = 0 |
| 101 | + result.stderr = "" |
| 102 | + return result |
| 103 | + elif "start" in cmd_str: |
| 104 | + result = Mock() |
| 105 | + result.returncode = 1 |
| 106 | + result.stdout = "" |
| 107 | + result.stderr = "SyntaxError: invalid syntax" |
| 108 | + return result |
| 109 | + else: |
| 110 | + raise ValueError(f"Unexpected subprocess call: {cmd_str}") |
| 111 | + |
| 112 | + try: |
| 113 | + with patch( |
| 114 | + "forge.actors.coder.subprocess.run", side_effect=mock_subprocess_run |
| 115 | + ): |
| 116 | + coder = _SandboxedPythonCoder( |
| 117 | + docker_image="docker://python:3.10", |
| 118 | + sqsh_image_path=image_path, |
| 119 | + container_name=container_name, |
| 120 | + ) |
| 121 | + |
| 122 | + await coder.setup() |
| 123 | + output, err = await coder.execute(code="invalid syntax") |
| 124 | + assert "SyntaxError" in err |
| 125 | + finally: |
| 126 | + if os.path.exists(image_path): |
| 127 | + os.unlink(image_path) |
0 commit comments