-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathtest_cli_deploy.py
More file actions
685 lines (577 loc) · 20.7 KB
/
test_cli_deploy.py
File metadata and controls
685 lines (577 loc) · 20.7 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# 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.
"""Tests for utilities in cli_deploy."""
from __future__ import annotations
import importlib
from pathlib import Path
import shutil
import subprocess
import sys
import types
from typing import Any
from typing import Callable
from typing import Dict
from typing import List
from typing import Tuple
from unittest import mock
import click
import pytest
import src.google.adk.cli.cli_deploy as cli_deploy
# Helpers
class _Recorder:
"""A callable object that records every invocation."""
def __init__(self) -> None:
self.calls: List[Tuple[Tuple[Any, ...], Dict[str, Any]]] = []
def __call__(self, *args: Any, **kwargs: Any) -> None:
self.calls.append((args, kwargs))
def get_last_call_args(self) -> Tuple[Any, ...]:
"""Returns the positional arguments of the last call."""
if not self.calls:
raise IndexError("No calls have been recorded.")
return self.calls[-1][0]
def get_last_call_kwargs(self) -> Dict[str, Any]:
"""Returns the keyword arguments of the last call."""
if not self.calls:
raise IndexError("No calls have been recorded.")
return self.calls[-1][1]
# Fixtures
@pytest.fixture(autouse=True)
def _mute_click(monkeypatch: pytest.MonkeyPatch) -> None:
"""Suppress click.echo to keep test output clean."""
monkeypatch.setattr(click, "echo", lambda *a, **k: None)
monkeypatch.setattr(click, "secho", lambda *a, **k: None)
@pytest.fixture(autouse=True)
def reload_cli_deploy():
"""Reload cli_deploy before each test."""
importlib.reload(cli_deploy)
yield # This allows the test to run after the module has been reloaded.
@pytest.fixture()
def agent_dir(tmp_path: Path) -> Callable[[bool, bool], Path]:
"""
Return a factory that creates a dummy agent directory tree.
"""
def _factory(include_requirements: bool, include_env: bool) -> Path:
base = tmp_path / "agent"
base.mkdir()
(base / "agent.py").write_text(
"# dummy agent\nroot_agent = 'dummy_agent'\n"
)
(base / "__init__.py").touch()
if include_requirements:
(base / "requirements.txt").write_text("pytest\n")
if include_env:
(base / ".env").write_text('TEST_VAR="test_value"\n')
return base
return _factory
# _resolve_project
def test_resolve_project_with_option() -> None:
"""It should return the explicit project value untouched."""
assert cli_deploy._resolve_project("my-project") == "my-project"
def test_resolve_project_from_gcloud(monkeypatch: pytest.MonkeyPatch) -> None:
"""It should fall back to `gcloud config get-value project` when no value supplied."""
monkeypatch.setattr(
subprocess,
"run",
lambda *a, **k: types.SimpleNamespace(stdout="gcp-proj\n"),
)
with mock.patch("click.echo") as mocked_echo:
assert cli_deploy._resolve_project(None) == "gcp-proj"
mocked_echo.assert_called_once()
def test_resolve_project_from_gcloud_fails(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""It should raise an exception if the gcloud command fails."""
monkeypatch.setattr(
subprocess,
"run",
mock.Mock(side_effect=subprocess.CalledProcessError(1, "cmd", "err")),
)
with pytest.raises(subprocess.CalledProcessError):
cli_deploy._resolve_project(None)
@pytest.mark.parametrize(
"adk_version, session_uri, artifact_uri, memory_uri, use_local_storage, "
"expected",
[
(
"1.3.0",
"sqlite://s",
"gs://a",
"rag://m",
None,
(
"--session_service_uri=sqlite://s --artifact_service_uri=gs://a"
" --memory_service_uri=rag://m"
),
),
(
"1.2.5",
"sqlite://s",
"gs://a",
"rag://m",
None,
"--session_db_url=sqlite://s --artifact_storage_uri=gs://a",
),
(
"0.5.0",
"sqlite://s",
"gs://a",
"rag://m",
None,
"--session_db_url=sqlite://s",
),
(
"1.3.0",
"sqlite://s",
None,
None,
None,
"--session_service_uri=sqlite://s",
),
(
"1.3.0",
None,
"gs://a",
"rag://m",
None,
"--artifact_service_uri=gs://a --memory_service_uri=rag://m",
),
(
"1.2.0",
None,
"gs://a",
None,
None,
"--artifact_storage_uri=gs://a",
),
(
"1.21.0",
None,
None,
None,
False,
"--no_use_local_storage",
),
(
"1.21.0",
None,
None,
None,
True,
"--use_local_storage",
),
(
"1.21.0",
"sqlite://s",
"gs://a",
None,
False,
"--session_service_uri=sqlite://s --artifact_service_uri=gs://a",
),
],
)
def test_get_service_option_by_adk_version(
adk_version: str,
session_uri: str | None,
artifact_uri: str | None,
memory_uri: str | None,
use_local_storage: bool | None,
expected: str,
) -> None:
"""It should return the correct service URI flags for a given ADK version."""
actual = cli_deploy._get_service_option_by_adk_version(
adk_version=adk_version,
session_uri=session_uri,
artifact_uri=artifact_uri,
memory_uri=memory_uri,
use_local_storage=use_local_storage,
)
assert actual.rstrip() == expected.rstrip()
def test_agent_engine_app_template_compiles_with_windows_paths() -> None:
"""It should not emit invalid Python when paths contain `\\u` segments."""
rendered = cli_deploy._AGENT_ENGINE_APP_TEMPLATE.format(
is_config_agent=True,
agent_folder=r".\user_agent_tmp20260101_000000",
adk_app_object="root_agent",
adk_app_type="agent",
trace_to_cloud_option=False,
express_mode=False,
agent_module='agent',
)
compile(rendered, "<agent_engine_app.py>", "exec")
@pytest.mark.parametrize("include_requirements", [True, False])
def test_to_agent_engine_happy_path(
monkeypatch: pytest.MonkeyPatch,
agent_dir: Callable[[bool, bool], Path],
include_requirements: bool,
) -> None:
"""Tests the happy path for the `to_agent_engine` function."""
rmtree_recorder = _Recorder()
monkeypatch.setattr(shutil, "rmtree", rmtree_recorder)
create_recorder = _Recorder()
fake_vertexai = types.ModuleType("vertexai")
class _FakeAgentEngines:
def create(self, *, config: Dict[str, Any]) -> Any:
create_recorder(config=config)
return types.SimpleNamespace(
api_resource=types.SimpleNamespace(
name="projects/p/locations/l/reasoningEngines/e"
)
)
def update(self, *, name: str, config: Dict[str, Any]) -> None:
del name
del config
class _FakeVertexClient:
def __init__(self, *args: Any, **kwargs: Any) -> None:
del args
del kwargs
self.agent_engines = _FakeAgentEngines()
fake_vertexai.Client = _FakeVertexClient
monkeypatch.setitem(sys.modules, "vertexai", fake_vertexai)
src_dir = agent_dir(include_requirements, False)
tmp_dir = src_dir.parent / "tmp"
cli_deploy.to_agent_engine(
agent_folder=str(src_dir),
temp_folder="tmp",
adk_app="my_adk_app",
trace_to_cloud=True,
project="my-gcp-project",
region="us-central1",
display_name="My Test Agent",
description="A test agent.",
)
agent_file = tmp_dir / "agent.py"
assert agent_file.is_file()
init_file = tmp_dir / "__init__.py"
assert init_file.is_file()
adk_app_file = tmp_dir / "my_adk_app.py"
assert adk_app_file.is_file()
content = adk_app_file.read_text()
assert "from .agent import root_agent" in content
assert "adk_app = AdkApp(" in content
assert "agent=root_agent" in content
assert "enable_tracing=True" in content
reqs_path = tmp_dir / "requirements.txt"
assert reqs_path.is_file()
assert "google-cloud-aiplatform[adk,agent_engines]" in reqs_path.read_text()
assert len(create_recorder.calls) == 1
assert str(rmtree_recorder.get_last_call_args()[0]) == str(tmp_dir)
def test_to_agent_engine_raises_when_explicit_config_file_missing(
monkeypatch: pytest.MonkeyPatch,
agent_dir: Callable[[bool, bool], Path],
tmp_path: Path,
) -> None:
"""It should fail with a clear error when --agent_engine_config_file is missing."""
monkeypatch.setattr(shutil, "rmtree", lambda *a, **k: None)
src_dir = agent_dir(False, False)
missing_config = tmp_path / "no_such_agent_engine_config.json"
expected_abs = str(missing_config.resolve())
with pytest.raises(click.ClickException) as exc_info:
cli_deploy.to_agent_engine(
agent_folder=str(src_dir),
temp_folder="tmp",
adk_app="my_adk_app",
trace_to_cloud=True,
project="my-gcp-project",
region="us-central1",
display_name="My Test Agent",
description="A test agent.",
agent_engine_config_file=str(missing_config),
)
assert "Agent engine config file not found" in str(exc_info.value)
assert expected_abs in str(exc_info.value)
def test_to_agent_engine_skips_agent_import_validation_by_default(
monkeypatch: pytest.MonkeyPatch,
agent_dir: Callable[[bool, bool], Path],
) -> None:
"""It should skip agent.py import validation by default."""
validate_recorder = _Recorder()
def _validate_agent_import(*args: Any, **kwargs: Any) -> None:
validate_recorder(*args, **kwargs)
raise AssertionError("_validate_agent_import should not be called")
monkeypatch.setattr(
cli_deploy, "_validate_agent_import", _validate_agent_import
)
fake_vertexai = types.ModuleType("vertexai")
class _FakeAgentEngines:
def create(self, *, config: Dict[str, Any]) -> Any:
del config
return types.SimpleNamespace(
api_resource=types.SimpleNamespace(
name="projects/p/locations/l/reasoningEngines/e"
)
)
class _FakeVertexClient:
def __init__(self, *args: Any, **kwargs: Any) -> None:
del args
del kwargs
self.agent_engines = _FakeAgentEngines()
fake_vertexai.Client = _FakeVertexClient
monkeypatch.setitem(sys.modules, "vertexai", fake_vertexai)
src_dir = agent_dir(False, False)
cli_deploy.to_agent_engine(
agent_folder=str(src_dir),
temp_folder="tmp",
adk_app="my_adk_app",
trace_to_cloud=True,
project="my-gcp-project",
region="us-central1",
display_name="My Test Agent",
description="A test agent.",
)
assert validate_recorder.calls == []
def test_to_agent_engine_validates_agent_import_when_enabled(
monkeypatch: pytest.MonkeyPatch,
agent_dir: Callable[[bool, bool], Path],
) -> None:
"""It should run agent.py import validation when enabled."""
validate_recorder = _Recorder()
def _validate_agent_import(*args: Any, **kwargs: Any) -> None:
validate_recorder(*args, **kwargs)
monkeypatch.setattr(
cli_deploy, "_validate_agent_import", _validate_agent_import
)
fake_vertexai = types.ModuleType("vertexai")
class _FakeAgentEngines:
def create(self, *, config: Dict[str, Any]) -> Any:
del config
return types.SimpleNamespace(
api_resource=types.SimpleNamespace(
name="projects/p/locations/l/reasoningEngines/e"
)
)
class _FakeVertexClient:
def __init__(self, *args: Any, **kwargs: Any) -> None:
del args
del kwargs
self.agent_engines = _FakeAgentEngines()
fake_vertexai.Client = _FakeVertexClient
monkeypatch.setitem(sys.modules, "vertexai", fake_vertexai)
src_dir = agent_dir(False, False)
cli_deploy.to_agent_engine(
agent_folder=str(src_dir),
temp_folder="tmp",
adk_app="my_adk_app",
trace_to_cloud=True,
project="my-gcp-project",
region="us-central1",
display_name="My Test Agent",
description="A test agent.",
skip_agent_import_validation=False,
)
assert len(validate_recorder.calls) == 1
@pytest.mark.parametrize("include_requirements", [True, False])
def test_to_gke_happy_path(
monkeypatch: pytest.MonkeyPatch,
agent_dir: Callable[[bool, bool], Path],
tmp_path: Path,
include_requirements: bool,
) -> None:
"""
Tests the happy path for the `to_gke` function.
"""
src_dir = agent_dir(include_requirements, False)
run_recorder = _Recorder()
rmtree_recorder = _Recorder()
def mock_subprocess_run(*args, **kwargs):
run_recorder(*args, **kwargs)
command_list = args[0]
if command_list and command_list[0:2] == ["kubectl", "apply"]:
fake_stdout = "deployment.apps/gke-svc created\nservice/gke-svc created"
return types.SimpleNamespace(stdout=fake_stdout)
return None
monkeypatch.setattr(subprocess, "run", mock_subprocess_run)
monkeypatch.setattr(shutil, "rmtree", rmtree_recorder)
cli_deploy.to_gke(
agent_folder=str(src_dir),
project="gke-proj",
region="us-east1",
cluster_name="my-gke-cluster",
service_name="gke-svc",
app_name="agent",
temp_folder=str(tmp_path),
port=9090,
trace_to_cloud=False,
otel_to_cloud=False,
with_ui=True,
log_level="debug",
adk_version="1.2.0",
allow_origins=["http://localhost:3000", "https://my-app.com"],
session_service_uri="sqlite:///",
artifact_service_uri="gs://gke-bucket",
)
dockerfile_path = tmp_path / "Dockerfile"
assert dockerfile_path.is_file()
dockerfile_content = dockerfile_path.read_text()
assert "CMD adk web --port=9090" in dockerfile_content
assert "RUN pip install google-adk==1.2.0" in dockerfile_content
assert len(run_recorder.calls) == 3, "Expected 3 subprocess calls"
build_args = run_recorder.calls[0][0][0]
expected_build_args = [
"gcloud",
"builds",
"submit",
"--tag",
"gcr.io/gke-proj/gke-svc",
"--verbosity",
"debug",
str(tmp_path),
]
assert build_args == expected_build_args
creds_args = run_recorder.calls[1][0][0]
expected_creds_args = [
"gcloud",
"container",
"clusters",
"get-credentials",
"my-gke-cluster",
"--region",
"us-east1",
"--project",
"gke-proj",
]
assert creds_args == expected_creds_args
assert (
"--allow_origins=http://localhost:3000,https://my-app.com"
in dockerfile_content
)
apply_args = run_recorder.calls[2][0][0]
expected_apply_args = ["kubectl", "apply", "-f", str(tmp_path)]
assert apply_args == expected_apply_args
deployment_yaml_path = tmp_path / "deployment.yaml"
assert deployment_yaml_path.is_file()
yaml_content = deployment_yaml_path.read_text()
assert "kind: Deployment" in yaml_content
assert "kind: Service" in yaml_content
assert "name: gke-svc" in yaml_content
assert "image: gcr.io/gke-proj/gke-svc" in yaml_content
assert f"containerPort: 9090" in yaml_content
assert f"targetPort: 9090" in yaml_content
assert "type: ClusterIP" in yaml_content
# 4. Verify cleanup
assert str(rmtree_recorder.get_last_call_args()[0]) == str(tmp_path)
# _validate_agent_import tests
class TestValidateAgentImport:
"""Tests for the _validate_agent_import function."""
def test_skips_config_agents(self, tmp_path: Path) -> None:
"""Config agents should skip validation."""
# This should not raise even with no agent.py file
cli_deploy._validate_agent_import(
str(tmp_path), "root_agent", is_config_agent=True
)
def test_raises_on_missing_agent_module(self, tmp_path: Path) -> None:
"""Should raise when agent.py is missing."""
with pytest.raises(click.ClickException) as exc_info:
cli_deploy._validate_agent_import(
str(tmp_path), "root_agent", is_config_agent=False
)
assert "Agent module not found" in str(exc_info.value)
def test_raises_on_missing_export(self, tmp_path: Path) -> None:
"""Should raise when the expected export is missing."""
agent_file = tmp_path / "agent.py"
agent_file.write_text("some_other_var = 'hello'\n")
(tmp_path / "__init__.py").touch()
with pytest.raises(click.ClickException) as exc_info:
cli_deploy._validate_agent_import(
str(tmp_path), "root_agent", is_config_agent=False
)
assert "does not export 'root_agent'" in str(exc_info.value)
assert "some_other_var" in str(exc_info.value)
def test_success_with_root_agent_export(self, tmp_path: Path) -> None:
"""Should succeed when root_agent is exported."""
agent_file = tmp_path / "agent.py"
agent_file.write_text("root_agent = 'my_agent'\n")
(tmp_path / "__init__.py").touch()
# Should not raise
cli_deploy._validate_agent_import(
str(tmp_path), "root_agent", is_config_agent=False
)
def test_success_with_app_export(self, tmp_path: Path) -> None:
"""Should succeed when app is exported."""
agent_file = tmp_path / "agent.py"
agent_file.write_text("app = 'my_app'\n")
(tmp_path / "__init__.py").touch()
# Should not raise
cli_deploy._validate_agent_import(
str(tmp_path), "app", is_config_agent=False
)
def test_success_with_relative_imports(self, tmp_path: Path) -> None:
"""Should succeed when agent.py uses relative imports."""
(tmp_path / "helper.py").write_text("VALUE = 'my_agent'\n")
(tmp_path / "__init__.py").touch()
(tmp_path / "agent.py").write_text(
"from .helper import VALUE\n\nroot_agent = VALUE\n"
)
cli_deploy._validate_agent_import(
str(tmp_path), "root_agent", is_config_agent=False
)
def test_raises_on_import_error(self, tmp_path: Path) -> None:
"""Should raise with helpful message on ImportError."""
agent_file = tmp_path / "agent.py"
agent_file.write_text("from nonexistent_module import something\n")
(tmp_path / "__init__.py").touch()
with pytest.raises(click.ClickException) as exc_info:
cli_deploy._validate_agent_import(
str(tmp_path), "root_agent", is_config_agent=False
)
assert "Failed to import agent module" in str(exc_info.value)
assert "nonexistent_module" in str(exc_info.value)
def test_raises_on_basellm_import_error(self, tmp_path: Path) -> None:
"""Should provide specific guidance for BaseLlm import errors."""
agent_file = tmp_path / "agent.py"
agent_file.write_text(
"from google.adk.models.base_llm import NonexistentBaseLlm\n"
)
(tmp_path / "__init__.py").touch()
with pytest.raises(click.ClickException) as exc_info:
cli_deploy._validate_agent_import(
str(tmp_path), "root_agent", is_config_agent=False
)
assert "BaseLlm-related error" in str(exc_info.value)
assert "custom LLM" in str(exc_info.value)
def test_raises_on_syntax_error(self, tmp_path: Path) -> None:
"""Should raise on syntax errors in agent.py."""
agent_file = tmp_path / "agent.py"
agent_file.write_text("def invalid syntax here:\n")
(tmp_path / "__init__.py").touch()
with pytest.raises(click.ClickException) as exc_info:
cli_deploy._validate_agent_import(
str(tmp_path), "root_agent", is_config_agent=False
)
assert "Error while loading agent module" in str(exc_info.value)
def test_cleans_up_sys_modules(self, tmp_path: Path) -> None:
"""Should clean up sys.modules after validation."""
agent_file = tmp_path / "agent.py"
agent_file.write_text("root_agent = 'my_agent'\n")
(tmp_path / "__init__.py").touch()
module_name = tmp_path.name
agent_module_key = f"{module_name}.agent"
# Ensure module is not in sys.modules before
assert module_name not in sys.modules
assert agent_module_key not in sys.modules
cli_deploy._validate_agent_import(
str(tmp_path), "root_agent", is_config_agent=False
)
# Ensure module is cleaned up after
assert module_name not in sys.modules
assert agent_module_key not in sys.modules
def test_restores_sys_path(self, tmp_path: Path) -> None:
"""Should restore sys.path after validation."""
agent_file = tmp_path / "agent.py"
agent_file.write_text("root_agent = 'my_agent'\n")
(tmp_path / "__init__.py").touch()
original_path = sys.path.copy()
cli_deploy._validate_agent_import(
str(tmp_path), "root_agent", is_config_agent=False
)
assert sys.path == original_path