|
5 | 5 |
|
6 | 6 | import sys |
7 | 7 | import os |
| 8 | +import tempfile |
8 | 9 | from unittest.mock import patch, MagicMock, AsyncMock |
9 | 10 | import pytest |
10 | 11 |
|
@@ -473,5 +474,208 @@ def test_get_container_logs_exception(self, mock_manager): |
473 | 474 | assert "Error retrieving logs" in logs |
474 | 475 |
|
475 | 476 |
|
| 477 | +# --------------------------------------------------------------------------- |
| 478 | +# Test load_image_from_tar_file |
| 479 | +# --------------------------------------------------------------------------- |
| 480 | + |
| 481 | + |
| 482 | +class TestLoadImageFromTarFile: |
| 483 | + """Test load_image_from_tar_file method""" |
| 484 | + |
| 485 | + @pytest.fixture |
| 486 | + def mock_manager(self): |
| 487 | + """Create MCPContainerManager instance with mocked client""" |
| 488 | + with patch('services.mcp_container_service.create_container_client_from_config'), \ |
| 489 | + patch('services.mcp_container_service.DockerContainerConfig'): |
| 490 | + manager = MCPContainerManager() |
| 491 | + manager.client = MagicMock() |
| 492 | + return manager |
| 493 | + |
| 494 | + @pytest.mark.asyncio |
| 495 | + async def test_load_image_from_tar_file_success_with_tags(self, mock_manager): |
| 496 | + """Test successful loading of image with tags""" |
| 497 | + # Create a temporary file |
| 498 | + with tempfile.NamedTemporaryFile(delete=False, suffix='.tar') as temp_file: |
| 499 | + temp_file.write(b"fake tar content") |
| 500 | + temp_file_path = temp_file.name |
| 501 | + |
| 502 | + try: |
| 503 | + mock_image = MagicMock() |
| 504 | + mock_image.tags = ["test-image:latest", "test-image:v1.0"] |
| 505 | + mock_image.id = "sha256:1234567890abcdef" |
| 506 | + |
| 507 | + mock_manager.client.client.images.load.return_value = [mock_image] |
| 508 | + |
| 509 | + result = await mock_manager.load_image_from_tar_file(temp_file_path) |
| 510 | + |
| 511 | + assert result == "test-image:latest" |
| 512 | + mock_manager.client.client.images.load.assert_called_once() |
| 513 | + finally: |
| 514 | + # Clean up |
| 515 | + try: |
| 516 | + os.unlink(temp_file_path) |
| 517 | + except: |
| 518 | + pass |
| 519 | + |
| 520 | + @pytest.mark.asyncio |
| 521 | + async def test_load_image_from_tar_file_success_without_tags(self, mock_manager): |
| 522 | + """Test successful loading of image without tags""" |
| 523 | + # Create a temporary file |
| 524 | + with tempfile.NamedTemporaryFile(delete=False, suffix='.tar') as temp_file: |
| 525 | + temp_file.write(b"fake tar content") |
| 526 | + temp_file_path = temp_file.name |
| 527 | + |
| 528 | + try: |
| 529 | + mock_image = MagicMock() |
| 530 | + mock_image.tags = [] |
| 531 | + mock_image.id = "sha256:1234567890abcdef" |
| 532 | + |
| 533 | + mock_manager.client.client.images.load.return_value = [mock_image] |
| 534 | + |
| 535 | + result = await mock_manager.load_image_from_tar_file(temp_file_path) |
| 536 | + |
| 537 | + assert result == "sha256:1234567890abcdef" |
| 538 | + mock_manager.client.client.images.load.assert_called_once() |
| 539 | + finally: |
| 540 | + # Clean up |
| 541 | + try: |
| 542 | + os.unlink(temp_file_path) |
| 543 | + except: |
| 544 | + pass |
| 545 | + |
| 546 | + @pytest.mark.asyncio |
| 547 | + async def test_load_image_from_tar_file_empty_images(self, mock_manager): |
| 548 | + """Test loading when no images are found in tar file (covers lines 69-70)""" |
| 549 | + # Create a temporary file |
| 550 | + with tempfile.NamedTemporaryFile(delete=False, suffix='.tar') as temp_file: |
| 551 | + temp_file.write(b"fake tar content") |
| 552 | + temp_file_path = temp_file.name |
| 553 | + |
| 554 | + try: |
| 555 | + mock_manager.client.client.images.load.return_value = [] |
| 556 | + |
| 557 | + with pytest.raises(MCPContainerError, match="No images found in tar file"): |
| 558 | + await mock_manager.load_image_from_tar_file(temp_file_path) |
| 559 | + finally: |
| 560 | + # Clean up |
| 561 | + try: |
| 562 | + os.unlink(temp_file_path) |
| 563 | + except: |
| 564 | + pass |
| 565 | + |
| 566 | + @pytest.mark.asyncio |
| 567 | + async def test_load_image_from_tar_file_exception(self, mock_manager): |
| 568 | + """Test loading when exception occurs (covers lines 80-82)""" |
| 569 | + # Create a temporary file |
| 570 | + with tempfile.NamedTemporaryFile(delete=False, suffix='.tar') as temp_file: |
| 571 | + temp_file.write(b"fake tar content") |
| 572 | + temp_file_path = temp_file.name |
| 573 | + |
| 574 | + try: |
| 575 | + mock_manager.client.client.images.load.side_effect = Exception( |
| 576 | + "File not found") |
| 577 | + |
| 578 | + with pytest.raises(MCPContainerError, match="Failed to load image from tar file: File not found"): |
| 579 | + await mock_manager.load_image_from_tar_file(temp_file_path) |
| 580 | + finally: |
| 581 | + # Clean up |
| 582 | + try: |
| 583 | + os.unlink(temp_file_path) |
| 584 | + except: |
| 585 | + pass |
| 586 | + |
| 587 | + |
| 588 | +# --------------------------------------------------------------------------- |
| 589 | +# Test start_mcp_container_from_tar |
| 590 | +# --------------------------------------------------------------------------- |
| 591 | + |
| 592 | + |
| 593 | +class TestStartMCPContainerFromTar: |
| 594 | + """Test start_mcp_container_from_tar method""" |
| 595 | + |
| 596 | + @pytest.fixture |
| 597 | + def mock_manager(self): |
| 598 | + """Create MCPContainerManager instance with mocked client""" |
| 599 | + with patch('services.mcp_container_service.create_container_client_from_config'), \ |
| 600 | + patch('services.mcp_container_service.DockerContainerConfig'): |
| 601 | + manager = MCPContainerManager() |
| 602 | + manager.client = MagicMock() |
| 603 | + return manager |
| 604 | + |
| 605 | + @pytest.mark.asyncio |
| 606 | + async def test_start_mcp_container_from_tar_success(self, mock_manager): |
| 607 | + """Test successful starting of MCP container from tar file""" |
| 608 | + # Mock load_image_from_tar_file |
| 609 | + mock_manager.load_image_from_tar_file = AsyncMock( |
| 610 | + return_value="loaded-image:latest") |
| 611 | + |
| 612 | + # Mock start_mcp_container |
| 613 | + mock_manager.start_mcp_container = AsyncMock(return_value={ |
| 614 | + "container_id": "container-123", |
| 615 | + "mcp_url": "http://localhost:5020/mcp", |
| 616 | + "host_port": "5020", |
| 617 | + "status": "started", |
| 618 | + "container_name": "test-service-user1234" |
| 619 | + }) |
| 620 | + |
| 621 | + result = await mock_manager.start_mcp_container_from_tar( |
| 622 | + tar_file_path="/path/to/image.tar", |
| 623 | + service_name="test-service", |
| 624 | + tenant_id="tenant123", |
| 625 | + user_id="user12345", |
| 626 | + env_vars={"NODE_ENV": "production"}, |
| 627 | + host_port=5020, |
| 628 | + full_command=["npx", "-y", "test-mcp"] |
| 629 | + ) |
| 630 | + |
| 631 | + assert result["container_id"] == "container-123" |
| 632 | + assert result["mcp_url"] == "http://localhost:5020/mcp" |
| 633 | + mock_manager.load_image_from_tar_file.assert_called_once_with( |
| 634 | + "/path/to/image.tar") |
| 635 | + mock_manager.start_mcp_container.assert_called_once_with( |
| 636 | + service_name="test-service", |
| 637 | + tenant_id="tenant123", |
| 638 | + user_id="user12345", |
| 639 | + env_vars={"NODE_ENV": "production"}, |
| 640 | + host_port=5020, |
| 641 | + image="loaded-image:latest", |
| 642 | + full_command=["npx", "-y", "test-mcp"] |
| 643 | + ) |
| 644 | + |
| 645 | + @pytest.mark.asyncio |
| 646 | + async def test_start_mcp_container_from_tar_load_image_error(self, mock_manager): |
| 647 | + """Test starting container when load_image_from_tar_file fails (covers lines 178-181)""" |
| 648 | + # Mock load_image_from_tar_file to raise error |
| 649 | + mock_manager.load_image_from_tar_file = AsyncMock( |
| 650 | + side_effect=MCPContainerError("Failed to load image")) |
| 651 | + |
| 652 | + with pytest.raises(MCPContainerError, match="Failed to start container from tar file: Failed to load image"): |
| 653 | + await mock_manager.start_mcp_container_from_tar( |
| 654 | + tar_file_path="/path/to/image.tar", |
| 655 | + service_name="test-service", |
| 656 | + tenant_id="tenant123", |
| 657 | + user_id="user12345" |
| 658 | + ) |
| 659 | + |
| 660 | + @pytest.mark.asyncio |
| 661 | + async def test_start_mcp_container_from_tar_start_container_error(self, mock_manager): |
| 662 | + """Test starting container when start_mcp_container fails (covers lines 178-181)""" |
| 663 | + # Mock load_image_from_tar_file to succeed |
| 664 | + mock_manager.load_image_from_tar_file = AsyncMock( |
| 665 | + return_value="loaded-image:latest") |
| 666 | + |
| 667 | + # Mock start_mcp_container to raise error |
| 668 | + mock_manager.start_mcp_container = AsyncMock( |
| 669 | + side_effect=MCPContainerError("Container startup failed")) |
| 670 | + |
| 671 | + with pytest.raises(MCPContainerError, match="Failed to start container from tar file: Container startup failed"): |
| 672 | + await mock_manager.start_mcp_container_from_tar( |
| 673 | + tar_file_path="/path/to/image.tar", |
| 674 | + service_name="test-service", |
| 675 | + tenant_id="tenant123", |
| 676 | + user_id="user12345" |
| 677 | + ) |
| 678 | + |
| 679 | + |
476 | 680 | if __name__ == "__main__": |
477 | 681 | pytest.main([__file__]) |
0 commit comments