|
| 1 | +# Copyright 2026 Google LLC |
| 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 sys |
| 16 | +from unittest.mock import MagicMock |
| 17 | +from unittest.mock import patch |
| 18 | + |
| 19 | +from google.adk.agents.base_agent import BaseAgent |
| 20 | +from google.adk.agents.invocation_context import InvocationContext |
| 21 | +from google.adk.code_executors.code_execution_utils import CodeExecutionInput |
| 22 | +from google.adk.code_executors.code_execution_utils import CodeExecutionResult |
| 23 | +from google.adk.integrations.cloud_run import CloudRunSandboxCodeExecutor |
| 24 | +from google.adk.sessions.base_session_service import BaseSessionService |
| 25 | +from google.adk.sessions.session import Session |
| 26 | +import pytest |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def mock_invocation_context() -> InvocationContext: |
| 31 | + """Provides a mock InvocationContext.""" |
| 32 | + mock_agent = MagicMock(spec=BaseAgent) |
| 33 | + mock_session = MagicMock(spec=Session) |
| 34 | + mock_session_service = MagicMock(spec=BaseSessionService) |
| 35 | + return InvocationContext( |
| 36 | + invocation_id="test_invocation", |
| 37 | + agent=mock_agent, |
| 38 | + session=mock_session, |
| 39 | + session_service=mock_session_service, |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +class TestCloudRunSandboxCodeExecutor: |
| 44 | + |
| 45 | + def test_init_default(self): |
| 46 | + executor = CloudRunSandboxCodeExecutor() |
| 47 | + assert not executor.stateful |
| 48 | + assert not executor.optimize_data_file |
| 49 | + assert executor.sandbox_bin == "/usr/local/gcp/bin/sandbox" |
| 50 | + assert not executor.allow_egress |
| 51 | + |
| 52 | + def test_init_stateful_raises_error(self): |
| 53 | + with pytest.raises( |
| 54 | + ValueError, |
| 55 | + match="Cannot set `stateful=True` in CloudRunSandboxCodeExecutor.", |
| 56 | + ): |
| 57 | + CloudRunSandboxCodeExecutor(stateful=True) |
| 58 | + |
| 59 | + def test_init_optimize_data_file_raises_error(self): |
| 60 | + with pytest.raises( |
| 61 | + ValueError, |
| 62 | + match=( |
| 63 | + "Cannot set `optimize_data_file=True` in" |
| 64 | + " CloudRunSandboxCodeExecutor." |
| 65 | + ), |
| 66 | + ): |
| 67 | + CloudRunSandboxCodeExecutor(optimize_data_file=True) |
| 68 | + |
| 69 | + @patch("subprocess.run") |
| 70 | + def test_execute_code_success( |
| 71 | + self, mock_run, mock_invocation_context: InvocationContext |
| 72 | + ): |
| 73 | + # Setup mock subprocess.run response |
| 74 | + mock_response = MagicMock() |
| 75 | + mock_response.stdout = "hello world\n" |
| 76 | + mock_response.stderr = "" |
| 77 | + mock_response.returncode = 0 |
| 78 | + mock_run.return_value = mock_response |
| 79 | + |
| 80 | + executor = CloudRunSandboxCodeExecutor() |
| 81 | + code_input = CodeExecutionInput(code='print("hello world")') |
| 82 | + result = executor.execute_code(mock_invocation_context, code_input) |
| 83 | + |
| 84 | + assert isinstance(result, CodeExecutionResult) |
| 85 | + assert result.stdout == "hello world\n" |
| 86 | + assert result.stderr == "" |
| 87 | + assert result.output_files == [] |
| 88 | + |
| 89 | + # Verify subprocess.run was called with correct arguments |
| 90 | + expected_python = sys.executable or "python3" |
| 91 | + mock_run.assert_called_once_with( |
| 92 | + ["/usr/local/gcp/bin/sandbox", "do", expected_python], |
| 93 | + input='print("hello world")', |
| 94 | + capture_output=True, |
| 95 | + text=True, |
| 96 | + timeout=None, |
| 97 | + check=False, |
| 98 | + ) |
| 99 | + |
| 100 | + @patch("subprocess.run") |
| 101 | + def test_execute_code_with_egress_and_custom_bin( |
| 102 | + self, mock_run, mock_invocation_context: InvocationContext |
| 103 | + ): |
| 104 | + mock_response = MagicMock() |
| 105 | + mock_response.stdout = "egress success\n" |
| 106 | + mock_response.stderr = "" |
| 107 | + mock_response.returncode = 0 |
| 108 | + mock_run.return_value = mock_response |
| 109 | + |
| 110 | + executor = CloudRunSandboxCodeExecutor( |
| 111 | + sandbox_bin="/usr/bin/custom-sandbox", |
| 112 | + allow_egress=True, |
| 113 | + timeout_seconds=10, |
| 114 | + ) |
| 115 | + code_input = CodeExecutionInput(code="import requests; print('ok')") |
| 116 | + result = executor.execute_code(mock_invocation_context, code_input) |
| 117 | + |
| 118 | + assert result.stdout == "egress success\n" |
| 119 | + |
| 120 | + expected_python = sys.executable or "python3" |
| 121 | + mock_run.assert_called_once_with( |
| 122 | + ["/usr/bin/custom-sandbox", "do", "--allow-egress", expected_python], |
| 123 | + input="import requests; print('ok')", |
| 124 | + capture_output=True, |
| 125 | + text=True, |
| 126 | + timeout=10, |
| 127 | + check=False, |
| 128 | + ) |
| 129 | + |
| 130 | + @patch("subprocess.run") |
| 131 | + def test_execute_code_with_error( |
| 132 | + self, mock_run, mock_invocation_context: InvocationContext |
| 133 | + ): |
| 134 | + mock_response = MagicMock() |
| 135 | + mock_response.stdout = "" |
| 136 | + mock_response.stderr = "Traceback ... ValueError: Test error\n" |
| 137 | + mock_response.returncode = 1 |
| 138 | + mock_run.return_value = mock_response |
| 139 | + |
| 140 | + executor = CloudRunSandboxCodeExecutor() |
| 141 | + code_input = CodeExecutionInput(code='raise ValueError("Test error")') |
| 142 | + result = executor.execute_code(mock_invocation_context, code_input) |
| 143 | + |
| 144 | + assert result.stdout == "" |
| 145 | + assert "ValueError: Test error" in result.stderr |
| 146 | + |
| 147 | + @patch("subprocess.run") |
| 148 | + def test_execute_code_timeout( |
| 149 | + self, mock_run, mock_invocation_context: InvocationContext |
| 150 | + ): |
| 151 | + import subprocess |
| 152 | + |
| 153 | + mock_run.side_effect = subprocess.TimeoutExpired( |
| 154 | + cmd=["sandbox", "do", "python3"], |
| 155 | + timeout=5, |
| 156 | + output="partial stdout", |
| 157 | + stderr="partial stderr", |
| 158 | + ) |
| 159 | + |
| 160 | + executor = CloudRunSandboxCodeExecutor(timeout_seconds=5) |
| 161 | + code_input = CodeExecutionInput(code="import time\ntime.sleep(10)") |
| 162 | + result = executor.execute_code(mock_invocation_context, code_input) |
| 163 | + |
| 164 | + assert result.stdout == "partial stdout" |
| 165 | + assert result.stderr == "partial stderr" |
| 166 | + |
| 167 | + @patch("subprocess.run") |
| 168 | + def test_execute_code_binary_not_found( |
| 169 | + self, mock_run, mock_invocation_context: InvocationContext |
| 170 | + ): |
| 171 | + mock_run.side_effect = FileNotFoundError( |
| 172 | + "[Errno 2] No such file or directory: 'sandbox'" |
| 173 | + ) |
| 174 | + |
| 175 | + executor = CloudRunSandboxCodeExecutor() |
| 176 | + code_input = CodeExecutionInput(code='print("hello")') |
| 177 | + result = executor.execute_code(mock_invocation_context, code_input) |
| 178 | + |
| 179 | + assert result.stdout == "" |
| 180 | + assert ( |
| 181 | + 'Sandbox binary "/usr/local/gcp/bin/sandbox" not found' in result.stderr |
| 182 | + ) |
0 commit comments