forked from kubernetes-sigs/agent-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
321 lines (263 loc) · 12 KB
/
test_client.py
File metadata and controls
321 lines (263 loc) · 12 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
# Copyright 2025 The Kubernetes Authors.
#
# 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.
import argparse
import time
import logging
from unittest.mock import MagicMock
from pydantic import ValidationError
from k8s_agent_sandbox import SandboxClient
from k8s_agent_sandbox.models import (
SandboxDirectConnectionConfig,
SandboxGatewayConnectionConfig,
SandboxLocalTunnelConnectionConfig,
SandboxTracerConfig,
ExecutionResult,
FileEntry
)
from k8s_agent_sandbox.sandbox import Sandbox
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)
def run_sandbox_tests(sandbox: Sandbox):
"""Tests methods on the Sandbox object (execution, files, etc)."""
print("\n--- Testing Command Execution ---")
command_to_run = "echo 'Hello from the sandbox shruti!'"
print(f"Executing command: '{command_to_run}'")
result = sandbox.commands.run(command_to_run)
print(f"Stdout: {result.stdout.strip()}")
print(f"Stderr: {result.stderr.strip()}")
print(f"Exit Code: {result.exit_code}")
assert result.exit_code == 0
assert result.stdout.strip() == "Hello from the sandbox shruti!"
print("\n--- Command Execution Test Passed! ---")
# Test file operations
print("\n--- Testing File Operations ---")
file_content = "This is a test file."
file_path = "test.txt"
print(f"Writing content to '{file_path}'...")
sandbox.files.write(file_path, file_content)
print(f"Reading content from '{file_path}'...")
read_content = sandbox.files.read(file_path).decode('utf-8')
print(f"Read content: '{read_content}'")
assert read_content == file_content
print("--- File Operations Test Passed! ---")
# Test list and exists
print("\n--- Testing List and Exists ---")
print(f"Checking if '{file_path}' exists...")
exists = sandbox.files.exists(file_path)
assert exists is True, f"Expected '{file_path}' to exist"
print("Checking if 'non_existent_file.txt' exists...")
not_exists = sandbox.files.exists("non_existent_file.txt")
assert not_exists is False, "Expected 'non_existent_file.txt' to not exist"
print("Listing files in '.' ...")
files = sandbox.files.list(".")
print(f"Files found: {[f.name for f in files]}")
found = any(f.name == file_path for f in files)
assert found is True, f"Expected '{file_path}' to be in the file list"
file_entry = next(f for f in files if f.name == file_path)
assert file_entry.size == len(file_content), f"Expected size {len(file_content)}, got {file_entry.size}"
print("--- List and Exists Test Passed! ---")
# Test introspection commands
print("\n--- Testing Pod Introspection ---")
print("\n--- Listing files in /app ---")
list_files_result = sandbox.commands.run("ls -la /app")
print(list_files_result.stdout)
print("\n--- Printing environment variables ---")
env_result = sandbox.commands.run("env")
print(env_result.stdout)
print("--- Introspection Tests Finished ---")
print("\n--- Testing Pydantic Validation ---")
# Test: ExecutionResult defaults (partial response)
original_send_request = sandbox.connector.send_request
mock_response = MagicMock()
mock_response.json.return_value = {} # Empty response
sandbox.connector.send_request = MagicMock(return_value=mock_response)
print("Testing ExecutionResult defaults with empty response...")
# This should not raise error because of defaults
res = sandbox.commands.run("echo test")
assert res.exit_code == -1
assert res.stdout == ""
assert isinstance(res, ExecutionResult)
print("ExecutionResult defaults verified.")
# Test: FileEntry validation (invalid type)
mock_response.json.return_value = [{
"name": "bad_file",
"size": 100,
"type": "invalid_type", # Invalid literal
"mod_time": 12345.6
}]
print("Testing FileEntry validation with invalid type...")
try:
sandbox.files.list(".")
raise AssertionError("RuntimeError not raised for invalid FileEntry type")
except RuntimeError as e:
print(f"Caught expected RuntimeError: {e}")
assert "Server returned invalid file entry format" in str(e)
# Restore original method
sandbox.connector.send_request = original_send_request
print("--- Pydantic Validation Tests Passed ---")
def run_client_tests(client: SandboxClient, template_name: str, namespace: str):
print(f"Creating sandbox with template '{template_name}' in namespace '{namespace}'...")
sandbox = client.create_sandbox(template_name, namespace=namespace)
print(f"Sandbox created with claim name: {sandbox.claim_name}")
print(f"Creating second sandbox with template '{template_name}' in namespace '{namespace}'...")
sandbox2 = client.create_sandbox(template_name, namespace=namespace)
print(f"Sandbox 2 created with claim name: {sandbox2.claim_name}")
print("\n--- Verifying Active Sandboxes ---")
active_sandboxes = client.list_active_sandboxes()
print(f"Active sandboxes: {active_sandboxes}")
assert (sandbox.namespace, sandbox.claim_name) in active_sandboxes
assert (sandbox2.namespace, sandbox2.claim_name) in active_sandboxes
# Test get_sandbox
print("\n--- Testing get_sandbox ---")
reattached_sandbox = client.get_sandbox(sandbox.claim_name, namespace=namespace)
print(f"Re-attached to sandbox: {reattached_sandbox.claim_name}")
# Verify it is the same sandbox
assert sandbox is reattached_sandbox, "Expected same sandbox objects"
assert sandbox.connector.session is reattached_sandbox.connector.session, "Expected same requests.Session objects"
reattached_result = reattached_sandbox.commands.run("echo 'Re-attached'")
print(f"Re-attached execution result: {reattached_result.stdout.strip()}")
assert reattached_result.exit_code == 0
assert reattached_result.stdout.strip() == "Re-attached"
print("\n--- get_sandbox Test Passed ---")
# Run Sandbox Tests
run_sandbox_tests(sandbox)
print("\n--- Testing Termination and Get ---")
print(f"Terminating sandbox {sandbox.claim_name}...")
sandbox.terminate()
client.delete_sandbox(sandbox.claim_name, namespace=namespace)
print(f"Attempting to get terminated sandbox {sandbox.claim_name}...")
# Wait for K8s to fully delete the resource
start_time = time.monotonic()
while True:
try:
client.get_sandbox(sandbox.claim_name, namespace=namespace)
if time.monotonic() - start_time > 60:
raise AssertionError(f"Sandbox {sandbox.claim_name} was not deleted within timeout")
print("Sandbox still exists, waiting...")
time.sleep(2)
except RuntimeError as e:
print(f"Caught expected RuntimeError: {e}")
assert "not found" in str(e)
break
print("--- Termination and Get Test Passed ---")
print("\n--- Testing delete_all ---")
# Ensure sandbox2 is still active
assert (sandbox2.namespace, sandbox2.claim_name) in client.list_active_sandboxes()
print("Calling client.delete_all()...")
client.delete_all()
# Verify client registry is empty
active_sandboxes_after = client.list_active_sandboxes()
assert len(active_sandboxes_after) == 0, f"Expected 0 active sandboxes, got {active_sandboxes_after}"
# Verify sandbox2 state
assert not sandbox2.is_active, "Sandbox 2 should be marked inactive"
assert sandbox2.commands is None, "Sandbox 2 commands engine should be None"
assert sandbox2.files is None, "Sandbox 2 files engine should be None"
print("--- delete_all Test Passed ---")
print("\n--- Verifying Sandbox 2 is unusable ---")
try:
sandbox2.commands.run("echo 'Should not work'")
raise AssertionError("Sandbox 2 should be unusable after delete_all")
except AttributeError:
print("Verified: Sandbox 2 commands is None.")
print("\n--- Verifying Sandbox 2 cannot be retrieved ---")
# Wait for K8s to fully delete the resource
start_time = time.monotonic()
while True:
try:
client.get_sandbox(sandbox2.claim_name, namespace=namespace)
if time.monotonic() - start_time > 60:
raise AssertionError(f"Sandbox {sandbox2.claim_name} was not deleted within timeout")
print("Sandbox still exists, waiting...")
time.sleep(2)
except RuntimeError as e:
print(f"Caught expected RuntimeError: {e}")
assert "not found" in str(e)
break
print("--- Sandbox 2 Retrieval Failure Verified ---")
def main(template_name: str, gateway_name: str | None, api_url: str | None, namespace: str,
server_port: int, enable_tracing: bool):
"""
Tests the Sandbox client by creating a sandbox, running a command,
and then cleaning up.
"""
print(
f"--- Starting Sandbox Client Test (Namespace: {namespace}, Port: {server_port}) ---")
if gateway_name:
print(f"Mode: Gateway Discovery ({gateway_name})")
elif api_url:
print(f"Mode: Direct API URL ({api_url})")
else:
print("Mode: Local Port-Forward fallback")
# Create Connection Config object
if gateway_name:
connection_config = SandboxGatewayConnectionConfig(
gateway_name=gateway_name,
server_port=server_port
)
elif api_url:
connection_config = SandboxDirectConnectionConfig(
api_url=api_url,
server_port=server_port
)
else:
connection_config = SandboxLocalTunnelConnectionConfig(
server_port=server_port
)
tracer_config = SandboxTracerConfig(
enable_tracing=enable_tracing,
trace_service_name="sandbox-client-test"
)
client = SandboxClient(
connection_config=connection_config,
tracer_config=tracer_config
)
try:
run_client_tests(client, template_name, namespace)
except Exception as e:
print(f"\n--- An error occurred during the test: {e} ---")
finally:
print("Cleaning up all sandboxes...")
client.delete_all()
print("\n--- Sandbox Client Test Finished ---")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Test the Sandbox client.")
parser.add_argument(
"--template-name",
default="python-sandbox-template",
help="The name of the sandbox template to use for the test."
)
# Default is None to allow testing the Port-Forward fallback
parser.add_argument(
"--gateway-name",
default=None,
help="The name of the Gateway resource. If omitted, defaults to local port-forward mode."
)
parser.add_argument(
"--api-url", help="Direct URL to router (e.g. http://localhost:8080)", default=None)
parser.add_argument("--namespace", default="default",
help="Namespace to create sandbox in")
parser.add_argument("--server-port", type=int, default=8888,
help="Port the sandbox container listens on")
parser.add_argument("--enable-tracing",
action="store_true",
help="Enable OpenTelemetry tracing in the agentic-sandbox-client."
)
args = parser.parse_args()
main(
template_name=args.template_name,
gateway_name=args.gateway_name,
api_url=args.api_url,
namespace=args.namespace,
server_port=args.server_port,
enable_tracing=args.enable_tracing
)