-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_agent_handlers.py
More file actions
443 lines (351 loc) · 14.4 KB
/
Copy pathtest_agent_handlers.py
File metadata and controls
443 lines (351 loc) · 14.4 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
"""Tests for agent_handlers module - prepare_cloud_build_context and package CLI command."""
from __future__ import annotations
import io
import os
import json
import tarfile
import tempfile
from pathlib import Path
from collections.abc import Iterator
import pytest
from typer.testing import CliRunner
from agentex.lib.cli.commands.agents import agents
from agentex.lib.cli.handlers.agent_handlers import (
CloudBuildContext,
parse_build_args,
prepare_cloud_build_context,
)
runner = CliRunner()
class TestParseBuildArgs:
"""Tests for parse_build_args helper function."""
def test_parse_empty_build_args(self):
"""Test parsing None or empty list returns empty dict."""
assert parse_build_args(None) == {}
assert parse_build_args([]) == {}
def test_parse_single_build_arg(self):
"""Test parsing a single KEY=VALUE argument."""
result = parse_build_args(["FOO=bar"])
assert result == {"FOO": "bar"}
def test_parse_multiple_build_args(self):
"""Test parsing multiple KEY=VALUE arguments."""
result = parse_build_args(["FOO=bar", "BAZ=qux", "NUM=123"])
assert result == {"FOO": "bar", "BAZ": "qux", "NUM": "123"}
def test_parse_build_arg_with_equals_in_value(self):
"""Test that values containing '=' are handled correctly."""
result = parse_build_args(["URL=https://example.com?foo=bar"])
assert result == {"URL": "https://example.com?foo=bar"}
def test_parse_invalid_build_arg_ignored(self):
"""Test that invalid format args (no '=') are ignored."""
result = parse_build_args(["VALID=value", "invalid_no_equals"])
assert result == {"VALID": "value"}
class TestPrepareCloudBuildContext:
"""Tests for prepare_cloud_build_context function."""
@pytest.fixture
def temp_agent_dir(self) -> Iterator[Path]:
"""Create a temporary agent directory with minimal required files."""
with tempfile.TemporaryDirectory() as tmpdir:
agent_dir = Path(tmpdir)
# Create a minimal Dockerfile
dockerfile = agent_dir / "Dockerfile"
dockerfile.write_text("FROM python:3.12-slim\nCMD ['echo', 'hello']")
# Create a simple Python file to include
src_dir = agent_dir / "src"
src_dir.mkdir()
(src_dir / "main.py").write_text("print('hello')")
# Create manifest.yaml
manifest = agent_dir / "manifest.yaml"
manifest.write_text(
"""
build:
context:
root: .
include_paths:
- src
dockerfile: Dockerfile
agent:
name: test-agent
acp_type: sync
description: Test agent
temporal:
enabled: false
deployment:
image:
repository: test-repo/test-agent
tag: v1.0.0
"""
)
yield agent_dir
@pytest.fixture
def temp_agent_dir_no_deployment(self) -> Iterator[Path]:
"""Create a temporary agent directory without deployment config."""
with tempfile.TemporaryDirectory() as tmpdir:
agent_dir = Path(tmpdir)
dockerfile = agent_dir / "Dockerfile"
dockerfile.write_text("FROM python:3.12-slim")
src_dir = agent_dir / "src"
src_dir.mkdir()
(src_dir / "main.py").write_text("print('hello')")
manifest = agent_dir / "manifest.yaml"
manifest.write_text(
"""
build:
context:
root: .
include_paths:
- src
dockerfile: Dockerfile
agent:
name: test-agent-no-deploy
acp_type: sync
description: Test agent without deployment config
temporal:
enabled: false
"""
)
yield agent_dir
def test_prepare_cloud_build_context_returns_cloud_build_context(
self, temp_agent_dir: Path
):
"""Test that prepare_cloud_build_context returns a CloudBuildContext."""
manifest_path = str(temp_agent_dir / "manifest.yaml")
result = prepare_cloud_build_context(manifest_path=manifest_path)
assert isinstance(result, CloudBuildContext)
assert result.agent_name == "test-agent"
assert result.tag == "v1.0.0" # From manifest deployment.image.tag
assert result.image_name == "test-agent" # Last part of repository
assert result.dockerfile_path == "Dockerfile"
assert len(result.archive_bytes) > 0
assert result.build_context_size_kb > 0
def test_prepare_cloud_build_context_writes_build_info(self, temp_agent_dir: Path):
"""build-info.json ships in the archive and matches the captured provenance."""
manifest_path = str(temp_agent_dir / "manifest.yaml")
result = prepare_cloud_build_context(manifest_path=manifest_path)
# Non-git temp dir → the content hash is the identity, no commit.
assert result.provenance.commit is None
assert result.provenance.working_tree_hash is not None
with tarfile.open(fileobj=io.BytesIO(result.archive_bytes), mode="r:gz") as archive:
build_info_name = next(n for n in archive.getnames() if n.endswith("build-info.json"))
member = archive.extractfile(build_info_name)
assert member is not None
shipped = json.loads(member.read())
assert shipped == result.provenance.build_info()
def test_prepare_cloud_build_context_with_tag_override(self, temp_agent_dir: Path):
"""Test that tag parameter overrides manifest tag."""
manifest_path = str(temp_agent_dir / "manifest.yaml")
result = prepare_cloud_build_context(manifest_path=manifest_path, tag="custom-tag")
assert result.tag == "custom-tag"
def test_prepare_cloud_build_context_defaults_to_latest_when_no_deployment(
self, temp_agent_dir_no_deployment: Path
):
"""Test that tag defaults to 'latest' when no deployment config exists."""
manifest_path = str(temp_agent_dir_no_deployment / "manifest.yaml")
result = prepare_cloud_build_context(manifest_path=manifest_path)
assert result.tag == "latest"
assert result.image_name == "<repository>" # No repository in deployment config
def test_prepare_cloud_build_context_archive_is_valid_tarball(
self, temp_agent_dir: Path
):
"""Test that the archive bytes are a valid tar.gz file."""
manifest_path = str(temp_agent_dir / "manifest.yaml")
result = prepare_cloud_build_context(manifest_path=manifest_path)
# Write to temp file and verify it's a valid tar.gz
with tempfile.NamedTemporaryFile(suffix=".tar.gz", delete=False) as f:
f.write(result.archive_bytes)
temp_tar_path = f.name
try:
with tarfile.open(temp_tar_path, "r:gz") as tar:
names = tar.getnames()
# Should contain Dockerfile and src/main.py
assert "Dockerfile" in names
assert "src/main.py" in names
finally:
os.unlink(temp_tar_path)
def test_prepare_cloud_build_context_missing_dockerfile_raises_error(self):
"""Test that missing Dockerfile raises FileNotFoundError."""
with tempfile.TemporaryDirectory() as tmpdir:
agent_dir = Path(tmpdir)
# Create manifest pointing to non-existent Dockerfile
manifest = agent_dir / "manifest.yaml"
manifest.write_text(
"""
build:
context:
root: .
include_paths: []
dockerfile: NonExistentDockerfile
agent:
name: test-agent
acp_type: sync
description: Test agent
temporal:
enabled: false
"""
)
with pytest.raises(FileNotFoundError, match="Dockerfile not found"):
prepare_cloud_build_context(manifest_path=str(manifest))
def test_prepare_cloud_build_context_dockerfile_is_directory_raises_error(self):
"""Test that Dockerfile path pointing to directory raises ValueError."""
with tempfile.TemporaryDirectory() as tmpdir:
agent_dir = Path(tmpdir)
# Create a directory instead of a file for Dockerfile
dockerfile_dir = agent_dir / "Dockerfile"
dockerfile_dir.mkdir()
manifest = agent_dir / "manifest.yaml"
manifest.write_text(
"""
build:
context:
root: .
include_paths: []
dockerfile: Dockerfile
agent:
name: test-agent
acp_type: sync
description: Test agent
temporal:
enabled: false
"""
)
with pytest.raises(ValueError, match="not a file"):
prepare_cloud_build_context(manifest_path=str(manifest))
def test_prepare_cloud_build_context_with_build_args(self, temp_agent_dir: Path):
"""Test that build_args are accepted (they're logged but not included in archive)."""
manifest_path = str(temp_agent_dir / "manifest.yaml")
# Should not raise - build_args are accepted even though they're just logged
result = prepare_cloud_build_context(
manifest_path=manifest_path,
build_args=["ARG1=value1", "ARG2=value2"],
)
assert isinstance(result, CloudBuildContext)
class TestPackageCommand:
"""Tests for the 'agentex agents package' CLI command."""
@pytest.fixture
def temp_agent_dir(self) -> Iterator[Path]:
"""Create a temporary agent directory with minimal required files."""
with tempfile.TemporaryDirectory() as tmpdir:
agent_dir = Path(tmpdir)
dockerfile = agent_dir / "Dockerfile"
dockerfile.write_text("FROM python:3.12-slim\nCMD ['echo', 'hello']")
src_dir = agent_dir / "src"
src_dir.mkdir()
(src_dir / "main.py").write_text("print('hello')")
manifest = agent_dir / "manifest.yaml"
manifest.write_text(
"""
build:
context:
root: .
include_paths:
- src
dockerfile: Dockerfile
agent:
name: test-agent
acp_type: sync
description: Test agent
temporal:
enabled: false
deployment:
image:
repository: test-repo/test-agent
tag: v1.0.0
"""
)
yield agent_dir
def test_package_command_creates_tarball(self, temp_agent_dir: Path):
"""Test that package command creates a tarball file."""
manifest_path = str(temp_agent_dir / "manifest.yaml")
# Change to temp dir so output goes there
original_cwd = os.getcwd()
os.chdir(temp_agent_dir)
try:
result = runner.invoke(agents, ["package", "--manifest", manifest_path])
assert result.exit_code == 0, f"Command failed: {result.output}"
assert "Tarball saved to:" in result.output
# Check that tarball was created
expected_tarball = temp_agent_dir / "test-agent-v1.0.0.tar.gz"
assert expected_tarball.exists()
# Verify it's a valid tar.gz
with tarfile.open(expected_tarball, "r:gz") as tar:
names = tar.getnames()
assert "Dockerfile" in names
finally:
os.chdir(original_cwd)
def test_package_command_with_custom_tag(self, temp_agent_dir: Path):
"""Test package command with custom tag override."""
manifest_path = str(temp_agent_dir / "manifest.yaml")
original_cwd = os.getcwd()
os.chdir(temp_agent_dir)
try:
result = runner.invoke(
agents, ["package", "--manifest", manifest_path, "--tag", "custom-tag"]
)
assert result.exit_code == 0, f"Command failed: {result.output}"
# Check that tarball with custom tag was created
expected_tarball = temp_agent_dir / "test-agent-custom-tag.tar.gz"
assert expected_tarball.exists()
finally:
os.chdir(original_cwd)
def test_package_command_with_custom_output(self, temp_agent_dir: Path):
"""Test package command with custom output filename."""
manifest_path = str(temp_agent_dir / "manifest.yaml")
original_cwd = os.getcwd()
os.chdir(temp_agent_dir)
try:
result = runner.invoke(
agents,
["package", "--manifest", manifest_path, "--output", "my-custom-output.tar.gz"],
)
assert result.exit_code == 0, f"Command failed: {result.output}"
expected_tarball = temp_agent_dir / "my-custom-output.tar.gz"
assert expected_tarball.exists()
finally:
os.chdir(original_cwd)
def test_package_command_missing_manifest(self, temp_agent_dir: Path):
"""Test package command fails gracefully with missing manifest."""
original_cwd = os.getcwd()
os.chdir(temp_agent_dir)
try:
result = runner.invoke(
agents, ["package", "--manifest", "nonexistent-manifest.yaml"]
)
assert result.exit_code == 1
assert "manifest not found" in result.output
finally:
os.chdir(original_cwd)
def test_package_command_shows_build_parameters(self, temp_agent_dir: Path):
"""Test that package command outputs build parameters for cloud build."""
manifest_path = str(temp_agent_dir / "manifest.yaml")
original_cwd = os.getcwd()
os.chdir(temp_agent_dir)
try:
result = runner.invoke(agents, ["package", "--manifest", manifest_path])
assert result.exit_code == 0, f"Command failed: {result.output}"
assert "Build Parameters for Cloud Build API:" in result.output
assert "agent_name:" in result.output
assert "test-agent" in result.output
assert "image_name:" in result.output
assert "tag:" in result.output
finally:
os.chdir(original_cwd)
def test_package_command_with_build_args(self, temp_agent_dir: Path):
"""Test package command with build arguments."""
manifest_path = str(temp_agent_dir / "manifest.yaml")
original_cwd = os.getcwd()
os.chdir(temp_agent_dir)
try:
result = runner.invoke(
agents,
[
"package",
"--manifest",
manifest_path,
"--build-arg",
"ARG1=value1",
"--build-arg",
"ARG2=value2",
],
)
assert result.exit_code == 0, f"Command failed: {result.output}"
assert "build_args:" in result.output
finally:
os.chdir(original_cwd)