forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gke_code_executor.py
More file actions
364 lines (313 loc) · 12.5 KB
/
test_gke_code_executor.py
File metadata and controls
364 lines (313 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest.mock import MagicMock
from unittest.mock import patch
from google.adk.agents.invocation_context import InvocationContext
from google.adk.code_executors.code_execution_utils import CodeExecutionInput
from google.adk.code_executors.gke_code_executor import GkeCodeExecutor
from kubernetes import client
from kubernetes import config
from kubernetes.client.rest import ApiException
import pytest
@pytest.fixture
def mock_invocation_context() -> InvocationContext:
"""Fixture for a mock InvocationContext."""
mock = MagicMock(spec=InvocationContext)
mock.invocation_id = "test-invocation-123"
return mock
@pytest.fixture(autouse=True)
def mock_k8s_config():
"""Fixture for auto-mocking Kubernetes config loading."""
with patch(
"google.adk.code_executors.gke_code_executor.config"
) as mock_config:
# Simulate fallback from in-cluster to kubeconfig
mock_config.ConfigException = config.ConfigException
mock_config.load_incluster_config.side_effect = config.ConfigException
yield mock_config
@pytest.fixture
def mock_k8s_clients():
"""Fixture for mock Kubernetes API clients."""
with patch(
"google.adk.code_executors.gke_code_executor.client"
) as mock_client_class:
mock_batch_v1 = MagicMock(spec=client.BatchV1Api)
mock_core_v1 = MagicMock(spec=client.CoreV1Api)
mock_client_class.BatchV1Api.return_value = mock_batch_v1
mock_client_class.CoreV1Api.return_value = mock_core_v1
yield {
"batch_v1": mock_batch_v1,
"core_v1": mock_core_v1,
}
class TestGkeCodeExecutor:
"""Unit tests for the GkeCodeExecutor."""
def test_init_defaults(self):
"""Tests that the executor initializes with correct default values."""
executor = GkeCodeExecutor()
assert executor.namespace == "default"
assert executor.image == "python:3.11-slim"
assert executor.timeout_seconds == 300
assert executor.cpu_requested == "200m"
assert executor.mem_limit == "512Mi"
assert executor.executor_type == "job"
def test_init_with_overrides(self):
"""Tests that class attributes can be overridden at instantiation."""
executor = GkeCodeExecutor(
namespace="test-ns",
image="custom-python:latest",
timeout_seconds=60,
cpu_limit="1000m",
executor_type="sandbox",
)
assert executor.namespace == "test-ns"
assert executor.image == "custom-python:latest"
assert executor.timeout_seconds == 60
assert executor.cpu_limit == "1000m"
assert executor.executor_type == "sandbox"
assert executor.sandbox_template == "python-sandbox-template"
def test_init_invalid_executor_type(self):
"""Tests that init raises ValueError for invalid executor_type."""
with pytest.raises(ValueError, match="Invalid executor_type"):
GkeCodeExecutor(executor_type="invalid_type")
@patch("google.adk.code_executors.gke_code_executor.Watch")
def test_execute_code_success(
self,
mock_watch,
mock_k8s_clients,
mock_invocation_context,
):
"""Tests the happy path for successful code execution."""
# Setup Mocks
mock_job = MagicMock()
mock_job.status.succeeded = True
mock_job.status.failed = None
mock_watch.return_value.stream.return_value = [{"object": mock_job}]
mock_pod_list = MagicMock()
mock_pod_list.items = [MagicMock()]
mock_pod_list.items[0].metadata.name = "test-pod-name"
mock_k8s_clients["core_v1"].list_namespaced_pod.return_value = mock_pod_list
mock_k8s_clients["core_v1"].read_namespaced_pod_log.return_value = (
"hello world"
)
# Execute
executor = GkeCodeExecutor()
code_input = CodeExecutionInput(code='print("hello world")')
result = executor.execute_code(mock_invocation_context, code_input)
# Assert
assert result.stdout == "hello world"
assert result.stderr == ""
mock_k8s_clients[
"core_v1"
].create_namespaced_config_map.assert_called_once()
mock_k8s_clients["batch_v1"].create_namespaced_job.assert_called_once()
mock_k8s_clients["core_v1"].patch_namespaced_config_map.assert_called_once()
mock_k8s_clients["core_v1"].read_namespaced_pod_log.assert_called_once()
@patch("google.adk.code_executors.gke_code_executor.Watch")
def test_execute_code_job_failed(
self,
mock_watch,
mock_k8s_clients,
mock_invocation_context,
):
"""Tests the path where the Kubernetes Job fails."""
mock_job = MagicMock()
mock_job.status.succeeded = None
mock_job.status.failed = True
mock_watch.return_value.stream.return_value = [{"object": mock_job}]
mock_k8s_clients["core_v1"].read_namespaced_pod_log.return_value = (
"Traceback...\nValueError: failure"
)
executor = GkeCodeExecutor()
result = executor.execute_code(
mock_invocation_context, CodeExecutionInput(code="fail")
)
assert result.stdout == ""
assert "Job failed. Logs:" in result.stderr
assert "ValueError: failure" in result.stderr
def test_execute_code_api_exception(
self, mock_k8s_clients, mock_invocation_context
):
"""Tests handling of an ApiException from the K8s client."""
mock_k8s_clients["core_v1"].create_namespaced_config_map.side_effect = (
ApiException(reason="Test API Error")
)
executor = GkeCodeExecutor()
result = executor.execute_code(
mock_invocation_context, CodeExecutionInput(code="...")
)
assert result.stdout == ""
assert "Kubernetes API error: Test API Error" in result.stderr
@patch("google.adk.code_executors.gke_code_executor.Watch")
def test_execute_code_timeout(
self,
mock_watch,
mock_k8s_clients,
mock_invocation_context,
):
"""Tests the case where the job watch times out."""
mock_watch.return_value.stream.return_value = (
[]
) # Empty stream simulates timeout
mock_k8s_clients["core_v1"].read_namespaced_pod_log.return_value = (
"Still running..."
)
executor = GkeCodeExecutor(timeout_seconds=1)
result = executor.execute_code(
mock_invocation_context, CodeExecutionInput(code="...")
)
assert result.stdout == ""
assert "Executor timed out" in result.stderr
assert "did not complete within 1s" in result.stderr
assert "Pod Logs:\nStill running..." in result.stderr
def test_create_job_manifest_structure(self, mock_invocation_context):
"""Tests the correctness of the generated Job manifest."""
executor = GkeCodeExecutor(namespace="test-ns", image="test-img:v1")
job = executor._create_job_manifest(
"test-job", "test-cm", mock_invocation_context
)
# Check top-level properties
assert isinstance(job, client.V1Job)
assert job.api_version == "batch/v1"
assert job.kind == "Job"
assert job.metadata.name == "test-job"
assert job.spec.backoff_limit == 0
assert job.spec.ttl_seconds_after_finished == 600
# Check pod template properties
pod_spec = job.spec.template.spec
assert pod_spec.restart_policy == "Never"
assert pod_spec.runtime_class_name == "gvisor"
assert len(pod_spec.tolerations) == 1
assert pod_spec.tolerations[0].value == "gvisor"
assert len(pod_spec.volumes) == 1
assert pod_spec.volumes[0].name == "code-volume"
assert pod_spec.volumes[0].config_map.name == "test-cm"
# Check container properties
container = pod_spec.containers[0]
assert container.name == "code-runner"
assert container.image == "test-img:v1"
assert container.command == ["python3", "/app/code.py"]
# Check security context
sec_context = container.security_context
assert sec_context.run_as_non_root is True
assert sec_context.run_as_user == 1001
assert sec_context.allow_privilege_escalation is False
assert sec_context.read_only_root_filesystem is True
assert sec_context.capabilities.drop == ["ALL"]
@patch("google.adk.code_executors.gke_code_executor.SandboxClient")
def test_execute_code_forks_to_sandbox(
self,
mock_sandbox_client,
mock_invocation_context,
mock_k8s_clients,
):
"""Tests that execute_code uses SandboxClient when executor_type='sandbox'."""
# Setup Sandbox mock
mock_sandbox_instance = (
mock_sandbox_client.return_value.__enter__.return_value
)
mock_run_result = MagicMock()
mock_run_result.stdout = "sandbox stdout"
mock_run_result.stderr = None
mock_sandbox_instance.run.return_value = mock_run_result
# Instantiate with sandbox type
executor = GkeCodeExecutor(executor_type="sandbox")
code_input = CodeExecutionInput(code='print("sandbox")')
# Execute
result = executor.execute_code(mock_invocation_context, code_input)
# Assertions
assert result.stdout == "sandbox stdout"
# Verify SandboxClient was used
mock_sandbox_client.assert_called_once()
mock_sandbox_instance.run.assert_called_once()
# Verify Job path was NOT taken
mock_k8s_clients["batch_v1"].create_namespaced_job.assert_not_called()
@patch("google.adk.code_executors.gke_code_executor.SandboxClient")
def test_execute_code_sandbox_exception(
self,
mock_sandbox_client,
mock_invocation_context,
):
"""Tests handling of exceptions from SandboxClient."""
# Setup Sandbox mock to raise exception
mock_sandbox_client.return_value.__enter__.side_effect = Exception(
"Connection failed"
)
# Instantiate with sandbox type
executor = GkeCodeExecutor(executor_type="sandbox")
code_input = CodeExecutionInput(code='print("sandbox")')
# Execute
result = executor.execute_code(mock_invocation_context, code_input)
# Assertions
assert result.stdout == ""
assert "Sandbox execution failed: Connection failed" in result.stderr
@patch("google.adk.code_executors.gke_code_executor.SandboxClient")
@patch("google.adk.code_executors.gke_code_executor.Watch")
def test_execute_code_forks_to_job(
self,
mock_watch,
mock_sandbox_client,
mock_invocation_context,
mock_k8s_clients,
):
"""Tests that execute_code uses K8s Job when executor_type='job'."""
# Setup K8s Job mocks (success path)
mock_job = MagicMock()
mock_job.status.succeeded = True
mock_watch.return_value.stream.return_value = [{"object": mock_job}]
mock_pod = MagicMock()
mock_pod.metadata.name = "pod-1"
mock_k8s_clients["core_v1"].list_namespaced_pod.return_value.items = [
mock_pod
]
mock_k8s_clients["core_v1"].read_namespaced_pod_log.return_value = (
"job stdout"
)
# Instantiate with job type
executor = GkeCodeExecutor(executor_type="job")
code_input = CodeExecutionInput(code='print("job")')
# Execute
result = executor.execute_code(mock_invocation_context, code_input)
# Assertions
assert result.stdout == "job stdout"
# Verify Job path WAS taken
mock_k8s_clients["batch_v1"].create_namespaced_job.assert_called_once()
# Verify SandboxClient was NOT used
mock_sandbox_client.assert_not_called()
@patch("google.adk.code_executors.gke_code_executor.SandboxClient")
def test_execute_in_sandbox_returns_stderr(
self,
mock_sandbox_client,
mock_invocation_context,
):
"""Tests that stderr from the sandbox run is propagated to the result."""
# Setup Sandbox mock
mock_sandbox_instance = (
mock_sandbox_client.return_value.__enter__.return_value
)
mock_run_result = MagicMock()
mock_run_result.stdout = ""
mock_run_result.stderr = "oops\n"
mock_sandbox_instance.run.return_value = mock_run_result
# Instantiate with sandbox type
executor = GkeCodeExecutor(executor_type="sandbox")
code_input = CodeExecutionInput(
code="import sys; print('oops', file=sys.stderr)"
)
# Execute
result = executor.execute_code(mock_invocation_context, code_input)
# Assertions
assert result.stdout == ""
assert result.stderr == "oops\n"
mock_sandbox_instance.write.assert_called_with("script.py", code_input.code)
mock_sandbox_instance.run.assert_called_with("python3 script.py")