|
1 | 1 | from unittest.mock import Mock, call |
2 | 2 |
|
| 3 | +import pytest |
| 4 | + |
| 5 | +from openstack.exceptions import ConflictException, NotFoundException |
| 6 | + |
3 | 7 | from openstack_mcp_server.tools.compute_tools import ComputeTools |
4 | 8 | from openstack_mcp_server.tools.response.compute import Flavor, Server |
5 | 9 |
|
@@ -261,9 +265,10 @@ def test_register_tools(self): |
261 | 265 | call(compute_tools.get_server), |
262 | 266 | call(compute_tools.create_server), |
263 | 267 | call(compute_tools.get_flavors), |
| 268 | + call(compute_tools.action_server), |
264 | 269 | ], |
265 | 270 | ) |
266 | | - assert mock_tool_decorator.call_count == 4 |
| 271 | + assert mock_tool_decorator.call_count == 5 |
267 | 272 |
|
268 | 273 | def test_compute_tools_instantiation(self): |
269 | 274 | """Test ComputeTools can be instantiated.""" |
@@ -346,3 +351,84 @@ def test_get_flavors_empty_list(self, mock_get_openstack_conn): |
346 | 351 |
|
347 | 352 | assert result == [] |
348 | 353 | mock_conn.compute.flavors.assert_called_once() |
| 354 | + |
| 355 | + @pytest.mark.parametrize( |
| 356 | + "action", |
| 357 | + [ |
| 358 | + "pause", |
| 359 | + "unpause", |
| 360 | + "suspend", |
| 361 | + "resume", |
| 362 | + "lock", |
| 363 | + "unlock", |
| 364 | + "rescue", |
| 365 | + "unrescue", |
| 366 | + "start", |
| 367 | + "stop", |
| 368 | + "shelve", |
| 369 | + "shelve_offload", |
| 370 | + "unshelve", |
| 371 | + ], |
| 372 | + ) |
| 373 | + def test_action_server_success(self, mock_get_openstack_conn, action): |
| 374 | + """Test action_server with all supported actions.""" |
| 375 | + mock_conn = mock_get_openstack_conn |
| 376 | + server_id = "test-server-id" |
| 377 | + |
| 378 | + # Mock the action method to avoid calling actual methods |
| 379 | + action_method = getattr(mock_conn.compute, f"{action}_server") |
| 380 | + action_method.return_value = None |
| 381 | + |
| 382 | + compute_tools = ComputeTools() |
| 383 | + result = compute_tools.action_server(server_id, action) |
| 384 | + |
| 385 | + # Verify the result is None (void function) |
| 386 | + assert result is None |
| 387 | + |
| 388 | + # Verify the correct method was called with server ID |
| 389 | + action_method.assert_called_once_with(server_id) |
| 390 | + |
| 391 | + def test_action_server_unsupported_action(self, mock_get_openstack_conn): |
| 392 | + """Test action_server with unsupported action raises ValueError.""" |
| 393 | + server_id = "test-server-id" |
| 394 | + unsupported_action = "invalid_action" |
| 395 | + |
| 396 | + compute_tools = ComputeTools() |
| 397 | + |
| 398 | + with pytest.raises( |
| 399 | + ValueError, |
| 400 | + match=f"Unsupported action: {unsupported_action}", |
| 401 | + ): |
| 402 | + compute_tools.action_server(server_id, unsupported_action) |
| 403 | + |
| 404 | + def test_action_server_not_found(self, mock_get_openstack_conn): |
| 405 | + """Test action_server when server does not exist.""" |
| 406 | + mock_conn = mock_get_openstack_conn |
| 407 | + server_id = "non-existent-server-id" |
| 408 | + action = "pause" |
| 409 | + |
| 410 | + # Mock the action method to raise NotFoundException |
| 411 | + mock_conn.compute.pause_server.side_effect = NotFoundException() |
| 412 | + |
| 413 | + compute_tools = ComputeTools() |
| 414 | + |
| 415 | + with pytest.raises(NotFoundException): |
| 416 | + compute_tools.action_server(server_id, action) |
| 417 | + |
| 418 | + mock_conn.compute.pause_server.assert_called_once_with(server_id) |
| 419 | + |
| 420 | + def test_action_server_conflict_exception(self, mock_get_openstack_conn): |
| 421 | + """Test action_server when action cannot be performed due to Conflict Exception.""" |
| 422 | + mock_conn = mock_get_openstack_conn |
| 423 | + server_id = "test-server-id" |
| 424 | + action = "start" |
| 425 | + |
| 426 | + # Mock the action method to raise ConflictException |
| 427 | + mock_conn.compute.start_server.side_effect = ConflictException() |
| 428 | + |
| 429 | + compute_tools = ComputeTools() |
| 430 | + |
| 431 | + with pytest.raises(ConflictException): |
| 432 | + compute_tools.action_server(server_id, action) |
| 433 | + |
| 434 | + mock_conn.compute.start_server.assert_called_once_with(server_id) |
0 commit comments