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