@@ -73,15 +73,16 @@ def test_init_new_deployment_no_llm_config(
7373 mock_prompt_managed_identity .assert_called_once ()
7474 mock_agent_manager .deploy_agent .assert_called_once ()
7575
76+ @patch ('azext_aks_agent.custom._get_existing_cluster_role' )
7677 @patch ('azext_aks_agent.custom._setup_and_create_llm_config' )
7778 @patch ('azext_aks_agent.custom.get_aks_credentials' )
7879 @patch ('azext_aks_agent.custom.AKSAgentManager' )
7980 @patch ('azext_aks_agent.custom.get_subscription_id' )
8081 @patch ('azext_aks_agent.custom.get_console' )
8182 def test_init_existing_llm_config_user_skips_update (
8283 self , mock_get_console , mock_get_subscription_id , mock_aks_manager_class ,
83- mock_get_aks_creds , mock_setup_llm ):
84- """Test initialization when LLM config exists and user skips update."""
84+ mock_get_aks_creds , mock_setup_llm , mock_get_cluster_role ):
85+ """Test initialization raises AzCLIError when LLM config exists, user skips update, but cluster role not found ."""
8586 # Setup mocks
8687 mock_console = MagicMock ()
8788 mock_get_console .return_value = mock_console
@@ -98,22 +99,29 @@ def test_init_existing_llm_config_user_skips_update(
9899 mock_agent_manager .managed_identity_client_id = ""
99100 mock_aks_manager_class .return_value = mock_agent_manager
100101
101- # Execute
102- aks_agent_init (self .mock_cmd , self .mock_client , self .resource_group , self .cluster_name )
102+ mock_get_cluster_role .return_value = None # Cannot find cluster role
103+
104+ # Execute - should raise AzCLIError with wrapped message
105+ with self .assertRaises (AzCLIError ) as cm :
106+ aks_agent_init (self .mock_cmd , self .mock_client , self .resource_group , self .cluster_name )
103107
104108 # Assert
105109 mock_agent_manager .check_llm_config_exists .assert_called_once ()
106110 mock_setup_llm .assert_not_called ()
111+ # Verify the error message contains the wrapped format
112+ self .assertIn ("Agent initialization failed:" , str (cm .exception ))
113+ self .assertIn ("Could not determine existing cluster role" , str (cm .exception ))
107114
115+ @patch ('azext_aks_agent.custom._get_existing_cluster_role' )
108116 @patch ('azext_aks_agent.custom._setup_and_create_llm_config' )
109117 @patch ('azext_aks_agent.custom.get_aks_credentials' )
110118 @patch ('azext_aks_agent.custom.AKSAgentManager' )
111119 @patch ('azext_aks_agent.custom.get_subscription_id' )
112120 @patch ('azext_aks_agent.custom.get_console' )
113121 def test_init_existing_llm_config_user_updates (
114122 self , mock_get_console , mock_get_subscription_id , mock_aks_manager_class ,
115- mock_get_aks_creds , mock_setup_llm ):
116- """Test initialization when LLM config exists and user chooses to update ."""
123+ mock_get_aks_creds , mock_setup_llm , mock_get_cluster_role ):
124+ """Test initialization raises AzCLIError when LLM config exists, user updates, but cluster role not found ."""
117125 # Setup mocks
118126 mock_console = MagicMock ()
119127 mock_get_console .return_value = mock_console
@@ -130,12 +138,18 @@ def test_init_existing_llm_config_user_updates(
130138 mock_agent_manager .managed_identity_client_id = ""
131139 mock_aks_manager_class .return_value = mock_agent_manager
132140
133- # Execute
134- aks_agent_init (self .mock_cmd , self .mock_client , self .resource_group , self .cluster_name )
141+ mock_get_cluster_role .return_value = None # Cannot find cluster role
142+
143+ # Execute - should raise AzCLIError with wrapped message
144+ with self .assertRaises (AzCLIError ) as cm :
145+ aks_agent_init (self .mock_cmd , self .mock_client , self .resource_group , self .cluster_name )
135146
136147 # Assert
137148 mock_agent_manager .check_llm_config_exists .assert_called_once ()
138149 mock_setup_llm .assert_called_once_with (mock_console , mock_agent_manager )
150+ # Verify the error message contains the wrapped format
151+ self .assertIn ("Agent initialization failed:" , str (cm .exception ))
152+ self .assertIn ("Could not determine existing cluster role" , str (cm .exception ))
139153
140154 @patch ('azext_aks_agent.custom._get_existing_cluster_role' )
141155 @patch ('azext_aks_agent.custom._display_cluster_role_rules' )
@@ -226,7 +240,7 @@ def test_init_with_custom_cluster_role(
226240 def test_init_deployment_failure_logs_error (
227241 self , mock_get_console , mock_get_subscription_id , mock_aks_manager_class ,
228242 mock_get_aks_creds , mock_setup_llm ):
229- """Test initialization logs error when deployment fails."""
243+ """Test initialization raises AzCLIError when deployment fails."""
230244 # Setup mocks
231245 mock_console = MagicMock ()
232246 mock_get_console .return_value = mock_console
@@ -239,13 +253,13 @@ def test_init_deployment_failure_logs_error(
239253 mock_agent_manager .deploy_agent .return_value = (False , "Deployment failed" )
240254 mock_aks_manager_class .return_value = mock_agent_manager
241255
242- # Execute - should not raise, but handle the error internally
243- aks_agent_init (self .mock_cmd , self .mock_client , self .resource_group , self .cluster_name )
256+ # Execute - should raise AzCLIError with wrapped message
257+ with self .assertRaises (AzCLIError ) as cm :
258+ aks_agent_init (self .mock_cmd , self .mock_client , self .resource_group , self .cluster_name )
244259
245- # Verify error message was printed to console
246- error_calls = [call for call in mock_console .print .call_args_list
247- if 'Failed to deploy agent' in str (call )]
248- self .assertTrue (len (error_calls ) > 0 , "Expected error message about deployment failure to be printed" )
260+ # Verify the error message contains the wrapped format
261+ self .assertIn ("Agent initialization failed:" , str (cm .exception ))
262+ self .assertIn ("Failed to deploy agent" , str (cm .exception ))
249263
250264 @patch ('azext_aks_agent.custom._get_existing_cluster_role' )
251265 @patch ('azext_aks_agent.custom._setup_and_create_llm_config' )
@@ -256,7 +270,7 @@ def test_init_deployment_failure_logs_error(
256270 def test_init_deployed_no_cluster_role_logs_error (
257271 self , mock_get_console , mock_get_subscription_id , mock_aks_manager_class ,
258272 mock_get_aks_creds , mock_setup_llm , mock_get_cluster_role ):
259- """Test initialization logs error when deployed but cannot determine cluster role."""
273+ """Test initialization raises AzCLIError when deployed but cannot determine cluster role."""
260274 # Setup mocks
261275 mock_console = MagicMock ()
262276 mock_get_console .return_value = mock_console
@@ -271,13 +285,13 @@ def test_init_deployed_no_cluster_role_logs_error(
271285
272286 mock_get_cluster_role .return_value = None # Cannot find cluster role
273287
274- # Execute - should not raise, but handle the error internally
275- aks_agent_init (self .mock_cmd , self .mock_client , self .resource_group , self .cluster_name )
288+ # Execute - should raise AzCLIError with wrapped message
289+ with self .assertRaises (AzCLIError ) as cm :
290+ aks_agent_init (self .mock_cmd , self .mock_client , self .resource_group , self .cluster_name )
276291
277- # Verify error message was printed to console
278- error_calls = [call for call in mock_console .print .call_args_list
279- if 'Could not determine existing cluster role' in str (call )]
280- self .assertTrue (len (error_calls ) > 0 , "Expected error message about cluster role to be printed" )
292+ # Verify the error message contains the wrapped format
293+ self .assertIn ("Agent initialization failed:" , str (cm .exception ))
294+ self .assertIn ("Could not determine existing cluster role" , str (cm .exception ))
281295
282296 @patch ('azext_aks_agent.custom._prompt_managed_identity_configuration' )
283297 @patch ('azext_aks_agent.custom._prompt_cluster_role_configuration' )
0 commit comments