forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_deploy.py
More file actions
176 lines (140 loc) · 5.05 KB
/
Copy pathtest_cli_deploy.py
File metadata and controls
176 lines (140 loc) · 5.05 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
# Copyright 2025 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
from pathlib import Path
import shutil
import subprocess
import tempfile
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 google.adk.cli.cli_deploy as cli_deploy
import pytest
# 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))
# 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)
@pytest.fixture()
def agent_dir(tmp_path: Path) -> Callable[[bool], Path]:
"""Return a factory that creates a dummy agent directory tree."""
def _factory(include_requirements: bool) -> Path:
base = tmp_path / "agent"
base.mkdir()
(base / "agent.py").write_text("# dummy agent")
(base / "__init__.py").touch()
if include_requirements:
(base / "requirements.txt").write_text("pytest\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()
# to_cloud_run
@pytest.mark.parametrize("include_requirements", [True, False])
def test_to_cloud_run_happy_path(
monkeypatch: pytest.MonkeyPatch,
agent_dir: Callable[[bool], Path],
include_requirements: bool,
) -> None:
"""
End-to-end execution test for `to_cloud_run` covering both presence and
absence of *requirements.txt*.
"""
tmp_dir = Path(tempfile.mkdtemp())
src_dir = agent_dir(include_requirements)
copy_recorder = _Recorder()
run_recorder = _Recorder()
# Cache the ORIGINAL copytree before patching
original_copytree = cli_deploy.shutil.copytree
def _recording_copytree(*args: Any, **kwargs: Any):
copy_recorder(*args, **kwargs)
return original_copytree(*args, **kwargs)
monkeypatch.setattr(cli_deploy.shutil, "copytree", _recording_copytree)
# Skip actual cleanup so that we can inspect generated files later.
monkeypatch.setattr(cli_deploy.shutil, "rmtree", lambda *_a, **_k: None)
monkeypatch.setattr(subprocess, "run", run_recorder)
cli_deploy.to_cloud_run(
agent_folder=str(src_dir),
project="proj",
region="asia-northeast1",
service_name="svc",
app_name="app",
temp_folder=str(tmp_dir),
port=8080,
trace_to_cloud=True,
with_ui=True,
verbosity="info",
session_db_url="sqlite://",
adk_version="0.0.5",
)
# Assertions
assert (
len(copy_recorder.calls) == 1
), "Agent sources must be copied exactly once."
assert run_recorder.calls, "gcloud command should be executed at least once."
assert (tmp_dir / "Dockerfile").exists(), "Dockerfile must be generated."
# Manual cleanup because we disabled rmtree in the monkeypatch.
shutil.rmtree(tmp_dir, ignore_errors=True)
def test_to_cloud_run_cleans_temp_dir(
monkeypatch: pytest.MonkeyPatch,
agent_dir: Callable[[bool], Path],
) -> None:
"""`to_cloud_run` should always delete the temporary folder on exit."""
tmp_dir = Path(tempfile.mkdtemp())
src_dir = agent_dir(False)
deleted: Dict[str, Path] = {}
def _fake_rmtree(path: str | Path, *a: Any, **k: Any) -> None:
deleted["path"] = Path(path)
monkeypatch.setattr(cli_deploy.shutil, "rmtree", _fake_rmtree)
monkeypatch.setattr(subprocess, "run", _Recorder())
cli_deploy.to_cloud_run(
agent_folder=str(src_dir),
project="proj",
region=None,
service_name="svc",
app_name="app",
temp_folder=str(tmp_dir),
port=8080,
trace_to_cloud=False,
with_ui=False,
verbosity="info",
session_db_url=None,
adk_version="0.0.5",
)
assert deleted["path"] == tmp_dir