Skip to content

Commit 6c10d6b

Browse files
authored
chore: update test (#3105)
1 parent 2ce16ad commit 6c10d6b

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

test/toolkits/test_code_execution.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,52 @@
1212
# limitations under the License.
1313
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
1414

15+
import socket
16+
1517
import pytest
1618

1719
from camel.toolkits.code_execution import CodeExecutionToolkit
1820
from camel.utils import is_docker_running
1921

2022

23+
def is_microsandbox_available(
24+
server_url: str = "http://192.168.122.56:5555",
25+
) -> bool:
26+
"""Check if microsandbox server is available with a quick timeout."""
27+
try:
28+
from urllib.parse import urlparse
29+
30+
parsed_url = urlparse(server_url)
31+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
32+
sock.settimeout(0.5) # Quick timeout
33+
result = sock.connect_ex((parsed_url.hostname, parsed_url.port))
34+
sock.close()
35+
return result == 0
36+
except Exception:
37+
return False
38+
39+
2140
@pytest.fixture
2241
def code_execution_toolkit():
2342
return CodeExecutionToolkit()
2443

2544

2645
@pytest.fixture
2746
def jupyter_code_execution_toolkit():
47+
# Try to check if jupyter kernel is available
48+
try:
49+
from jupyter_client import kernelspec
50+
51+
# This will raise an exception if the kernel spec is not found
52+
spec = kernelspec.get_kernel_spec('python3')
53+
# Check if the python executable exists
54+
import os
55+
56+
if not os.path.exists(spec.argv[0]):
57+
pytest.skip(f"Jupyter kernel Python not found: {spec.argv[0]}")
58+
except Exception as e:
59+
pytest.skip(f"Jupyter kernel not available: {e}")
60+
2861
return CodeExecutionToolkit(
2962
sandbox="jupyter",
3063
verbose=True,
@@ -60,6 +93,9 @@ def microsandbox_code_execution_toolkit():
6093
"namespace": "test-code-execution",
6194
"timeout": 30,
6295
}
96+
# Check if microsandbox server is running
97+
if not is_microsandbox_available(microsandbox_config["server_url"]):
98+
pytest.skip("Microsandbox server is not running")
6399
return CodeExecutionToolkit(
64100
sandbox="microsandbox",
65101
verbose=True,
@@ -167,6 +203,10 @@ def test_verbose_output(code_execution_toolkit):
167203
assert "test" in result
168204

169205

206+
@pytest.mark.skipif(
207+
not is_microsandbox_available("http://192.168.122.56:5555"),
208+
reason="Microsandbox server is not running",
209+
)
170210
def test_microsandbox_execute_code(microsandbox_code_execution_toolkit):
171211
"""Test executing Python code with microsandbox."""
172212
code = "x = 10 + 5\nprint(f'Result: {x}')"
@@ -176,6 +216,10 @@ def test_microsandbox_execute_code(microsandbox_code_execution_toolkit):
176216
assert "Executed the code below:" in result
177217

178218

219+
@pytest.mark.skipif(
220+
not is_microsandbox_available("http://192.168.122.56:5555"),
221+
reason="Microsandbox server is not running",
222+
)
179223
def test_microsandbox_execute_javascript(microsandbox_code_execution_toolkit):
180224
"""Test executing JavaScript code with microsandbox."""
181225
code = "console.log('Hello from JavaScript');"
@@ -187,6 +231,10 @@ def test_microsandbox_execute_javascript(microsandbox_code_execution_toolkit):
187231
assert "Executed the code below:" in result
188232

189233

234+
@pytest.mark.skipif(
235+
not is_microsandbox_available("http://192.168.122.56:5555"),
236+
reason="Microsandbox server is not running",
237+
)
190238
def test_microsandbox_execute_command(microsandbox_code_execution_toolkit):
191239
"""Test executing shell commands with microsandbox."""
192240
result = microsandbox_code_execution_toolkit.execute_command("echo test")
@@ -196,6 +244,10 @@ def test_microsandbox_execute_command(microsandbox_code_execution_toolkit):
196244
assert "Executed the command below:" in result
197245

198246

247+
@pytest.mark.skipif(
248+
not is_microsandbox_available("http://192.168.122.56:5555"),
249+
reason="Microsandbox server is not running",
250+
)
199251
def test_microsandbox_error_handling(microsandbox_code_execution_toolkit):
200252
"""Test error handling with microsandbox."""
201253
code = "x = 1 / 0" # Division by zero - more reliable error
@@ -205,5 +257,3 @@ def test_microsandbox_error_handling(microsandbox_code_execution_toolkit):
205257
assert result is not None
206258
assert len(result) > 0
207259
assert "Executed the code below:" in result
208-
209-
# The specific error format may vary, so just ensure execution completes

0 commit comments

Comments
 (0)