1515import os
1616import tempfile
1717import pytest
18- import sys
1918
2019from unittest .mock import Mock , patch , AsyncMock
2120
22- sys .modules ["typer" ] = Mock ()
21+ # 在任何导入之前就设置环境变量
22+ os .environ ["VOLCENGINE_ACCESS_KEY" ] = "test_access_key"
23+ os .environ ["VOLCENGINE_SECRET_KEY" ] = "test_secret_key"
2324
24- from veadk .cloud .cloud_agent_engine import CloudAgentEngine # noqa: E402
25+ from veadk .cloud .cloud_agent_engine import CloudAgentEngine
2526
2627
2728@pytest .mark .asyncio
@@ -36,100 +37,90 @@ async def test_cloud():
3637 with open (os .path .join (temp_dir , "agent.py" ), "w" ) as f :
3738 f .write (f"# Test agent implementation with { key } " )
3839
39- with patch .dict (
40- os .environ ,
41- {
42- "VOLCENGINE_ACCESS_KEY" : "test_access_key" ,
43- "VOLCENGINE_SECRET_KEY" : "test_secret_key" ,
44- },
45- ):
46- # Mock shutil.copy to avoid template file copying issues
47- with patch ("shutil.copy" ):
48- with patch (
49- "veadk.cloud.cloud_agent_engine.VeFaaS"
50- ) as mock_vefaas_class :
51- # Setup mock VeFaaS service for all operations
52- mock_vefaas_service = Mock ()
53- mock_vefaas_class .return_value = mock_vefaas_service
54-
55- # Mock deploy operation
56- mock_vefaas_service .deploy .return_value = (
57- test_endpoint ,
58- "app-123" ,
59- "func-456" ,
40+ # 这里不再需要 patch.dict,因为环境变量已经设置
41+ # Mock shutil.copy to avoid template file copying issues
42+ with patch ("shutil.copy" ):
43+ with patch ("veadk.cloud.cloud_agent_engine.VeFaaS" ) as mock_vefaas_class :
44+ # Setup mock VeFaaS service for all operations
45+ mock_vefaas_service = Mock ()
46+ mock_vefaas_class .return_value = mock_vefaas_service
47+
48+ # Mock deploy operation
49+ mock_vefaas_service .deploy .return_value = (
50+ test_endpoint ,
51+ "app-123" ,
52+ "func-456" ,
53+ )
54+
55+ # Mock update operation
56+ mock_vefaas_service ._update_function_code .return_value = (
57+ test_endpoint ,
58+ "app-123" ,
59+ "func-456" ,
60+ )
61+
62+ # Mock remove operation
63+ mock_vefaas_service .find_app_id_by_name .return_value = "app-123"
64+ mock_vefaas_service .delete .return_value = None
65+
66+ # Test CloudAgentEngine creation and deploy functionality
67+ engine = CloudAgentEngine ()
68+
69+ # Test deploy operation
70+ cloud_app = engine .deploy (application_name = app_name , path = temp_dir )
71+
72+ # Verify deployment result contains expected values
73+ assert cloud_app .vefaas_application_name == app_name
74+ assert cloud_app .vefaas_endpoint == test_endpoint
75+ assert cloud_app .vefaas_application_id == "app-123"
76+
77+ # Test update_function_code operation
78+ updated_app = engine .update_function_code (
79+ application_name = app_name , path = temp_dir
80+ )
81+
82+ # Verify update result maintains same endpoint
83+ assert updated_app .vefaas_endpoint == test_endpoint
84+
85+ # Test remove operation with mocked user input
86+ with patch ("builtins.input" , return_value = "y" ):
87+ engine .remove (app_name )
88+ mock_vefaas_service .find_app_id_by_name .assert_called_with (app_name )
89+ mock_vefaas_service .delete .assert_called_with ("app-123" )
90+
91+ # Test CloudApp message_send functionality
92+ mock_response = Mock ()
93+ mock_message = Mock ()
94+ mock_response .root .result = mock_message
95+
96+ with patch .object (cloud_app , "_get_a2a_client" ) as mock_get_client :
97+ mock_client = AsyncMock ()
98+ mock_client .send_message = AsyncMock (return_value = mock_response )
99+ mock_get_client .return_value = mock_client
100+
101+ # Test message sending to cloud agent
102+ result = await cloud_app .message_send (
103+ message = test_message ,
104+ session_id = "session-123" ,
105+ user_id = "user-456" ,
60106 )
61107
62- # Mock update operation
63- mock_vefaas_service ._update_function_code .return_value = (
64- test_endpoint ,
65- "app-123" ,
66- "func-456" ,
67- )
68-
69- # Mock remove operation
70- mock_vefaas_service .find_app_id_by_name .return_value = "app-123"
71- mock_vefaas_service .delete .return_value = None
72-
73- # Test CloudAgentEngine creation and deploy functionality
74- engine = CloudAgentEngine ()
75-
76- # Test deploy operation
77- cloud_app = engine .deploy (application_name = app_name , path = temp_dir )
78-
79- # Verify deployment result contains expected values
80- assert cloud_app .vefaas_application_name == app_name
81- assert cloud_app .vefaas_endpoint == test_endpoint
82- assert cloud_app .vefaas_application_id == "app-123"
83-
84- # Test update_function_code operation
85- updated_app = engine .update_function_code (
86- application_name = app_name , path = temp_dir
87- )
88-
89- # Verify update result maintains same endpoint
90- assert updated_app .vefaas_endpoint == test_endpoint
91-
92- # Test remove operation with mocked user input
93- with patch ("builtins.input" , return_value = "y" ):
94- engine .remove (app_name )
95- mock_vefaas_service .find_app_id_by_name .assert_called_with (
96- app_name
97- )
98- mock_vefaas_service .delete .assert_called_with ("app-123" )
99-
100- # Test CloudApp message_send functionality
101- mock_response = Mock ()
102- mock_message = Mock ()
103- mock_response .root .result = mock_message
104-
105- with patch .object (cloud_app , "_get_a2a_client" ) as mock_get_client :
106- mock_client = AsyncMock ()
107- mock_client .send_message = AsyncMock (return_value = mock_response )
108- mock_get_client .return_value = mock_client
109-
110- # Test message sending to cloud agent
111- result = await cloud_app .message_send (
112- message = test_message ,
113- session_id = "session-123" ,
114- user_id = "user-456" ,
115- )
116-
117- # Verify message sending result
118- assert result == mock_message
119- mock_client .send_message .assert_called_once ()
120-
121- # Test CloudApp delete_self functionality
122- with patch ("builtins.input" , return_value = "y" ):
123- with patch (
124- "veadk.cli.services.vefaas.vefaas.VeFaaS"
125- ) as mock_vefaas_in_app :
126- mock_vefaas_client = Mock ()
127- mock_vefaas_in_app .return_value = mock_vefaas_client
128- mock_vefaas_client .delete .return_value = None
129-
130- cloud_app .delete_self ()
131- mock_vefaas_client .delete .assert_called_with ("app-123" )
132-
133- # Verify all mocks were called as expected
134- mock_vefaas_service .deploy .assert_called_once ()
135- mock_vefaas_service ._update_function_code .assert_called_once ()
108+ # Verify message sending result
109+ assert result == mock_message
110+ mock_client .send_message .assert_called_once ()
111+
112+ # Test CloudApp delete_self functionality
113+ with patch ("builtins.input" , return_value = "y" ):
114+ with patch (
115+ "veadk.cli.services.vefaas.vefaas.VeFaaS"
116+ ) as mock_vefaas_in_app :
117+ mock_vefaas_client = Mock ()
118+ mock_vefaas_in_app .return_value = mock_vefaas_client
119+ mock_vefaas_client .delete .return_value = None
120+
121+ cloud_app .delete_self ()
122+ mock_vefaas_client .delete .assert_called_with ("app-123" )
123+
124+ # Verify all mocks were called as expected
125+ mock_vefaas_service .deploy .assert_called_once ()
126+ mock_vefaas_service ._update_function_code .assert_called_once ()
0 commit comments