Skip to content

Commit 585b7d2

Browse files
authored
test(vefaas): update Unitest of CloudAgentEngine and CloudApp and update deploy docs (#39)
* test(vefaas): update Unitest of CloudAgentEngine and CloudApp * fix(test): test_cloud typer dependency * test(vefaas): volcenginesdkcore dependency, uv sync --all-extras * test(vefaas): modify chinese annotation * test(vefaas): fix deploy docs --------- Co-authored-by: tangou <[email protected]>
1 parent 6c8b658 commit 585b7d2

File tree

3 files changed

+169
-3
lines changed

3 files changed

+169
-3
lines changed

.github/workflows/unit-tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
run: |
5050
uv venv .venv
5151
source .venv/bin/activate
52-
uv sync
52+
uv sync --all-extras
5353
uv pip install -e .
5454
5555
- name: Run unit tests with pytest

docs/docs/deploy.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,47 @@ app = CloudApp(name="veadk-agent", endpoint=ENDPOINT)
117117
cloud_app.invoke(user_id=USER_ID, session_id=SESSION_ID, message=...)
118118
```
119119

120-
- 更新自身代码
121-
- 删除自身
120+
## 更新云应用
121+
122+
### 通过 CloudAgentEngine 更新
123+
124+
当您需要更新已部署的Agent代码时,可以使用`update_function_code`方法:
125+
126+
```python
127+
from veadk.cloud.cloud_agent_engine import CloudAgentEngine
128+
129+
engine = CloudAgentEngine()
130+
131+
# 更新现有应用的代码,保持相同的访问端点
132+
updated_cloud_app = engine.update_function_code(
133+
application_name="my-agent-app", # 现有应用名称
134+
path="/my-agent-project" # 本地项目路径
135+
)
136+
137+
# 可以使用updated_cloud_app.vefaas_endpoint访问您的项目
138+
```
139+
140+
**注意事项:**
141+
- 更新操作会保持相同的访问端点URL
142+
- 确保项目路径包含`agent.py`文件
143+
144+
145+
## 删除云应用
146+
147+
### 通过 CloudAgentEngine 删除
148+
149+
```python
150+
from veadk.cloud.cloud_agent_engine import CloudAgentEngine
151+
152+
engine = CloudAgentEngine()
153+
154+
# 删除指定的云应用
155+
engine.remove(app_name="my-agent-app")
156+
```
157+
158+
执行时会提示确认:
159+
```
160+
Confirm delete cloud app my-agent-app? (y/N): y
161+
```
162+
163+
输入`y`确认删除,输入其他任何字符或直接回车则取消删除。

tests/test_cloud.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import tempfile
17+
import pytest
18+
19+
from unittest.mock import Mock, patch, AsyncMock
20+
21+
os.environ["VOLCENGINE_ACCESS_KEY"] = "test_access_key"
22+
os.environ["VOLCENGINE_SECRET_KEY"] = "test_secret_key"
23+
24+
from veadk.cloud.cloud_agent_engine import CloudAgentEngine
25+
26+
27+
@pytest.mark.asyncio
28+
async def test_cloud():
29+
app_name = "test-app"
30+
key = "CloudTestIdentifier123"
31+
test_endpoint = "https://test-endpoint.volcengine.com"
32+
test_message = "Hello cloud agent"
33+
34+
# Create temporary directory with required agent.py file for testing
35+
with tempfile.TemporaryDirectory() as temp_dir:
36+
with open(os.path.join(temp_dir, "agent.py"), "w") as f:
37+
f.write(f"# Test agent implementation with {key}")
38+
39+
# Mock shutil.copy to avoid template file copying issues
40+
with patch("shutil.copy"):
41+
with patch("veadk.cloud.cloud_agent_engine.VeFaaS") as mock_vefaas_class:
42+
# Setup mock VeFaaS service for all operations
43+
mock_vefaas_service = Mock()
44+
mock_vefaas_class.return_value = mock_vefaas_service
45+
46+
# Mock deploy operation
47+
mock_vefaas_service.deploy.return_value = (
48+
test_endpoint,
49+
"app-123",
50+
"func-456",
51+
)
52+
53+
# Mock update operation
54+
mock_vefaas_service._update_function_code.return_value = (
55+
test_endpoint,
56+
"app-123",
57+
"func-456",
58+
)
59+
60+
# Mock remove operation
61+
mock_vefaas_service.find_app_id_by_name.return_value = "app-123"
62+
mock_vefaas_service.delete.return_value = None
63+
64+
# Test CloudAgentEngine creation and deploy functionality
65+
engine = CloudAgentEngine()
66+
67+
# Test deploy operation
68+
cloud_app = engine.deploy(application_name=app_name, path=temp_dir)
69+
70+
# Verify deployment result contains expected values
71+
assert cloud_app.vefaas_application_name == app_name
72+
assert cloud_app.vefaas_endpoint == test_endpoint
73+
assert cloud_app.vefaas_application_id == "app-123"
74+
75+
# Test update_function_code operation
76+
updated_app = engine.update_function_code(
77+
application_name=app_name, path=temp_dir
78+
)
79+
80+
# Verify update result maintains same endpoint
81+
assert updated_app.vefaas_endpoint == test_endpoint
82+
83+
# Test remove operation with mocked user input
84+
with patch("builtins.input", return_value="y"):
85+
engine.remove(app_name)
86+
mock_vefaas_service.find_app_id_by_name.assert_called_with(app_name)
87+
mock_vefaas_service.delete.assert_called_with("app-123")
88+
89+
# Test CloudApp message_send functionality
90+
mock_response = Mock()
91+
mock_message = Mock()
92+
mock_response.root.result = mock_message
93+
94+
with patch.object(cloud_app, "_get_a2a_client") as mock_get_client:
95+
mock_client = AsyncMock()
96+
mock_client.send_message = AsyncMock(return_value=mock_response)
97+
mock_get_client.return_value = mock_client
98+
99+
# Test message sending to cloud agent
100+
result = await cloud_app.message_send(
101+
message=test_message,
102+
session_id="session-123",
103+
user_id="user-456",
104+
)
105+
106+
# Verify message sending result
107+
assert result == mock_message
108+
mock_client.send_message.assert_called_once()
109+
110+
# Test CloudApp delete_self functionality
111+
with patch("builtins.input", return_value="y"):
112+
with patch(
113+
"veadk.cli.services.vefaas.vefaas.VeFaaS"
114+
) as mock_vefaas_in_app:
115+
mock_vefaas_client = Mock()
116+
mock_vefaas_in_app.return_value = mock_vefaas_client
117+
mock_vefaas_client.delete.return_value = None
118+
119+
cloud_app.delete_self()
120+
mock_vefaas_client.delete.assert_called_with("app-123")
121+
122+
# Verify all mocks were called as expected
123+
mock_vefaas_service.deploy.assert_called_once()
124+
mock_vefaas_service._update_function_code.assert_called_once()

0 commit comments

Comments
 (0)