|
| 1 | +"""Test pipeline execution and cleanup scenarios.""" |
| 2 | +import os |
| 3 | +import tempfile |
| 4 | + |
1 | 5 | import pytest
|
2 | 6 |
|
3 | 7 | from mcp_shell_server.shell_executor import ShellExecutor
|
4 | 8 |
|
5 | 9 |
|
| 10 | +def clear_env(monkeypatch): |
| 11 | + monkeypatch.delenv("ALLOW_COMMANDS", raising=False) |
| 12 | + monkeypatch.delenv("ALLOWED_COMMANDS", raising=False) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def executor(): |
| 17 | + return ShellExecutor() |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def temp_test_dir(): |
| 22 | + """Create a temporary directory for testing""" |
| 23 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 24 | + # Return the real path to handle macOS /private/tmp symlink |
| 25 | + yield os.path.realpath(tmpdirname) |
| 26 | + |
| 27 | + |
6 | 28 | @pytest.mark.asyncio
|
7 |
| -async def test_pipeline_split(): |
| 29 | +async def test_pipeline_split(executor): |
8 | 30 | """Test pipeline command splitting functionality"""
|
9 |
| - executor = ShellExecutor() |
10 |
| - |
11 | 31 | # Test basic pipe command
|
12 | 32 | commands = executor._split_pipe_commands(["echo", "hello", "|", "grep", "h"])
|
13 | 33 | assert len(commands) == 2
|
@@ -35,19 +55,45 @@ async def test_pipeline_split():
|
35 | 55 |
|
36 | 56 |
|
37 | 57 | @pytest.mark.asyncio
|
38 |
| -async def test_pipeline_execution_success(): |
| 58 | +async def test_pipeline_execution_success(executor, temp_test_dir, monkeypatch): |
39 | 59 | """Test successful pipeline execution with proper return value"""
|
40 |
| - executor = ShellExecutor() |
41 |
| - import os |
42 |
| - |
43 |
| - os.environ["ALLOWED_COMMANDS"] = "echo,grep" |
| 60 | + clear_env(monkeypatch) |
| 61 | + monkeypatch.setenv("ALLOWED_COMMANDS", "echo,grep") |
44 | 62 |
|
45 | 63 | result = await executor.execute(
|
46 |
| - ["echo", "hello world", "|", "grep", "world"], directory="/tmp", timeout=5 |
| 64 | + ["echo", "hello world", "|", "grep", "world"], directory=temp_test_dir, timeout=5 |
47 | 65 | )
|
48 | 66 |
|
49 | 67 | assert result["error"] is None
|
50 | 68 | assert result["status"] == 0
|
51 | 69 | assert "world" in result["stdout"]
|
52 | 70 | assert "execution_time" in result
|
53 |
| - assert result["directory"] == "/tmp" |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.asyncio |
| 74 | +async def test_pipeline_cleanup_and_timeouts(executor, temp_test_dir, monkeypatch): |
| 75 | + """Test cleanup of processes in pipelines and timeout handling""" |
| 76 | + clear_env(monkeypatch) |
| 77 | + monkeypatch.setenv("ALLOW_COMMANDS", "cat,tr,head,sleep") |
| 78 | + |
| 79 | + # Test pipeline with early termination |
| 80 | + test_file = os.path.join(temp_test_dir, "test.txt") |
| 81 | + with open(test_file, "w") as f: |
| 82 | + f.write("test\n" * 1000) |
| 83 | + |
| 84 | + result = await executor.execute( |
| 85 | + ["cat", test_file, "|", "tr", "[:lower:]", "[:upper:]", "|", "head", "-n", "1"], |
| 86 | + temp_test_dir, |
| 87 | + timeout=2 |
| 88 | + ) |
| 89 | + assert result["status"] == 0 |
| 90 | + assert result["stdout"].strip() == "TEST" |
| 91 | + |
| 92 | + # Test timeout handling in pipeline |
| 93 | + result = await executor.execute( |
| 94 | + ["sleep", "5"], |
| 95 | + temp_test_dir, |
| 96 | + timeout=1 |
| 97 | + ) |
| 98 | + assert result["status"] == -1 |
| 99 | + assert "timed out" in result["error"].lower() # タイムアウトエラーの確認 |
0 commit comments